Associative arrays/Creation/C: Difference between revisions
Content added Content deleted
(→From Scratch: include headers, use hash_t instead of Hash as struct name) |
(fixed the wiki-markup) |
||
Line 7: | Line 7: | ||
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: |
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> |
#include <stdlib.h> |
||
Line 50: | Line 50: | ||
printf("a => %s\n", hash_lookup(h, "a")); |
printf("a => %s\n", hash_lookup(h, "a")); |
||
return 0; |
return 0; |
||
}</<syntaxhighlight> |
|||
}</lang> |
|||
==Libraries== |
==Libraries== |
||
Line 58: | Line 58: | ||
{{libheader|Judy}} |
{{libheader|Judy}} |
||
< |
<syntaxhighlight lang="с">#include <stdio.h> |
||
#include <Judy.h> |
#include <Judy.h> |
||
Line 91: | Line 91: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|Judy}} |
{{libheader|Judy}} |
||
Line 97: | Line 97: | ||
We can easily iterate over pair of keys (indexes) and values. |
We can easily iterate over pair of keys (indexes) and values. |
||
< |
<syntaxhighlight lang="с">#include <stdio.h> |
||
#include <Judy.h> |
#include <Judy.h> |
||
Line 128: | Line 128: | ||
JudySLFreeArray(&assoc_arr, PJE0); |
JudySLFreeArray(&assoc_arr, PJE0); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
===POSIX hsearch()=== |
===POSIX hsearch()=== |
||
Line 150: | Line 150: | ||
{{libheader|POSIX}} |
{{libheader|POSIX}} |
||
< |
<syntaxhighlight lang="с">#include <inttypes.h> /* intptr_t, PRIxPTR */ |
||
#include <search.h> /* hcreate(), hsearch() */ |
#include <search.h> /* hcreate(), hsearch() */ |
||
#include <stdio.h> /* perror(), printf() */ |
#include <stdio.h> /* perror(), printf() */ |
||
Line 241: | Line 241: | ||
*/ |
*/ |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<pre>red has value ff0000 |
<pre>red has value ff0000 |
||
Line 253: | Line 253: | ||
====To delete or iterate==== |
====To delete or iterate==== |
||
{{libheader|POSIX}} |
{{libheader|POSIX}} |
||
< |
<syntaxhighlight lang="с">#include <inttypes.h> |
||
#include <search.h> |
#include <search.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 402: | Line 402: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
<pre>5 is not deleted |
<pre>5 is not deleted |
||
Line 429: | Line 429: | ||
{{works with|OpenBSD|4.8}} |
{{works with|OpenBSD|4.8}} |
||
< |
<syntaxhighlight lang="с">#include <sys/types.h> |
||
#include <err.h> /* err() */ |
#include <err.h> /* err() */ |
||
Line 654: | Line 654: | ||
number_example(); |
number_example(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 677: | Line 677: | ||
{{works with|OpenBSD|4.8}} |
{{works with|OpenBSD|4.8}} |
||
< |
<syntaxhighlight lang="с">#include <sys/tree.h> |
||
#include <err.h> /* err() */ |
#include <err.h> /* err() */ |
||
Line 899: | Line 899: | ||
number_example(); |
number_example(); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |