rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rptrarray.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2017 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_PTR_ARRAY_H__
19#define __R_PTR_ARRAY_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
49#include <rlib/rtypes.h>
50
51#include <rlib/rref.h>
52
54
56#define R_PTR_ARRAY_INVALID_IDX RSIZE_MAX
57
59typedef struct RPtrArray RPtrArray;
60
65#define R_PTR_ARRAY_INIT { R_REF_STATIC_INIT (NULL), 0, 0, NULL }
70
72#define r_ptr_array_new() r_ptr_array_new_sized (0)
76#define r_ptr_array_ref r_ref_ref
78#define r_ptr_array_unref r_ref_unref
79
81#define r_ptr_array_size(array) (array)->nsize
83#define r_ptr_array_alloc_size(array) (array)->nalloc
84
94
96#define r_ptr_array_find(array, data) r_ptr_array_find_range (array, data, 0, -1)
108
124 rpointer data, RDestroyNotify notify);
129 rpointer data, RDestroyNotify notify);
137 rpointer data, RDestroyNotify notify);
139#define r_ptr_array_clear_idx(array, idx) r_ptr_array_update_idx (array, idx, NULL, NULL)
145
162#define r_ptr_array_remove_all(array) r_ptr_array_remove_range_clear_full (array, 0, -1, NULL, NULL)
164#define r_ptr_array_remove_all_full(array, func, user) r_ptr_array_remove_range_clear_full (array, 0, -1, func, user)
165
167#define r_ptr_array_remove_idx(array, idx) r_ptr_array_remove_idx_full (array, idx, NULL, NULL)
169#define r_ptr_array_remove_idx_fast(array, idx) r_ptr_array_remove_idx_fast_full (array, idx, NULL, NULL)
171#define r_ptr_array_remove_idx_clear(array, idx) r_ptr_array_remove_idx_clear_full (array, idx, NULL, NULL)
174 RFunc func, rpointer user);
177 RFunc func, rpointer user);
180 RFunc func, rpointer user);
181
183#define r_ptr_array_remove_first_full(array, data, func, user) r_ptr_array_remove_idx_full (array, r_ptr_array_find (array, data), func, user)
185#define r_ptr_array_remove_first_fast_full(array, data, func, user) r_ptr_array_remove_idx_fast_full (array, r_ptr_array_find (array, data), func, user)
187#define r_ptr_array_remove_first_clear_full(array, data, func, user) r_ptr_array_remove_idx_clear_full (array, r_ptr_array_find (array, data), func, user)
189#define r_ptr_array_remove_first(array, data) r_ptr_array_remove_first_full (array, data, NULL, NULL)
191#define r_ptr_array_remove_first_fast(array, data) r_ptr_array_remove_first_fast_full (array, data, NULL, NULL)
193#define r_ptr_array_remove_first_clear(array, data) r_ptr_array_remove_first_clear_full (array, data, NULL, NULL)
194
196#define r_ptr_array_remove_range(array, idx, inc) r_ptr_array_remove_range_full (array, idx, inc, NULL, NULL)
198#define r_ptr_array_remove_range_fast(array, idx, inc) r_ptr_array_remove_range_fast_full (array, idx, inc, NULL, NULL)
200#define r_ptr_array_remove_range_clear(array, idx, inc) r_ptr_array_remove_range_clear_full (array, idx, inc, NULL, NULL)
203 rsize idx, rssize size, RFunc func, rpointer user);
206 rsize idx, rssize size, RFunc func, rpointer user);
209 rsize idx, rssize size, RFunc func, rpointer user);
210
217#define r_ptr_array_foreach(array, func, user) r_ptr_array_foreach_range (array, 0, -1, func, user)
220 RFunc func, rpointer user);
221
224
241
242R_END_DECLS
243
246#endif /* __R_PTR_ARRAY_H__ */
247
248
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
rsize r_ptr_array_remove_idx_full(RPtrArray *array, rsize idx, RFunc func, rpointer user)
Stable remove of slot idx; func runs on the displaced item.
rsize r_ptr_array_foreach_range(RPtrArray *array, rsize idx, rssize size, RFunc func, rpointer user)
Invoke func on each item in the range [idx, idx+size).
rsize r_ptr_array_update_idx(RPtrArray *array, rsize idx, rpointer data, RDestroyNotify notify)
Overwrite the slot at idx.
rsize r_ptr_array_remove_idx_fast_full(RPtrArray *array, rsize idx, RFunc func, rpointer user)
Swap-with-last remove of slot idx; func runs on the displaced item.
rsize r_ptr_array_remove_range_fast_full(RPtrArray *array, rsize idx, rssize size, RFunc func, rpointer user)
Swap-with-last range remove with per-item function.
rsize r_ptr_array_clear_range(RPtrArray *array, rsize idx, rssize size)
Write NULL into every slot in the range; previous values run through their destroy notifiers.
rpointer r_ptr_array_get(RPtrArray *array, rsize idx)
Return the pointer stored at idx.
rsize r_ptr_array_remove_range_clear_full(RPtrArray *array, rsize idx, rssize size, RFunc func, rpointer user)
Clear-in-place over the range with per-item function.
void r_ptr_array_init(RPtrArray *array)
Initialise an embedded / stack RPtrArray to empty.
void r_ptr_array_sort(RPtrArray *array, RCmpFunc cmp)
In-place sort using comparator cmp.
rconstpointer r_ptr_array_get_const(const RPtrArray *array, rsize idx)
Const-correct variant of r_ptr_array_get.
rsize r_ptr_array_remove_range_full(RPtrArray *array, rsize idx, rssize size, RFunc func, rpointer user)
Stable range remove with per-item function.
rsize r_ptr_array_remove_idx_clear_full(RPtrArray *array, rsize idx, RFunc func, rpointer user)
Clear-in-place at slot idx; func runs on the displaced item.
RPtrArray * r_ptr_array_new_sized(rsize size)
Heap-allocate an array with a preset initial capacity.
rsize r_ptr_array_add(RPtrArray *array, rpointer data, RDestroyNotify notify)
Append data to the array.
rsize r_ptr_array_insert(RPtrArray *array, rsize idx, rpointer data, RDestroyNotify notify)
Insert data at idx, shifting later items up by one.
rsize r_ptr_array_find_range(RPtrArray *array, rpointer data, rsize idx, rssize size)
Find first occurrence of data within the range starting at idx for size items.
void r_ptr_array_clear(RPtrArray *array)
Drop every item and release backing storage; destroy notifiers run.
void(* RFunc)(rpointer data, rpointer user)
Generic iteration callback over (data, user) pairs.
Definition rtypes.h:403
int(* RCmpFunc)(rconstpointer a, rconstpointer b)
Comparison callback returning <0 / 0 / >0 for a vs b.
Definition rtypes.h:405
void(* RDestroyNotify)(rpointer ptr)
Destructor callback: free / release the value at ptr.
Definition rtypes.h:401
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
const void * rconstpointer
Generic const pointer (alias for const void *).
Definition rtypes.h:329
unsigned long rsize
Unsigned pointer-sized size type (like size_t).
Definition rtypes.h:290
signed long rssize
Signed pointer-sized size type (like ssize_t).
Definition rtypes.h:291
Refcount base struct shared by every refcounted type in rlib.
Foundational type aliases used by every rlib header.
Public definition of RPtrArray.
Definition rptrarray.h:234
rsize nalloc
Definition rptrarray.h:237
rpointer mem
Definition rptrarray.h:239
RRef ref
Definition rptrarray.h:235
rsize nsize
Definition rptrarray.h:238
Refcount base struct.
Definition rref.h:58