rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rkvptrarray.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2018 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_KV_PTR_ARRAY_H__
19#define __R_KV_PTR_ARRAY_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
45#include <rlib/rtypes.h>
46#include <rlib/rref.h>
47
49
51
53#define R_KV_PTR_ARRAY_INVALID_IDX RSIZE_MAX
54
56typedef struct RKVPtrArray RKVPtrArray;
57
62#define R_KV_PTR_ARRAY_INIT_WITH_FUNC(eqfunc) { R_REF_STATIC_INIT (NULL), eqfunc, 0, 0, NULL }
64#define R_KV_PTR_ARRAY_INIT R_KV_PTR_ARRAY_INIT_WITH_FUNC (NULL)
66#define R_KV_PTR_ARRAY_INIT_STR R_KV_PTR_ARRAY_INIT_WITH_FUNC (r_str_equal)
75#define r_kv_ptr_array_init_str(array) r_kv_ptr_array_init (array, r_str_equal)
78
80#define r_kv_ptr_array_new() r_kv_ptr_array_new_sized (0, NULL)
82#define r_kv_ptr_array_new_str() r_kv_ptr_array_new_sized (0, r_str_equal)
89#define r_kv_ptr_array_ref r_ref_ref
91#define r_kv_ptr_array_unref r_ref_unref
92
94#define r_kv_ptr_array_size(array) (array)->nsize
96#define r_kv_ptr_array_alloc_size(array) (array)->nalloc
97
117
119#define r_kv_ptr_array_find(array, key) r_kv_ptr_array_find_range (array, key, 0, -1)
125
142 rpointer key, RDestroyNotify keynotify, rpointer val, RDestroyNotify valnotify);
145 rpointer key, RDestroyNotify keynotify, rpointer val, RDestroyNotify valnotify);
149#define r_kv_ptr_array_remove_idx(array, idx) r_kv_ptr_array_remove_range (array, idx, 1)
151#define r_kv_ptr_array_remove_all(array) r_kv_ptr_array_remove_range (array, 0, -1)
156
164 rsize idx, rssize size, RKeyValueFunc func, rpointer user);
166#define r_kv_ptr_array_foreach(array, func, user) \
167 r_kv_ptr_array_foreach_range (array, 0, -1, func, user)
168
178 rsize idx, rssize size, RKeyValueConstFunc func, rpointer user);
180#define r_kv_ptr_array_foreach_const(array, func, user) \
181 r_kv_ptr_array_foreach_const_range (array, 0, -1, func, user)
182
202
203R_END_DECLS
204
207#endif /* __R_KV_PTR_ARRAY_H__ */
208
void r_kv_ptr_array_clear(RKVPtrArray *array)
Drop every pair, run destroy notifiers, release storage.
rsize r_kv_ptr_array_update_idx(RKVPtrArray *array, rsize idx, rpointer key, RDestroyNotify keynotify, rpointer val, RDestroyNotify valnotify)
Overwrite slot idx; previous key/value run through their destroy notifiers.
rpointer r_kv_ptr_array_get_key(RKVPtrArray *array, rsize idx)
Key at idx.
rsize r_kv_ptr_array_add(RKVPtrArray *array, rpointer key, RDestroyNotify keynotify, rpointer val, RDestroyNotify valnotify)
Append a (key, value) pair with per-side destroy notifiers.
rsize r_kv_ptr_array_remove_range(RKVPtrArray *array, rsize idx, rssize size)
Stable range remove; destroy notifiers run on each entry.
rsize r_kv_ptr_array_foreach_range(RKVPtrArray *array, rsize idx, rssize size, RKeyValueFunc func, rpointer user)
Invoke func on each pair in [idx, idx+size).
rsize r_kv_ptr_array_foreach_const_range(const RKVPtrArray *array, rsize idx, rssize size, RKeyValueConstFunc func, rpointer user)
Const-visitor variant of r_kv_ptr_array_foreach_range.
rpointer r_kv_ptr_array_get_val(RKVPtrArray *array, rsize idx)
Value at idx.
void r_kv_ptr_array_init(RKVPtrArray *array, REqualFunc eqfunc)
Initialise an embedded / stack array with the given equality function.
rsize r_kv_ptr_array_remove_key_all(RKVPtrArray *array, rpointer key)
Remove every slot whose key matches key.
rsize r_kv_ptr_array_find_range(const RKVPtrArray *array, rconstpointer key, rsize idx, rssize size)
Find the first slot whose key matches key within [idx, idx+size).
rconstpointer r_kv_ptr_array_get_const(const RKVPtrArray *array, rsize idx, rconstpointer *key)
Const-correct variant of r_kv_ptr_array_get.
rpointer r_kv_ptr_array_get(RKVPtrArray *array, rsize idx, rpointer *key)
Return the value at idx and write the key into key.
RKVPtrArray * r_kv_ptr_array_new_sized(rsize size, REqualFunc eqfunc)
Heap-allocate an array with size initial slots and the caller's equality function.
rsize r_kv_ptr_array_remove_key_first(RKVPtrArray *array, rpointer key)
Remove the first slot whose key matches key.
#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
rboolean(* REqualFunc)(rconstpointer a, rconstpointer b)
Equality predicate.
Definition rtypes.h:407
void(* RKeyValueConstFunc)(rconstpointer key, rconstpointer value, rpointer user)
Const-visitor iteration callback over (key, value, user) triples.
Definition rtypes.h:413
void(* RDestroyNotify)(rpointer ptr)
Destructor callback: free / release the value at ptr.
Definition rtypes.h:401
void(* RKeyValueFunc)(rpointer key, rpointer value, rpointer user)
Iteration callback over (key, value, user) triples.
Definition rtypes.h:411
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
Pre-built hash / equality function pairs for the common key types (pointer-identity,...
Refcount base struct shared by every refcounted type in rlib.
Foundational type aliases used by every rlib header.
Public definition of RKVPtrArray.
Definition rkvptrarray.h:193
rpointer mem
Definition rkvptrarray.h:200
rsize nalloc
Definition rkvptrarray.h:198
RRef ref
Definition rkvptrarray.h:194
REqualFunc eqfunc
Definition rkvptrarray.h:196
rsize nsize
Definition rkvptrarray.h:199
Refcount base struct.
Definition rref.h:58