summaryrefslogtreecommitdiffstats
path: root/lib/vector.h
blob: f80f85c69a440cb37caeb7e387457384c3c6799c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/* Generic vector interface header.
 * Copyright (C) 1997, 98 Kunihiro Ishiguro
 *
 * 24-Nov-2009  -- extended to add a number of new operations on vectors.
 *                 Copyright (C) 2009 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifndef _ZEBRA_VECTOR_H
#define _ZEBRA_VECTOR_H

#include "misc.h"

/*------------------------------------------------------------------------------
 * types and struct for vector
 *
 * NB: an entirely zero structure represents an entirely empty vector.
 */
typedef void*         p_vector_item ;
typedef unsigned int  vector_index_t ;
typedef unsigned int  vector_length_t ;

enum {
  VECTOR_LENGTH_MAX = (vector_length_t)INT_MAX + 1
} ;

struct vector
{
  p_vector_item*  p_items ;     /* pointer to array of vector item pointers */
  vector_length_t end ;         /* number of "active" item entries          */
  vector_length_t limit ;       /* number of allocated item entries         */
};

typedef struct vector  vector_t[1] ;    /* embedded vector structure    */
typedef struct vector* vector ;         /* pointer to vector structure  */

/* Setting a vector object to all zeros is enough to initialise it to
 * an empty vector.
 */
enum
{
  VECTOR_INIT_ALL_ZEROS = true
} ;

#define VECTOR_INIT_EMPTY  { .p_items = NULL, .end = 0, .limit = 0 }

/* Under very controlled circumstances, may access the vector body
 */
typedef p_vector_item const* vector_body_t ;

/*------------------------------------------------------------------------------
 * Values that control the allocation of the vector body.
 * NB: these must all be powers of 2.
 */
enum
{
  /* The body, when allocated, will have at least this many entries.
   */
  VECTOR_LIMIT_MIN           = 8,

  /* When the body grows, it doubles in size, until it is this big.
   * After that it grows in units of this much.
   */
  VECTOR_LIMIT_DOUBLE_MAX   = 2048,

  /* "Midway" between VECTOR_LIMIT_MIN and VECTOR_LIMIT_DOUBLE_MAX.
   */
  VECTOR_LIMIT_MID          = 128,

  /* When growing in units of VECTOR_LIMIT_DOUBLE_MAX, this is the
   * minimum slack space to leave after the logical end of the vector.
   */
  VECTOR_LIMIT_SLACK_MIN    = ((VECTOR_LIMIT_DOUBLE_MAX) / 8)
} ;

CONFIRM(IS_POW_OF_2(VECTOR_LIMIT_MIN)) ;
CONFIRM(IS_POW_OF_2(VECTOR_LIMIT_DOUBLE_MAX)) ;
CONFIRM(IS_POW_OF_2(VECTOR_LIMIT_MID)) ;
CONFIRM(IS_POW_OF_2(VECTOR_LIMIT_SLACK_MIN)) ;

/*------------------------------------------------------------------------------
 * (Sometimes) useful macros.
 */

/* Reference item at given index.
 *
 * NB: does not guarantee the item is "active" (that is: within the
 *     (logical) vector) -- but see vector_ensure.
 *
 * Returns address of vector item.
 *
 * See: VECTOR_ITEMS() for walking items in a vector.
 * See: vector_get_item(), which is preferable.
 */
#define vector_slot(V,I)  ((V)->p_items[(I)])

/* Number of "active" item entries -- (logical) end of the vector.
 *
 * Note that this differs from vector_count() as this count will
 * *include* any NULL items.
 */
#define vector_active(V) ((V)->end)

/* To walk all items in a vector:
 *
 *   vector_index_t i ;
 *   xxxxx* p_v ;
 *
 *   for (VECTOR_ITEMS(v, p_v, i))
 *     {
 *       ...  i    is index of current item
 *       ...  p_v  is address of current item value -- may be NULL
 *     } ;
 *
 *   ... i is number of items in vector (including any NULLs)
 */
#define VECTOR_ITEMS(v, p_v, i)\
  (i) = 0 ;\
  (i) < (v)->end ? (((p_v) = (void*)(v)->p_items[i]), 1) \
                 : (((p_v) = NULL),                   0) ;\
  ++(i)

/*==============================================================================
 *  Prototypes.
 */

extern vector vector_init (vector_length_t size);
Inline void vector_ensure(vector v, vector_index_t i) ;
extern int vector_set (vector v, void *val);
extern int vector_set_index (vector v, vector_index_t i, void *val);
#define vector_unset(v, i) (void)vector_unset_item(v, i)
extern vector_length_t vector_count (vector v);
extern void vector_free (vector v);
extern vector vector_copy (vector v);

extern void *vector_lookup (vector, vector_index_t);
extern void *vector_lookup_ensure (vector, vector_index_t);

extern vector vector_init_new(vector v, vector_length_t size) ;
extern vector vector_new(vector_length_t size) ;
extern vector vector_re_init(vector v, vector_length_t size) ;
extern vector vector_reset(vector v, free_keep_b free_structure) ;
extern p_vector_item vector_ream(vector v, free_keep_b free_structure) ;

