Cthulhu  0.2.10
Cthulhu compiler collection
map.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 
3 #pragma once
4 
5 #include <ctu_std_api.h>
6 
7 #include "core/analyze.h"
8 
9 #include "std/typeinfo.h"
10 
12 
13 typedef struct arena_t arena_t;
14 typedef struct typevec_t typevec_t;
15 typedef struct vector_t vector_t;
16 
31 
33 typedef struct bucket_t bucket_t;
34 
37 typedef struct map_t
38 {
41 
44 
46  STA_FIELD_RANGE(>, 0) size_t size;
47 
49  STA_FIELD_RANGE(<, size) size_t used;
50 
53 } map_t;
54 
56 CT_STD_API extern const map_t kEmptyMap;
57 
65 
74 CT_STD_API map_t map_make(IN_DOMAIN(>, 0) size_t size, hash_info_t info, IN_NOTNULL arena_t *arena);
75 
84 CT_STD_API map_t *map_new(IN_DOMAIN(>, 0) size_t size, hash_info_t info, IN_NOTNULL arena_t *arena);
85 
95 
102 CT_STD_API void map_set(IN_NOTNULL map_t *map, IN_NOTNULL const void *key, void *value);
103 
111 CT_STD_API void *map_get(IN_NOTNULL const map_t *map, IN_NOTNULL const void *key);
112 
121 CT_STD_API void *map_get_default(IN_NOTNULL const map_t *map, IN_NOTNULL const void *key, void *other);
122 
130 CT_STD_API bool map_contains(IN_NOTNULL const map_t *map, IN_NOTNULL const void *key);
131 
139 CT_STD_API bool map_delete(IN_NOTNULL map_t *map, IN_NOTNULL const void *key);
140 
147 CT_STD_API vector_t *map_values(IN_NOTNULL map_t *map);
148 
156 CT_STD_API typevec_t *map_entries(IN_NOTNULL map_t *map);
157 
164 CT_STD_API size_t map_count(IN_NOTNULL const map_t *map);
165 
171 CT_STD_API void map_reset(IN_NOTNULL map_t *map);
172 
174 typedef struct map_entry_t
175 {
176  const void *key;
177  void *value;
178 } map_entry_t;
179 
182 typedef struct map_iter_t
183 {
184  const map_t *map;
185  size_t index;
186 
189 } map_iter_t;
190 
199 CT_STD_API map_iter_t map_iter(IN_NOTNULL const map_t *map);
200 
208 CT_STD_API map_entry_t map_next(IN_NOTNULL map_iter_t *iter);
209 
216 CT_STD_API bool map_has_next(IN_NOTNULL const map_iter_t *iter);
217 
229 CT_STD_API bool map_next_pair(IN_NOTNULL map_iter_t *iter, IN_NOTNULL const void **key, IN_NOTNULL void **value);
230 
250 #define CTU_MAP_NEXT(iter, key, value) map_next_pair(iter, (const void **)(key), (void **)(value))
251 
253 
CT_NODISCARD size_t size
Definition: scan.h:128
#define CT_PUREFN
mark a function as pure, always returns the same value for the same arguments
Definition: analyze.h:228
#define CT_NODISCARD
mark a function as returning a value that must be used
#define IN_NOTNULL
annotate a parameter as not being null
#define IN_DOMAIN(cmp, it)
annotate a parameter as being bounded by the expression of cmp and it
#define OUT_NOTNULL
Definition: analyze.h:99
#define CT_NOALIAS
mark a function as only modifying pointers passed to it the same as CT_CONSTFN but allowed to modify/...
Definition: analyze.h:226
#define CT_BEGIN_API
Definition: compiler.h:129
#define CT_END_API
Definition: compiler.h:130
CT_STD_API void map_init(OUT_NOTNULL map_t *map, size_t size, hash_info_t info, arena_t *arena)
initialize a map
CT_NODISCARD CT_PUREFN CT_STD_API size_t map_count(const map_t *map)
get the number of key-value pairs in a map
Definition: map.c:176
CT_NODISCARD CT_PUREFN CT_STD_API void * map_get_default(const map_t *map, const void *key, void *other)
get a value from a map or a default value
Definition: map.c:333
CT_STD_API const map_t kEmptyMap
an empty map
Definition: map.c:34
CT_NODISCARD CT_STD_API typevec_t * map_entries(map_t *map)
collect all key-value pairs in a map into a vector returns a typevec_t<map_entry_t>
Definition: map.c:161
CT_NODISCARD CT_STD_API map_t * map_optimal(size_t size, hash_info_t info, arena_t *arena)
create a new map with an optimal size
Definition: optimal.c:85
CT_NODISCARD CT_PUREFN CT_STD_API bool map_has_next(const map_iter_t *iter)
check if a map iterator has more elements
Definition: map.c:509
CT_STD_API void map_reset(map_t *map)
clear all key-value pairs from a map
Definition: map.c:386
CT_STD_API bool map_delete(map_t *map, const void *key)
delete a key-value pair from a map
Definition: map.c:361
CT_NODISCARD CT_STD_API vector_t * map_values(map_t *map)
collect all the values from a map into a vector
Definition: map.c:137
CT_NODISCARD CT_STD_API bool map_next_pair(map_iter_t *iter, const void **key, void **value)
get the next key-value pair from a map iterator returns the values via out parameters for ease of use...
Definition: map.c:492
CT_NODISCARD CT_STD_API map_t map_make(size_t size, hash_info_t info, arena_t *arena)
create a new map on the stack
Definition: map.c:105
CT_NODISCARD CT_PUREFN CT_STD_API map_iter_t map_iter(const map_t *map)
create a new map iterator
Definition: map.c:456
CT_STD_API void map_set(map_t *map, const void *key, void *value)
set a key-value pair in a map
Definition: map.c:294
CT_NODISCARD CT_STD_API map_t * map_new(size_t size, hash_info_t info, arena_t *arena)
create a new map on the heap
Definition: map.c:113
CT_NODISCARD CT_PUREFN CT_STD_API bool map_contains(const map_t *map, const void *key)
check if a map contains a key
Definition: map.c:351
CT_NODISCARD CT_PUREFN CT_STD_API void * map_get(const map_t *map, const void *key)
get a value from a map
Definition: map.c:324
CT_NODISCARD CT_NOALIAS CT_STD_API map_entry_t map_next(map_iter_t *iter)
get the next key-value pair from a map iterator
Definition: map.c:476
an allocator object
Definition: arena.h:86
a single node in a map
Definition: map.c:23
information for using a type in a hashset or hashmap
Definition: typeinfo.h:39
a key-value pair in a map
Definition: map.h:175
const void * key
the key of this entry
Definition: map.h:176
void * value
the value of this entry
Definition: map.h:177
a map iterator handle
Definition: map.h:183
bucket_t * next
the next bucket in the chain
Definition: map.h:188
size_t index
current top level bucket index
Definition: map.h:185
bucket_t * bucket
the current bucket
Definition: map.h:187
const map_t * map
the map being iterated over
Definition: map.h:184
an unordered hash map
Definition: map.h:38
STA_FIELD_RANGE(>, 0) size_t size
the number of top level buckets
STA_FIELD_SIZE(size) bucket_t *data
bucket data
arena_t * arena
the arena this map allocates from
Definition: map.h:40
hash_info_t info
the hash function for this map
Definition: map.h:43
STA_FIELD_RANGE(<, size) size_t used
the number of buckets used
A vector with a fixed type size.
Definition: vector.h:24
a generic vector of pointers
Definition: vector.c:16