The C Utility Toolkit
Main Page | Data Structures | File List | Data Fields | Globals

strhmap.h

Go to the documentation of this file.
00001 /* 00002 ** This file is part of the C Utility Toolkit (CUTK) 00003 ** Copyright (C) 2002-2005 Chris Osgood 00004 ** http://www.cutk.org 00005 ** 00006 ** This library is free software; you can redistribute it and/or 00007 ** modify it under the terms of the GNU Lesser General Public 00008 ** License as published by the Free Software Foundation; either 00009 ** version 2.1 of the License, or (at your option) any later version. 00010 ** 00011 ** This library is distributed in the hope that it will be useful, 00012 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 ** Lesser General Public License for more details. 00015 ** 00016 ** You should have received a copy of the GNU Lesser General Public 00017 ** License along with this library; if not, write to the Free Software 00018 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 ** 00020 ** Please report all bugs and problems to "bugs at cutk.org". 00021 */ 00022 00023 /* 00024 ** A fast hash map implementation similar to STL hash_map<char*,void*>. It's 00025 ** very important to note that the keys in the hash map are just stored as 00026 ** pointers, therefore you can't reuse the same string pointer for inserting 00027 ** different keys. It's also not a good idea to change the value of a key 00028 ** while it's still in a hash map. 00029 ** 00030 ** Erased items do not free memory unless StrHmapCompact is called. However, 00031 ** erased item's memory will be reused if new items are inserted. 00032 ** 00033 ** Function reference: 00034 ** StrHmapAlloc -- Allocates a new hash map with space for "size" 00035 ** initial elements. 00036 ** StrHmapFree -- Free a hash map 00037 ** StrHmapFind -- Finds an element in the hash map 00038 ** StrHmapSize -- Approx number of elements in hash map (erased items 00039 ** may still show up in the count). 00040 ** StrHmapReserve -- Allocates memory for "size" elements and rehashes 00041 ** the map. 00042 ** StrHmapInsert -- Inserts a new element into the hash map. Will get an 00043 ** error if the key already exists. 00044 ** StrHmapReplace -- Inserts or replaces an element in the hash map 00045 ** StrHmapErase -- Removes element from hash map 00046 ** StrHmapDup -- Duplicates a hash map 00047 ** StrHmapCompact -- Frees memory held by erased items & rebuilds the tables 00048 ** StrHmapClear -- Erases all elements in the hash map. Does not free 00049 ** memory. Usr StrHmapClear then StrHmapCompact to free 00050 ** memory. 00051 ** 00052 ** Example usage: 00053 ** 00054 ** int sample_value; 00055 ** StrHmap* map = StrHmapAlloc(0); 00056 ** StrHmapInsert(map, "1234", "value for key 1234"); 00057 ** StrHmapInsert(map, "5678", "value for key 5678"); 00058 ** StrHmapInsert(map, "intvalue", &sample_value); 00059 ** int* findval = StrHmapFind(map, "intvalue"); 00060 ** if (findval) 00061 ** printf("the value is %d\n", *findval); 00062 ** ... 00063 ** StrHmapFree(map); 00064 ** 00065 ** $Id: strhmap.h 7 2005-01-02 01:02:22Z chris $ 00066 */ 00067 00068 #ifndef _STRHMAP_H 00069 #define _STRHMAP_H 00070 00071 #include <cutk/array.h> 00072 #include <string.h> 00073 00074 #ifdef __cplusplus 00075 extern "C" { 00076 #endif 00077 00078 /* Type used to store items in the hash map */ 00079 typedef struct StrHmap_itemtype 00080 { 00081 const char* key; 00082 const void* item; 00083 size_t next; 00084 } StrHmap_itemtype; 00085 00086 /* Hash map type. "items" is an array that can be used for iteration. */ 00087 /* When iterating, test the item->key value for NULL (erased items). */ 00088 /* Use StrHmapSize to determine the number of items. */ 00089 typedef struct StrHmap 00090 { 00091 size_t* hash_tbl; 00092 StrHmap_itemtype* items; 00093 size_t deleted; 00094 } StrHmap; 00095 00096 #define StrHmapSize(x) (arysize(&(x)->items)) 00097 00098 StrHmap* StrHmapAlloc(size_t size); 00099 void StrHmapClear(StrHmap* hashmap); 00100 int StrHmapCompact(StrHmap* hashmap); 00101 StrHmap* StrHmapDup(const StrHmap* hashmap); 00102 int StrHmapErase(StrHmap* hashmap, const char* key); 00103 void StrHmapFree(StrHmap* hashmap); 00104 void* StrHmapFind(StrHmap* hashmap, const char* key); 00105 int StrHmapInsert(StrHmap* hashmap, const char* key, const void* item); 00106 int StrHmapReplace(StrHmap* hashmap, const char* key, const void* item); 00107 int StrHmapReserve(StrHmap* hashmap, size_t size); 00108 00109 #ifdef __cplusplus 00110 } 00111 #endif 00112 00113 #endif /* _STRHMAP_H */

Generated on Wed Jan 12 13:13:42 2005 for CUTK by doxygen 1.3.7