hashtable implementation in C? - c

I was wondering if you knew of a robust implementation of a hashtable in C. I'm looking for something other than ghashtable in glib.
Thanks.

I've heard good things about the GLib Hash Table.

In C:
As previously mentioned g_hash_table in gLib:
http://library.gnome.org/devel/glib/stable/glib-Hash-Tables.html
uthash: https://github.com/troydhanson/uthash
khash in klib: https://github.com/attractivechaos/klib
cdada_map in libcdada: https://github.com/msune/libcdada (C API, backend C++)
If you can/want to use C++:
std::map in libstdc++ (look into std::unordered_map too)
As noted before (updated link) sparsehash: https://github.com/sparsehash/sparsehash (look into dense too)

Will this hashtable work? (got the link from the second post of this thread)
Perhaps this one will?
(got the above from a Google search for "hashtable in c", am not a C programmer)

For a hash table I'd use google-sparsehash
PD: I don't know your requirements, but take a look at HDF5, bear in mind it exists just in case.
update
Memory Structures Library (MemSL2), o MemSL2 in another link
it has implementations (one in pure C and wrappers for C++) of structures, for example, AVL trees, threaded trees, ..., and
Hash Tables with Separate Chaining,
Hash Tables with User-Defined Paging
Hash Tables with Dynamic Paging

A simple one in libc, see <hsearch.h> and man hsearch.
Update: just found that you can implement hashtable very easily with the help of hlist from Linux kernel. Take a look at <list.h> in Linux kernel source code for hlist_head/node and their operations.

You might want to look into using the Apache Portable Runtime? It's license is very liberal and it provides a decent hashtable implementation:
http://apr.apache.org/docs/apr/1.3/group__apr__hash.html

Related

Does standard c library provides linked list etc. data structures?

Do standard C library implementations, especially glibc (the GNU C Library) provide linked lists, stack et al. data structures, or do we have to roll our own?
Thanks.
The C Standard does not provide data structures like linked list and stack.Some compiler implementations might provide their own versions but their usage will be non portable across different compilers.
So Yes, You have to write your own.
The C standard doesn't, glibc however provides lists, tail queues, and circular queues in <sys/queue.h> according to the queue man page those come from BSD and not POSIX.
There are hash tables, binary trees and binary search stuff in glibc.
Those are part of C89, C99 and/or POSIX.1 standards.
Some reason linked list is not there.
More info from man pages: hsearch, tsearch and bsearch
Note: Some of those have bad design. For example: hsearch allows only one hash table per process. The GNU compiler, gcc/glibc, provides reentrant versions hcreate_r, hsearch_r, and hdestroy_r that allow multiple hash tables. See also Stack Overflow's How to use hcreate_r.
As such C does not provide data structures but you can use the glib provided by Gnome
Queue.h ad Tree.h also provides you some Data structures
There's a hash table implementation in POSIX (and GLibc); see the manpages for hcreate/hdestroy/hsearch.
But, as mentioned, using glib is probably the easiest way to save yourself from reimplementing the basic data structure.
As other answers already stated, there isn't a linked list library in the standard library.
I've written one for my own use a while ago. You can freely use it or use the code as reference.
You can find it here: libllist

Do dictionaries exist in C?

In C, can you create a dictionary? I come from a Objective-C background so I would like to know if there is anything similar to NSDictionary.
You can create anything you want in C. You just won't have native language support for most of it.
You can create a dictionary in C, but there is no dictionary built in to the standard C library.
A quick search on Google code shows that there are open-source (and generously licensed) C dictionary implementations here and here.
Posix does have a limited hash table -- see hcreate(), hsearch() and hdestroy() that can be used by a C program.
A discussion of the limitations appears in this stackoverflow question.
Without OOP and templates, it would be hard to implement a hash table or a balanced tree that is truly general, easy to use and performant, and therefore worthy to be in the run-time library that comes with the language.
That being said, you can always implement your own, or just use C++ (see unordered_map or map).

B-trees using C

can any one provide me a link for B-tree code in C,have understood the algorithm but still have problems in coding it
c++
http://touc.org/btree.html
c (slightly fancier than normal)
http://code.google.com/p/high-concurrency-btree/downloads/list

