rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rdictionary.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_DICTIONARY_H__
19#define __R_DICTIONARY_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
49
51
53#define RDictionary RHashTable
54
56static inline RDictionary * r_dictionary_new (void) R_ATTR_MALLOC;
58static inline RDictionary * r_dictionary_new_full (RDestroyNotify notify) R_ATTR_MALLOC;
60#define r_dictionary_ref r_ref_ref
62#define r_dictionary_unref r_ref_unref
63
65static inline rsize r_dictionary_size (RDictionary * dict);
68
72static inline rboolean r_dictionary_insert (RDictionary * dict, const rchar * key, rpointer value);
74static inline rpointer r_dictionary_lookup (RDictionary * dict, const rchar * key);
76static inline rboolean r_dictionary_lookup_full (RDictionary * dict, const rchar * key, rpointer * value);
78static inline rboolean r_dictionary_contains (RDictionary * dict, const rchar * key);
80static inline rboolean r_dictionary_steal (RDictionary * dict, const rchar * key, rpointer * value);
82static inline rboolean r_dictionary_remove (RDictionary * dict, const rchar * key);
83
85static inline rboolean r_dictionary_foreach (RDictionary * dict, RStrKeyValueFunc func, rpointer user);
86
87R_END_DECLS
88
89
90static inline RDictionary * r_dictionary_new (void)
91{
93}
94
99
100static inline rsize r_dictionary_size (RDictionary * dict)
101{
102 return r_hash_table_size (dict);
103}
104
106{
108}
109
110static inline rboolean r_dictionary_insert (RDictionary * dict, const rchar * key, rpointer value)
111{
112 return r_hash_table_insert (dict, (rpointer)key, value) == R_HASH_TABLE_OK;
113}
114
115static inline rpointer r_dictionary_lookup (RDictionary * dict, const rchar * key)
116{
117 return r_hash_table_lookup (dict, key);
118}
119
120static inline rboolean r_dictionary_lookup_full (RDictionary * dict, const rchar * key, rpointer * value)
121{
122 return r_hash_table_lookup_full (dict, key, NULL, value) == R_HASH_TABLE_OK;
123}
124
125static inline rboolean r_dictionary_contains (RDictionary * dict, const rchar * key)
126{
127 return r_hash_table_contains (dict, key) == R_HASH_TABLE_OK;
128}
129
130static inline rboolean r_dictionary_steal (RDictionary * dict, const rchar * key, rpointer * value)
131{
132 return r_hash_table_steal (dict, key, NULL, value) == R_HASH_TABLE_OK;
133}
134
135static inline rboolean r_dictionary_remove (RDictionary * dict, const rchar * key)
136{
137 return r_hash_table_remove (dict, key) == R_HASH_TABLE_OK;
138}
139
141{
142 return r_hash_table_foreach (dict, (RKeyValueFunc)func, user) == R_HASH_TABLE_OK;
143}
144
147#endif /* __R_DICTIONARY_H__ */
148
static RDictionary * r_dictionary_new(void)
Construct an empty string-keyed dictionary (no value notifier).
Definition rdictionary.h:90
static rboolean r_dictionary_contains(RDictionary *dict, const rchar *key)
TRUE iff key is present.
Definition rdictionary.h:125
static rboolean r_dictionary_remove(RDictionary *dict, const rchar *key)
Remove key (value destroy notifier runs).
Definition rdictionary.h:135
static rboolean r_dictionary_lookup_full(RDictionary *dict, const rchar *key, rpointer *value)
Look up key, returning the value via value.
Definition rdictionary.h:120
static rsize r_dictionary_size(RDictionary *dict)
Number of entries currently in the dictionary.
Definition rdictionary.h:100
static RDictionary * r_dictionary_new_full(RDestroyNotify notify)
Construct an empty dictionary with a value destroy notifier.
Definition rdictionary.h:95
static rboolean r_dictionary_steal(RDictionary *dict, const rchar *key, rpointer *value)
Remove key without running the value destroy notifier; return the value via value.
Definition rdictionary.h:130
static rpointer r_dictionary_lookup(RDictionary *dict, const rchar *key)
Return the value associated with key, or NULL.
Definition rdictionary.h:115
static rboolean r_dictionary_insert(RDictionary *dict, const rchar *key, rpointer value)
Insert (key, value).
Definition rdictionary.h:110
#define RDictionary
Alias: RDictionary is just an RHashTable.
Definition rdictionary.h:53
static rboolean r_dictionary_foreach(RDictionary *dict, RStrKeyValueFunc func, rpointer user)
Iterate every entry in the dictionary.
Definition rdictionary.h:140
static rsize r_dictionary_current_alloc_size(RDictionary *dict)
Allocated bucket count.
Definition rdictionary.h:105
rsize r_str_hash(rconstpointer data)
Hash a NUL-terminated C string.
rboolean r_str_equal(rconstpointer a, rconstpointer b)
Equality for NUL-terminated C strings (via strcmp).
RHashTable * r_hash_table_new_full(RHashFunc hash, REqualFunc equal, RDestroyNotify keynotify, RDestroyNotify valuenotify)
Construct a hash table with custom hash / equality and optional per-side destroy notifiers.
RHashTableError r_hash_table_remove(RHashTable *ht, rconstpointer key)
Remove the entry for key; destroy notifiers run.
RHashTableError r_hash_table_steal(RHashTable *ht, rconstpointer key, rpointer *keyout, rpointer *valueout)
Remove the entry for key without running its destroy notifiers, handing ownership of the key / value ...
RHashTableError r_hash_table_foreach(RHashTable *ht, RKeyValueFunc func, rpointer user)
Iterate every (key, value) pair in the table.
rsize r_hash_table_current_alloc_size(RHashTable *ht)
Allocated bucket count; useful for sizing diagnostics.
RHashTableError r_hash_table_insert(RHashTable *ht, rpointer key, rpointer value)
Insert or replace (key, value).
RHashTableError r_hash_table_lookup_full(RHashTable *ht, rconstpointer key, rpointer *keyout, rpointer *valueout)
Look up key, returning both the stored key and value.
rsize r_hash_table_size(RHashTable *ht)
Number of entries currently in the table.
rpointer r_hash_table_lookup(RHashTable *ht, rconstpointer key)
Return the value associated with key, or NULL.
RHashTableError r_hash_table_contains(RHashTable *ht, rconstpointer key)
R_HASH_TABLE_OK iff key is present, R_HASH_TABLE_NOT_FOUND otherwise.
@ R_HASH_TABLE_OK
Definition rhashtable.h:71
#define NULL
Null pointer constant (defined only if absent).
Definition rmacros.h:126
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
char rchar
Default character type (char).
Definition rtypes.h:137
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
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
unsigned long rsize
Unsigned pointer-sized size type (like size_t).
Definition rtypes.h:290
void(* RStrKeyValueFunc)(const rchar *key, rpointer value, rpointer user)
Iteration callback over (const char *key, value, user).
Definition rtypes.h:415
Refcounted hash-map container.