diff options
author | Thiago de Arruda <tpadilha84@gmail.com> | 2014-05-19 10:52:10 -0300 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-05-19 10:52:10 -0300 |
commit | 37dfe2d48f16e4227dd713ebeb03257490f78e67 (patch) | |
tree | e5348309b2cf6772ef0de12b2e731499709e523a /src/nvim/map.h | |
parent | 974eade1a6445222812fc03f60896def74b7dd06 (diff) | |
download | rneovim-37dfe2d48f16e4227dd713ebeb03257490f78e67.tar.gz rneovim-37dfe2d48f16e4227dd713ebeb03257490f78e67.tar.bz2 rneovim-37dfe2d48f16e4227dd713ebeb03257490f78e67.zip |
Improve map module: Refactor into a macro library
The map_* declarations and definitions are now created by a macro invocation
with a key type parameter. Also refactored server module to use the updated
version.
Diffstat (limited to 'src/nvim/map.h')
-rw-r--r-- | src/nvim/map.h | 63 |
1 files changed, 22 insertions, 41 deletions
diff --git a/src/nvim/map.h b/src/nvim/map.h index 3e9ab389bd..e85b4d3a5e 100644 --- a/src/nvim/map.h +++ b/src/nvim/map.h @@ -1,4 +1,3 @@ -// General-purpose string->pointer associative array with a simple API #ifndef NVIM_MAP_H #define NVIM_MAP_H @@ -6,46 +5,28 @@ #include "nvim/map_defs.h" -/// Creates a new `Map` instance -/// -/// @return a pointer to the new instance -Map *map_new(void); - -/// Frees memory for a `Map` instance -/// -/// @param map The `Map` instance -void map_free(Map *map); - -/// Gets the value corresponding to a key in a `Map` instance -/// -/// @param map The `Map` instance -/// @param key A key string -/// @return The value if the key exists in the map, or NULL if it doesn't -void *map_get(Map *map, const char *key); - -/// Checks if a key exists in the map -/// -/// @param map The `Map` instance -/// @param key A key string -/// @return true if the key exists, false otherwise -bool map_has(Map *map, const char *key); - -/// Set the value corresponding to a key in a `Map` instance and returns -/// the old value. -/// -/// @param map The `Map` instance -/// @param key A key string -/// @param value A value -/// @return The current value if exists or NULL otherwise -void *map_put(Map *map, const char *key, void *value); - -/// Deletes the value corresponding to a key in a `Map` instance and returns -/// the old value. -/// -/// @param map The `Map` instance -/// @param key A key string -/// @return The current value if exists or NULL otherwise -void *map_del(Map *map, const char *key); +#define MAP_DECLS(T) \ + KHASH_DECLARE(T##_map, T, void *) \ + \ + typedef struct { \ + khash_t(T##_map) *table; \ + } Map(T); \ + \ + Map(T) *map_##T##_new(void); \ + void map_##T##_free(Map(T) *map); \ + void *map_##T##_get(Map(T) *map, T key); \ + bool map_##T##_has(Map(T) *map, T key); \ + void *map_##T##_put(Map(T) *map, T key, void *value); \ + void *map_##T##_del(Map(T) *map, T key); + +MAP_DECLS(cstr_t) + +#define map_new(T) map_##T##_new +#define map_free(T) map_##T##_free +#define map_get(T) map_##T##_get +#define map_has(T) map_##T##_has +#define map_put(T) map_##T##_put +#define map_del(T) map_##T##_del #define map_foreach(map, key, value, block) \ kh_foreach(map->table, key, value, block) |