Associative arrays/Creation/C: Difference between revisions

loop
(The page must be in only one collection (so I guess). Remove text about awful libraries; the bigger problem seems to be awful code on this page.)
(loop)
 
(8 intermediate revisions by 4 users not shown)
Line 1:
{{collection|Associative arrays/Creation}}
 
There are no associative arrays in the C language. Some libraries provide hash tables, red-black trees, or other data structures that can become associative arrays.
 
* 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>#include <stdio.h>
#include <stdlib.h>
 
typedef struct {
int size;
void **keys;
void **values;
} hash_t;
 
hash_t *hash_new (int size) {
hash_t *h = calloc(1, sizeof (hash_t));
h->keys = calloc(size, sizeof (void *));
h->values = calloc(size, sizeof (void *));
h->size = size;
return h;
}
 
int hash_index (hash_t *h, void *key) {
int i = (int) key % h->size;
while (h->keys[i] && h->keys[i] != key)
i = (i + 1) % h->size;
return i;
}
 
void hash_insert (hash_t *h, void *key, void *value) {
int i = hash_index(h, key);
h->keys[i] = key;
h->values[i] = value;
}
 
void *hash_lookup (hash_t *h, void *key) {
int i = hash_index(h, key);
return h->values[i];
}
 
int main () {
hash_t *h = hash_new(15);
hash_insert(h, "hello", "world");
hash_insert(h, "a", "b");
printf("hello => %s\n", hash_lookup(h, "hello"));
printf("herp => %s\n", hash_lookup(h, "herp"));
printf("a => %s\n", hash_lookup(h, "a"));
return 0;
}
</syntaxhighlight>
 
==Libraries==
Line 12 ⟶ 59:
{{libheader|Judy}}
 
<syntaxhighlight lang="с">
<lang c>#include <stdio.h>
#include <stdio.h>
#include <Judy.h>
 
Line 45 ⟶ 93:
 
return 0;
}
}</lang>
</syntaxhighlight>
 
{{libheader|Judy}}
Line 51 ⟶ 100:
We can easily iterate over pair of keys (indexes) and values.
 
<langsyntaxhighlight clang="с">#include <stdio.h>
#include <Judy.h>
 
Line 82 ⟶ 131:
JudySLFreeArray(&assoc_arr, PJE0);
return 0;
}</langsyntaxhighlight>
 
===POSIX hsearch()===
Line 104 ⟶ 153:
 
{{libheader|POSIX}}
<langsyntaxhighlight clang="с">#include <inttypes.h> /* intptr_t, PRIxPTR */
#include <search.h> /* hcreate(), hsearch() */
#include <stdio.h> /* perror(), printf() */
Line 195 ⟶ 244:
*/
return 0;
}</langsyntaxhighlight>
 
<pre>red has value ff0000
Line 207 ⟶ 256:
====To delete or iterate====
{{libheader|POSIX}}
<langsyntaxhighlight clang="с">#include <inttypes.h>
#include <search.h>
#include <stdio.h>
Line 356 ⟶ 405:
 
return 0;
}</langsyntaxhighlight>
 
<pre>5 is not deleted
Line 383 ⟶ 432:
{{works with|OpenBSD|4.8}}
 
<langsyntaxhighlight clang="с">#include <sys/types.h>
 
#include <err.h> /* err() */
Line 608 ⟶ 657:
number_example();
return 0;
}</langsyntaxhighlight>
 
Output:
Line 631 ⟶ 680:
{{works with|OpenBSD|4.8}}
 
<langsyntaxhighlight clang="с">#include <sys/tree.h>
 
#include <err.h> /* err() */
Line 853 ⟶ 902:
number_example();
return 0;
}</langsyntaxhighlight>
 
Output:
45

edits