maptable.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 1999-2003, 2006-2007 Apple Inc. All Rights Reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apple Public Source License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. /* maptable.h
  24. Scalable hash table of mappings.
  25. Bertrand, August 1990
  26. Copyright 1990-1996 NeXT Software, Inc.
  27. */
  28. #ifndef _OBJC_MAPTABLE_H_
  29. #define _OBJC_MAPTABLE_H_
  30. #ifndef _OBJC_PRIVATE_H_
  31. # define OBJC_MAP_AVAILABILITY \
  32. __OSX_DEPRECATED(10.0, 10.1, "NXMapTable is deprecated") \
  33. __IOS_UNAVAILABLE __TVOS_UNAVAILABLE \
  34. __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
  35. #else
  36. # define OBJC_MAP_AVAILABILITY
  37. #endif
  38. #include <objc/objc.h>
  39. __BEGIN_DECLS
  40. /*************** Definitions ***************/
  41. /* This module allows hashing of arbitrary associations [key -> value]. Keys and values must be pointers or integers, and client is responsible for allocating/deallocating this data. A deallocation call-back is provided.
  42. NX_MAPNOTAKEY (-1) is used internally as a marker, and therefore keys must always be different from -1.
  43. As well-behaved scalable data structures, hash tables double in size when they start becoming full, thus guaranteeing both average constant time access and linear size. */
  44. typedef struct _NXMapTable {
  45. /* private data structure; may change */
  46. const struct _NXMapTablePrototype * _Nonnull prototype;
  47. unsigned count;
  48. unsigned nbBucketsMinusOne;
  49. void * _Nullable buckets;
  50. } NXMapTable OBJC_MAP_AVAILABILITY;
  51. typedef struct _NXMapTablePrototype {
  52. unsigned (* _Nonnull hash)(NXMapTable * _Nonnull,
  53. const void * _Nullable key);
  54. int (* _Nonnull isEqual)(NXMapTable * _Nonnull,
  55. const void * _Nullable key1,
  56. const void * _Nullable key2);
  57. void (* _Nonnull free)(NXMapTable * _Nonnull,
  58. void * _Nullable key,
  59. void * _Nullable value);
  60. int style; /* reserved for future expansion; currently 0 */
  61. } NXMapTablePrototype OBJC_MAP_AVAILABILITY;
  62. /* invariants assumed by the implementation:
  63. A - key != -1
  64. B - key1 == key2 => hash(key1) == hash(key2)
  65. when key varies over time, hash(key) must remain invariant
  66. e.g. if string key, the string must not be changed
  67. C - isEqual(key1, key2) => key1 == key2
  68. */
  69. #define NX_MAPNOTAKEY ((void * _Nonnull)(-1))
  70. /*************** Functions ***************/
  71. OBJC_EXPORT NXMapTable * _Nonnull
  72. NXCreateMapTableFromZone(NXMapTablePrototype prototype,
  73. unsigned capacity, void * _Nullable z)
  74. OBJC_MAP_AVAILABILITY;
  75. OBJC_EXPORT NXMapTable * _Nonnull
  76. NXCreateMapTable(NXMapTablePrototype prototype, unsigned capacity)
  77. OBJC_MAP_AVAILABILITY;
  78. /* capacity is only a hint; 0 creates a small table */
  79. OBJC_EXPORT void
  80. NXFreeMapTable(NXMapTable * _Nonnull table)
  81. OBJC_MAP_AVAILABILITY;
  82. /* call free for each pair, and recovers table */
  83. OBJC_EXPORT void
  84. NXResetMapTable(NXMapTable * _Nonnull table)
  85. OBJC_MAP_AVAILABILITY;
  86. /* free each pair; keep current capacity */
  87. OBJC_EXPORT BOOL
  88. NXCompareMapTables(NXMapTable * _Nonnull table1, NXMapTable * _Nonnull table2)
  89. OBJC_MAP_AVAILABILITY;
  90. /* Returns YES if the two sets are equal (each member of table1 in table2, and table have same size) */
  91. OBJC_EXPORT unsigned
  92. NXCountMapTable(NXMapTable * _Nonnull table)
  93. OBJC_MAP_AVAILABILITY;
  94. /* current number of data in table */
  95. OBJC_EXPORT void * _Nullable
  96. NXMapMember(NXMapTable * _Nonnull table, const void * _Nullable key,
  97. void * _Nullable * _Nonnull value) OBJC_MAP_AVAILABILITY;
  98. /* return original table key or NX_MAPNOTAKEY. If key is found, value is set */
  99. OBJC_EXPORT void * _Nullable
  100. NXMapGet(NXMapTable * _Nonnull table, const void * _Nullable key)
  101. OBJC_MAP_AVAILABILITY;
  102. /* return original corresponding value or NULL. When NULL need be stored as value, NXMapMember can be used to test for presence */
  103. OBJC_EXPORT void * _Nullable
  104. NXMapInsert(NXMapTable * _Nonnull table, const void * _Nullable key,
  105. const void * _Nullable value)
  106. OBJC_MAP_AVAILABILITY;
  107. /* override preexisting pair; Return previous value or NULL. */
  108. OBJC_EXPORT void * _Nullable
  109. NXMapRemove(NXMapTable * _Nonnull table, const void * _Nullable key)
  110. OBJC_MAP_AVAILABILITY;
  111. /* previous value or NULL is returned */
  112. /* Iteration over all elements of a table consists in setting up an iteration state and then to progress until all entries have been visited. An example of use for counting elements in a table is:
  113. unsigned count = 0;
  114. const MyKey *key;
  115. const MyValue *value;
  116. NXMapState state = NXInitMapState(table);
  117. while(NXNextMapState(table, &state, &key, &value)) {
  118. count++;
  119. }
  120. */
  121. typedef struct {int index;} NXMapState OBJC_MAP_AVAILABILITY;
  122. /* callers should not rely on actual contents of the struct */
  123. OBJC_EXPORT NXMapState
  124. NXInitMapState(NXMapTable * _Nonnull table)
  125. OBJC_MAP_AVAILABILITY;
  126. OBJC_EXPORT int
  127. NXNextMapState(NXMapTable * _Nonnull table, NXMapState * _Nonnull state,
  128. const void * _Nullable * _Nonnull key,
  129. const void * _Nullable * _Nonnull value)
  130. OBJC_MAP_AVAILABILITY;
  131. /* returns 0 when all elements have been visited */
  132. /*************** Conveniences ***************/
  133. OBJC_EXPORT const NXMapTablePrototype NXPtrValueMapPrototype
  134. OBJC_MAP_AVAILABILITY;
  135. /* hashing is pointer/integer hashing;
  136. isEqual is identity;
  137. free is no-op. */
  138. OBJC_EXPORT const NXMapTablePrototype NXStrValueMapPrototype
  139. OBJC_MAP_AVAILABILITY;
  140. /* hashing is string hashing;
  141. isEqual is strcmp;
  142. free is no-op. */
  143. OBJC_EXPORT const NXMapTablePrototype NXObjectMapPrototype
  144. OBJC2_UNAVAILABLE;
  145. /* for objects; uses methods: hash, isEqual:, free, all for key. */
  146. __END_DECLS
  147. #endif /* _OBJC_MAPTABLE_H_ */