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