2020-11-11 01:19:49 +03:00
|
|
|
/*
|
|
|
|
* MIT License
|
|
|
|
*
|
2021-02-05 20:46:59 +03:00
|
|
|
* Copyright (c) 2021 Ozan Tezcan
|
2020-11-11 01:19:49 +03:00
|
|
|
*
|
|
|
|
* 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 use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SC_MAP_H
|
|
|
|
#define SC_MAP_H
|
|
|
|
|
|
|
|
#include <memory.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-01-23 20:57:08 +03:00
|
|
|
#ifdef SC_HAVE_CONFIG_H
|
2021-02-07 20:42:08 +03:00
|
|
|
#include "config.h"
|
2021-01-23 20:57:08 +03:00
|
|
|
#else
|
2021-01-25 00:07:07 +03:00
|
|
|
#define sc_map_calloc calloc
|
|
|
|
#define sc_map_free free
|
2021-01-23 20:57:08 +03:00
|
|
|
#endif
|
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
#define sc_map_of_strkey(name, K, V) \
|
|
|
|
struct sc_map_item_##name \
|
|
|
|
{ \
|
|
|
|
K key; \
|
|
|
|
V value; \
|
|
|
|
uint32_t hash; \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
sc_map_of(name, K, V)
|
|
|
|
|
|
|
|
#define sc_map_of_scalar(name, K, V) \
|
|
|
|
struct sc_map_item_##name \
|
|
|
|
{ \
|
|
|
|
K key; \
|
|
|
|
V value; \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
sc_map_of(name, K, V)
|
|
|
|
|
|
|
|
#define sc_map_of(name, K, V) \
|
|
|
|
struct sc_map_##name \
|
|
|
|
{ \
|
|
|
|
struct sc_map_item_##name *mem; \
|
|
|
|
uint32_t cap; \
|
|
|
|
uint32_t size; \
|
|
|
|
uint32_t load_factor; \
|
|
|
|
uint32_t remap; \
|
2020-11-16 03:58:37 +03:00
|
|
|
bool used; \
|
2020-11-11 01:19:49 +03:00
|
|
|
}; \
|
|
|
|
\
|
2021-02-03 08:09:50 +03:00
|
|
|
\
|
|
|
|
/** \
|
|
|
|
* Create map \
|
|
|
|
* \
|
|
|
|
* struct sc_map_str map; \
|
|
|
|
* sc_map_init_str(&map, 0, 0); \
|
|
|
|
* \
|
|
|
|
* @param map map \
|
|
|
|
* @param cap initial capacity, zero is accepted \
|
|
|
|
* @param load_factor must be >25 and <95. Pass 0 for default value. \
|
|
|
|
* @return 'true' on success, \
|
|
|
|
* 'false' on out of memory or if 'load_factor' value is invalid. \
|
|
|
|
*/ \
|
2020-11-16 03:58:37 +03:00
|
|
|
bool sc_map_init_##name(struct sc_map_##name *map, uint32_t cap, \
|
|
|
|
uint32_t load_factor); \
|
2021-02-03 08:09:50 +03:00
|
|
|
\
|
|
|
|
/** \
|
|
|
|
* Destroy map. \
|
|
|
|
* \
|
|
|
|
* struct sc_map_str map; \
|
|
|
|
* sc_map_term_str(&map); \
|
|
|
|
* \
|
|
|
|
* @param map map \
|
|
|
|
*/ \
|
2020-11-11 01:19:49 +03:00
|
|
|
void sc_map_term_##name(struct sc_map_##name *map); \
|
2021-02-03 08:09:50 +03:00
|
|
|
\
|
|
|
|
/** \
|
|
|
|
* Get map element count \
|
|
|
|
* \
|
|
|
|
* struct sc_map_str map; \
|
|
|
|
* uint32_t count = sc_map_size_str(&map); \
|
|
|
|
* \
|
|
|
|
* @param map map \
|
|
|
|
* @return element count \
|
|
|
|
*/ \
|
2020-11-11 01:19:49 +03:00
|
|
|
uint32_t sc_map_size_##name(struct sc_map_##name *map); \
|
2021-02-03 08:09:50 +03:00
|
|
|
\
|
|
|
|
/** \
|
|
|
|
* Get map element count \
|
|
|
|
* \
|
|
|
|
* struct sc_map_str map; \
|
|
|
|
* uint32_t count = sc_map_size_str(&map); \
|
|
|
|
* \
|
|
|
|
* @param map map \
|
|
|
|
*/ \
|
2020-11-11 01:19:49 +03:00
|
|
|
void sc_map_clear_##name(struct sc_map_##name *map); \
|
2021-02-03 08:09:50 +03:00
|
|
|
\
|
|
|
|
/** \
|
|
|
|
* Put element to the map \
|
|
|
|
* \
|
|
|
|
* struct sc_map_str map; \
|
|
|
|
* sc_map_put_str(&map, "key", "value"); \
|
|
|
|
* \
|
|
|
|
* @param map map \
|
|
|
|
* @param K key \
|
|
|
|
* @param V value \
|
|
|
|
* @return 'true' on success, 'false' on out of memory. \
|
|
|
|
*/ \
|
2020-11-16 03:58:37 +03:00
|
|
|
bool sc_map_put_##name(struct sc_map_##name *map, K key, V val); \
|
2021-02-03 08:09:50 +03:00
|
|
|
\
|
|
|
|
/** \
|
|
|
|
* Get element \
|
|
|
|
* \
|
|
|
|
* bool found; \
|
|
|
|
* char *value; \
|
|
|
|
* struct sc_map_str map; \
|
|
|
|
* \
|
|
|
|
* found = sc_map_get_str(&map, "key", &value); \
|
|
|
|
* \
|
|
|
|
* @param map map \
|
|
|
|
* @param K key \
|
2021-02-06 21:36:18 +03:00
|
|
|
* @param V pointer to put value, if key is missing, value is undefined \
|
|
|
|
* @return 'true' if key exists, 'false' otherwise \
|
2021-02-03 08:09:50 +03:00
|
|
|
*/ \
|
2021-01-24 17:56:18 +03:00
|
|
|
bool sc_map_get_##name(struct sc_map_##name *map, K key, V *val); \
|
2021-02-03 08:09:50 +03:00
|
|
|
\
|
|
|
|
/** \
|
|
|
|
* Delete element \
|
|
|
|
* \
|
|
|
|
* bool found; \
|
|
|
|
* char *value; \
|
|
|
|
* struct sc_map_str map; \
|
|
|
|
* \
|
|
|
|
* found = sc_map_del_str(&map, "key", &value); \
|
|
|
|
* \
|
|
|
|
* @param map map \
|
|
|
|
* @param K key \
|
2021-02-06 21:36:18 +03:00
|
|
|
* @param V pointer to put current value \
|
2021-02-03 08:09:50 +03:00
|
|
|
* - if key does not exist, value is undefined \
|
2021-02-06 21:36:18 +03:00
|
|
|
* - Pass NULL if you don't want to get previous 'value' \
|
|
|
|
* @return 'true' if key exists, 'false' otherwise \
|
2021-02-03 08:09:50 +03:00
|
|
|
*/ \
|
2021-01-24 17:56:18 +03:00
|
|
|
bool sc_map_del_##name(struct sc_map_##name *map, K key, V *val);
|
2020-11-11 01:19:49 +03:00
|
|
|
|
2021-02-03 08:09:50 +03:00
|
|
|
/**
|
|
|
|
* Foreach loop
|
|
|
|
*
|
|
|
|
* char *key, *value;
|
|
|
|
* struct sc_map_str map;
|
|
|
|
*
|
|
|
|
* sc_map_foreach(&map, key, value) {
|
|
|
|
* printf("key = %s, value = %s \n");
|
|
|
|
* }
|
|
|
|
*/
|
2020-11-11 01:19:49 +03:00
|
|
|
#define sc_map_foreach(map, K, V) \
|
2021-01-25 00:07:07 +03:00
|
|
|
for (int64_t __i = -1, __b = 0; __i < (map)->cap; __i++) \
|
2020-11-11 01:19:49 +03:00
|
|
|
for ((V) = (map)->mem[__i].value, (K) = (map)->mem[__i].key, __b = 1; \
|
2021-01-25 00:07:07 +03:00
|
|
|
__b && ((__i == -1 && (map)->used) || (K) != 0); __b = 0)
|
2020-11-11 01:19:49 +03:00
|
|
|
|
2021-02-03 08:09:50 +03:00
|
|
|
/**
|
|
|
|
* Foreach loop for keys
|
|
|
|
*
|
|
|
|
* char *key;
|
|
|
|
* struct sc_map_str map;
|
|
|
|
*
|
|
|
|
* sc_map_foreach_key(&map, key) {
|
|
|
|
* printf("key = %s \n");
|
|
|
|
* }
|
|
|
|
*/
|
2020-11-11 01:19:49 +03:00
|
|
|
#define sc_map_foreach_key(map, K) \
|
2021-01-25 00:07:07 +03:00
|
|
|
for (int64_t __i = -1, __b = 0; __i < (map)->cap; __i++) \
|
|
|
|
for ((K) = (map)->mem[__i].key, __b = 1; \
|
|
|
|
__b && ((__i == -1 && (map)->used) || (K) != 0); __b = 0)
|
2020-11-11 01:19:49 +03:00
|
|
|
|
2021-02-03 08:09:50 +03:00
|
|
|
/**
|
|
|
|
* Foreach loop for values
|
|
|
|
*
|
|
|
|
* char *value;
|
|
|
|
* struct sc_map_str map;
|
|
|
|
*
|
|
|
|
* sc_map_foreach_value(&map, value) {
|
|
|
|
* printf("value = %s \n");
|
|
|
|
* }
|
|
|
|
*/
|
2020-11-11 01:19:49 +03:00
|
|
|
#define sc_map_foreach_value(map, V) \
|
2021-01-25 00:07:07 +03:00
|
|
|
for (int64_t __i = -1, __b = 0; __i < (map)->cap; __i++) \
|
2020-12-27 17:41:44 +03:00
|
|
|
for ((V) = (map)->mem[__i].value, __b = 1; \
|
2021-01-25 00:07:07 +03:00
|
|
|
__b && ((__i == -1 && (map)->used) || (map)->mem[__i].key != 0); \
|
|
|
|
__b = 0)
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
2020-12-27 17:41:44 +03:00
|
|
|
// name key type value type
|
|
|
|
sc_map_of_scalar(32, uint32_t, uint32_t)
|
|
|
|
sc_map_of_scalar(64, uint64_t, uint64_t)
|
|
|
|
sc_map_of_scalar(64v, uint64_t, void *)
|
|
|
|
sc_map_of_scalar(64s, uint64_t, const char *)
|
|
|
|
sc_map_of_strkey(str, const char *, const char *)
|
|
|
|
sc_map_of_strkey(sv, const char *, void*)
|
|
|
|
sc_map_of_strkey(s64, const char *, uint64_t)
|
2020-11-11 01:19:49 +03:00
|
|
|
|
|
|
|
// clang-format on
|
2020-11-16 03:58:37 +03:00
|
|
|
|
2020-11-11 01:19:49 +03:00
|
|
|
#endif
|