]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Generic vector interface routine |
2 | * Copyright (C) 1997 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
896014f4 DL |
16 | * You should have received a copy of the GNU General Public License along |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
718e3744 | 19 | */ |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "vector.h" | |
24 | #include "memory.h" | |
25 | ||
d62a17ae | 26 | DEFINE_MTYPE_STATIC(LIB, VECTOR, "Vector") |
66d29a54 | 27 | DEFINE_MTYPE_STATIC(LIB, VECTOR_INDEX, "Vector index") |
4a1ab8e4 | 28 | |
718e3744 | 29 | /* Initialize vector : allocate memory and return vector. */ |
d62a17ae | 30 | vector vector_init(unsigned int size) |
718e3744 | 31 | { |
d62a17ae | 32 | vector v = XCALLOC(MTYPE_VECTOR, sizeof(struct _vector)); |
718e3744 | 33 | |
d62a17ae | 34 | /* allocate at least one slot */ |
35 | if (size == 0) | |
36 | size = 1; | |
718e3744 | 37 | |
d62a17ae | 38 | v->alloced = size; |
39 | v->active = 0; | |
40 | v->index = XCALLOC(MTYPE_VECTOR_INDEX, sizeof(void *) * size); | |
41 | return v; | |
718e3744 | 42 | } |
43 | ||
d62a17ae | 44 | void vector_free(vector v) |
718e3744 | 45 | { |
d62a17ae | 46 | XFREE(MTYPE_VECTOR_INDEX, v->index); |
47 | XFREE(MTYPE_VECTOR, v); | |
718e3744 | 48 | } |
49 | ||
d62a17ae | 50 | vector vector_copy(vector v) |
718e3744 | 51 | { |
d62a17ae | 52 | unsigned int size; |
53 | vector new = XCALLOC(MTYPE_VECTOR, sizeof(struct _vector)); | |
718e3744 | 54 | |
d62a17ae | 55 | new->active = v->active; |
56 | new->alloced = v->alloced; | |
718e3744 | 57 | |
d62a17ae | 58 | size = sizeof(void *) * (v->alloced); |
59 | new->index = XCALLOC(MTYPE_VECTOR_INDEX, size); | |
60 | memcpy(new->index, v->index, size); | |
718e3744 | 61 | |
d62a17ae | 62 | return new; |
718e3744 | 63 | } |
64 | ||
65 | /* Check assigned index, and if it runs short double index pointer */ | |
d62a17ae | 66 | void vector_ensure(vector v, unsigned int num) |
718e3744 | 67 | { |
d62a17ae | 68 | if (v->alloced > num) |
69 | return; | |
70 | ||
71 | v->index = XREALLOC(MTYPE_VECTOR_INDEX, v->index, | |
72 | sizeof(void *) * (v->alloced * 2)); | |
73 | memset(&v->index[v->alloced], 0, sizeof(void *) * v->alloced); | |
74 | v->alloced *= 2; | |
75 | ||
76 | if (v->alloced <= num) | |
77 | vector_ensure(v, num); | |
718e3744 | 78 | } |
79 | ||
80 | /* This function only returns next empty slot index. It dose not mean | |
81 | the slot's index memory is assigned, please call vector_ensure() | |
82 | after calling this function. */ | |
d62a17ae | 83 | int vector_empty_slot(vector v) |
718e3744 | 84 | { |
d62a17ae | 85 | unsigned int i; |
718e3744 | 86 | |
d62a17ae | 87 | if (v->active == 0) |
88 | return 0; | |
718e3744 | 89 | |
d62a17ae | 90 | for (i = 0; i < v->active; i++) |
91 | if (v->index[i] == 0) | |
92 | return i; | |
718e3744 | 93 | |
d62a17ae | 94 | return i; |
718e3744 | 95 | } |
96 | ||
97 | /* Set value to the smallest empty slot. */ | |
d62a17ae | 98 | int vector_set(vector v, void *val) |
718e3744 | 99 | { |
d62a17ae | 100 | unsigned int i; |
718e3744 | 101 | |
d62a17ae | 102 | i = vector_empty_slot(v); |
103 | vector_ensure(v, i); | |
718e3744 | 104 | |
d62a17ae | 105 | v->index[i] = val; |
718e3744 | 106 | |
d62a17ae | 107 | if (v->active <= i) |
108 | v->active = i + 1; | |
718e3744 | 109 | |
d62a17ae | 110 | return i; |
718e3744 | 111 | } |
112 | ||
113 | /* Set value to specified index slot. */ | |
d62a17ae | 114 | int vector_set_index(vector v, unsigned int i, void *val) |
718e3744 | 115 | { |
d62a17ae | 116 | vector_ensure(v, i); |
718e3744 | 117 | |
d62a17ae | 118 | v->index[i] = val; |
718e3744 | 119 | |
d62a17ae | 120 | if (v->active <= i) |
121 | v->active = i + 1; | |
718e3744 | 122 | |
d62a17ae | 123 | return i; |
718e3744 | 124 | } |
125 | ||
126 | /* Look up vector. */ | |
d62a17ae | 127 | void *vector_lookup(vector v, unsigned int i) |
718e3744 | 128 | { |
d62a17ae | 129 | if (i >= v->active) |
130 | return NULL; | |
131 | return v->index[i]; | |
718e3744 | 132 | } |
133 | ||
134 | /* Lookup vector, ensure it. */ | |
d62a17ae | 135 | void *vector_lookup_ensure(vector v, unsigned int i) |
718e3744 | 136 | { |
d62a17ae | 137 | vector_ensure(v, i); |
138 | return v->index[i]; | |
718e3744 | 139 | } |
140 | ||
141 | /* Unset value at specified index slot. */ | |
d62a17ae | 142 | void vector_unset(vector v, unsigned int i) |
718e3744 | 143 | { |
d62a17ae | 144 | if (i >= v->alloced) |
145 | return; | |
718e3744 | 146 | |
d62a17ae | 147 | v->index[i] = NULL; |
718e3744 | 148 | |
d62a17ae | 149 | if (i + 1 == v->active) { |
150 | v->active--; | |
151 | while (i && v->index[--i] == NULL && v->active--) | |
152 | ; /* Is this ugly ? */ | |
153 | } | |
718e3744 | 154 | } |
155 | ||
62bece44 QY |
156 | void vector_remove(vector v, unsigned int ix) |
157 | { | |
158 | if (ix >= v->active) | |
159 | return; | |
160 | ||
161 | int n = (--v->active) - ix; | |
162 | ||
163 | memmove(&v->index[ix], &v->index[ix + 1], n * sizeof(void *)); | |
164 | v->index[v->active] = NULL; | |
165 | } | |
166 | ||
f428cb8a QY |
167 | void vector_compact(vector v) |
168 | { | |
169 | for (unsigned int i = 0; i < vector_active(v); ++i) { | |
170 | if (vector_slot(v, i) == NULL) { | |
171 | vector_remove(v, i); | |
172 | --i; | |
173 | } | |
174 | } | |
175 | } | |
176 | ||
d62a17ae | 177 | void vector_unset_value(vector v, void *val) |
0d9e2046 | 178 | { |
d62a17ae | 179 | size_t i; |
180 | ||
181 | for (i = 0; i < v->active; i++) | |
182 | if (v->index[i] == val) { | |
183 | v->index[i] = NULL; | |
184 | break; | |
185 | } | |
186 | ||
187 | if (i + 1 == v->active) | |
188 | do | |
189 | v->active--; | |
190 | while (i && v->index[--i] == NULL); | |
0d9e2046 DL |
191 | } |
192 | ||
718e3744 | 193 | /* Count the number of not emplty slot. */ |
d62a17ae | 194 | unsigned int vector_count(vector v) |
718e3744 | 195 | { |
d62a17ae | 196 | unsigned int i; |
197 | unsigned count = 0; | |
718e3744 | 198 | |
d62a17ae | 199 | for (i = 0; i < v->active; i++) |
200 | if (v->index[i] != NULL) | |
201 | count++; | |
718e3744 | 202 | |
d62a17ae | 203 | return count; |
718e3744 | 204 | } |
fe011935 QY |
205 | |
206 | void vector_to_array(vector v, void ***dest, int *argc) | |
207 | { | |
208 | *dest = XCALLOC(MTYPE_TMP, sizeof(void *) * v->active); | |
209 | memcpy(*dest, v->index, sizeof(void *) * v->active); | |
210 | *argc = v->active; | |
211 | } | |
212 | ||
213 | vector array_to_vector(void **src, int argc) | |
214 | { | |
215 | vector v = vector_init(VECTOR_MIN_SIZE); | |
0a334343 | 216 | |
fe011935 QY |
217 | for (int i = 0; i < argc; i++) |
218 | vector_set_index(v, i, src[i]); | |
219 | return v; | |
220 | } |