Inline  void vector_set_min_length(vector v, vector_length_t len) ;
Private void vector_set_new_min_length(vector v, vector_length_t len) ;

Inline void vector_set_length(vector v, vector_length_t len) ;
#define     vector_set_end(v, l) vector_set_length(v, l)

Inline vector_length_t vector_length(vector v) ;
#define                vector_end(v) vector_length(v)
Inline bool vector_is_empty(vector v) ;

Inline vector_body_t vector_body(vector v) ;

Inline p_vector_item vector_get_item(vector v, vector_index_t i) ;
Inline p_vector_item vector_get_first_item(vector v) ;
Inline p_vector_item vector_get_last_item(vector v) ;
Inline void vector_set_item(vector v, vector_index_t i, p_vector_item p_v) ;
Inline void vector_assign_item(vector v, vector_index_t dst,
                                         vector_index_t src) ;
extern p_vector_item vector_unset_item(vector v, vector_index_t i) ;
extern vector_length_t vector_trim(vector v) ;
extern vector_length_t vector_condense(vector v) ;

extern void vector_insert_item(vector v, vector_index_t i, p_vector_item p_v) ;
extern void vector_insert_item_here(vector v, vector_index_t i, int rider,
							  p_vector_item p_v) ;
extern void vector_move_item(vector v, vector_index_t dst, vector_index_t src) ;
extern void vector_move_item_here(vector v, vector_index_t dst, int rider,
							   vector_index_t src) ;
extern p_vector_item vector_delete_item(vector v, vector_index_t i) ;
extern void vector_reverse(vector v) ;
extern void vector_part_reverse(vector v, vector_index_t i, vector_length_t n) ;

Inline void vector_push_item(vector v, p_vector_item p_v) ;
Inline p_vector_item vector_pop_item(vector v) ;
Inline void vector_unshift_item(vector v, p_vector_item p_v) ;
Inline p_vector_item vector_shift_item(vector v) ;

extern void vector_insert(vector v, vector_index_t i, vector_length_t n) ;
extern void vector_delete(vector v, vector_index_t i, vector_length_t n) ;

typedef int vector_bsearch_cmp(const cvp* pp_val, const cvp* item) ;
vector_index_t vector_bsearch(vector v, vector_bsearch_cmp* cmp,
					      const void* p_val, int* result) ;
typedef int vector_sort_cmp(const cvp* a, const cvp* b) ;
void vector_sort(vector v, vector_sort_cmp* cmp) ;

extern vector vector_copy_here(vector dst, vector src) ;
extern vector vector_move_here(vector dst, vector src) ;
extern vector vector_copy_append(vector dst, vector src) ;
extern vector vector_move_append(vector dst, vector src) ;

#define vector_copy_extract(to, src, i_src, n_src) \
  vector_sak(1, to,   NULL, 0,    0,     src, i_src, n_src, 0)
#define vector_move_extract(to, src, i_src, n_src) \
  vector_sak(1, to,   NULL, 0,    0,     src, i_src, n_src, 1)
#define vector_copy_splice(to, dst, i_dst, n_dst, src, i_src, n_src) \
  vector_sak(1, to,   dst, i_dst, n_dst, src, i_src, n_src, 0)
#define vector_move_splice(to, dst, i_dst, n_dst, src, i_src, n_src) \
  vector_sak(1, to,   dst, i_dst, n_dst, src, i_src, n_src, 1)
#define vector_copy_replace(dst, i_dst, n_dst, src, i_src, n_src) \
  vector_sak(0, NULL, dst, i_dst, n_dst, src, i_src, n_src, 0)
#define vector_move_replace(dst, i_dst, n_dst, src, i_src, n_src) \
  vector_sak(0, NULL, dst, i_dst, n_dst, src, i_src, n_src, 1)

extern vector vector_sak(int to_copy, vector to,
        vector dst, vector_index_t i_dst, vector_length_t n_dst,
        vector src, vector_index_t i_src, vector_length_t n_src, int src_move) ;

extern void vector_discard(vector v, vector_index_t i) ;
extern void vector_chop(vector v) ;
extern void vector_decant(vector v) ;

Inline vector_index_t vector_extend_by_1(vector v) ;
extern void vector_extend(vector v, vector_length_t new_end) ;

/*==============================================================================
 * The inline functions:
 */

/*------------------------------------------------------------------------------
 * Extend vector by one item at the end, which is about to be set.
 * Returns index of new least item in the vector.
 * NB: if left unset, the item may be UNDEFINED.
 */
Inline vector_index_t
vector_extend_by_1(vector v)
{
  vector_index_t i = v->end ;

  if (i < v->limit)
    return v->end++ ;                   /* simple if we have room       */

  vector_extend(v, i + 1) ;             /* the hard way                 */
  return i ;
} ;

/*------------------------------------------------------------------------------
 * Ensure given index is "active".
 * Adjusts logical and physical end of the vector as required, filling
 * with NULLs upto any new logical end.
 */
