ucx
UAP Common Extensions
Loading...
Searching...
No Matches
map.h
Go to the documentation of this file.
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
35
36#ifndef UCX_MAP_H
37#define UCX_MAP_H
38
39#include "common.h"
40#include "collection.h"
41#include "string.h"
42#include "hash_key.h"
43
44#ifndef UCX_LIST_H
45// forward-declare CxList
46typedef struct cx_list_s CxList;
47#endif
48
50typedef struct cx_map_s CxMap;
51
54
57
60
70
78 const CxHashKey *key;
82 void *value;
83};
84
102
111
116
122 void *elem;
123
131
137 size_t slot;
138
143 size_t index;
144
148 size_t elem_size;
149
157
162};
163
171 void (*deallocate)(struct cx_map_s *map);
172
176 void (*clear)(struct cx_map_s *map);
177
184 CxMapEntry (*put)(CxMap *map, CxHashKey key, void *value);
185
189 void *(*get)(const CxMap *map, CxHashKey key);
190
201 int (*remove)(CxMap *map, CxHashKey key, void *targetbuf);
202
207};
208
217CX_EXPORT extern CxMap *const cxEmptyMap;
218
227void cxMapFree(CxMap *map);
228
237void cxMapClear(CxMap *map);
238
246size_t cxMapSize(const CxMap *map);
247
263
278
295
317int cx_map_put(CxMap *map, CxHashKey key, void *value);
318
339#define cxMapPut(map, key, value) cx_map_put(map, CX_HASH_KEY(key), value)
340
360
378#define cxMapEmplace(map, key) cx_map_emplace(map, CX_HASH_KEY(key))
379
393void *cx_map_get(const CxMap *map, CxHashKey key);
394
407#define cxMapGet(map, key) cx_map_get(map, CX_HASH_KEY(key))
408
418#define cxMapContains(map, key) (cxMapGet(map, key) != NULL)
419
436int cx_map_remove(CxMap *map, CxHashKey key, void *targetbuf);
437
451#define cxMapRemove(map, key) cx_map_remove(map, CX_HASH_KEY(key), NULL)
452
473#define cxMapRemoveAndGet(map, key, targetbuf) cx_map_remove(map, CX_HASH_KEY(key), targetbuf)
474
498int cxMapClone(CxMap *dst, const CxMap *src,
499 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
500
513CX_EXTERN CX_NONNULL_ARG(1, 2, 3, 4)
514int cxMapDifference(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend,
515 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
516
534CX_EXTERN CX_NONNULL_ARG(1, 2, 3, 4)
535int cxMapListDifference(CxMap *dst, const CxMap *src, const CxList *keys,
536 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
537
538
551CX_EXTERN CX_NONNULL_ARG(1, 2, 3, 4)
552int cxMapIntersection(CxMap *dst, const CxMap *src, const CxMap *other,
553 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
554
572CX_EXTERN CX_NONNULL_ARG(1, 2, 3, 4)
573int cxMapListIntersection(CxMap *dst, const CxMap *src, const CxList *keys,
574 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
575
593int cxMapUnion(CxMap *dst, const CxMap *src,
594 cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data);
595
620int cxMapCloneShallow(CxMap *dst, const CxMap *src);
621
635int cxMapDifferenceShallow(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend);
636
656int cxMapListDifferenceShallow(CxMap *dst, const CxMap *src, const CxList *keys);
657
671int cxMapIntersectionShallow(CxMap *dst, const CxMap *src, const CxMap *other);
672
691int cxMapListIntersectionShallow(CxMap *dst, const CxMap *src, const CxList *keys);
692
710int cxMapUnionShallow(CxMap *dst, const CxMap *src);
711
725int cxMapCompare(const CxMap *map, const CxMap *other);
726
727#endif // UCX_MAP_H
void *(* cx_clone_func)(void *target, const void *source, const CxAllocator *allocator, void *data)
Function pointer type for clone functions.
Definition allocator.h:141
struct cx_allocator_s CxAllocator
High-Level type alias for the allocator type.
Definition allocator.h:80
Common definitions for various collection implementations.
#define CX_COLLECTION_BASE
Use this macro to declare common members for a collection structure.
Definition collection.h:118
Common definitions and feature checks.
#define CX_EXPORT
Only used for building Windows DLLs.
Definition common.h:289
#define CX_NONNULL
All pointer arguments must be non-NULL.
Definition common.h:141
#define CX_NODISCARD
Warn about discarded return value.
Definition common.h:256
#define CX_NONNULL_ARG(...)
The specified pointer arguments must be non-NULL.
Definition common.h:146
#define CX_EXTERN
Declares a function with external linkage.
Definition common.h:297
Interface for map implementations.
struct cx_hash_key_s CxHashKey
Type for a hash key.
Definition hash_key.h:61
#define CX_ITERATOR_BASE
Declares base attributes for an iterator.
Definition iterator.h:94
struct cx_list_s CxList
Common type for all list implementations.
Definition list.h:179
int cx_map_put(CxMap *map, CxHashKey key, void *value)
Puts a key/value-pair into the map.
int cxMapClone(CxMap *dst, const CxMap *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Performs a deep clone of one map into another.
CxMapIterator cxMapIterator(const CxMap *map)
Creates an iterator for a map.
int cxMapCloneShallow(CxMap *dst, const CxMap *src)
Performs a shallow clone of one map into another.
int cxMapUnion(CxMap *dst, const CxMap *src, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Clones entries into a map if their key does not exist yet.
cx_map_iterator_type
The type of iterator for a map.
Definition map.h:88
@ CX_MAP_ITERATOR_KEYS
Iterates over keys only.
Definition map.h:96
@ CX_MAP_ITERATOR_PAIRS
Iterates over key/value pairs.
Definition map.h:92
@ CX_MAP_ITERATOR_VALUES
Iterates over values only.
Definition map.h:100
int cx_map_remove(CxMap *map, CxHashKey key, void *targetbuf)
Removes a key/value-pair from the map by using the key.
struct cx_map_iterator_s CxMapIterator
Type for a map iterator.
Definition map.h:56
int cxMapListDifferenceShallow(CxMap *dst, const CxMap *src, const CxList *keys)
Clones entries of a map if their key is not present in a list.
int cxMapIntersection(CxMap *dst, const CxMap *src, const CxMap *other, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Clones entries of a map only if their key is present in another map.
struct cx_map_entry_s CxMapEntry
Type for a map entry.
Definition map.h:53
struct cx_map_class_s cx_map_class
Type for map class definitions.
Definition map.h:59
int cxMapListIntersectionShallow(CxMap *dst, const CxMap *src, const CxList *keys)
Clones entries of a map only if their key is present in a list.
int cxMapListDifference(CxMap *dst, const CxMap *src, const CxList *keys, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Clones entries of a map if their key is not present in a list.
void cxMapFree(CxMap *map)
Deallocates the memory of the specified map.
int cxMapDifference(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Clones entries of a map if their key is not present in another map.
int cxMapDifferenceShallow(CxMap *dst, const CxMap *minuend, const CxMap *subtrahend)
Clones entries of a map if their key is not present in another map.
int cxMapIntersectionShallow(CxMap *dst, const CxMap *src, const CxMap *other)
Clones entries of a map only if their key is present in another map.
void * cx_map_emplace(CxMap *map, CxHashKey key)
Allocates memory for a value in the map associated with the specified key.
struct cx_map_s CxMap
Type for the UCX map.
Definition map.h:50
int cxMapCompare(const CxMap *map, const CxMap *other)
Compares the entries of two maps.
int cxMapUnionShallow(CxMap *dst, const CxMap *src)
Clones entries into a map if their key does not exist yet.
int cxMapListIntersection(CxMap *dst, const CxMap *src, const CxList *keys, cx_clone_func clone_func, const CxAllocator *clone_allocator, void *data)
Clones entries of a map only if their key is present in a list.
CxMapIterator cxMapIteratorValues(const CxMap *map)
Creates a value iterator for a map.
void cxMapClear(CxMap *map)
Clears a map by removing all elements.
CxMapIterator cxMapIteratorKeys(const CxMap *map)
Creates a key iterator for a map.
CxMap *const cxEmptyMap
A shared instance of an empty map.
size_t cxMapSize(const CxMap *map)
Returns the number of elements in this map.
void * cx_map_get(const CxMap *map, CxHashKey key)
Retrieves a value by using a key.
Strings that know their length.
Structure for holding the base data of a list.
Definition list.h:50
The class definition for arbitrary maps.
Definition map.h:167
void(* deallocate)(struct cx_map_s *map)
Deallocates the entire memory.
Definition map.h:171
int(* remove)(CxMap *map, CxHashKey key, void *targetbuf)
Removes an element.
Definition map.h:201
void(* clear)(struct cx_map_s *map)
Removes all elements.
Definition map.h:176
CxMapIterator(* iterator)(const CxMap *map, enum cx_map_iterator_type type)
Creates an iterator for this map.
Definition map.h:206
CxMapEntry(* put)(CxMap *map, CxHashKey key, void *value)
Add or overwrite an element.
Definition map.h:184
A map entry.
Definition map.h:74
const CxHashKey * key
A pointer to the key.
Definition map.h:78
void * value
A pointer to the value.
Definition map.h:82
Internal iterator struct - use CxMapIterator.
Definition map.h:106
size_t slot
Field for storing the current slot number.
Definition map.h:137
CxMapEntry entry
Reserved memory for a map entry.
Definition map.h:130
size_t index
Counts the elements successfully.
Definition map.h:143
size_t elem_count
May contain the total number of elements, if known.
Definition map.h:156
void * elem
Handle for the current element.
Definition map.h:122
size_t elem_size
The size of a value stored in this map.
Definition map.h:148
enum cx_map_iterator_type type
The type of this iterator.
Definition map.h:161
CxMap * map
Handle for the source map.
Definition map.h:115
Structure for the UCX map.
Definition map.h:62
cx_map_class * cl
The map class definition.
Definition map.h:68