2 * generic vector interface routine
3 * Copyright (C) 1997 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 /* Initialize vector : allocate memory and return vector. */
30 vector_init (unsigned int size)
32 vector v = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
34 /* allocate at least one slot */
40 v->index = XCALLOC (MTYPE_VECTOR_INDEX, sizeof (void *) * size);
45 vector_only_wrapper_free (vector v)
47 XFREE (MTYPE_VECTOR, v);
51 vector_only_index_free (void *index)
53 XFREE (MTYPE_VECTOR_INDEX, index);
57 vector_free (vector v)
59 XFREE (MTYPE_VECTOR_INDEX, v->index);
60 XFREE (MTYPE_VECTOR, v);
64 vector_copy (vector v)
67 vector new = XCALLOC (MTYPE_VECTOR, sizeof (struct _vector));
70 new->alloced = v->alloced;
72 size = sizeof (void *) * (v->alloced);
73 new->index = XCALLOC (MTYPE_VECTOR_INDEX, size);
74 memcpy (new->index, v->index, size);
79 /* Check assigned index, and if it runs short double index pointer */
81 vector_ensure (vector v, unsigned int num)
86 v->index = XREALLOC (MTYPE_VECTOR_INDEX,
87 v->index, sizeof (void *) * (v->alloced * 2));
88 memset (&v->index[v->alloced], 0, sizeof (void *) * v->alloced);
91 if (v->alloced <= num)
92 vector_ensure (v, num);
95 /* This function only returns next empty slot index. It dose not mean
96 the slot's index memory is assigned, please call vector_ensure()
97 after calling this function. */
99 vector_empty_slot (vector v)
106 for (i = 0; i < v->max; i++)
107 if (v->index[i] == 0)
113 /* Set value to the smallest empty slot. */
115 vector_set (vector v, void *val)
119 i = vector_empty_slot (v);
120 vector_ensure (v, i);
130 /* Set value to specified index slot. */
132 vector_set_index (vector v, unsigned int i, void *val)
134 vector_ensure (v, i);
144 /* Lookup vector, ensure it. */
146 vector_lookup_index (vector v, unsigned int i)
148 vector_ensure (v, i);
152 /* Unset value at specified index slot. */
154 vector_unset (vector v, unsigned int i)
164 while (i && v->index[--i] == NULL && v->max--)
165 ; /* Is this ugly ? */
169 /* Count the number of not emplty slot. */
171 vector_count (vector v)
176 for (i = 0; i < v->max; i++)
177 if (v->index[i] != NULL)