]>
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. */
30 vector
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
);
44 void vector_free(vector v
)
46 XFREE(MTYPE_VECTOR_INDEX
, v
->index
);
47 XFREE(MTYPE_VECTOR
, v
);
50 vector
vector_copy(vector v
)
53 vector
new = XCALLOC(MTYPE_VECTOR
, sizeof(struct _vector
));
55 new->active
= v
->active
;
56 new->alloced
= v
->alloced
;
58 size
= sizeof(void *) * (v
->alloced
);
59 new->index
= XCALLOC(MTYPE_VECTOR_INDEX
, size
);
60 memcpy(new->index
, v
->index
, size
);
65 /* Check assigned index, and if it runs short double index pointer */
66 void vector_ensure(vector v
, unsigned int num
)
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
);
76 if (v
->alloced
<= num
)
77 vector_ensure(v
, num
);
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
)
90 for (i
= 0; i
< v
->active
; i
++)
97 /* Set value to the smallest empty slot. */
98 int vector_set(vector v
, void *val
)
102 i
= vector_empty_slot(v
);
113 /* Set value to specified index slot. */
114 int vector_set_index(vector v
, unsigned int i
, void *val
)
126 /* Look up vector. */
127 void *vector_lookup(vector v
, unsigned int i
)
134 /* Lookup vector, ensure it. */
135 void *vector_lookup_ensure(vector v
, unsigned int i
)
141 /* Unset value at specified index slot. */
142 void vector_unset(vector v
, unsigned int i
)
149 if (i
+ 1 == v
->active
) {
151 while (i
&& v
->index
[--i
] == NULL
&& v
->active
--)
152 ; /* Is this ugly ? */
156 void vector_remove(vector v
, unsigned int ix
)
161 int n
= (--v
->active
) - ix
;
163 memmove(&v
->index
[ix
], &v
->index
[ix
+ 1], n
* sizeof(void *));
164 v
->index
[v
->active
] = NULL
;
167 void vector_compact(vector v
)
169 for (unsigned int i
= 0; i
< vector_active(v
); ++i
) {
170 if (vector_slot(v
, i
) == NULL
) {
177 void vector_unset_value(vector v
, void *val
)
181 for (i
= 0; i
< v
->active
; i
++)
182 if (v
->index
[i
] == val
) {
187 if (i
+ 1 == v
->active
)
190 while (i
&& v
->index
[--i
] == NULL
);
193 /* Count the number of not emplty slot. */
194 unsigned int vector_count(vector v
)
199 for (i
= 0; i
< v
->active
; i
++)
200 if (v
->index
[i
] != NULL
)
206 void vector_to_array(vector v
, void ***dest
, int *argc
)
208 *dest
= XCALLOC(MTYPE_TMP
, sizeof(void *) * v
->active
);
209 memcpy(*dest
, v
->index
, sizeof(void *) * v
->active
);
213 vector
array_to_vector(void **src
, int argc
)
215 vector v
= vector_init(VECTOR_MIN_SIZE
);
217 for (int i
= 0; i
< argc
; i
++)
218 vector_set_index(v
, i
, src
[i
]);