Container Class / Library for C [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Does anyone know of any C container libraries? I am looking for something which gives standard implementations of linked lists, arrays, hash tables etc, much in the same way as the C++ STL does. Key concerns are:
Client code should be able to create containers for multiple different data types without modifying the library.
The interface for creating and using the containers should be intuitive.
I just came across SGLIB while looking for a C implementation of a map/dictionary container. Unfortunately, no map but it seems to include the containers you asked about. I have no idea how good it is.
http://sglib.sourceforge.net.
Sglib is an excellent generic data structures library. The library currently provides generic implementation for:
sorting arrays
linked lists
sorted linked lists
double linked lists
red-black trees
hashed containers
It's very fast. Faster that glib. It's inspired by the Standard Template Library. Download here
Another solution is Attractive Chaos sotware. C macro library:
kbtree.h: efficient B-tree library in C.
khash.h: fast and light-weighted hash table library in C.
kvec.h: simple vector container in C.
Kulesh Shanmugasundaram presents the generic Linux Kernel Linked List and a generic hash table based in the Linux Kernel Linked List.
Sglib and Attractive Chaos sotware and Linux Kernel Linked List are C macro libraries. Using void* to implement generic containers in C may be inefficient. C macros mimic C++ templates and are as efficient as a C++ template.
Chuck Falconer has a decent hash library written in C that includes a C++ interface, click on hashlib.zip on the webpage to download.
Ben Pfaff has very nice and extremely well-documented binary and balanced tree library, GNU libavl, that implements most major tree structures including binary search trees, AVL trees, red-black trees and threaded versions of each.
libavl is licensed under the LGPL (as of version 2.0.3), hashlib is GPL.
I'm not sure what you are looking for as far as arrays and linked lists go as the former is supported directly by the language and the latter is generally trivial enough to implement without warranting a library.
How about ccl? This is a container library which has been (unsuccessfully) proposed for C standard. Maybe it's best fit for you. You can see https://code.google.com/p/ccl/ https://github.com/jacob-navia/ccl.
Enjoy it.
I've been using a library I've been growing from Hanson's "C Interface and Implementations" book. His source is downloadable at
cii book website
Everything is an Abstract Data Type. There is List, Set, Table (map).
#include "queue.h" to get access to the implementations of singly-linked lists, singly-linked tail queues, lists and tail queues.
I found a generic cache for storing arbitrary objects in memory by D. J. Bernstein (http://cr.yp.to/djbdns.html) to be both clean, simple and super fast. Look up cache.h and cache.c in djdns tarball.
Some of the ones that I have heard of (but never used) are
Glib
iMatix Standard Function Library
disparate elements from the Linux kernel headers (e.g. list)
This seems to cover most of the containers and some algorithms. There is also no licensing, all the headers contain - 'code may be used without restriction.' http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=13867&lngWId=3

C Analog To STL

Just because I'm curious--is there any C analog to the functionality of the STL in C++? I've seen mention of a GTK+ library called glib that a few people consider fills the bill but are there other libraries that would provide STL functionality in C?
Yes, glib is a pretty good choice: it includes a lot of utilities for manipulating containers like linked lists, arrays, hash tables, etc. And there is also an object-oriented framework called GObject that you can use to make objects with signals and slots in C (albeit with rather verbose function call names like gobject_set_property, since C doesn't have any syntax for objects). And there is also code for main loops so you can write event-driven programs.
For more info see wikipedia: https://en.wikipedia.org/wiki/GLib
Glib was originally part of GTK, but the non-GUI code has been completely factored out so that you can use it in command-line programs: http://library.gnome.org/devel/glib/stable/
CLIB
Adding another option (full disclosure, I am the author); if you can compile and link C++, you can have a look into libcdada, which has a pure C API, but uses libstdc++ as a backend for most of the containers:
https://github.com/msune/libcdada
Well since STL's very nature is based on templates which C doesn't have, it would be difficult to even come close to the STL in C. The best you could hope for is some collection classes which manipulate void* pointers to unknown object.

Resources