Giter Site home page Giter Site logo

map's Introduction

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.

map's People

Contributors

rxi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

map's Issues

Custom type data

Hello, I learned your map code,After applying for space on the heap, calling map ﹣ deinit ﹣ will not release the empty space applied by the user. It is not very clear about this. If you want to request, email: [email protected], QQ: 8101309. If you can, can you direct me.

中文:您好,我学习了你的map代码,请问自定数据类型在堆上申请空间后,调用map_deinit_后不会释放用户自己申请的空吧,对这块不是很清楚,想请求下, 邮箱:[email protected],qq:8101309 如果可以的话能否直接指点下。

Valgrind predicts memory loss

Hello!

I am using many of your awesome libraries on several projects! Thank you for the awesome libs you write! I am also a love2d user so let's chat once you join the IRC room!

Anyway, currently, I am using map for a computational graph library: https://github.com/praisethemoon/cgraph

Note that this may be a false alarm, for my own code might be the source of this issues, but I would like to hear your own opinion on this one: as I profile my code with valgrind, I get this (among other problems related to my own code):

==3822== 8 bytes in 1 blocks are definitely lost in loss record 1 of 2
==3822==    at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3822==    by 0x4C2FDEF: realloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3822==    by 0x407541: map_resize (map.c:75)
==3822==    by 0x407808: map_set_ (map.c:147)
==3822==    by 0x4425DA: graphSetVar (cg_factory.c:114)
==3822==    by 0x4461E8: runGraphExample (main.c:792)
==3822==    by 0x446379: main (main.c:855)

What do you think?

Cheers!

maybe should test map_str_t

in map_set_(&(m)->base, key, &(m)->tmp, sizeof((m)->tmp)) ) , sizeof((m)->tmp)) is sizeof(ptr),the return value is always 4 or 8. I think there are some other bugs.

Anything refer to the code explain?

Hi,
This is very strong code in practical.But i want to understand code through,could anyone give me some links refer to the code,
Thanks in advance.

Simple showcase

Hello, you library look very nice at first glance. However in order to check for usability, i would have to write a main and test your features. Maybe you could provide a small main function showcasing the use and correct behaviour of your library ? Also it could give a few benchmark data maybe, but i'd rather have a simple showcase than a complex messy benchmark first.

undefined refence to « map_get_ »

Hello,

I try to compile this code but I get an error, do you know what's wrong because it works when I put it in the main of the map.c file.

Thanks

gcc test.c

#include <stdio.h>
#include "map.h"

int main (int argc, char *argv[])
{
	map_int_t m;
	map_init(&m);  
	int tt = map_set(&m, "testkey", 123);
	printf("%d", tt);
	int *val = map_get(&m, "testkey");
	if (val) {
		printf("value: %d\n", *val);
	} else {
		printf("value not found\n");
	}
	map_deinit(&m);
	return 0;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.