Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ****************************************************************************** |
michael@0 | 3 | * Copyright (C) 1997-2011, International Business Machines |
michael@0 | 4 | * Corporation and others. All Rights Reserved. |
michael@0 | 5 | ****************************************************************************** |
michael@0 | 6 | * Date Name Description |
michael@0 | 7 | * 03/22/00 aliu Adapted from original C++ ICU Hashtable. |
michael@0 | 8 | * 07/06/01 aliu Modified to support int32_t keys on |
michael@0 | 9 | * platforms with sizeof(void*) < 32. |
michael@0 | 10 | ****************************************************************************** |
michael@0 | 11 | */ |
michael@0 | 12 | |
michael@0 | 13 | #ifndef UHASH_H |
michael@0 | 14 | #define UHASH_H |
michael@0 | 15 | |
michael@0 | 16 | #include "unicode/utypes.h" |
michael@0 | 17 | #include "cmemory.h" |
michael@0 | 18 | #include "uelement.h" |
michael@0 | 19 | |
michael@0 | 20 | /** |
michael@0 | 21 | * UHashtable stores key-value pairs and does moderately fast lookup |
michael@0 | 22 | * based on keys. It provides a good tradeoff between access time and |
michael@0 | 23 | * storage space. As elements are added to it, it grows to accomodate |
michael@0 | 24 | * them. By default, the table never shrinks, even if all elements |
michael@0 | 25 | * are removed from it. |
michael@0 | 26 | * |
michael@0 | 27 | * Keys and values are stored as void* pointers. These void* pointers |
michael@0 | 28 | * may be actual pointers to strings, objects, or any other structure |
michael@0 | 29 | * in memory, or they may simply be integral values cast to void*. |
michael@0 | 30 | * UHashtable doesn't care and manipulates them via user-supplied |
michael@0 | 31 | * functions. These functions hash keys, compare keys, delete keys, |
michael@0 | 32 | * and delete values. Some function pointers are optional (may be |
michael@0 | 33 | * NULL); others must be supplied. Several prebuilt functions exist |
michael@0 | 34 | * to handle common key types. |
michael@0 | 35 | * |
michael@0 | 36 | * UHashtable ownership of keys and values is flexible, and controlled |
michael@0 | 37 | * by whether or not the key deleter and value deleter functions are |
michael@0 | 38 | * set. If a void* key is actually a pointer to a deletable object, |
michael@0 | 39 | * then UHashtable can be made to delete that object by setting the |
michael@0 | 40 | * key deleter function pointer to a non-NULL value. If this is done, |
michael@0 | 41 | * then keys passed to uhash_put() are owned by the hashtable and will |
michael@0 | 42 | * be deleted by it at some point, either as keys are replaced, or |
michael@0 | 43 | * when uhash_close() is finally called. The same is true of values |
michael@0 | 44 | * and the value deleter function pointer. Keys passed to methods |
michael@0 | 45 | * other than uhash_put() are never owned by the hashtable. |
michael@0 | 46 | * |
michael@0 | 47 | * NULL values are not allowed. uhash_get() returns NULL to indicate |
michael@0 | 48 | * a key that is not in the table, and having a NULL value in the |
michael@0 | 49 | * table would generate an ambiguous result. If a key and a NULL |
michael@0 | 50 | * value is passed to uhash_put(), this has the effect of doing a |
michael@0 | 51 | * uhash_remove() on that key. This keeps uhash_get(), uhash_count(), |
michael@0 | 52 | * and uhash_nextElement() consistent with one another. |
michael@0 | 53 | * |
michael@0 | 54 | * To see everything in a hashtable, use uhash_nextElement() to |
michael@0 | 55 | * iterate through its contents. Each call to this function returns a |
michael@0 | 56 | * UHashElement pointer. A hash element contains a key, value, and |
michael@0 | 57 | * hashcode. During iteration an element may be deleted by calling |
michael@0 | 58 | * uhash_removeElement(); iteration may safely continue thereafter. |
michael@0 | 59 | * The uhash_remove() function may also be safely called in |
michael@0 | 60 | * mid-iteration. However, if uhash_put() is called during iteration |
michael@0 | 61 | * then the iteration will be out of sync. Under no circumstances |
michael@0 | 62 | * should the UHashElement returned by uhash_nextElement be modified |
michael@0 | 63 | * directly. |
michael@0 | 64 | * |
michael@0 | 65 | * By default, the hashtable grows when necessary, but never shrinks, |
michael@0 | 66 | * even if all items are removed. For most applications this is |
michael@0 | 67 | * optimal. However, in a highly dynamic usage where memory is at a |
michael@0 | 68 | * premium, the table can be set to both grow and shrink by calling |
michael@0 | 69 | * uhash_setResizePolicy() with the policy U_GROW_AND_SHRINK. In a |
michael@0 | 70 | * situation where memory is critical and the client wants a table |
michael@0 | 71 | * that does not grow at all, the constant U_FIXED can be used. |
michael@0 | 72 | */ |
michael@0 | 73 | |
michael@0 | 74 | /******************************************************************** |
michael@0 | 75 | * Data Structures |
michael@0 | 76 | ********************************************************************/ |
michael@0 | 77 | |
michael@0 | 78 | U_CDECL_BEGIN |
michael@0 | 79 | |
michael@0 | 80 | /** |
michael@0 | 81 | * A key or value within a UHashtable. |
michael@0 | 82 | * The hashing and comparison functions take a pointer to a |
michael@0 | 83 | * UHashTok, but the deleter receives the void* pointer within it. |
michael@0 | 84 | */ |
michael@0 | 85 | typedef UElement UHashTok; |
michael@0 | 86 | |
michael@0 | 87 | /** |
michael@0 | 88 | * This is a single hash element. |
michael@0 | 89 | */ |
michael@0 | 90 | struct UHashElement { |
michael@0 | 91 | /* Reorder these elements to pack nicely if necessary */ |
michael@0 | 92 | int32_t hashcode; |
michael@0 | 93 | UHashTok value; |
michael@0 | 94 | UHashTok key; |
michael@0 | 95 | }; |
michael@0 | 96 | typedef struct UHashElement UHashElement; |
michael@0 | 97 | |
michael@0 | 98 | /** |
michael@0 | 99 | * A hashing function. |
michael@0 | 100 | * @param key A key stored in a hashtable |
michael@0 | 101 | * @return A NON-NEGATIVE hash code for parm. |
michael@0 | 102 | */ |
michael@0 | 103 | typedef int32_t U_CALLCONV UHashFunction(const UHashTok key); |
michael@0 | 104 | |
michael@0 | 105 | /** |
michael@0 | 106 | * A key equality (boolean) comparison function. |
michael@0 | 107 | */ |
michael@0 | 108 | typedef UElementsAreEqual UKeyComparator; |
michael@0 | 109 | |
michael@0 | 110 | /** |
michael@0 | 111 | * A value equality (boolean) comparison function. |
michael@0 | 112 | */ |
michael@0 | 113 | typedef UElementsAreEqual UValueComparator; |
michael@0 | 114 | |
michael@0 | 115 | /* see cmemory.h for UObjectDeleter and uprv_deleteUObject() */ |
michael@0 | 116 | |
michael@0 | 117 | /** |
michael@0 | 118 | * This specifies whether or not, and how, the hastable resizes itself. |
michael@0 | 119 | * See uhash_setResizePolicy(). |
michael@0 | 120 | */ |
michael@0 | 121 | enum UHashResizePolicy { |
michael@0 | 122 | U_GROW, /* Grow on demand, do not shrink */ |
michael@0 | 123 | U_GROW_AND_SHRINK, /* Grow and shrink on demand */ |
michael@0 | 124 | U_FIXED /* Never change size */ |
michael@0 | 125 | }; |
michael@0 | 126 | |
michael@0 | 127 | /** |
michael@0 | 128 | * The UHashtable struct. Clients should treat this as an opaque data |
michael@0 | 129 | * type and manipulate it only through the uhash_... API. |
michael@0 | 130 | */ |
michael@0 | 131 | struct UHashtable { |
michael@0 | 132 | |
michael@0 | 133 | /* Main key-value pair storage array */ |
michael@0 | 134 | |
michael@0 | 135 | UHashElement *elements; |
michael@0 | 136 | |
michael@0 | 137 | /* Function pointers */ |
michael@0 | 138 | |
michael@0 | 139 | UHashFunction *keyHasher; /* Computes hash from key. |
michael@0 | 140 | * Never null. */ |
michael@0 | 141 | UKeyComparator *keyComparator; /* Compares keys for equality. |
michael@0 | 142 | * Never null. */ |
michael@0 | 143 | UValueComparator *valueComparator; /* Compares the values for equality */ |
michael@0 | 144 | |
michael@0 | 145 | UObjectDeleter *keyDeleter; /* Deletes keys when required. |
michael@0 | 146 | * If NULL won't do anything */ |
michael@0 | 147 | UObjectDeleter *valueDeleter; /* Deletes values when required. |
michael@0 | 148 | * If NULL won't do anything */ |
michael@0 | 149 | |
michael@0 | 150 | /* Size parameters */ |
michael@0 | 151 | |
michael@0 | 152 | int32_t count; /* The number of key-value pairs in this table. |
michael@0 | 153 | * 0 <= count <= length. In practice we |
michael@0 | 154 | * never let count == length (see code). */ |
michael@0 | 155 | int32_t length; /* The physical size of the arrays hashes, keys |
michael@0 | 156 | * and values. Must be prime. */ |
michael@0 | 157 | |
michael@0 | 158 | /* Rehashing thresholds */ |
michael@0 | 159 | |
michael@0 | 160 | int32_t highWaterMark; /* If count > highWaterMark, rehash */ |
michael@0 | 161 | int32_t lowWaterMark; /* If count < lowWaterMark, rehash */ |
michael@0 | 162 | float highWaterRatio; /* 0..1; high water as a fraction of length */ |
michael@0 | 163 | float lowWaterRatio; /* 0..1; low water as a fraction of length */ |
michael@0 | 164 | |
michael@0 | 165 | int8_t primeIndex; /* Index into our prime table for length. |
michael@0 | 166 | * length == PRIMES[primeIndex] */ |
michael@0 | 167 | UBool allocated; /* Was this UHashtable allocated? */ |
michael@0 | 168 | }; |
michael@0 | 169 | typedef struct UHashtable UHashtable; |
michael@0 | 170 | |
michael@0 | 171 | U_CDECL_END |
michael@0 | 172 | |
michael@0 | 173 | /******************************************************************** |
michael@0 | 174 | * API |
michael@0 | 175 | ********************************************************************/ |
michael@0 | 176 | |
michael@0 | 177 | /** |
michael@0 | 178 | * Initialize a new UHashtable. |
michael@0 | 179 | * @param keyHash A pointer to the key hashing function. Must not be |
michael@0 | 180 | * NULL. |
michael@0 | 181 | * @param keyComp A pointer to the function that compares keys. Must |
michael@0 | 182 | * not be NULL. |
michael@0 | 183 | * @param status A pointer to an UErrorCode to receive any errors. |
michael@0 | 184 | * @return A pointer to a UHashtable, or 0 if an error occurred. |
michael@0 | 185 | * @see uhash_openSize |
michael@0 | 186 | */ |
michael@0 | 187 | U_CAPI UHashtable* U_EXPORT2 |
michael@0 | 188 | uhash_open(UHashFunction *keyHash, |
michael@0 | 189 | UKeyComparator *keyComp, |
michael@0 | 190 | UValueComparator *valueComp, |
michael@0 | 191 | UErrorCode *status); |
michael@0 | 192 | |
michael@0 | 193 | /** |
michael@0 | 194 | * Initialize a new UHashtable with a given initial size. |
michael@0 | 195 | * @param keyHash A pointer to the key hashing function. Must not be |
michael@0 | 196 | * NULL. |
michael@0 | 197 | * @param keyComp A pointer to the function that compares keys. Must |
michael@0 | 198 | * not be NULL. |
michael@0 | 199 | * @param size The initial capacity of this hash table. |
michael@0 | 200 | * @param status A pointer to an UErrorCode to receive any errors. |
michael@0 | 201 | * @return A pointer to a UHashtable, or 0 if an error occurred. |
michael@0 | 202 | * @see uhash_open |
michael@0 | 203 | */ |
michael@0 | 204 | U_CAPI UHashtable* U_EXPORT2 |
michael@0 | 205 | uhash_openSize(UHashFunction *keyHash, |
michael@0 | 206 | UKeyComparator *keyComp, |
michael@0 | 207 | UValueComparator *valueComp, |
michael@0 | 208 | int32_t size, |
michael@0 | 209 | UErrorCode *status); |
michael@0 | 210 | |
michael@0 | 211 | /** |
michael@0 | 212 | * Initialize an existing UHashtable. |
michael@0 | 213 | * @param keyHash A pointer to the key hashing function. Must not be |
michael@0 | 214 | * NULL. |
michael@0 | 215 | * @param keyComp A pointer to the function that compares keys. Must |
michael@0 | 216 | * not be NULL. |
michael@0 | 217 | * @param status A pointer to an UErrorCode to receive any errors. |
michael@0 | 218 | * @return A pointer to a UHashtable, or 0 if an error occurred. |
michael@0 | 219 | * @see uhash_openSize |
michael@0 | 220 | */ |
michael@0 | 221 | U_CAPI UHashtable* U_EXPORT2 |
michael@0 | 222 | uhash_init(UHashtable *hash, |
michael@0 | 223 | UHashFunction *keyHash, |
michael@0 | 224 | UKeyComparator *keyComp, |
michael@0 | 225 | UValueComparator *valueComp, |
michael@0 | 226 | UErrorCode *status); |
michael@0 | 227 | |
michael@0 | 228 | /** |
michael@0 | 229 | * Close a UHashtable, releasing the memory used. |
michael@0 | 230 | * @param hash The UHashtable to close. If hash is NULL no operation is performed. |
michael@0 | 231 | */ |
michael@0 | 232 | U_CAPI void U_EXPORT2 |
michael@0 | 233 | uhash_close(UHashtable *hash); |
michael@0 | 234 | |
michael@0 | 235 | |
michael@0 | 236 | |
michael@0 | 237 | /** |
michael@0 | 238 | * Set the function used to hash keys. |
michael@0 | 239 | * @param hash The UHashtable to set |
michael@0 | 240 | * @param fn the function to be used hash keys; must not be NULL |
michael@0 | 241 | * @return the previous key hasher; non-NULL |
michael@0 | 242 | */ |
michael@0 | 243 | U_CAPI UHashFunction *U_EXPORT2 |
michael@0 | 244 | uhash_setKeyHasher(UHashtable *hash, UHashFunction *fn); |
michael@0 | 245 | |
michael@0 | 246 | /** |
michael@0 | 247 | * Set the function used to compare keys. The default comparison is a |
michael@0 | 248 | * void* pointer comparison. |
michael@0 | 249 | * @param hash The UHashtable to set |
michael@0 | 250 | * @param fn the function to be used compare keys; must not be NULL |
michael@0 | 251 | * @return the previous key comparator; non-NULL |
michael@0 | 252 | */ |
michael@0 | 253 | U_CAPI UKeyComparator *U_EXPORT2 |
michael@0 | 254 | uhash_setKeyComparator(UHashtable *hash, UKeyComparator *fn); |
michael@0 | 255 | |
michael@0 | 256 | /** |
michael@0 | 257 | * Set the function used to compare values. The default comparison is a |
michael@0 | 258 | * void* pointer comparison. |
michael@0 | 259 | * @param hash The UHashtable to set |
michael@0 | 260 | * @param fn the function to be used compare keys; must not be NULL |
michael@0 | 261 | * @return the previous key comparator; non-NULL |
michael@0 | 262 | */ |
michael@0 | 263 | U_CAPI UValueComparator *U_EXPORT2 |
michael@0 | 264 | uhash_setValueComparator(UHashtable *hash, UValueComparator *fn); |
michael@0 | 265 | |
michael@0 | 266 | /** |
michael@0 | 267 | * Set the function used to delete keys. If this function pointer is |
michael@0 | 268 | * NULL, this hashtable does not delete keys. If it is non-NULL, this |
michael@0 | 269 | * hashtable does delete keys. This function should be set once |
michael@0 | 270 | * before any elements are added to the hashtable and should not be |
michael@0 | 271 | * changed thereafter. |
michael@0 | 272 | * @param hash The UHashtable to set |
michael@0 | 273 | * @param fn the function to be used delete keys, or NULL |
michael@0 | 274 | * @return the previous key deleter; may be NULL |
michael@0 | 275 | */ |
michael@0 | 276 | U_CAPI UObjectDeleter *U_EXPORT2 |
michael@0 | 277 | uhash_setKeyDeleter(UHashtable *hash, UObjectDeleter *fn); |
michael@0 | 278 | |
michael@0 | 279 | /** |
michael@0 | 280 | * Set the function used to delete values. If this function pointer |
michael@0 | 281 | * is NULL, this hashtable does not delete values. If it is non-NULL, |
michael@0 | 282 | * this hashtable does delete values. This function should be set |
michael@0 | 283 | * once before any elements are added to the hashtable and should not |
michael@0 | 284 | * be changed thereafter. |
michael@0 | 285 | * @param hash The UHashtable to set |
michael@0 | 286 | * @param fn the function to be used delete values, or NULL |
michael@0 | 287 | * @return the previous value deleter; may be NULL |
michael@0 | 288 | */ |
michael@0 | 289 | U_CAPI UObjectDeleter *U_EXPORT2 |
michael@0 | 290 | uhash_setValueDeleter(UHashtable *hash, UObjectDeleter *fn); |
michael@0 | 291 | |
michael@0 | 292 | /** |
michael@0 | 293 | * Specify whether or not, and how, the hastable resizes itself. |
michael@0 | 294 | * By default, tables grow but do not shrink (policy U_GROW). |
michael@0 | 295 | * See enum UHashResizePolicy. |
michael@0 | 296 | * @param hash The UHashtable to set |
michael@0 | 297 | * @param policy The way the hashtable resizes itself, {U_GROW, U_GROW_AND_SHRINK, U_FIXED} |
michael@0 | 298 | */ |
michael@0 | 299 | U_CAPI void U_EXPORT2 |
michael@0 | 300 | uhash_setResizePolicy(UHashtable *hash, enum UHashResizePolicy policy); |
michael@0 | 301 | |
michael@0 | 302 | /** |
michael@0 | 303 | * Get the number of key-value pairs stored in a UHashtable. |
michael@0 | 304 | * @param hash The UHashtable to query. |
michael@0 | 305 | * @return The number of key-value pairs stored in hash. |
michael@0 | 306 | */ |
michael@0 | 307 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 308 | uhash_count(const UHashtable *hash); |
michael@0 | 309 | |
michael@0 | 310 | /** |
michael@0 | 311 | * Put a (key=pointer, value=pointer) item in a UHashtable. If the |
michael@0 | 312 | * keyDeleter is non-NULL, then the hashtable owns 'key' after this |
michael@0 | 313 | * call. If the valueDeleter is non-NULL, then the hashtable owns |
michael@0 | 314 | * 'value' after this call. Storing a NULL value is the same as |
michael@0 | 315 | * calling uhash_remove(). |
michael@0 | 316 | * @param hash The target UHashtable. |
michael@0 | 317 | * @param key The key to store. |
michael@0 | 318 | * @param value The value to store, may be NULL (see above). |
michael@0 | 319 | * @param status A pointer to an UErrorCode to receive any errors. |
michael@0 | 320 | * @return The previous value, or NULL if none. |
michael@0 | 321 | * @see uhash_get |
michael@0 | 322 | */ |
michael@0 | 323 | U_CAPI void* U_EXPORT2 |
michael@0 | 324 | uhash_put(UHashtable *hash, |
michael@0 | 325 | void *key, |
michael@0 | 326 | void *value, |
michael@0 | 327 | UErrorCode *status); |
michael@0 | 328 | |
michael@0 | 329 | /** |
michael@0 | 330 | * Put a (key=integer, value=pointer) item in a UHashtable. |
michael@0 | 331 | * keyDeleter must be NULL. If the valueDeleter is non-NULL, then the |
michael@0 | 332 | * hashtable owns 'value' after this call. Storing a NULL value is |
michael@0 | 333 | * the same as calling uhash_remove(). |
michael@0 | 334 | * @param hash The target UHashtable. |
michael@0 | 335 | * @param key The integer key to store. |
michael@0 | 336 | * @param value The value to store, may be NULL (see above). |
michael@0 | 337 | * @param status A pointer to an UErrorCode to receive any errors. |
michael@0 | 338 | * @return The previous value, or NULL if none. |
michael@0 | 339 | * @see uhash_get |
michael@0 | 340 | */ |
michael@0 | 341 | U_CAPI void* U_EXPORT2 |
michael@0 | 342 | uhash_iput(UHashtable *hash, |
michael@0 | 343 | int32_t key, |
michael@0 | 344 | void* value, |
michael@0 | 345 | UErrorCode *status); |
michael@0 | 346 | |
michael@0 | 347 | /** |
michael@0 | 348 | * Put a (key=pointer, value=integer) item in a UHashtable. If the |
michael@0 | 349 | * keyDeleter is non-NULL, then the hashtable owns 'key' after this |
michael@0 | 350 | * call. valueDeleter must be NULL. Storing a 0 value is the same as |
michael@0 | 351 | * calling uhash_remove(). |
michael@0 | 352 | * @param hash The target UHashtable. |
michael@0 | 353 | * @param key The key to store. |
michael@0 | 354 | * @param value The integer value to store. |
michael@0 | 355 | * @param status A pointer to an UErrorCode to receive any errors. |
michael@0 | 356 | * @return The previous value, or 0 if none. |
michael@0 | 357 | * @see uhash_get |
michael@0 | 358 | */ |
michael@0 | 359 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 360 | uhash_puti(UHashtable *hash, |
michael@0 | 361 | void* key, |
michael@0 | 362 | int32_t value, |
michael@0 | 363 | UErrorCode *status); |
michael@0 | 364 | |
michael@0 | 365 | /** |
michael@0 | 366 | * Put a (key=integer, value=integer) item in a UHashtable. If the |
michael@0 | 367 | * keyDeleter is non-NULL, then the hashtable owns 'key' after this |
michael@0 | 368 | * call. valueDeleter must be NULL. Storing a 0 value is the same as |
michael@0 | 369 | * calling uhash_remove(). |
michael@0 | 370 | * @param hash The target UHashtable. |
michael@0 | 371 | * @param key The key to store. |
michael@0 | 372 | * @param value The integer value to store. |
michael@0 | 373 | * @param status A pointer to an UErrorCode to receive any errors. |
michael@0 | 374 | * @return The previous value, or 0 if none. |
michael@0 | 375 | * @see uhash_get |
michael@0 | 376 | */ |
michael@0 | 377 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 378 | uhash_iputi(UHashtable *hash, |
michael@0 | 379 | int32_t key, |
michael@0 | 380 | int32_t value, |
michael@0 | 381 | UErrorCode *status); |
michael@0 | 382 | |
michael@0 | 383 | /** |
michael@0 | 384 | * Retrieve a pointer value from a UHashtable using a pointer key, |
michael@0 | 385 | * as previously stored by uhash_put(). |
michael@0 | 386 | * @param hash The target UHashtable. |
michael@0 | 387 | * @param key A pointer key stored in a hashtable |
michael@0 | 388 | * @return The requested item, or NULL if not found. |
michael@0 | 389 | */ |
michael@0 | 390 | U_CAPI void* U_EXPORT2 |
michael@0 | 391 | uhash_get(const UHashtable *hash, |
michael@0 | 392 | const void *key); |
michael@0 | 393 | |
michael@0 | 394 | /** |
michael@0 | 395 | * Retrieve a pointer value from a UHashtable using a integer key, |
michael@0 | 396 | * as previously stored by uhash_iput(). |
michael@0 | 397 | * @param hash The target UHashtable. |
michael@0 | 398 | * @param key An integer key stored in a hashtable |
michael@0 | 399 | * @return The requested item, or NULL if not found. |
michael@0 | 400 | */ |
michael@0 | 401 | U_CAPI void* U_EXPORT2 |
michael@0 | 402 | uhash_iget(const UHashtable *hash, |
michael@0 | 403 | int32_t key); |
michael@0 | 404 | |
michael@0 | 405 | /** |
michael@0 | 406 | * Retrieve an integer value from a UHashtable using a pointer key, |
michael@0 | 407 | * as previously stored by uhash_puti(). |
michael@0 | 408 | * @param hash The target UHashtable. |
michael@0 | 409 | * @param key A pointer key stored in a hashtable |
michael@0 | 410 | * @return The requested item, or 0 if not found. |
michael@0 | 411 | */ |
michael@0 | 412 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 413 | uhash_geti(const UHashtable *hash, |
michael@0 | 414 | const void* key); |
michael@0 | 415 | /** |
michael@0 | 416 | * Retrieve an integer value from a UHashtable using an integer key, |
michael@0 | 417 | * as previously stored by uhash_iputi(). |
michael@0 | 418 | * @param hash The target UHashtable. |
michael@0 | 419 | * @param key An integer key stored in a hashtable |
michael@0 | 420 | * @return The requested item, or 0 if not found. |
michael@0 | 421 | */ |
michael@0 | 422 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 423 | uhash_igeti(const UHashtable *hash, |
michael@0 | 424 | int32_t key); |
michael@0 | 425 | |
michael@0 | 426 | /** |
michael@0 | 427 | * Remove an item from a UHashtable stored by uhash_put(). |
michael@0 | 428 | * @param hash The target UHashtable. |
michael@0 | 429 | * @param key A key stored in a hashtable |
michael@0 | 430 | * @return The item removed, or NULL if not found. |
michael@0 | 431 | */ |
michael@0 | 432 | U_CAPI void* U_EXPORT2 |
michael@0 | 433 | uhash_remove(UHashtable *hash, |
michael@0 | 434 | const void *key); |
michael@0 | 435 | |
michael@0 | 436 | /** |
michael@0 | 437 | * Remove an item from a UHashtable stored by uhash_iput(). |
michael@0 | 438 | * @param hash The target UHashtable. |
michael@0 | 439 | * @param key An integer key stored in a hashtable |
michael@0 | 440 | * @return The item removed, or NULL if not found. |
michael@0 | 441 | */ |
michael@0 | 442 | U_CAPI void* U_EXPORT2 |
michael@0 | 443 | uhash_iremove(UHashtable *hash, |
michael@0 | 444 | int32_t key); |
michael@0 | 445 | |
michael@0 | 446 | /** |
michael@0 | 447 | * Remove an item from a UHashtable stored by uhash_puti(). |
michael@0 | 448 | * @param hash The target UHashtable. |
michael@0 | 449 | * @param key An key stored in a hashtable |
michael@0 | 450 | * @return The item removed, or 0 if not found. |
michael@0 | 451 | */ |
michael@0 | 452 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 453 | uhash_removei(UHashtable *hash, |
michael@0 | 454 | const void* key); |
michael@0 | 455 | |
michael@0 | 456 | /** |
michael@0 | 457 | * Remove an item from a UHashtable stored by uhash_iputi(). |
michael@0 | 458 | * @param hash The target UHashtable. |
michael@0 | 459 | * @param key An integer key stored in a hashtable |
michael@0 | 460 | * @return The item removed, or 0 if not found. |
michael@0 | 461 | */ |
michael@0 | 462 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 463 | uhash_iremovei(UHashtable *hash, |
michael@0 | 464 | int32_t key); |
michael@0 | 465 | |
michael@0 | 466 | /** |
michael@0 | 467 | * Remove all items from a UHashtable. |
michael@0 | 468 | * @param hash The target UHashtable. |
michael@0 | 469 | */ |
michael@0 | 470 | U_CAPI void U_EXPORT2 |
michael@0 | 471 | uhash_removeAll(UHashtable *hash); |
michael@0 | 472 | |
michael@0 | 473 | /** |
michael@0 | 474 | * Locate an element of a UHashtable. The caller must not modify the |
michael@0 | 475 | * returned object. The primary use of this function is to obtain the |
michael@0 | 476 | * stored key when it may not be identical to the search key. For |
michael@0 | 477 | * example, if the compare function is a case-insensitive string |
michael@0 | 478 | * compare, then the hash key may be desired in order to obtain the |
michael@0 | 479 | * canonical case corresponding to a search key. |
michael@0 | 480 | * @param hash The target UHashtable. |
michael@0 | 481 | * @param key A key stored in a hashtable |
michael@0 | 482 | * @return a hash element, or NULL if the key is not found. |
michael@0 | 483 | */ |
michael@0 | 484 | U_CAPI const UHashElement* U_EXPORT2 |
michael@0 | 485 | uhash_find(const UHashtable *hash, const void* key); |
michael@0 | 486 | |
michael@0 | 487 | /** |
michael@0 | 488 | * Iterate through the elements of a UHashtable. The caller must not |
michael@0 | 489 | * modify the returned object. However, uhash_removeElement() may be |
michael@0 | 490 | * called during iteration to remove an element from the table. |
michael@0 | 491 | * Iteration may safely be resumed afterwards. If uhash_put() is |
michael@0 | 492 | * called during iteration the iteration will then be out of sync and |
michael@0 | 493 | * should be restarted. |
michael@0 | 494 | * @param hash The target UHashtable. |
michael@0 | 495 | * @param pos This should be set to -1 initially, and left untouched |
michael@0 | 496 | * thereafter. |
michael@0 | 497 | * @return a hash element, or NULL if no further key-value pairs |
michael@0 | 498 | * exist in the table. |
michael@0 | 499 | */ |
michael@0 | 500 | U_CAPI const UHashElement* U_EXPORT2 |
michael@0 | 501 | uhash_nextElement(const UHashtable *hash, |
michael@0 | 502 | int32_t *pos); |
michael@0 | 503 | |
michael@0 | 504 | /** |
michael@0 | 505 | * Remove an element, returned by uhash_nextElement(), from the table. |
michael@0 | 506 | * Iteration may be safely continued afterwards. |
michael@0 | 507 | * @param hash The hashtable |
michael@0 | 508 | * @param e The element, returned by uhash_nextElement(), to remove. |
michael@0 | 509 | * Must not be NULL. Must not be an empty or deleted element (as long |
michael@0 | 510 | * as this was returned by uhash_nextElement() it will not be empty or |
michael@0 | 511 | * deleted). Note: Although this parameter is const, it will be |
michael@0 | 512 | * modified. |
michael@0 | 513 | * @return the value that was removed. |
michael@0 | 514 | */ |
michael@0 | 515 | U_CAPI void* U_EXPORT2 |
michael@0 | 516 | uhash_removeElement(UHashtable *hash, const UHashElement* e); |
michael@0 | 517 | |
michael@0 | 518 | /******************************************************************** |
michael@0 | 519 | * UHashTok convenience |
michael@0 | 520 | ********************************************************************/ |
michael@0 | 521 | |
michael@0 | 522 | /** |
michael@0 | 523 | * Return a UHashTok for an integer. |
michael@0 | 524 | * @param i The given integer |
michael@0 | 525 | * @return a UHashTok for an integer. |
michael@0 | 526 | */ |
michael@0 | 527 | /*U_CAPI UHashTok U_EXPORT2 |
michael@0 | 528 | uhash_toki(int32_t i);*/ |
michael@0 | 529 | |
michael@0 | 530 | /** |
michael@0 | 531 | * Return a UHashTok for a pointer. |
michael@0 | 532 | * @param p The given pointer |
michael@0 | 533 | * @return a UHashTok for a pointer. |
michael@0 | 534 | */ |
michael@0 | 535 | /*U_CAPI UHashTok U_EXPORT2 |
michael@0 | 536 | uhash_tokp(void* p);*/ |
michael@0 | 537 | |
michael@0 | 538 | /******************************************************************** |
michael@0 | 539 | * UChar* and char* Support Functions |
michael@0 | 540 | ********************************************************************/ |
michael@0 | 541 | |
michael@0 | 542 | /** |
michael@0 | 543 | * Generate a hash code for a null-terminated UChar* string. If the |
michael@0 | 544 | * string is not null-terminated do not use this function. Use |
michael@0 | 545 | * together with uhash_compareUChars. |
michael@0 | 546 | * @param key The string (const UChar*) to hash. |
michael@0 | 547 | * @return A hash code for the key. |
michael@0 | 548 | */ |
michael@0 | 549 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 550 | uhash_hashUChars(const UHashTok key); |
michael@0 | 551 | |
michael@0 | 552 | /** |
michael@0 | 553 | * Generate a hash code for a null-terminated char* string. If the |
michael@0 | 554 | * string is not null-terminated do not use this function. Use |
michael@0 | 555 | * together with uhash_compareChars. |
michael@0 | 556 | * @param key The string (const char*) to hash. |
michael@0 | 557 | * @return A hash code for the key. |
michael@0 | 558 | */ |
michael@0 | 559 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 560 | uhash_hashChars(const UHashTok key); |
michael@0 | 561 | |
michael@0 | 562 | /** |
michael@0 | 563 | * Generate a case-insensitive hash code for a null-terminated char* |
michael@0 | 564 | * string. If the string is not null-terminated do not use this |
michael@0 | 565 | * function. Use together with uhash_compareIChars. |
michael@0 | 566 | * @param key The string (const char*) to hash. |
michael@0 | 567 | * @return A hash code for the key. |
michael@0 | 568 | */ |
michael@0 | 569 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 570 | uhash_hashIChars(const UHashTok key); |
michael@0 | 571 | |
michael@0 | 572 | /** |
michael@0 | 573 | * Comparator for null-terminated UChar* strings. Use together with |
michael@0 | 574 | * uhash_hashUChars. |
michael@0 | 575 | * @param key1 The string for comparison |
michael@0 | 576 | * @param key2 The string for comparison |
michael@0 | 577 | * @return true if key1 and key2 are equal, return false otherwise. |
michael@0 | 578 | */ |
michael@0 | 579 | U_CAPI UBool U_EXPORT2 |
michael@0 | 580 | uhash_compareUChars(const UHashTok key1, const UHashTok key2); |
michael@0 | 581 | |
michael@0 | 582 | /** |
michael@0 | 583 | * Comparator for null-terminated char* strings. Use together with |
michael@0 | 584 | * uhash_hashChars. |
michael@0 | 585 | * @param key1 The string for comparison |
michael@0 | 586 | * @param key2 The string for comparison |
michael@0 | 587 | * @return true if key1 and key2 are equal, return false otherwise. |
michael@0 | 588 | */ |
michael@0 | 589 | U_CAPI UBool U_EXPORT2 |
michael@0 | 590 | uhash_compareChars(const UHashTok key1, const UHashTok key2); |
michael@0 | 591 | |
michael@0 | 592 | /** |
michael@0 | 593 | * Case-insensitive comparator for null-terminated char* strings. Use |
michael@0 | 594 | * together with uhash_hashIChars. |
michael@0 | 595 | * @param key1 The string for comparison |
michael@0 | 596 | * @param key2 The string for comparison |
michael@0 | 597 | * @return true if key1 and key2 are equal, return false otherwise. |
michael@0 | 598 | */ |
michael@0 | 599 | U_CAPI UBool U_EXPORT2 |
michael@0 | 600 | uhash_compareIChars(const UHashTok key1, const UHashTok key2); |
michael@0 | 601 | |
michael@0 | 602 | /******************************************************************** |
michael@0 | 603 | * UnicodeString Support Functions |
michael@0 | 604 | ********************************************************************/ |
michael@0 | 605 | |
michael@0 | 606 | /** |
michael@0 | 607 | * Hash function for UnicodeString* keys. |
michael@0 | 608 | * @param key The string (const char*) to hash. |
michael@0 | 609 | * @return A hash code for the key. |
michael@0 | 610 | */ |
michael@0 | 611 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 612 | uhash_hashUnicodeString(const UElement key); |
michael@0 | 613 | |
michael@0 | 614 | /** |
michael@0 | 615 | * Hash function for UnicodeString* keys (case insensitive). |
michael@0 | 616 | * Make sure to use together with uhash_compareCaselessUnicodeString. |
michael@0 | 617 | * @param key The string (const char*) to hash. |
michael@0 | 618 | * @return A hash code for the key. |
michael@0 | 619 | */ |
michael@0 | 620 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 621 | uhash_hashCaselessUnicodeString(const UElement key); |
michael@0 | 622 | |
michael@0 | 623 | /******************************************************************** |
michael@0 | 624 | * int32_t Support Functions |
michael@0 | 625 | ********************************************************************/ |
michael@0 | 626 | |
michael@0 | 627 | /** |
michael@0 | 628 | * Hash function for 32-bit integer keys. |
michael@0 | 629 | * @param key The string (const char*) to hash. |
michael@0 | 630 | * @return A hash code for the key. |
michael@0 | 631 | */ |
michael@0 | 632 | U_CAPI int32_t U_EXPORT2 |
michael@0 | 633 | uhash_hashLong(const UHashTok key); |
michael@0 | 634 | |
michael@0 | 635 | /** |
michael@0 | 636 | * Comparator function for 32-bit integer keys. |
michael@0 | 637 | * @param key1 The integer for comparison |
michael@0 | 638 | * @param Key2 The integer for comparison |
michael@0 | 639 | * @return true if key1 and key2 are equal, return false otherwise |
michael@0 | 640 | */ |
michael@0 | 641 | U_CAPI UBool U_EXPORT2 |
michael@0 | 642 | uhash_compareLong(const UHashTok key1, const UHashTok key2); |
michael@0 | 643 | |
michael@0 | 644 | /******************************************************************** |
michael@0 | 645 | * Other Support Functions |
michael@0 | 646 | ********************************************************************/ |
michael@0 | 647 | |
michael@0 | 648 | /** |
michael@0 | 649 | * Deleter for Hashtable objects. |
michael@0 | 650 | * @param obj The object to be deleted |
michael@0 | 651 | */ |
michael@0 | 652 | U_CAPI void U_EXPORT2 |
michael@0 | 653 | uhash_deleteHashtable(void *obj); |
michael@0 | 654 | |
michael@0 | 655 | /* Use uprv_free() itself as a deleter for any key or value allocated using uprv_malloc. */ |
michael@0 | 656 | |
michael@0 | 657 | /** |
michael@0 | 658 | * Checks if the given hash tables are equal or not. |
michael@0 | 659 | * @param hash1 |
michael@0 | 660 | * @param hash2 |
michael@0 | 661 | * @return true if the hashtables are equal and false if not. |
michael@0 | 662 | */ |
michael@0 | 663 | U_CAPI UBool U_EXPORT2 |
michael@0 | 664 | uhash_equals(const UHashtable* hash1, const UHashtable* hash2); |
michael@0 | 665 | |
michael@0 | 666 | #endif |