Merged own patch for bug #390 (rewrite zebra/zebra_rib.c:nexthop_active_update())
[jleu-quagga.git] / lib / vector.c
blob7c1486285f38c443e4a40c7d332aac1f6d611392
1 /* Generic vector interface routine
2 * Copyright (C) 1997 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
22 #include <zebra.h>
24 #include "vector.h"
25 #include "memory.h"
27 /* Initialize vector : allocate memory and return vector. */
28 vector
29 vector_init (unsigned int size)
31 vector v = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
33 /* allocate at least one slot */
34 if (size == 0)
35 size = 1;
37 v->alloced = size;
38 v->active = 0;
39 v->index = XCALLOC (MTYPE_VECTOR_INDEX, sizeof (void *) * size);
40 return v;
43 void
44 vector_only_wrapper_free (vector v)
46 XFREE (MTYPE_VECTOR, v);
49 void
50 vector_only_index_free (void *index)
52 XFREE (MTYPE_VECTOR_INDEX, index);
55 void
56 vector_free (vector v)
58 XFREE (MTYPE_VECTOR_INDEX, v->index);
59 XFREE (MTYPE_VECTOR, v);
62 vector
63 vector_copy (vector v)
65 unsigned int size;
66 vector new = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
68 new->active = v->active;
69 new->alloced = v->alloced;
71 size = sizeof (void *) * (v->alloced);
72 new->index = XCALLOC (MTYPE_VECTOR_INDEX, size);
73 memcpy (new->index, v->index, size);
75 return new;
78 /* Check assigned index, and if it runs short double index pointer */
79 void
80 vector_ensure (vector v, unsigned int num)
82 if (v->alloced > num)
83 return;
85 v->index = XREALLOC (MTYPE_VECTOR_INDEX,
86 v->index, sizeof (void *) * (v->alloced * 2));
87 memset (&v->index[v->alloced], 0, sizeof (void *) * v->alloced);
88 v->alloced *= 2;
90 if (v->alloced <= num)
91 vector_ensure (v, num);
94 /* This function only returns next empty slot index. It dose not mean
95 the slot's index memory is assigned, please call vector_ensure()
96 after calling this function. */
97 int
98 vector_empty_slot (vector v)
100 unsigned int i;
102 if (v->active == 0)
103 return 0;
105 for (i = 0; i < v->active; i++)
106 if (v->index[i] == 0)
107 return i;
109 return i;
112 /* Set value to the smallest empty slot. */
114 vector_set (vector v, void *val)
116 unsigned int i;
118 i = vector_empty_slot (v);
119 vector_ensure (v, i);
121 v->index[i] = val;
123 if (v->active <= i)
124 v->active = i + 1;
126 return i;
129 /* Set value to specified index slot. */
131 vector_set_index (vector v, unsigned int i, void *val)
133 vector_ensure (v, i);
135 v->index[i] = val;
137 if (v->active <= i)
138 v->active = i + 1;
140 return i;
143 /* Look up vector. */
144 void *
145 vector_lookup (vector v, unsigned int i)
147 if (i >= v->active)
148 return NULL;
149 return v->index[i];
152 /* Lookup vector, ensure it. */
153 void *
154 vector_lookup_ensure (vector v, unsigned int i)
156 vector_ensure (v, i);
157 return v->index[i];
160 /* Unset value at specified index slot. */
161 void
162 vector_unset (vector v, unsigned int i)
164 if (i >= v->alloced)
165 return;
167 v->index[i] = NULL;
169 if (i + 1 == v->active)
171 v->active--;
172 while (i && v->index[--i] == NULL && v->active--)
173 ; /* Is this ugly ? */
177 /* Count the number of not emplty slot. */
178 unsigned int
179 vector_count (vector v)
181 unsigned int i;
182 unsigned count = 0;
184 for (i = 0; i < v->active; i++)
185 if (v->index[i] != NULL)
186 count++;
188 return count;