annotate array.c @ 1:411b28d78483

standard arrays (nearly)
author David A. Holland
date Sun, 19 Dec 2010 16:47:59 -0500
parents
children 9c1cecba517c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
1 /*-
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
2 * Copyright (c) 2009 The NetBSD Foundation, Inc.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
3 * All rights reserved.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
4 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
5 * This code is derived from software contributed to The NetBSD Foundation
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
6 * by David A. Holland.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
7 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
10 * are met:
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
15 * documentation and/or other materials provided with the distribution.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
16 *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
27 * POSSIBILITY OF SUCH DAMAGE.
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
28 */
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
29
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
30 #include <stdlib.h>
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
31 #include <string.h>
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
32
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
33 #define ARRAYINLINE
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
34 #include "array.h"
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
35
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
36 struct array *
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
37 array_create(void)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
38 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
39 struct array *a;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
40
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
41 a = malloc(sizeof(*a));
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
42 if (a != NULL) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
43 array_init(a);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
44 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
45 return a;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
46 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
47
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
48 void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
49 array_destroy(struct array *a)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
50 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
51 array_cleanup(a);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
52 free(a);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
53 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
54
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
55 void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
56 array_init(struct array *a)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
57 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
58 a->num = a->max = 0;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
59 a->v = NULL;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
60 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
61
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
62 void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
63 array_cleanup(struct array *a)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
64 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
65 arrayassert(a->num == 0);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
66 free(a->v);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
67 #ifdef ARRAYS_CHECKED
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
68 a->v = NULL;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
69 #endif
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
70 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
71
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
72 int
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
73 array_setsize(struct array *a, unsigned num)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
74 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
75 unsigned newmax;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
76 void **newptr;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
77
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
78 if (num > a->max) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
79 newmax = a->max;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
80 while (num > newmax) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
81 newmax = newmax ? newmax*2 : 4;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
82 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
83 newptr = realloc(a->v, newmax*sizeof(*a->v));
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
84 if (newptr == NULL) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
85 return -1;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
86 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
87 a->v = newptr;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
88 a->max = newmax;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
89 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
90 a->num = num;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
91 return 0;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
92 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
93
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
94 int
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
95 array_insert(struct array *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
96 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
97 unsigned movers;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
98
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
99 arrayassert(a->num <= a->max);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
100 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
101
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
102 movers = a->num - index_;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
103
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
104 if (array_setsize(a, a->num + 1)) {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
105 return -1;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
106 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
107
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
108 memmove(a->v + index_+1, a->v + index_, movers*sizeof(*a->v));
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
109 return 0;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
110 }
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
111
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
112 void
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
113 array_remove(struct array *a, unsigned index_)
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
114 {
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
115 unsigned movers;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
116
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
117 arrayassert(a->num <= a->max);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
118 arrayassert(index_ < a->num);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
119
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
120 movers = a->num - (index_ + 1);
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
121 memmove(a->v + index_, a->v + index_+1, movers*sizeof(*a->v));
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
122 a->num--;
411b28d78483 standard arrays (nearly)
David A. Holland
parents:
diff changeset
123 }