A lightweight collection of generic data structures for C
- Provides data structures that aren't part of C standard library.
- Designed with minimal dependencies.
- Generic, reusable implementatiions.
- Additional data structures are planned and will be added over time.
For Linux and friends:
Install it via
# Clone the repository via GitHub
git clone https://github.com/curious-builder76/dtypes.git dtypes
# Or Codeberg
git clone https://codeberg.org/curious-builder76/dtypes.git dtypes
cd dtypes
# if you simply want to build the library
# run
./build.sh
# if you want to run benchmarks along with building it.
# run
BENCH=1 PURGE=1 ./build.sh
The above commands will create a file libdtypes.a
on the directory dtypes.
and finally
cd ..Once done; now you have to just include it into your projects and link against it.
Now your project directory shall look something like this.
[user@localhost][~/path/to/project] $ tree
.
├── dtypes
│ ├── array.c
│ ├── array.h
│ ├── benchmarks
│ │ ├── array_benchmark.c
│ │ ├── benchmark.txt
│ │ ├── hashmap_benchmark.c
│ │ ...
│ │
│ ├── build.sh
│ ├── hash_function.c
│ ├── hash_function.h
│ ├── hashmap.c
│ ├── ...
│ ├── libdtypes.a
│ ├── ...
│ └── trie.h
└── project.c
Example:
// project.c
#include<stdio.h>
#include<stdint.h>
#include<string.h>
#include "dtypes/hashmap.h"
typedef struct{
int id;
int salary;
char* name;
char* role;
}member_t;
int main(){
member_t team[]={
{ 1, 60000, "John Doe", "Senior Devloper"},
{ 2, 70000, "Alice K." , "Team member" },
{ 3, 50000, "Wazir A.", "Junior Devloper"},
{ 4, 45000, "Bob H." , "Bug Tester"},
{ 5, 46000, "Raman Prakash" , "Projet Dealer" },
{ -1, -1, NULL , NULL }
};
hashmap_t* map= hashmap_new( sizeof(int), sizeof(member_t)) ;
if(!map){
printf("Failed to create hashmap\n");
return 1;
}
for(size_t idx=0;; idx++){
member_t* member=team + idx;
if( member->name==NULL)
break;
printf("Adding member: %s\n",member->name);
if(hashmap_put(map, &member->id, member) != 0){
printf("Failed to add member: %s\n",member->name);
}else {
printf("Success!\n");
}
}
int member_id=3;
printf("Getting a member\n");
member_t* member=hashmap_get(map, &member_id);
if(member==NULL){
printf("Cannot get member id : %d\n",member_id);
hashmap_destroy(map);
map=NULL;
return 1;
}
printf(
"Name: %s\n"
"Salary: %d\n"
"Role: %s\n"
"Member id: %d\n",
member->name,
member->salary,
member->role,
member->id
);
hashmap_destroy(map);
map=NULL;
return 0;
}gcc project.c -o project -O2 -Wall -Wextra -I. -Ldtypes -ldtypes
./projectThe api design was tried to be kept as consistent as possible.
Once you are familiar with it;
I believe you can even write code even in Notepad.
Every header file of this library exposes
functions prefixed that way.
For example:
If you do #include "dtypes/hashmap.h"
the API now exposes
hashmap_t and hashmap_* functions to your project
If you do #include "dtypes/array.h"
the API now exposes
array_t and array_* functions to your project
and so on
and every header file consists of
*_custom() // which allows you to fine tune the data structure as much the api allows you to
// (including custom allocators and deallocators).
*_new() // which is made for ease of use; and demands the required parameters only.
*_destroy() // which releases the resources.So why does this library exists?
This library exists to solve a common problem
every C developer faces once in a life time.
that is searching on web for implementations in C
and their web history looks like
bloom filter implementation C github
trie implementation in C
trie implementation in C industry grade
hashmap implementation in C
hashmap implementation in C supporting custom allocators.
...
and so on.
This library contains all those data types in one place with the support of custom allocators
Currently it provides
| Data structure | Header file | Source file |
|---|---|---|
| Dynamic Arrays | array.h | array.c |
| Bitset | bitset.h | bitset.c |
| Bloom filters | bloom.h | bloom.c |
| Deques | deque.h | deque.c |
| Hash functions | hash_function.h | hash_function.c |
| Hashmap | hashmap.h | hashmap.c |
| Hashset | hashset.h | hashset.c |
| Doubly linked lists | list.h | list.c |
| Lookup (experimental) | lookup.h | lookup.c |
| ND Arrays | ndarray.h | ndarray.c |
| Trie | trie.h | trie.c |
Everything at last boils down to one question
Shall I use this library?
If you are going to use it for personal projects
Definetely a big YES.
If you are going to use it for real industry grade projects
Probably no, This might be ready for industries but I don't feel confident enough
to say that it is industry ready.
If you are going to implement it in lower lever hardwares such as ESP-32 or Raspberry PI.
Definetely no. as the project itself is a little RAM hungry (intentionally).
and those systems may merely contain the ram required by this.
- Allocates everything in contigious memory.
- Friendly to CPU cache.
- Easier to install (especially on
*NIXsystems) - Consistent API.
- Supports custom allocators.
- A little RAM hungry
- API is stll evolving.
- Requires dynamic memory allocation
- Trade-offs speed with space.
- Not thread safe.
Checkout DOCS.md
The documentation isn't complete and the work is still in progress
The function <name>_oom isn't implemented for every data type.
See CONTRIBUTING.md
Checkout benchmark/benchmark.txt
This repository does NOT promises FUCK YEAH , SPEED GOES BRRR... or PRETTY DARN SPEED performance but they are pretty decent,