Associative arrays/Creation/C: Difference between revisions

loop
(Add simple simple way to create hash tables in C)
(loop)
 
(6 intermediate revisions by 3 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;
}
 
voidint *hash_lookuphash_index (Hashhash_t *hashh, void *key) {
void hash_free (Hash *hash) {
freeint i = (hashint) key % h->keys)size;
while (h->keys[i] && h->keys[i] != key)
free(hash->values);
free i = (hashi + 1) % h->size;
return indexi;
}
 
intvoid hash_indexhash_insert (Hashhash_t *hashh, void *key, void *value) {
int indexi = hash_index(int)h, key % hash->size);
while (hash->keys[index] && hashh->keys[indexi] != key);
hashh->values[indexi] = value;
index = (index + 1) % hash->size;
return index;
}
 
void hash_insert*hash_lookup (Hashhash_t *hashh, void *key, void *value) {
int indexi = hash_index(hashh, key);
hashreturn h->keysvalues[indexi] = key;
hash->values[index] = value;
 
void *hash_lookup (Hash *hash, void *key) {
int index = hash_index(hash, key);
return hash->values[index];
}
 
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 61 ⟶ 59:
{{libheader|Judy}}
 
<syntaxhighlight lang="с">
<lang c>#include <stdio.h>
#include <stdio.h>
#include <Judy.h>
 
Line 94 ⟶ 93:
 
return 0;
}
}</lang>
</syntaxhighlight>
 
{{libheader|Judy}}
Line 100:
We can easily iterate over pair of keys (indexes) and values.
 
<langsyntaxhighlight clang="с">#include <stdio.h>
#include <Judy.h>
 
Line 131:
JudySLFreeArray(&assoc_arr, PJE0);
return 0;
}</langsyntaxhighlight>
 
===POSIX hsearch()===
Line 153:
 
{{libheader|POSIX}}
<langsyntaxhighlight clang="с">#include <inttypes.h> /* intptr_t, PRIxPTR */
#include <search.h> /* hcreate(), hsearch() */
#include <stdio.h> /* perror(), printf() */
Line 244:
*/
return 0;
}</langsyntaxhighlight>
 
<pre>red has value ff0000
Line 256:
====To delete or iterate====
{{libheader|POSIX}}
<langsyntaxhighlight clang="с">#include <inttypes.h>
#include <search.h>
#include <stdio.h>
Line 405:
 
return 0;
}</langsyntaxhighlight>
 
<pre>5 is not deleted
Line 432:
{{works with|OpenBSD|4.8}}
 
<langsyntaxhighlight clang="с">#include <sys/types.h>
 
#include <err.h> /* err() */
Line 657:
number_example();
return 0;
}</langsyntaxhighlight>
 
Output:
Line 680:
{{works with|OpenBSD|4.8}}
 
<langsyntaxhighlight clang="с">#include <sys/tree.h>
 
#include <err.h> /* err() */
Line 902:
number_example();
return 0;
}</langsyntaxhighlight>
 
Output:
45

edits