]>
Commit | Line | Data |
---|---|---|
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 | * | |
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 | |
19 | */ | |
20 | ||
21 | #include <zebra.h> | |
22 | ||
23 | #include "vector.h" | |
24 | #include "memory.h" | |
25 | ||
26 | DEFINE_MTYPE_STATIC(LIB, VECTOR, "Vector") | |
27 | DEFINE_MTYPE_STATIC(LIB, VECTOR_INDEX, "Vector index") | |
28 | ||
29 | /* Initialize vector : allocate memory and return vector. */ | |
30 | vector vector_init(unsigned int size) | |
31 | { | |
32 | vector v = XCALLOC(MTYPE_VECTOR, sizeof(struct _vector)); | |
33 | ||
34 | /* allocate at least one slot */ | |
35 | if (size == 0) | |
36 | size = 1; | |
37 | ||
38 | v->alloced = size; | |
39 | v->active = 0; | |
40 | v->index = XCALLOC(MTYPE_VECTOR_INDEX, sizeof(void *) * size); | |
41 | return v; | |
42 | } | |
43 | ||
44 | void vector_free(vector v) | |
45 | { | |
46 | XFREE(MTYPE_VECTOR_INDEX, v->index); | |
47 | XFREE(MTYPE_VECTOR, v); | |
48 | } | |
49 | ||
50 | vector vector_copy(vector v) | |
51 | { | |
52 | unsigned int size; | |
53 | vector new = XCALLOC(MTYPE_VECTOR, sizeof(struct _vector)); | |
54 | ||
55 | new->active = v->active; | |
56 | new->alloced = v->alloced; | |
57 | ||
58 | size = sizeof(void *) * (v->alloced); | |
59 | new->index = XCALLOC(MTYPE_VECTOR_INDEX, size); | |
60 | memcpy(new->index, v->index, size); | |
61 | ||
62 | return new; | |
63 | } | |
64 | ||
65 | /* Check assigned index, and if it runs short double index pointer */ | |
66 | void vector_ensure(vector v, unsigned int num) | |
67 | { | |
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); | |
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. */ | |
83 | int vector_empty_slot(vector v) | |
84 | { | |
85 | unsigned int i; | |
86 | ||
87 | if (v->active == 0) | |
88 | return 0; | |
89 | ||
90 | for (i = 0; i < v->active; i++) | |
91 | if (v->index[i] == 0) | |
92 | return i; | |
93 | ||
94 | return i; | |
95 | } | |
96 | ||
97 | /* Set value to the smallest empty slot. */ | |
98 | int vector_set(vector v, void *val) | |
99 | { | |
100 | unsigned int i; | |
101 | ||
102 | i = vector_empty_slot(v); | |
103 | vector_ensure(v, i); | |
104 | ||
105 | v->index[i] = val; | |
106 | ||
107 | if (v->active <= i) | |
108 | v->active = i + 1; | |
109 | ||
110 | return i; | |
111 | } | |
112 | ||
113 | /* Set value to specified index slot. */ | |
114 | int vector_set_index(vector v, unsigned int i, void *val) | |
115 | { | |
116 | vector_ensure(v, i); | |
117 | ||
118 | v->index[i] = val; | |
119 | ||
120 | if (v->active <= i) | |
121 | v->active = i + 1; | |
122 | ||
123 | return i; | |
124 | } | |
125 | ||
126 | /* Look up vector. */ | |
127 | void *vector_lookup(vector v, unsigned int i) | |
128 | { | |
129 | if (i >= v->active) | |
130 | return NULL; | |
131 | return v->index[i]; | |
132 | } | |
133 | ||
134 | /* Lookup vector, ensure it. */ | |
135 | void *vector_lookup_ensure(vector v, unsigned int i) | |
136 | { | |
137 | vector_ensure(v, i); | |
138 | return v->index[i]; | |
139 | } | |
140 | ||
141 | /* Unset value at specified index slot. */ | |
142 | void vector_unset(vector v, unsigned int i) | |
143 | { | |
144 | if (i >= v->alloced) | |
145 | return; | |
146 | ||
147 | v->index[i] = NULL; | |
148 | ||
149 | if (i + 1 == v->active) { | |
150 | v->active--; | |
151 | while (i && v->index[--i] == NULL && v->active--) | |
152 | ; /* Is this ugly ? */ | |
153 | } | |
154 | } | |
155 | ||
156 | void vector_unset_value(vector v, void *val) | |
157 | { | |
158 | size_t i; | |
159 | ||
160 | for (i = 0; i < v->active; i++) | |
161 | if (v->index[i] == val) { | |
162 | v->index[i] = NULL; | |
163 | break; | |
164 | } | |
165 | ||
166 | if (i + 1 == v->active) | |
167 | do | |
168 | v->active--; | |
169 | while (i && v->index[--i] == NULL); | |
170 | } | |
171 | ||
172 | /* Count the number of not emplty slot. */ | |
173 | unsigned int vector_count(vector v) | |
174 | { | |
175 | unsigned int i; | |
176 | unsigned count = 0; | |
177 | ||
178 | for (i = 0; i < v->active; i++) | |
179 | if (v->index[i] != NULL) | |
180 | count++; | |
181 | ||
182 | return count; | |
183 | } |