- Allow menu highlight colour to be configured through an ARGB hex value in
[AROS.git] / tools / toollib / hash.c
blobf9027255aad1b64d402c97f0f44dc0d5679d4ae3
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <string.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9 #include <toollib/hash.h>
11 typedef struct _HashNode HashNode;
13 struct _HashNode
15 HashNode * Next;
16 const char * key;
17 const void * data;
20 struct _Hash
22 HashNode * Nodes[256];
25 static int calchash (const char * key)
27 int code = 0;
29 while (*key)
30 code = (code + *key++) & 0xFF;
32 return code;
35 static int calchashNC (const char * key)
37 int code = 0;
39 while (*key)
41 code = (code + toupper(*key)) & 0xFF;
42 key ++;
45 return code;
48 Hash * Hash_New (void)
50 Hash * hash = xmalloc (sizeof (Hash));
51 memset (hash, 0, sizeof(Hash));
52 return hash;
55 void Hash_Store (Hash * hash, const char * key, const void * data)
57 int code = calchash (key);
58 HashNode * node, * newNode;
60 for (node=hash->Nodes[code]; node; node=node->Next)
62 if (node->key[0] == *key && !strcmp (node->key, key))
64 node->data = data;
65 return;
69 newNode = xmalloc (sizeof (HashNode));
71 newNode->Next = hash->Nodes[code];
72 newNode->key = key;
73 newNode->data = data;
75 hash->Nodes[code] = newNode;
78 void Hash_StoreNC (Hash * hash, const char * key, const void * data)
80 int code = calchashNC (key);
81 HashNode * node, * newNode;
83 for (node=hash->Nodes[code]; node; node=node->Next)
85 if (node->key[0] == *key && !strcmp (node->key, key))
87 node->data = data;
88 return;
92 newNode = xmalloc (sizeof (HashNode));
94 newNode->Next = hash->Nodes[code];
95 newNode->key = key;
96 newNode->data = data;
98 hash->Nodes[code] = newNode;
101 void * Hash_Find (Hash * hash, const char * key)
103 int code = calchash (key);
104 HashNode * node;
106 for (node=hash->Nodes[code]; node; node=node->Next)
108 if (node->key[0] == *key && !strcmp (node->key, key))
110 return ((void *)node->data);
114 return NULL;
117 void * Hash_FindNC (Hash * hash, const char * key)
119 int code = calchashNC (key);
120 HashNode * node;
122 for (node=hash->Nodes[code]; node; node=node->Next)
124 if (node->key[0] == *key && !strcmp (node->key, key))
126 return ((void *)node->data);
130 return NULL;
133 void Hash_Traverse (Hash * hash, CB tp, CBD userdata)
135 int t;
136 HashNode * node;
138 for (t=0; t<256; t++)
140 for (node=hash->Nodes[t]; node; node=node->Next)
142 CallCB (tp, (void *)node->key, node->data, userdata);
146 xfree (hash);
149 void Hash_Delete (Hash * hash, CB dnp, CBD userdata)
151 int t;
152 HashNode * node, * next;
154 for (t=0; t<256; t++)
156 for (next=hash->Nodes[t]; (node=next); )
158 next=next->Next;
160 if (dnp)
161 CallCB (dnp, (void *)node->key, node->data, userdata);
163 xfree (node);
167 xfree (hash);