Associative arrays/Creation/C: Difference between revisions
loop
(Split from Associative arrays/Creation and Associative arrays/Iteration, to make room for expansion. This text has multiple authors.) |
(loop) |
||
(11 intermediate revisions by 4 users not shown) | |||
Line 1:
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]].
==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="с">
#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 10 ⟶ 59:
{{libheader|Judy}}
<syntaxhighlight lang="с">
#include <stdio.h>
#include <Judy.h>
Line 43 ⟶ 93:
return 0;
}
</syntaxhighlight>
{{libheader|Judy}}
Line 49 ⟶ 100:
We can easily iterate over pair of keys (indexes) and values.
<
#include <Judy.h>
Line 80 ⟶ 131:
JudySLFreeArray(&assoc_arr, PJE0);
return 0;
}</
===POSIX
POSIX defines hcreate(), hdestroy() and hsearch() to manage a hash table. If you have a [[Unix]] system or clone, then your libc probably has these functions, so there is no extra library to install.
These functions have some major limitations:
* You can only have one hash table, in the entire program!
* There is no way to delete an entry from the table!
* There is no way to iterate all keys in the table!
The Linux manual page [http://
====To create the hash table====
The hash table has a fixed capacity. <code>hcreate(50)</code> creates a table for 50 entries. The library might increase 50 to a convenient value (perhaps 64 being a power of 2, or 67 being a prime number). The hash table might have only 64 or 67 slots. Each slot might hold one entry, or one list of entries. Access to the hash table is near [[O|O(1)]], but slows to [[O|O(n)]] as the slots become full.
The hash table is an associative array of key-value pairs. Each key must be a NUL-terminated string. Each value is a void *.
====To fetch or store====
To use the hash table as an associative array, this program defines fetch() and store().
{{libheader|POSIX}}
<syntaxhighlight lang="с">#include <inttypes.h> /* intptr_t, PRIxPTR */
#include <
#include <
void
Line 115 ⟶ 164:
exit(1);
}
/*
* Must hcreate() the hash table before calling fetch() or store().
*
* Because
* void * and intptr_t.
*/
/* Fetch value from the hash table. */
int
fetch(const char *key, intptr_t *value)
{
ENTRY e = {key: (char *)key}, *p;
p = hsearch(e, FIND);
if (p) {
*value = (intptr_t)p->data;
return 1;
} else
return 0;
}
/* Store key-value pair into the hash table. */
void
store(const char *key, intptr_t value)
{
/*
* hsearch() may insert a new entry or find an existing entry
* with the same key. hsearch() ignores e.data if it finds an
* existing entry. We must call hsearch(), then set p->data.
*/
ENTRY e = {key: (char *)key}, *p;
p = hsearch(e, ENTER);
if (p == NULL)
fail("hsearch");
p->data = (void *)value;
}
/*
* Use the hash table to map color strings to integer values,
* like "red" => 0xff0000.
*/
int
main()
{
static const char *const keys[] =
{"red", "orange", "yellow", "green", "blue", "white", "black"};
intptr_t value;
int i;
/* First, create an empty table that can hold 50 entries. */
if (hcreate(50) == 0)
fail("hcreate");
/*
* Some colors from CSS2,
* http://www.w3.org/TR/CSS2/syndata.html#value-def-color
*/
store("red", 0xff0000);
store("orange", 0x123456); /* Insert wrong value! */
store("green", 0x008000);
store("blue", 0x0000ff);
store("white", 0xffffff);
store("black", 0x000000);
store("orange", 0xffa500); /* Replace with correct value. */
if (fetch(keys[i], &value))
printf("%s has value %06" PRIxPTR "\n",
else
printf("%s is not in table\n", keys[i]);
}
/*
* DO NOT CALL hdestroy().
*
* With BSD libc, hdestroy() would call free() with each key in
* table. Our keys are static strings, so free() would crash.
*/
return 0;
}</syntaxhighlight>
<pre>red has value ff0000
orange has value ffa500
yellow is not in table
green has value 008000
blue has value 0000ff
black has value 000000</pre>
====To delete or iterate====
{{libheader|POSIX}}
<syntaxhighlight lang="с">#include <inttypes.h>
#include <search.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void
fail(char *message)
{
perror(message);
exit(1);
}
/* A key-value pair */
struct pair {
/* prev is q_forw, so insque(d, &head) sets head.prev = d */
struct pair *prev; /* q_forw */
struct pair *next; /* q_back */
int32_t key;
int32_t value;
int deleted;
};
/*
* A circular queue of all pairs in the hash table.
* head.next begins a list of all pairs in order of insertion.
*/
/* Fetch value from the hash table. */
int
fetch(int32_t key, int32_t *value)
{
ENTRY e, *p;
char buf[16];
snprintf(buf, sizeof buf, "%"PRId32, key);
e.key = buf;
p = hsearch(e, FIND);
if (p) {
struct pair *d = p->data;
if (d->deleted)
return 0;
else {
*value = d->value;
return 1;
}
} else
return 0;
}
/* Store key-value pair into the hash table. */
void
store(int32_t key, int32_t value)
{
ENTRY e, *
char buf[16];
snprintf(buf, sizeof buf, "%"PRId32, key);
e.key = buf;
p = hsearch(e, ENTER);
if (p == NULL)
fail("hsearch");
if (p->key == buf) {
/* Allocate and initialize a new pair. */
struct pair *d = malloc(sizeof *d);
if (d == NULL)
fail("malloc");
d->value = value;
d->deleted = 0;
fail("strdup");
/*
* Insert the new pair into the hash table's entry, and
* into the circular queue.
*/
p->data = d;
insque(d, &head);
} else {
/* Replace the value. */
struct pair *d = p->data;
d->value = value;
if (d->deleted) {
/* Restore a deleted key. */
insque(d, &head);
d->deleted = 0;
}
}
}
/* Delete key from the hash table. */
int
delete(int32_t key)
{
ENTRY e, *p;
char buf[16];
snprintf(buf, sizeof buf, "%"PRId32, key);
e.key = buf;
p = hsearch(e, FIND);
if (p) {
struct pair *d = p->data;
if (d->deleted)
return 0;
else {
}
} else
return 0;
}
int
main()
{
struct pair *p;
int32_t value;
int i;
if (hcreate(50) == 0)
fail("hcreate");
store(1, mrand48());
store(2, mrand48());
store(3, mrand48());
for (i = 0; i < 3; i++)
store(mrand48(), mrand48());
store(4, mrand48());
delete(1) || puts("1 is not deleted");
delete(2) || puts("2 is not deleted");
delete(5) || puts("5 is not deleted");
store(1, mrand48());
store(3, mrand48());
fetch(2, &value) ? puts("2 is in table") : puts("2 is missing");
fetch(4, &value) ? puts("4 is in table") : puts("4 is missing");
fetch(6, &value) ? puts("6 is in table") : puts("6 is missing");
puts("Iterating the hash table:");
for (p = head.next; p != &head; p = p->next) {
}
return 0;
}</
<pre>5 is not deleted
2 is missing
4 is in table
6 is missing
Iterating the hash table:
3 => 252797108
1368775034 => 1918061247
66927828 => -487786166
684483038 => -1786318902
4 => 1648047133
1 => -1327126111</pre>
====hdestroy()====
hdestroy() is almost impossible to use. With BSD libc, hdestroy() will call free() with each key in the table. With other systems, hdestroy() might leak memory, because the program has no way to iterate the keys to free them. Most programs keep the hash table and never call hdestroy().
===BSD dbopen()===
Line 362 ⟶ 427:
* Warning: some GNU/Linux systems have a dbopen(3) manual page without a real dbopen() function. See [http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=337581 Debian bug #337581].
{{libheader|BSD libc}}
{{works with|OpenBSD|4.8}}
<
#include <err.h> /* err() */
Line 592 ⟶ 657:
number_example();
return 0;
}</
Output:
Line 615 ⟶ 680:
{{works with|OpenBSD|4.8}}
<
#include <err.h> /* err() */
Line 837 ⟶ 902:
number_example();
return 0;
}</
Output:
|