Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Allow an initial number of buckets. #5

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e3e9fa1
Allow an initial number of buckets. It works only with power of 2, ot…
gcerretani Apr 17, 2018
7b37760
check for null arguments in the API functions.
gcerretani Oct 8, 2018
dd4f31c
unsigned -> unsigned int. useless typedef removed.
gcerretani Oct 8, 2018
c7e36a3
map_newnode() adjusted to be more readable (at cost of 2 additional c…
gcerretani Oct 23, 2018
03d6724
added test
gcerretani Oct 23, 2018
cd75152
test improved
gcerretani Oct 23, 2018
86aa6db
readme improved
gcerretani Oct 23, 2018
7ae8d43
typo
gcerretani Nov 5, 2018
7e9108f
Interface with original version restored
gcerretani Jan 2, 2019
f4eab84
Minor
gcerretani Jan 2, 2019
0cb55e6
Update README for new changes
gcerretani Jan 2, 2019
eea6b63
test fixed. minor improvements.
gcerretani Jan 2, 2019
42112c8
Updated README.md
gcerretani Jan 3, 2019
85b5514
Minor fix
gcerretani Feb 7, 2019
5a8d342
to version 0.1.2
gcerretani Mar 26, 2019
abd63a8
Update README.md
gcerretani Oct 16, 2019
6b90ee6
added Makefile for tests
gcerretani Dec 4, 2019
2031ac4
Create ccpp.yml
gcerretani Dec 4, 2019
89d757c
test improved
gcerretani Dec 5, 2019
95dee52
Merge branch 'master' of https://github.com/gcerretani/map
gcerretani Dec 5, 2019
72d0d06
added run test on github action
gcerretani Dec 5, 2019
0713d72
fixed call to test
gcerretani Dec 5, 2019
3776edb
fix test memory leak
gcerretani Sep 14, 2022
7e6c1c3
Update LICENSE
gcerretani Oct 5, 2022
f1aca0e
Update map.h
gcerretani Oct 5, 2022
168096a
Update map.c
gcerretani Oct 5, 2022
ef9576f
Update test.c
gcerretani Oct 5, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: C/C++ CI

on: [push]

jobs:
build:

runs-on: [ubuntu-latest]

steps:
- uses: actions/checkout@v1
- name: make
run: make
- name: make test
run: make test
- name: run test
run: ./test
- name: make clean
run: make clean
1 change: 0 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Copyright (c) 2014 rxi


Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
Expand Down
24 changes: 24 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CC = gcc
CFLAGS = -fPIC -Wall -Wextra
LDFLAGS = -shared
RM = rm -f
NAME_LIB = map
TARGET_LIB = lib$(NAME_LIB).so

SRCS = src/map.c
TEST_SRCS = src/test.c
TEST_OUTPUT = test
OBJS = $(SRCS:.c=.o)

.PHONY: all
all: $(TARGET_LIB)

$(TARGET_LIB): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $^

.PHONY: clean
clean:
-$(RM) $(TARGET_LIB) $(OBJS) $(TEST_OUTPUT)

test:
-$(CC) $(TEST_SRCS) -l$(NAME_LIB) -Wl,-rpath,. -o $(TEST_OUTPUT) -L.
39 changes: 21 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
# map
A type-safe generic hashmap implementation for C.
Forked from [rxi/map](https://github.com/rxi/map), it adds the possibility to
reserve an arbitrary (power of 2) number of buckets for performance
improvements.

## Installation
The [map.c](src/map.c?raw=1) and [map.h](src/map.h?raw=1) files can be dropped
into an existing C project and compiled along with it.


## Usage
Before using a map it should first be initialised using the `map_init()`
function.
Before using a map, it should first be initialised using the `map_init_reserve()`
macro.
The second argument is an integer with the number of buckets to be pre-allocated.
If is not a power of 2 or 0, no pre-allocation is done. It can improve the speed but,
on the other hand, could allocate useless space.
```c
map_int_t m;
unsigned initial_nbuckets = 128;
map_init_reserve(&m, initial_nbuckets);
```

It pre-allocation is not needed, the macro `map_init()` can be used as well.
```c
map_int_t m;
map_init(&m);
```

Values can added to a map using the `map_set()` function.
Values can added to a map using the `map_set()` macro.
```c
map_set(&m, "testkey", 123);
```

To retrieve a value from a map, the `map_get()` function can be used.
To retrieve a value from a map, the `map_get()` macro can be used.
`map_get()` will return a pointer to the key's value, or `NULL` if no mapping
for that key exists.
```c
Expand All @@ -31,25 +44,14 @@ if (val) {
}
```

When you are done with a map the `map_deinit()` function should be called on
When you are done with a map the `map_deinit()` macro should be called on
it. This will free any memory the map allocated during use.
```c
map_deinit(&m);
```


## Types
map.h provides the following predefined map types:

Contained Type | Type name
----------------|----------------------------------
void* | map_void_t
char* | map_str_t
int | map_int_t
char | map_char_t
float | map_float_t
double | map_double_t

To define a new map type the `map_t()` macro should be used:
```c
/* Creates the type uint_map_t for storing unsigned ints */
Expand All @@ -68,8 +70,9 @@ Creates a map struct for containing values of type `T`.
typedef map_t(FILE*) fp_map_t;
```

### map\_init(m)
Initialises the map, this must be called before the map can be used.
### map\_init(m, initial_nbuckets)
Initialises the map, this must be called before the map can be used. The parameter
`initial_nbuckets` sets the number of buckets to be pre-allocated.

### map\_deinit(m)
Deinitialises the map, freeing the memory the map allocated during use;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "map",
"version": "0.1.0",
"repo": "rxi/map",
"version": "0.1.2",
"repo": "gcerretani/map",
"description": "Type-safe generic hash map",
"keywords": ["hashmap", "map", "table", "hashtable", "dict", "dictionary"],
"license": "MIT",
Expand Down
Loading