]>
git.proxmox.com Git - mirror_frr.git/blob - lib/vector.c
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
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 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
26 DEFINE_MTYPE_STATIC(LIB
, VECTOR
, "Vector")
27 DEFINE_MTYPE_STATIC(LIB
, VECTOR_INDEX
, "Vector index")
29 /* Initialize vector : allocate memory and return vector. */
31 vector_init (unsigned int size
)
33 vector v
= XCALLOC (MTYPE_VECTOR
, sizeof (struct _vector
));
35 /* allocate at least one slot */
41 v
->index
= XCALLOC (MTYPE_VECTOR_INDEX
, sizeof (void *) * size
);
46 vector_free (vector v
)
48 XFREE (MTYPE_VECTOR_INDEX
, v
->index
);
49 XFREE (MTYPE_VECTOR
, v
);
53 vector_copy (vector v
)
56 vector
new = XCALLOC (MTYPE_VECTOR
, sizeof (struct _vector
));
58 new->active
= v
->active
;
59 new->alloced
= v
->alloced
;
61 size
= sizeof (void *) * (v
->alloced
);
62 new->index
= XCALLOC (MTYPE_VECTOR_INDEX
, size
);
63 memcpy (new->index
, v
->index
, size
);
68 /* Check assigned index, and if it runs short double index pointer */
70 vector_ensure (vector v
, unsigned int num
)
75 v
->index
= XREALLOC (MTYPE_VECTOR_INDEX
,
76 v
->index
, sizeof (void *) * (v
->alloced
* 2));
77 memset (&v
->index
[v
->alloced
], 0, sizeof (void *) * v
->alloced
);
80 if (v
->alloced
<= num
)
81 vector_ensure (v
, num
);
84 /* This function only returns next empty slot index. It dose not mean
85 the slot's index memory is assigned, please call vector_ensure()
86 after calling this function. */
88 vector_empty_slot (vector v
)
95 for (i
= 0; i
< v
->active
; i
++)
102 /* Set value to the smallest empty slot. */
104 vector_set (vector v
, void *val
)
108 i
= vector_empty_slot (v
);
109 vector_ensure (v
, i
);
119 /* Set value to specified index slot. */
121 vector_set_index (vector v
, unsigned int i
, void *val
)
123 vector_ensure (v
, i
);
133 /* Look up vector. */
135 vector_lookup (vector v
, unsigned int i
)
142 /* Lookup vector, ensure it. */
144 vector_lookup_ensure (vector v
, unsigned int i
)
146 vector_ensure (v
, i
);
150 /* Unset value at specified index slot. */
152 vector_unset (vector v
, unsigned int i
)
159 if (i
+ 1 == v
->active
)
162 while (i
&& v
->index
[--i
] == NULL
&& v
->active
--)
163 ; /* Is this ugly ? */
168 vector_unset_value (vector v
, void *val
)
172 for (i
= 0; i
< v
->active
; i
++)
173 if (v
->index
[i
] == val
)
179 if (i
+ 1 == v
->active
)
182 while (i
&& v
->index
[--i
] == NULL
);
185 /* Count the number of not emplty slot. */
187 vector_count (vector v
)
192 for (i
= 0; i
< v
->active
; i
++)
193 if (v
->index
[i
] != NULL
)