• This repository has been archived on 27/May/2021
  • Stars
    star
    558
  • Rank 79,819 (Top 2 %)
  • Language
    C
  • License
    MIT License
  • Created about 10 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A type-safe hash map implementation for C

map

A type-safe generic hashmap implementation for C.

Installation

The map.c and map.h 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.

map_int_t m;
map_init(&m);

Values can added to a map using the map_set() function.

map_set(&m, "testkey", 123);

To retrieve a value from a map, the map_get() function can be used. map_get() will return a pointer to the key's value, or NULL if no mapping for that key exists.

int *val = map_get(&m, "testkey");
if (val) {
  printf("value: %d\n", *val);
} else {
  printf("value not found\n");
}

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

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:

/* Creates the type uint_map_t for storing unsigned ints */
typedef map_t(unsigned int) uint_map_t;

Functions

All map functions are macro functions. The parameter m in each function should be a pointer to the map struct which the operation is to be performed on. The key parameter should always be a string value.

map_t(T)

Creates a map struct for containing values of type T.

/* Typedefs the struct `fp_map_t` as a container for type FILE* */
typedef map_t(FILE*) fp_map_t;

map_init(m)

Initialises the map, this must be called before the map can be used.

map_deinit(m)

Deinitialises the map, freeing the memory the map allocated during use; this should be called when we're finished with a map.

map_get(m, key)

Returns a pointer to the value of the given key. If no mapping for the key exists then NULL will be returned.

map_set(m, key, value)

Sets the given key to the given value. Returns 0 on success, otherwise -1 is returned and the map remains unchanged.

map_remove(m, key)

Removes the mapping of the given key from the map. If the key does not exist in the map then the function has no effect.

map_iter(m)

Returns a map_iter_t which can be used with map_next() to iterate all the keys in the map.

map_next(m, iter)

Uses the map_iter_t returned by map_iter() to iterate all the keys in the map. map_next() returns a key with each call and returns NULL when there are no more keys.

const char *key;
map_iter_t iter = map_iter(&m);

while ((key = map_next(&m, &iter))) {
  printf("%s -> %d", key, *map_get(&m, key));
}

License

This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.

More Repositories

1

lite

A lightweight text editor written in Lua
Lua
7,287
star
2

microui

A tiny immediate-mode UI library
C
3,124
star
3

log.c

A simple logging library implemented in C99
C
2,472
star
4

json.lua

A lightweight JSON library for Lua
Lua
1,734
star
5

dyad

Asynchronous networking for C
C
1,437
star
6

fe

A tiny, embeddable language implemented in ANSI C
C
1,277
star
7

lume

Lua functions geared towards gamedev
Lua
945
star
8

classic

Tiny class module for Lua
Lua
799
star
9

lovedos

A framework for making 2D DOS games in Lua
C
666
star
10

vec

A type-safe dynamic array implementation for C
C
436
star
11

microtar

A lightweight tar library written in ANSI C
C
423
star
12

flux

A fast, lightweight tweening library for Lua
Lua
366
star
13

lite-plugins

Plugins for the lite text editor
Lua
346
star
14

ini

A tiny ANSI C library for loading .ini config files
C
315
star
15

log.lua

A tiny logging module for Lua
Lua
296
star
16

lovebird

A browser-based debug console for LÖVE
Lua
286
star
17

lurker

Auto-swaps changed Lua files in a running LÖVE project
Lua
268
star
18

kit

A tiny library for making small games with big pixels
C
240
star
19

aq

A lightweight framework for creating audio toys
C
236
star
20

uuid4

A tiny C library for generating uuid4 strings
C
214
star
21

sfd

A small C library for opening a file dialog
C
171
star
22

sandbird

A tiny embeddable HTTP server written in C89
C
169
star
23

cmixer

Portable ANSI C audio mixer for games
C
168
star
24

aria

A tiny, embeddable lisp-shaped language implemented in C89
C
166
star
25

juno

Framework for making 2D games with chunky pixels in Lua
C
160
star
26

tick

Lua module for delaying function calls
Lua
123
star
27

dmt

Dynamic memory tracker for C
C
105
star
28

autobatch

Small LÖVE module to automate the use of SpriteBatches
Lua
103
star
29

lite-colors

Colors themes for the lite text editor
Lua
99
star
30

cembed

A small utility for embedding files in a C header
C
93
star
31

doq

A small tool for creating single-file html documents
C
82
star
32

lovebpm

A LÖVE library for syncing events to the BPM of an audio track
Lua
76
star
33

coil

A tiny cooperative threading module for Lua
Lua
71
star
34

shash

A simple, lightweight spatial hash for Lua
Lua
66
star
35

console

A console plugin for the lite text editor
Lua
58
star
36

atlas

A small utility for generating a texture atlas
C
41
star
37

gifwriter

A Nim library for writing animated GIFs
C
18
star
38

rxi.github.io

HTML
17
star
39

cmixer_sdl2-nim

Tiny Nim library for using cmixer with sdl2
Nim
3
star
40

cmixer-nim

Nim wrapper for cmixer, A lightweight audio mixer for games
C
3
star
41

crate

2
star