Inline void
vector_ensure(vector v, vector_index_t i)
{
  if (i < v->end)                       /* trivial if within vector     */
    return ;
  if ((i == v->end) && (i < v->limit))  /* simple if end and have room  */
    v->p_items[v->end++] = NULL ;       /* set NULL for complete safety */
  else
    vector_extend(v, i + 1) ;           /* do it the hard way           */
} ;

/*------------------------------------------------------------------------------
 * Want vector to be at least the given length.
 *
 * Adjusts logical and physical end of the vector as required, filling
 * with NULLs upto any new logical end -- does not allocate any more
 * than is exactly necessary.
 */
Inline void
vector_set_min_length(vector v, vector_length_t len)
{
  if (len > v->end)                     /* will not reduce the length   */
    vector_set_new_min_length(v, len) ;
} ;

/*------------------------------------------------------------------------------
 * Want vector to be the given length.
 *
 * If this is less than the current length, items are discarded.  It
 * is the caller's responsibility to have freed anything that needs it.
 *
 * Adjusts logical and physical end of the vector as required, filling
 * with NULLs upto any new logical end -- does not allocate any more
 * than is exactly necessary.
 */
Inline void
vector_set_length(vector v, vector_length_t len)
{
  if (len > v->end)
    vector_set_new_min_length(v, len) ; /* Extend if new length greater */
  else
    v->end = len ;                      /* chop                         */
} ;

/*------------------------------------------------------------------------------
 * Return index of end of vector (index of last item + 1)
 */
Inline vector_length_t
vector_length(vector v)
{
  return v->end ;
} ;

/*------------------------------------------------------------------------------
 * Returns whether vector is empty or not.
 */
Inline bool
vector_is_empty(vector v)
{
  return (v->end == 0) ;
} ;

/*------------------------------------------------------------------------------
 * Returns highly restricted pointer to vector body
 */
Inline vector_body_t
vector_body(vector v)
{
  return (vector_body_t)v->p_items ;
} ;

/*==============================================================================
 * Access functions -- Inline for obvious reasons.
 */

/*------------------------------------------------------------------------------
 * Get pointer to item.  Returns NULL if accessing beyond end.
 */
Inline p_vector_item
vector_get_item(vector v, vector_index_t i)
{
  return (i < v->end) ? v->p_items[i] : NULL ;
} ;

/*------------------------------------------------------------------------------
 * Get pointer to first item.  Returns NULL if vector empty.
 */
Inline p_vector_item
vector_get_first_item(vector v)
{
  return (v->end != 0) ? v->p_items[0] : NULL ;
} ;

/*------------------------------------------------------------------------------
 * Get pointer to last item.  Returns NULL if vector empty.
 */
Inline p_vector_item
vector_get_last_item(vector v)
{
  return (v->end != 0) ? v->p_items[v->end - 1] : NULL ;
} ;

/*------------------------------------------------------------------------------
 * Set item value in vector.  Extend vector if required.
 *
 * NB: it is the caller's responsibility to release memory used by any
 *     current value of the item, if required.
 */
Inline void
vector_set_item(vector v, vector_index_t i, p_vector_item p_v)
{
  vector_ensure(v, i) ;
  v->p_items[i] = (p_vector_item)p_v ;
} ;

/*------------------------------------------------------------------------------
 * Set dst item to be a copy of the src item.  Extend vector if required.
 *
 * NB: it is the caller's responsibility to look after the memory being
 *     used by the current dst item or the new (duplicated) src item.
 */
Inline void
vector_assign_item(vector v, vector_index_t dst, vector_index_t src)
{
  vector_set_item(v, dst, vector_get_item(v, src)) ;
} ;

/*------------------------------------------------------------------------------
 * Push value onto vector, extending as required.
 */
Inline void
vector_push_item(vector v, p_vector_item p_v)
{
  vector_index_t i = vector_extend_by_1(v) ;
  v->p_items[i] = (p_vector_item)p_v ;
} ;

/*------------------------------------------------------------------------------
 * Pop value from vector.  Returns NULL if vector is empty.
 *
 * NB: does NOT change the size of the vector body.
 */
Inline p_vector_item
vector_pop_item(vector v)
{
  return (v->end > 0) ? v->p_items[--v->end] : NULL ;
} ;

/*------------------------------------------------------------------------------
 * Unshift value onto start of vector, extending as required.
 */
Inline void
vector_unshift_item(vector v, p_vector_item p_v)
{
  vector_insert(v, 0, 1) ;
  v->p_items[0] = p_v ;
} ;

/*------------------------------------------------------------------------------
 * Pop value from vector.  Returns NULL if vector is empty.
 * NB: does NOT change the size of the vector body.
 */
Inline p_vector_item
vector_shift_item(vector v)
{
  p_vector_item p_v = vector_get_first_item(v) ;
  vector_delete(v, 0, 1) ;
  return p_v ;
} ;

#endif /* _ZEBRA_VECTOR_H */