TeoCCl library  0.1.7
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
map.h
Go to the documentation of this file.
1 /*
2  * The MIT License
3  *
4  * Copyright 2016-2018 Kirill Scherba <kirill@scherba.ru>.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
33 #ifndef MAP_H
34 #define MAP_H
35 
36 #include <string.h>
37 #include <stdint.h>
38 
39 #include "teoccl/queue.h"
40 
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44 
45 #define HASH_TABLE_SIZE 127
46 #define HASH_TABLE_INITVAL 77557755
47 
48 typedef struct teoMap {
49 
50  size_t length;
53  uint32_t collisions;
54  size_t hash_map_size;
55 
56 } teoMap;
57 
58 typedef struct teoMapElementData {
59 
60  uint32_t hash;
61  size_t key_length;
62  size_t data_length;
63  uint8_t data[]; // Key + Data
64 
66 
67 typedef struct teoMapIterator {
68 
69  uint32_t idx;
73 
75 
76 
85 uint8_t *teoMapGetFirst(teoMap *map, size_t *data_length);
86 
93 size_t teoMapSize(teoMap *map);
94 
95 
104 teoMap *teoMapNew(size_t size, int auto_resize_f);
105 
111 void teoMapDestroy(teoMap *map);
112 
113 
119 void teoMapClear(teoMap *map);
120 
121 
133 uint8_t *teoMapAdd(teoMap *map, const uint8_t *key, size_t key_length, const uint8_t *data,
134  size_t data_length);
135 
136 
146 static
147 inline uint8_t *teoMapAddStr(teoMap *map, const char *key, const uint8_t *data,
148  size_t data_length) {
149  return teoMapAdd(map, (const uint8_t*)key, strlen(key) + 1, data, data_length);
150 }
151 
152 uint8_t *teoMapGet(teoMap *map, const uint8_t *key, size_t key_length,
153  size_t *data_length);
154 
164 static
165 inline uint8_t *teoMapGetStr(teoMap *map, const char *key, size_t *data_length) {
166  return teoMapGet(map, (const uint8_t*)key, strlen(key) + 1, data_length);
167 }
168 
169 int teoMapDelete(teoMap *map, const uint8_t *key, size_t key_length);
170 
178 static
179 inline int teoMapDeleteStr(teoMap *map, const char *key) {
180  return teoMapDelete(map, (const uint8_t*)key, strlen(key) + 1);
181 }
182 
184 void teoMapIteratorReset(teoMapIterator *map_it, teoMap *map);
190 
197 static
198 inline teoMapElementData *teoMapIteratorElement(teoMapIterator *map_it) {
199  return map_it ? map_it->tmv : NULL;
200 }
208 static
209 inline uint8_t *teoMapIteratorElementKey(teoMapElementData *el,
210  size_t *key_length) {
211  if(key_length) *key_length = el->key_length;
212  return el->data;
213 }
221 static
222 inline uint8_t *teoMapIteratorElementData(teoMapElementData *el,
223  size_t *data_length) {
224  if(data_length) *data_length = el->data_length;
225  return el->data + el->key_length;
226 }
227 
228 typedef int (*teoMapForeachFunction)(teoMap *m, int idx, teoMapElementData *d, void* user_data);
229 int teoMapForeach(teoMap *m, teoMapForeachFunction callback, void *user_data);
230 
231 #ifdef __cplusplus
232 }
233 #endif
234 
235 #endif /* MAP_H */
teoMap * map
Definition: map.h:70
uint8_t * teoMapGet(teoMap *map, const uint8_t *key, size_t key_length, size_t *data_length)
Get key data from hash table.
Definition: map.c:345
uint32_t collisions
Definition: map.h:53
teoQueue ** q
Definition: map.h:51
Definition: queue.h:58
size_t data_length
Definition: map.h:62
size_t hash_map_size
Definition: map.h:54
struct teoMapIterator teoMapIterator
size_t key_length
Definition: map.h:61
int(* teoMapForeachFunction)(teoMap *m, int idx, teoMapElementData *d, void *user_data)
Definition: map.h:228
teoMap * teoMapNew(size_t size, int auto_resize_f)
Create new map.
Definition: map.c:65
teoMapElementData * tmv
Definition: map.h:72
int teoMapDelete(teoMap *map, const uint8_t *key, size_t key_length)
Delete keys element from map.
Definition: map.c:373
teoMapIterator * teoMapIteratorReverseNew(teoMap *map)
Create new map reverse iterator.
Definition: map.c:432
teoMapIterator * teoMapIteratorNew(teoMap *map)
Create new map iterator.
Definition: map.c:402
Definition: queue.h:66
teoMapElementData * teoMapIteratorNext(teoMapIterator *map_it)
Get next maps element.
Definition: map.c:477
int teoMapForeach(teoMap *m, teoMapForeachFunction callback, void *user_data)
Loop through map and call callback function with index and data in parameters.
Definition: map.c:537
size_t length
Definition: map.h:50
Queue module.
size_t teoMapSize(teoMap *map)
Get number of elements in TR-UPD map.
Definition: map.c:54
uint8_t * teoMapAdd(teoMap *map, const uint8_t *key, size_t key_length, const uint8_t *data, size_t data_length)
Add (or update) key data to the map.
Definition: map.c:282
uint32_t hash
Definition: map.h:60
Definition: map.h:48
void teoMapClear(teoMap *map)
Definition: map.c:171
uint8_t data[]
Definition: map.h:63
void teoMapDestroy(teoMap *map)
Destroy map.
Definition: map.c:161
uint32_t idx
Definition: map.h:69
int teoMapIteratorFree(teoMapIterator *map_it)
Destroy map iterator.
Definition: map.c:462
int auto_resize_f
Definition: map.h:52
Definition: map.h:58
Definition: map.h:67
uint8_t * teoMapGetFirst(teoMap *map, size_t *data_length)
Get first available element from hash table.
Definition: map.c:246
teoQueueIterator it
Definition: map.h:71
struct teoMap teoMap
void teoMapIteratorReverseReset(teoMapIterator *map_it, teoMap *map)
Reset map iterator.
Definition: map.c:446
void teoMapIteratorReset(teoMapIterator *map_it, teoMap *map)
Reset map iterator.
Definition: map.c:416
teoMapElementData * teoMapIteratorPrev(teoMapIterator *map_it)
Get previous maps element.
Definition: map.c:506
struct teoMapElementData teoMapElementData