1 /* ptrarray.h -- an expanding array of pointers
2  *
3  * Copyright (c) 1994-2011 Carnegie Mellon University.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. The name "Carnegie Mellon University" must not be used to
18  *    endorse or promote products derived from this software without
19  *    prior written permission. For permission or any legal
20  *    details, please contact
21  *      Carnegie Mellon University
22  *      Center for Technology Transfer and Enterprise Creation
23  *      4615 Forbes Avenue
24  *      Suite 302
25  *      Pittsburgh, PA  15213
26  *      (412) 268-7393, fax: (412) 268-7395
27  *      innovation@andrew.cmu.edu
28  *
29  * 4. Redistributions of any form whatsoever must retain the following
30  *    acknowledgment:
31  *    "This product includes software developed by Computing Services
32  *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33  *
34  * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35  * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36  * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37  * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39  * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41  *
42  * Author: Greg Banks
43  * Start Date: 2011/01/11
44  */
45 
46 #ifndef __CYRUS_PTRARRAY_H__
47 #define __CYRUS_PTRARRAY_H__
48 
49 #include <config.h>
50 #include <sys/types.h>
51 
52 typedef struct
53 {
54     int count;
55     int alloc;
56     void **data;
57 } ptrarray_t;
58 
59 #define PTRARRAY_INITIALIZER    { 0, 0, NULL }
60 #define ptrarray_init(pa)   (memset((pa), 0, sizeof(ptrarray_t)))
61 void ptrarray_fini(ptrarray_t *);
62 
63 ptrarray_t *ptrarray_new(void);
64 void ptrarray_free(ptrarray_t *);
65 
66 void ptrarray_append(ptrarray_t *, void *);
67 void ptrarray_add(ptrarray_t *, void *);
68 void ptrarray_set(ptrarray_t *, int idx, void *);
69 void ptrarray_insert(ptrarray_t *, int idx, void *);
70 void *ptrarray_remove(ptrarray_t *, int idx);
71 void *ptrarray_nth(const ptrarray_t *pa, int idx);
72 void ptrarray_truncate(ptrarray_t *pa, int newlen);
73 
74 #define ptrarray_shift(pa)          ptrarray_remove((pa), 0)
75 #define ptrarray_unshift(pa, s)     ptrarray_insert((pa), 0, (s))
76 
77 #define ptrarray_pop(pa)            ptrarray_remove((pa), -1)
78 #define ptrarray_push(pa, s)        ptrarray_append((pa), (s))
79 
80 #define ptrarray_tail(pa)           ptrarray_nth((pa), -1)
81 #define ptrarray_head(pa)           ptrarray_nth((pa), 0)
82 
83 void **ptrarray_takevf(ptrarray_t *pa);
84 
85 int ptrarray_find(const ptrarray_t *pa, void *match,
86                   int starting);
87 
88 void ptrarray_sort(ptrarray_t *pa, int (*compare)(const void **, const void **));
89 
90 int ptrarray_size(const ptrarray_t *pa);
91 
92 #endif /* __CYRUS_PTRARRAY_H__ */
93