2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
9 #include <toollib/hash.h>
11 typedef struct _HashNode HashNode
;
22 HashNode
* Nodes
[256];
25 static int calchash (const char * key
)
30 code
= (code
+ *key
++) & 0xFF;
35 static int calchashNC (const char * key
)
41 code
= (code
+ toupper(*key
)) & 0xFF;
48 Hash
* Hash_New (void)
50 Hash
* hash
= xmalloc (sizeof (Hash
));
51 memset (hash
, 0, sizeof(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
))
69 newNode
= xmalloc (sizeof (HashNode
));
71 newNode
->Next
= hash
->Nodes
[code
];
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
))
92 newNode
= xmalloc (sizeof (HashNode
));
94 newNode
->Next
= hash
->Nodes
[code
];
98 hash
->Nodes
[code
] = newNode
;
101 void * Hash_Find (Hash
* hash
, const char * key
)
103 int code
= calchash (key
);
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
);
117 void * Hash_FindNC (Hash
* hash
, const char * key
)
119 int code
= calchashNC (key
);
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
);
133 void Hash_Traverse (Hash
* hash
, CB tp
, CBD userdata
)
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
);
149 void Hash_Delete (Hash
* hash
, CB dnp
, CBD userdata
)
152 HashNode
* node
, * next
;
154 for (t
=0; t
<256; t
++)
156 for (next
=hash
->Nodes
[t
]; (node
=next
); )
161 CallCB (dnp
, (void *)node
->key
, node
->data
, userdata
);