Snapdragon® Telematics Application Framework (TelAF) Interface Specification
le_hashmap.h
Go to the documentation of this file.
1 
164 //--------------------------------------------------------------------------------------------------
172 //--------------------------------------------------------------------------------------------------
173 
174 #ifndef LEGATO_HASHMAP_INCLUDE_GUARD
175 #define LEGATO_HASHMAP_INCLUDE_GUARD
176 
177 #include "le_mem.h"
178 
179 #include "le_singlyLinkedList.h"
180 #include "le_doublyLinkedList.h"
181 
182 #if LE_CONFIG_REDUCE_FOOTPRINT
185 #else /* if not LE_CONFIG_REDUCE_FOOTPRINT */
188 #endif /* end LE_CONFIG_REDUCE_FOOTPRINT */
189 
190 //--------------------------------------------------------------------------------------------------
194 //--------------------------------------------------------------------------------------------------
195 typedef struct le_hashmap* le_hashmap_Ref_t;
196 
197 //--------------------------------------------------------------------------------------------------
201 //--------------------------------------------------------------------------------------------------
202 typedef struct le_hashmap_It* le_hashmap_It_Ref_t;
203 
204 //--------------------------------------------------------------------------------------------------
212 //--------------------------------------------------------------------------------------------------
213 typedef size_t (*le_hashmap_HashFunc_t)
214 (
215  const void* keyToHashPtr
216 );
217 
218 //--------------------------------------------------------------------------------------------------
228 //--------------------------------------------------------------------------------------------------
229 typedef bool (*le_hashmap_EqualsFunc_t)
230 (
231  const void* firstKeyPtr,
232  const void* secondKeyPtr
233 );
234 
235 
236 //--------------------------------------------------------------------------------------------------
246 //--------------------------------------------------------------------------------------------------
247 typedef bool (*le_hashmap_ForEachHandler_t)
248 (
249  const void* keyPtr,
250  const void* valuePtr,
251  void* contextPtr
252 );
253 
254 
260 typedef struct le_hashmap_Entry
261 {
263  const void *keyPtr;
264  const void *valuePtr;
265 }
267 
273 typedef struct le_hashmap_It
274 {
275  size_t currentIndex;
277 }
279 
285 typedef struct le_hashmap
286 {
288 
291 
294  size_t bucketCount;
295  size_t size;
296 
297 #if LE_CONFIG_HASHMAP_NAMES_ENABLED
298  const char *nameStr;
299  le_log_TraceRef_t traceRef;
300 #endif /* end LE_CONFIG_HASHMAP_NAMES_ENABLED */
301 }
303 
304 
305 #if LE_CONFIG_HASHMAP_NAMES_ENABLED
306 //--------------------------------------------------------------------------------------------------
322 //--------------------------------------------------------------------------------------------------
324 (
325  const char *nameStr,
326  size_t capacity,
327  le_hashmap_HashFunc_t hashFunc,
328  le_hashmap_EqualsFunc_t equalsFunc
329 );
330 #else /* if not LE_CONFIG_HASHMAP_NAMES_ENABLED */
331 //--------------------------------------------------------------------------------------------------
336 //--------------------------------------------------------------------------------------------------
337 le_hashmap_Ref_t _le_hashmap_Create
338 (
339  size_t capacity,
340  le_hashmap_HashFunc_t hashFunc,
341  le_hashmap_EqualsFunc_t equalsFunc
342 );
344 //--------------------------------------------------------------------------------------------------
360 //--------------------------------------------------------------------------------------------------
362 (
363  const char *nameStr,
364  size_t capacity,
365  le_hashmap_HashFunc_t hashFunc,
366  le_hashmap_EqualsFunc_t equalsFunc
367 )
368 {
369  LE_UNUSED(nameStr);
370  return _le_hashmap_Create(capacity, hashFunc, equalsFunc);
371 }
372 #endif /* end LE_CONFIG_HASHMAP_NAMES_ENABLED */
373 
374 
375 
376 //--------------------------------------------------------------------------------------------------
388 //--------------------------------------------------------------------------------------------------
389 #define LE_HASHMAP_BUCKET_COUNT(capacity) \
390  ((capacity)*4/3<=0x4?0x4: \
391  ((capacity)*4/3<=0x8?0x8: \
392  ((capacity)*4/3<=0x10?0x10: \
393  ((capacity)*4/3<=0x20?0x20: \
394  ((capacity)*4/3<=0x40?0x40: \
395  ((capacity)*4/3<=0x80?0x80: \
396  ((capacity)*4/3<=0x100?0x100: \
397  ((capacity)*4/3<=0x200?0x200: \
398  ((capacity)*4/3<=0x400?0x400: \
399  ((capacity)*4/3<=0x800?0x800: \
400  ((capacity)*4/3<=0x1000?0x1000: \
401  ((capacity)*4/3<=0x2000?0x2000: \
402  ((capacity)*4/3<=0x4000?0x4000: \
403  ((capacity)*4/3<=0x8000?0x8000: \
404  0x10000))))))))))))))
405 
406 //--------------------------------------------------------------------------------------------------
418 //--------------------------------------------------------------------------------------------------
419 #define LE_HASHMAP_DEFINE_STATIC(name, capacity) \
420  static le_hashmap_Hashmap_t _hashmap_##name##Hashmap; \
421  LE_MEM_DEFINE_STATIC_POOL(_hashmap_##name, (capacity), sizeof(le_hashmap_Entry_t)); \
422  static le_hashmap_Bucket_t _hashmap_##name##Buckets[LE_HASHMAP_BUCKET_COUNT(capacity)]
423 
424 //--------------------------------------------------------------------------------------------------
436 //--------------------------------------------------------------------------------------------------
437 #define LE_HASHMAP_DEFINE_STATIC_IN_SECTION(name, capacity, sectionName) \
438  static le_hashmap_Hashmap_t _hashmap_##name##Hashmap __attribute__((section(sectionName))); \
439  LE_MEM_DEFINE_STATIC_POOL_IN_SECTION(_hashmap_##name, (capacity), sizeof(le_hashmap_Entry_t), \
440  sectionName); \
441  static le_hashmap_Bucket_t _hashmap_##name##Buckets[ \
442  LE_HASHMAP_BUCKET_COUNT(capacity)] __attribute__((section(sectionName)))
443 
444 //--------------------------------------------------------------------------------------------------
458 //--------------------------------------------------------------------------------------------------
459 #if LE_CONFIG_HASHMAP_NAMES_ENABLED
460 # define le_hashmap_InitStatic(name, capacity, hashFunc, equalsFunc) \
461  (inline_static_assert( \
462  sizeof(_hashmap_##name##Buckets) == \
463  sizeof(le_hashmap_Entry_t *[LE_HASHMAP_BUCKET_COUNT(capacity)]), \
464  "hashmap init capacity does not match definition"), \
465  _le_hashmap_InitStatic(#name, (capacity), (hashFunc), (equalsFunc), \
466  &_hashmap_##name##Hashmap, \
467  le_mem_InitStaticPool(_hashmap_##name, \
468  (capacity), \
469  sizeof(le_hashmap_Entry_t)), \
470  _hashmap_##name##Buckets))
471 #else
472 # define le_hashmap_InitStatic(name, capacity, hashFunc, equalsFunc) \
473  (inline_static_assert( \
474  sizeof(_hashmap_##name##Buckets) == \
475  sizeof(le_hashmap_Entry_t *[LE_HASHMAP_BUCKET_COUNT(capacity)]), \
476  "hashmap init capacity does not match definition"), \
477  _le_hashmap_InitStatic((capacity), (hashFunc), (equalsFunc), \
478  &_hashmap_##name##Hashmap, \
479  le_mem_InitStaticPool(_hashmap_##name, \
480  (capacity), \
481  sizeof(le_hashmap_Entry_t)), \
482  _hashmap_##name##Buckets))
483 #endif
484 
486 //--------------------------------------------------------------------------------------------------
492 //--------------------------------------------------------------------------------------------------
493 le_hashmap_Ref_t _le_hashmap_InitStatic
494 (
495 #if LE_CONFIG_HASHMAP_NAMES_ENABLED
496  const char *nameStr,
497 #endif
498  size_t capacity,
499  le_hashmap_HashFunc_t hashFunc,
500  le_hashmap_EqualsFunc_t equalsFunc,
501  le_hashmap_Hashmap_t *mapPtr,
502  le_mem_PoolRef_t entryPoolRef,
503  le_hashmap_Bucket_t *bucketsPtr
504 );
506 
507 
508 //--------------------------------------------------------------------------------------------------
516 //--------------------------------------------------------------------------------------------------
517 
518 void* le_hashmap_Put
519 (
520  le_hashmap_Ref_t mapRef,
521  const void* keyPtr,
522  const void* valuePtr
523 );
524 
525 //--------------------------------------------------------------------------------------------------
532 //--------------------------------------------------------------------------------------------------
533 
534 void* le_hashmap_Get
535 (
536  le_hashmap_Ref_t mapRef,
537  const void* keyPtr
538 );
539 
540 //--------------------------------------------------------------------------------------------------
548 //--------------------------------------------------------------------------------------------------
550 (
551  le_hashmap_Ref_t mapRef,
552  const void* keyPtr
553 );
554 
555 //--------------------------------------------------------------------------------------------------
562 //--------------------------------------------------------------------------------------------------
563 
564 void* le_hashmap_Remove
565 (
566  le_hashmap_Ref_t mapRef,
567  const void* keyPtr
568 );
569 
570 //--------------------------------------------------------------------------------------------------
577 //--------------------------------------------------------------------------------------------------
578 
580 (
581  le_hashmap_Ref_t mapRef
582 );
583 
584 //--------------------------------------------------------------------------------------------------
591 //--------------------------------------------------------------------------------------------------
592 
593 size_t le_hashmap_Size
594 (
595  le_hashmap_Ref_t mapRef
596 );
597 
598 //--------------------------------------------------------------------------------------------------
605 //--------------------------------------------------------------------------------------------------
606 
608 (
609  le_hashmap_Ref_t mapRef,
610  const void* keyPtr
611 );
612 
613 //--------------------------------------------------------------------------------------------------
620 //--------------------------------------------------------------------------------------------------
621 
623 (
624  le_hashmap_Ref_t mapRef
625 );
626 
627 //--------------------------------------------------------------------------------------------------
635 //--------------------------------------------------------------------------------------------------
637 (
638  le_hashmap_Ref_t mapRef,
639  le_hashmap_ForEachHandler_t forEachFn,
640  void* contextPtr
641 );
642 
643 //--------------------------------------------------------------------------------------------------
656 //--------------------------------------------------------------------------------------------------
658 (
659  le_hashmap_Ref_t mapRef
660 );
661 
662 //--------------------------------------------------------------------------------------------------
670 //--------------------------------------------------------------------------------------------------
672 (
673  le_hashmap_It_Ref_t iteratorRef
674 );
675 
676 //--------------------------------------------------------------------------------------------------
684 //--------------------------------------------------------------------------------------------------
686 (
687  le_hashmap_It_Ref_t iteratorRef
688 );
689 
690 //--------------------------------------------------------------------------------------------------
700 //--------------------------------------------------------------------------------------------------
701 const void* le_hashmap_GetKey
702 (
703  le_hashmap_It_Ref_t iteratorRef
704 );
705 
706 //--------------------------------------------------------------------------------------------------
716 //--------------------------------------------------------------------------------------------------
718 (
719  le_hashmap_It_Ref_t iteratorRef
720 );
721 
722 //--------------------------------------------------------------------------------------------------
734 //--------------------------------------------------------------------------------------------------
736 (
737  le_hashmap_Ref_t mapRef,
738  void **firstKeyPtr,
739  void **firstValuePtr
740 );
741 
742 //--------------------------------------------------------------------------------------------------
755 //--------------------------------------------------------------------------------------------------
757 (
758  le_hashmap_Ref_t mapRef,
759  const void* keyPtr,
760  void **nextKeyPtr,
761  void **nextValuePtr
762 );
763 
764 
765 //--------------------------------------------------------------------------------------------------
773 //--------------------------------------------------------------------------------------------------
774 
776 (
777  le_hashmap_Ref_t mapRef
778 );
779 
780 //--------------------------------------------------------------------------------------------------
788 //--------------------------------------------------------------------------------------------------
789 
791 (
792  const void* stringToHashPtr
793 );
794 
795 //--------------------------------------------------------------------------------------------------
803 //--------------------------------------------------------------------------------------------------
804 
806 (
807  const void* firstStringPtr,
808  const void* secondStringPtr
809 );
810 
811 //--------------------------------------------------------------------------------------------------
819 //--------------------------------------------------------------------------------------------------
820 
822 (
823  const void* intToHashPtr
824 );
825 
826 //--------------------------------------------------------------------------------------------------
834 //--------------------------------------------------------------------------------------------------
835 
837 (
838  const void* firstIntPtr,
839  const void* secondIntPtr
840 );
841 
842 //--------------------------------------------------------------------------------------------------
850 //--------------------------------------------------------------------------------------------------
851 
853 (
854  const void* intToHashPtr
855 );
856 
857 //--------------------------------------------------------------------------------------------------
865 //--------------------------------------------------------------------------------------------------
866 
868 (
869  const void* firstIntPtr,
870  const void* secondIntPtr
871 );
872 
873 //--------------------------------------------------------------------------------------------------
881 //--------------------------------------------------------------------------------------------------
882 
884 (
885  const void* voidToHashPtr
886 );
887 
888 //--------------------------------------------------------------------------------------------------
896 //--------------------------------------------------------------------------------------------------
897 
899 (
900  const void* firstVoidPtr,
901  const void* secondVoidPtr
902 );
903 
904 
905 //--------------------------------------------------------------------------------------------------
909 //--------------------------------------------------------------------------------------------------
910 #define LE_HASHMAP_MAKE_HASH(type) \
911  static size_t Hash##type \
912  ( \
913  const void* type##Name \
914  ) \
915  { \
916  size_t byte=0, hash = 0; \
917  unsigned char c; \
918  const unsigned char* ptr = type##Name; \
919  for (byte = 0; byte < sizeof(type); ++byte) \
920  { \
921  c = *ptr++; \
922  hash = c + (hash << 6) + (hash << 16) - hash; \
923  } \
924  return hash; \
925  } \
926  static bool Equals##type \
927  ( \
928  const void* first##type, \
929  const void* second##type \
930  ) \
931  { \
932  return memcmp(first##type, second##type, sizeof(type)) == 0; \
933  }
934 
935 
936 
937 //--------------------------------------------------------------------------------------------------
944 //--------------------------------------------------------------------------------------------------
946 (
947  le_hashmap_Ref_t mapRef
948 );
949 
950 
951 //--------------------------------------------------------------------------------------------------
955 //--------------------------------------------------------------------------------------------------
957 (
958  le_hashmap_Ref_t mapRef
959 );
960 
961 
962 #endif /* LEGATO_HASHMAP_INCLUDE_GUARD */
bool le_hashmap_EqualsString(const void *firstStringPtr, const void *secondStringPtr)
le_result_t le_hashmap_NextNode(le_hashmap_It_Ref_t iteratorRef)
Definition: le_hashmap.h:273
size_t currentIndex
Current bucket index.
Definition: le_hashmap.h:275
size_t le_hashmap_CountCollisions(le_hashmap_Ref_t mapRef)
const void * keyPtr
Pointer to key data.
Definition: le_hashmap.h:263
size_t bucketCount
Number of buckets.
Definition: le_hashmap.h:294
size_t le_hashmap_HashVoidPointer(const void *voidToHashPtr)
Definition: le_singlyLinkedList.h:201
void le_hashmap_EnableTrace(le_hashmap_Ref_t mapRef)
const void * le_hashmap_GetKey(le_hashmap_It_Ref_t iteratorRef)
le_hashmap_HashFunc_t hashFuncPtr
Hash operator.
Definition: le_hashmap.h:290
le_result_t le_hashmap_GetNodeAfter(le_hashmap_Ref_t mapRef, const void *keyPtr, void **nextKeyPtr, void **nextValuePtr)
Definition: le_doublyLinkedList.h:200
le_mem_PoolRef_t entryPoolRef
Memory pool to expand into for expanding buckets.
Definition: le_hashmap.h:293
size_t size
Number of inserted entries.
Definition: le_hashmap.h:295
size_t le_hashmap_HashString(const void *stringToHashPtr)
le_hashmap_It_Ref_t le_hashmap_GetIterator(le_hashmap_Ref_t mapRef)
le_sls_Link_t le_hashmap_Link_t
Definition: le_hashmap.h:184
Definition: le_singlyLinkedList.h:186
bool le_hashmap_ForEach(le_hashmap_Ref_t mapRef, le_hashmap_ForEachHandler_t forEachFn, void *contextPtr)
bool le_hashmap_ContainsKey(le_hashmap_Ref_t mapRef, const void *keyPtr)
le_sls_List_t le_hashmap_Bucket_t
Definition: le_hashmap.h:183
Definition: le_hashmap.h:260
bool le_hashmap_EqualsVoidPointer(const void *firstVoidPtr, const void *secondVoidPtr)
size_t(* le_hashmap_HashFunc_t)(const void *keyToHashPtr)
Definition: le_hashmap.h:214
le_hashmap_EqualsFunc_t equalsFuncPtr
Equality operator.
Definition: le_hashmap.h:289
size_t le_hashmap_HashUInt64(const void *intToHashPtr)
void * le_hashmap_Put(le_hashmap_Ref_t mapRef, const void *keyPtr, const void *valuePtr)
*/
size_t le_hashmap_HashUInt32(const void *intToHashPtr)
le_hashmap_Bucket_t * bucketsPtr
Pointer to the array of hash map buckets.
Definition: le_hashmap.h:292
Definition: le_hashmap.h:285
void * le_hashmap_Get(le_hashmap_Ref_t mapRef, const void *keyPtr)
bool(* le_hashmap_ForEachHandler_t)(const void *keyPtr, const void *valuePtr, void *contextPtr)
Definition: le_hashmap.h:248
bool le_hashmap_EqualsUInt64(const void *firstIntPtr, const void *secondIntPtr)
Definition: le_doublyLinkedList.h:216
bool le_hashmap_EqualsUInt32(const void *firstIntPtr, const void *secondIntPtr)
const void * valuePtr
Pointer to value data.
Definition: le_hashmap.h:264
le_hashmap_HashmapIt_t iterator
Iterator instance.
Definition: le_hashmap.h:287
bool le_hashmap_isEmpty(le_hashmap_Ref_t mapRef)
void * le_hashmap_Remove(le_hashmap_Ref_t mapRef, const void *keyPtr)
void le_hashmap_RemoveAll(le_hashmap_Ref_t mapRef)
size_t le_hashmap_Size(le_hashmap_Ref_t mapRef)
#define LE_UNUSED(v)
Definition: le_basics.h:379
#define LE_DECLARE_INLINE
Definition: le_basics.h:330
le_log_TraceRef_t traceRef
Log trace reference for debugging the hashmap.
Definition: le_hashmap.h:299
void le_hashmap_MakeTraceable(le_hashmap_Ref_t mapRef)
le_result_t le_hashmap_PrevNode(le_hashmap_It_Ref_t iteratorRef)
bool(* le_hashmap_EqualsFunc_t)(const void *firstKeyPtr, const void *secondKeyPtr)
Definition: le_hashmap.h:230
struct le_mem_Pool * le_mem_PoolRef_t
Definition: le_mem.h:542
le_hashmap_Link_t entryListLink
Next entry in bucket.
Definition: le_hashmap.h:262
struct le_hashmap * le_hashmap_Ref_t
Definition: le_hashmap.h:195
le_hashmap_Link_t * currentLinkPtr
Current bucket list item pointer.
Definition: le_hashmap.h:276
const char * nameStr
Name of the hashmap for diagnostic purposes.
Definition: le_hashmap.h:298
le_result_t
Definition: le_basics.h:45
void * le_hashmap_GetStoredKey(le_hashmap_Ref_t mapRef, const void *keyPtr)
void * le_hashmap_GetValue(le_hashmap_It_Ref_t iteratorRef)
le_hashmap_Ref_t le_hashmap_Create(const char *nameStr, size_t capacity, le_hashmap_HashFunc_t hashFunc, le_hashmap_EqualsFunc_t equalsFunc)
*/
Definition: le_hashmap.h:362
struct le_hashmap_It * le_hashmap_It_Ref_t
Definition: le_hashmap.h:202
le_result_t le_hashmap_GetFirstNode(le_hashmap_Ref_t mapRef, void **firstKeyPtr, void **firstValuePtr)