old-graph: Move graph API into the common graph struct
[tig.git] / include / tig / map.h
blob28ecfe12fbd8cd6196a25d177983cd5757e33c8a
1 /* Copyright (c) 2006-2014 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #ifndef TIG_MAP_H
15 #define TIG_MAP_H
17 #include "tig/tig.h"
20 * String map.
23 typedef unsigned int string_map_key_t;
24 typedef string_map_key_t (*string_map_hash_fn)(const void *value);
25 typedef const char *(*string_map_key_fn)(const void *value);
26 typedef bool (*string_map_iterator_fn)(void *data, void *value);
28 struct string_map {
29 string_map_hash_fn hash_fn;
30 string_map_key_fn key_fn;
31 size_t init_size;
32 void *htab;
33 const char *key;
36 extern string_map_hash_fn string_map_hash_helper;
37 void *string_map_get(struct string_map *map, const char *key);
38 void *string_map_put(struct string_map *map, const char *key, void *value);
39 void **string_map_put_to(struct string_map *map, const char *key);
40 void *string_map_remove(struct string_map *map, void **slot);
41 void string_map_clear(struct string_map *map);
42 void string_map_foreach(struct string_map *map, string_map_iterator_fn iterator, void *data);
44 #define DEFINE_STRING_MAP(name, type, key_member, init_size) \
45 static const char * \
46 name ## _key(const void *value) \
47 { \
48 return ((type) value)->key_member; \
49 } \
50 static string_map_key_t \
51 name ## _hash(const void *value) \
52 { \
53 return string_map_hash_helper(name ## _key(value)); \
54 } \
55 static struct string_map name = { \
56 name ## _hash, \
57 name ## _key, \
58 init_size, \
61 #endif
62 /* vim: set ts=8 sw=8 noexpandtab: */