docs/Writerside/topics/map.h.md

changeset 1605
55b13f583356
parent 1604
68b75c091028
equal deleted inserted replaced
1604:68b75c091028 1605:55b13f583356
7 ```C 7 ```C
8 #include <cx/hash_map.h> 8 #include <cx/hash_map.h>
9 9
10 CxMap *cxHashMapCreate(const CxAllocator *allocator, 10 CxMap *cxHashMapCreate(const CxAllocator *allocator,
11 size_t itemsize, size_t buckets); 11 size_t itemsize, size_t buckets);
12
13 CxMap *cxHashMapCreateSimple(size_t itemsize);
14 ``` 12 ```
15 13
16 The function `cxHashMapCreate()` creates a new map where both the map structure 14 The function `cxHashMapCreate()` creates a new map where both the map structure
17 and the contained buckets are allocated by the specified `allocator`. 15 and the contained buckets are allocated by the specified `allocator`.
18 The [default allocator](allocator.h.md#default-allocator) is used in `cxHashMapCreateSimple()`.
19 16
20 The map will store items of size `itemsize`. 17 The map will store items of size `itemsize`.
21 You can use the `CX_STORE_POINTERS` macro for `itemsize` to indicate that the map shall store 18 You can use the `CX_STORE_POINTERS` macro for `itemsize` to indicate that the map shall store
22 pointers instead of actual items. 19 pointers instead of actual items.
23 20
24 If you pass zero for the number of `buckets`, or use `cxHashMapSimple()`, 21 If you pass zero for the number of `buckets`,
25 the map is initialized with a default of 16 buckets; otherwise the specified number of buckets is allocated. 22 the map is initialized with a default of 16 buckets; otherwise the specified number of buckets is allocated.
26 23
27 > If you want to lazy-initialize maps, you can use the global `cxEmptyMap` symbol as a placeholder instead of using a `NULL`-pointer. 24 > If you want to lazy-initialize maps, you can use the global `cxEmptyMap` symbol as a placeholder instead of using a `NULL`-pointer.
28 > While you *must not* insert elements into that map, you can safely access this map or create iterators. 25 > While you *must not* insert elements into that map, you can safely access this map or create iterators.
29 > This allows you to write clean code without checking for `NULL`-pointer everywhere. 26 > This allows you to write clean code without checking for `NULL`-pointer everywhere.
39 ```C 36 ```C
40 #include <stdio.h> 37 #include <stdio.h>
41 #include <cx/hash_map.h> 38 #include <cx/hash_map.h>
42 39
43 int main() { 40 int main() {
44 CxMap *contacts = cxHashMapCreateSimple(CX_STORE_POINTERS); 41 CxMap *contacts = cxHashMapCreate(NULL, CX_STORE_POINTERS, 0);
45 42
46 // Build a small phone book 43 // Build a small phone book
47 cxMapPut(contacts, "John", "123-0815"); 44 cxMapPut(contacts, "John", "123-0815");
48 cxMapPut(contacts, "Jane", "987-4711"); 45 cxMapPut(contacts, "Jane", "987-4711");
49 cxMapPut(contacts, "Michelle", "555-3141"); 46 cxMapPut(contacts, "Michelle", "555-3141");
90 #include <string.h> 87 #include <string.h>
91 #include <cx/hash_map.h> 88 #include <cx/hash_map.h>
92 89
93 int main() { 90 int main() {
94 // store strings in the map... 91 // store strings in the map...
95 CxMap *contacts = cxHashMapCreateSimple(sizeof(cxmutstr)); 92 CxMap *contacts = cxHashMapCreate(NULL, sizeof(cxmutstr), 0);
96 // ...which are automatically freed when removed 93 // ...which are automatically freed when removed
97 cxDefineDestructor(contacts, cx_strfree); 94 cxSetDestructor(contacts, cx_strfree);
98 95
99 // build a small interactive program 96 // build a small interactive program
100 const unsigned buffer_size = 256; 97 const unsigned buffer_size = 256;
101 char input[buffer_size]; 98 char input[buffer_size];
102 bool running = true; 99 bool running = true;

mercurial