1 /* |
1 /* |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 * |
3 * |
4 * Copyright 2015 Olaf Wintermann. All rights reserved. |
4 * Copyright 2016 Olaf Wintermann. All rights reserved. |
5 * |
5 * |
6 * Redistribution and use in source and binary forms, with or without |
6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions are met: |
7 * modification, are permitted provided that the following conditions are met: |
8 * |
8 * |
9 * 1. Redistributions of source code must retain the above copyright |
9 * 1. Redistributions of source code must retain the above copyright |
57 * <code>value</code> variable must be already declared as type information |
57 * <code>value</code> variable must be already declared as type information |
58 * cannot be inferred. |
58 * cannot be inferred. |
59 * |
59 * |
60 * @param key the variable name for the key |
60 * @param key the variable name for the key |
61 * @param value the variable name for the value |
61 * @param value the variable name for the value |
62 * @param iter an UcxMapIterator |
62 * @param iter a UcxMapIterator |
63 * @see ucx_map_iterator() |
63 * @see ucx_map_iterator() |
64 */ |
64 */ |
65 #define UCX_MAP_FOREACH(key,value,iter) \ |
65 #define UCX_MAP_FOREACH(key,value,iter) \ |
66 for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&value);) |
66 for(UcxKey key;ucx_map_iter_next(&iter,&key, (void**)&value);) |
67 |
67 |
68 /** Type for the UCX map. @see UcxMap */ |
68 /** Type for the UCX map. @see UcxMap */ |
69 typedef struct UcxMap UcxMap; |
69 typedef struct UcxMap UcxMap; |
70 |
70 |
71 /** Type for a key of an UcxMap. @see UcxKey */ |
71 /** Type for a key of a UcxMap. @see UcxKey */ |
72 typedef struct UcxKey UcxKey; |
72 typedef struct UcxKey UcxKey; |
73 |
73 |
74 /** Type for an element of an UcxMap. @see UcxMapElement */ |
74 /** Type for an element of a UcxMap. @see UcxMapElement */ |
75 typedef struct UcxMapElement UcxMapElement; |
75 typedef struct UcxMapElement UcxMapElement; |
76 |
76 |
77 /** Type for an iterator over an UcxMap. @see UcxMapIterator */ |
77 /** Type for an iterator over a UcxMap. @see UcxMapIterator */ |
78 typedef struct UcxMapIterator UcxMapIterator; |
78 typedef struct UcxMapIterator UcxMapIterator; |
79 |
79 |
80 /** Structure for the UCX map. */ |
80 /** Structure for the UCX map. */ |
81 struct UcxMap { |
81 struct UcxMap { |
82 /** An allocator that is used for the map elements. */ |
82 /** An allocator that is used for the map elements. */ |
87 size_t size; |
87 size_t size; |
88 /** The count of elements currently stored in this map. */ |
88 /** The count of elements currently stored in this map. */ |
89 size_t count; |
89 size_t count; |
90 }; |
90 }; |
91 |
91 |
92 /** Structure for a key of an UcxMap. */ |
92 /** Structure for a key of a UcxMap. */ |
93 struct UcxKey { |
93 struct UcxKey { |
94 /** The key data. */ |
94 /** The key data. */ |
95 void *data; |
95 void *data; |
96 /** The length of the key data. */ |
96 /** The length of the key data. */ |
97 size_t len; |
97 size_t len; |
98 /** The hash value of the key data. */ |
98 /** The hash value of the key data. */ |
99 int hash; |
99 int hash; |
100 }; |
100 }; |
101 |
101 |
102 /** Structure for an element of an UcxMap. */ |
102 /** Structure for an element of a UcxMap. */ |
103 struct UcxMapElement { |
103 struct UcxMapElement { |
104 /** The value data. */ |
104 /** The value data. */ |
105 void *data; |
105 void *data; |
106 |
106 |
107 /** A pointer to the next element in the current list. */ |
107 /** A pointer to the next element in the current list. */ |
109 |
109 |
110 /** The corresponding key. */ |
110 /** The corresponding key. */ |
111 UcxKey key; |
111 UcxKey key; |
112 }; |
112 }; |
113 |
113 |
114 /** Structure for an iterator over an UcxMap. */ |
114 /** Structure for an iterator over a UcxMap. */ |
115 struct UcxMapIterator { |
115 struct UcxMapIterator { |
116 /** The map to iterate over. */ |
116 /** The map to iterate over. */ |
117 UcxMap *map; |
117 UcxMap *map; |
118 |
118 |
119 /** The current map element. */ |
119 /** The current map element. */ |
134 * @return a pointer to the new hash map |
134 * @return a pointer to the new hash map |
135 */ |
135 */ |
136 UcxMap *ucx_map_new(size_t size); |
136 UcxMap *ucx_map_new(size_t size); |
137 |
137 |
138 /** |
138 /** |
139 * Creates a new hash map with the specified size using an UcxAllocator. |
139 * Creates a new hash map with the specified size using a UcxAllocator. |
140 * @param allocator the allocator to use |
140 * @param allocator the allocator to use |
141 * @param size the size of the hash map |
141 * @param size the size of the hash map |
142 * @return a pointer to the new hash map |
142 * @return a pointer to the new hash map |
143 */ |
143 */ |
144 UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size); |
144 UcxMap *ucx_map_new_a(UcxAllocator *allocator, size_t size); |
355 */ |
355 */ |
356 #define ucx_map_int_remove(map, key) \ |
356 #define ucx_map_int_remove(map, key) \ |
357 ucx_map_remove(map, ucx_key((void*)&key, sizeof(key))) |
357 ucx_map_remove(map, ucx_key((void*)&key, sizeof(key))) |
358 |
358 |
359 /** |
359 /** |
360 * Creates an UcxKey based on the given data. |
360 * Creates a UcxKey based on the given data. |
361 * |
361 * |
362 * This function implicitly computes the hash. |
362 * This function implicitly computes the hash. |
363 * |
363 * |
364 * @param data the data for the key |
364 * @param data the data for the key |
365 * @param len the length of the data |
365 * @param len the length of the data |
366 * @return an UcxKey with implicitly computed hash |
366 * @return a UcxKey with implicitly computed hash |
367 * @see ucx_hash() |
367 * @see ucx_hash() |
368 */ |
368 */ |
369 UcxKey ucx_key(void *data, size_t len); |
369 UcxKey ucx_key(void *data, size_t len); |
370 |
370 |
371 /** |
371 /** |
378 int ucx_hash(const char *data, size_t len); |
378 int ucx_hash(const char *data, size_t len); |
379 |
379 |
380 /** |
380 /** |
381 * Creates an iterator for a map. |
381 * Creates an iterator for a map. |
382 * |
382 * |
383 * <b>Note:</b> An UcxMapIterator iterates over all elements in all element |
383 * <b>Note:</b> A UcxMapIterator iterates over all elements in all element |
384 * lists successively. Therefore the order highly depends on the key hashes and |
384 * lists successively. Therefore the order highly depends on the key hashes and |
385 * may vary under different map sizes. So generally you may <b>NOT</b> rely on |
385 * may vary under different map sizes. So generally you may <b>NOT</b> rely on |
386 * the iteration order. |
386 * the iteration order. |
387 * |
387 * |
388 * <b>Note:</b> The iterator is <b>NOT</b> initialized. You need to call |
388 * <b>Note:</b> The iterator is <b>NOT</b> initialized. You need to call |
389 * ucx_map_iter_next() at least once before accessing any information. However, |
389 * ucx_map_iter_next() at least once before accessing any information. However, |
390 * it is not recommended to access the fields of an UcxMapIterator directly. |
390 * it is not recommended to access the fields of a UcxMapIterator directly. |
391 * |
391 * |
392 * @param map the map to create the iterator for |
392 * @param map the map to create the iterator for |
393 * @return an iterator initialized on the first element of the |
393 * @return an iterator initialized on the first element of the |
394 * first element list |
394 * first element list |
395 * @see ucx_map_iter_next() |
395 * @see ucx_map_iter_next() |