Associative arrays/Creation/C: Difference between revisions

loop
(→‎From Scratch: remove hash_free function)
(loop)
 
(4 intermediate revisions by 2 users not shown)
Line 2:
 
* Back to [[Associative arrays/Creation]].
* Back to [[Associative arrays/Iteration]].
 
==From Scratch==
A hash table can be implemented with the following. Because of this example's simplicity, it comes with some restrictions on use and capabilities: It can't be resized automatically, if you try to insert more values than its capacity it will freeze, the hashing function is very simple, etc. All are fixable with additional logic or using a library:
 
<syntaxhighlight lang="с">
<lang c>typedef struct {
<lang c>#include <stdio.h>
#include <stdlib.h>
 
<lang c>typedef struct {
int size;
void **keys;
void **values;
} Hashhash_t;
 
Hashhash_t *hash_new (int size) {
Hashhash_t *hashh = calloc(1, sizeof (Hashhash_t));
hashh->sizekeys = calloc(size, sizeof (void *));
hashh->keysvalues = calloc(size, sizeof (void *));
hashh->valuessize = calloc(size, sizeof (void *));
return hashh;
}
 
int hash_index (Hashhash_t *hashh, void *key) {
int indexi = (int) key % hashh->size;
while (hashh->keys[indexi] && hashh->keys[indexi] != key)
indexi = (indexi + 1) % hashh->size;
return indexi;
}
 
void hash_insert (Hashhash_t *hashh, void *key, void *value) {
int indexi = hash_index(hashh, key);
hashh->keys[indexi] = key;
hashh->values[indexi] = value;
}
 
void *hash_lookup (Hashhash_t *hashh, void *key) {
int indexi = hash_index(hashh, key);
return hashh->values[indexi];
}
 
int main () {
Hashhash_t *hashh = hash_new(15);
hash_insert(hashh, "hello", "world");
hash_insert(hashh, "a", "b");
printf("hello => %s\n", hash_lookup(hashh, "hello"));
printf("herp => %s\n", hash_lookup(hashh, "herp"));
printf("a => %s\n", hash_lookup(hashh, "a"));
return 0;
}
}</lang>
</syntaxhighlight>
 
==Libraries==
Line 55 ⟶ 59:
{{libheader|Judy}}
 
<syntaxhighlight lang="с">
<lang c>#include <stdio.h>
#include <stdio.h>
#include <Judy.h>
 
Line 88 ⟶ 93:
 
return 0;
}
}</lang>
</syntaxhighlight>
 
{{libheader|Judy}}
Line 94 ⟶ 100:
We can easily iterate over pair of keys (indexes) and values.
 
<langsyntaxhighlight clang="с">#include <stdio.h>
#include <Judy.h>
 
Line 125 ⟶ 131:
JudySLFreeArray(&assoc_arr, PJE0);
return 0;
}</langsyntaxhighlight>
 
===POSIX hsearch()===
Line 147 ⟶ 153:
 
{{libheader|POSIX}}
<langsyntaxhighlight clang="с">#include <inttypes.h> /* intptr_t, PRIxPTR */
#include <search.h> /* hcreate(), hsearch() */
#include <stdio.h> /* perror(), printf() */
Line 238 ⟶ 244:
*/
return 0;
}</langsyntaxhighlight>
 
<pre>red has value ff0000
Line 250 ⟶ 256:
====To delete or iterate====
{{libheader|POSIX}}
<langsyntaxhighlight clang="с">#include <inttypes.h>
#include <search.h>
#include <stdio.h>
Line 399 ⟶ 405:
 
return 0;
}</langsyntaxhighlight>
 
<pre>5 is not deleted
Line 426 ⟶ 432:
{{works with|OpenBSD|4.8}}
 
<langsyntaxhighlight clang="с">#include <sys/types.h>
 
#include <err.h> /* err() */
Line 651 ⟶ 657:
number_example();
return 0;
}</langsyntaxhighlight>
 
Output:
Line 674 ⟶ 680:
{{works with|OpenBSD|4.8}}
 
<langsyntaxhighlight clang="с">#include <sys/tree.h>
 
#include <err.h> /* err() */
Line 896 ⟶ 902:
number_example();
return 0;
}</langsyntaxhighlight>
 
Output:
45

edits