libref_array  0.6.0
ref_array.h
1 /*
2  REF ARRAY
3 
4  Header file for of the dynamic array with reference count.
5 
6  Copyright (C) Dmitri Pal <dpal@redhat.com> 2009
7 
8  This program is free software; you can redistribute it and/or modify
9  it under the terms of the GNU Lesser General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef REF_ARRAY_H
21 #define REF_ARRAY_H
22 
23 #include <stdint.h>
24 #include <stdlib.h>
25 
26 struct ref_array;
27 
28 #ifndef EOK
29 #define EOK 0
30 #endif
31 
74 typedef enum
75 {
76  REF_ARRAY_DESTROY,
77  REF_ARRAY_DELETE,
79 
80 
93 typedef void (*ref_array_fn)(void *elem,
94  ref_array_del_enum type,
95  void *data);
96 
112 typedef int (*ref_array_copy_cb)(void *elem,
113  void *new_elem);
114 
131 int ref_array_create(struct ref_array **ra,
132  size_t elem,
133  uint32_t grow_by,
134  ref_array_fn cb,
135  void *data);
136 
145 struct ref_array *ref_array_getref(struct ref_array *ra);
146 
154 void ref_array_destroy(struct ref_array *ra);
155 
172 int ref_array_append(struct ref_array *ra, void *element);
173 
198 void *ref_array_get(struct ref_array *ra, uint32_t idx, void *acptr);
199 
212 int ref_array_getlen(struct ref_array *ra, uint32_t *len);
213 
223 uint32_t ref_array_len(struct ref_array *ra);
224 
251 int ref_array_insert(struct ref_array *ra,
252  uint32_t idx,
253  void *element);
276 int ref_array_replace(struct ref_array *ra,
277  uint32_t idx,
278  void *element);
279 
280 
297 int ref_array_remove(struct ref_array *ra,
298  uint32_t idx);
299 
300 
317 int ref_array_swap(struct ref_array *ra,
318  uint32_t idx1,
319  uint32_t idx2);
320 
321 
337 void ref_array_reset(struct ref_array *ra);
338 
339 
359 int ref_array_copy(struct ref_array *ra,
360  ref_array_copy_cb copy_cb,
361  ref_array_fn cb,
362  void *data,
363  struct ref_array **copy_ra);
364 
365 
366 
380 void ref_array_debug(struct ref_array *ra, int num);
381 
387 #endif
ref_array_del_enum
Enumeration of the delete modes.
Definition: ref_array.h:74
void * ref_array_get(struct ref_array *ra, uint32_t idx, void *acptr)
Get element data.
Definition: ref_array.c:215
void(* ref_array_fn)(void *elem, ref_array_del_enum type, void *data)
Element cleanup callback.
Definition: ref_array.h:93
int ref_array_swap(struct ref_array *ra, uint32_t idx1, uint32_t idx2)
Swap two elements in the array.
Definition: ref_array.c:427
void ref_array_destroy(struct ref_array *ra)
Delete the array.
Definition: ref_array.c:141
struct ref_array * ref_array_getref(struct ref_array *ra)
Get new reference to an array.
Definition: ref_array.c:120
int ref_array_getlen(struct ref_array *ra, uint32_t *len)
Get array length.
Definition: ref_array.c:246
uint32_t ref_array_len(struct ref_array *ra)
Array length.
Definition: ref_array.c:262
int ref_array_append(struct ref_array *ra, void *element)
Add new element to the array.
Definition: ref_array.c:181
int(* ref_array_copy_cb)(void *elem, void *new_elem)
Copy callback.
Definition: ref_array.h:112
void ref_array_reset(struct ref_array *ra)
Reset array.
Definition: ref_array.c:399
int ref_array_replace(struct ref_array *ra, uint32_t idx, void *element)
Replace element in the array.
Definition: ref_array.c:327
int ref_array_create(struct ref_array **ra, size_t elem, uint32_t grow_by, ref_array_fn cb, void *data)
Create referenced array.
Definition: ref_array.c:77
int ref_array_insert(struct ref_array *ra, uint32_t idx, void *element)
Insert a new element into the array.
Definition: ref_array.c:278
void ref_array_debug(struct ref_array *ra, int num)
Print array for debugging purposes.
Definition: ref_array.c:547
int ref_array_copy(struct ref_array *ra, ref_array_copy_cb copy_cb, ref_array_fn cb, void *data, struct ref_array **copy_ra)
Copy array.
Definition: ref_array.c:475
int ref_array_remove(struct ref_array *ra, uint32_t idx)
Remove element from the array.
Definition: ref_array.c:362