mirror of
https://github.com/tezc/sc.git
synced 2025-01-14 06:43:04 +08:00
sc_map v2
This commit is contained in:
parent
cce603126f
commit
3150369d7a
@ -45,42 +45,42 @@ If you want to use structs anyway, you need to change the code a little bit.
|
||||
|
||||
void example_str()
|
||||
{
|
||||
const char *key, *value;
|
||||
struct sc_map_str map;
|
||||
const char *key, *value;
|
||||
struct sc_map_str map;
|
||||
|
||||
sc_map_init_str(&map, 0, 0);
|
||||
sc_map_init_str(&map, 0, 0);
|
||||
|
||||
sc_map_put_str(&map, "jack", "chicago");
|
||||
sc_map_put_str(&map, "jane", "new york");
|
||||
sc_map_put_str(&map, "janie", "atlanta");
|
||||
sc_map_put_str(&map, "jack", "chicago");
|
||||
sc_map_put_str(&map, "jane", "new york");
|
||||
sc_map_put_str(&map, "janie", "atlanta");
|
||||
|
||||
sc_map_foreach (&map, key, value) {
|
||||
printf("Key:[%s], Value:[%s] \n", key, value);
|
||||
}
|
||||
sc_map_foreach (&map, key, value) {
|
||||
printf("Key:[%s], Value:[%s] \n", key, value);
|
||||
}
|
||||
|
||||
sc_map_term_str(&map);
|
||||
sc_map_term_str(&map);
|
||||
}
|
||||
|
||||
void example_int_to_str()
|
||||
{
|
||||
uint32_t key;
|
||||
const char *value;
|
||||
struct sc_map_64s map;
|
||||
uint32_t key;
|
||||
const char *value;
|
||||
struct sc_map_64s map;
|
||||
|
||||
sc_map_init_64s(&map, 0, 0);
|
||||
sc_map_init_64s(&map, 0, 0);
|
||||
|
||||
sc_map_put_64s(&map, 100, "chicago");
|
||||
sc_map_put_64s(&map, 200, "new york");
|
||||
sc_map_put_64s(&map, 300, "atlanta");
|
||||
sc_map_put_64s(&map, 100, "chicago");
|
||||
sc_map_put_64s(&map, 200, "new york");
|
||||
sc_map_put_64s(&map, 300, "atlanta");
|
||||
|
||||
sc_map_del_64s(&map, 100, &value);
|
||||
printf("Deleted : %s \n", value);
|
||||
value = sc_map_del_64s(&map, 100);
|
||||
printf("Deleted : %s \n", value);
|
||||
|
||||
sc_map_foreach (&map, key, value) {
|
||||
printf("Key:[%d], Value:[%s] \n", key, value);
|
||||
}
|
||||
sc_map_foreach (&map, key, value) {
|
||||
printf("Key:[%d], Value:[%s] \n", key, value);
|
||||
}
|
||||
|
||||
sc_map_term_64s(&map);
|
||||
sc_map_term_64s(&map);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void example_str()
|
||||
void example_str(void)
|
||||
{
|
||||
const char *key, *value;
|
||||
struct sc_map_str map;
|
||||
@ -14,13 +14,13 @@ void example_str()
|
||||
sc_map_put_str(&map, "janie", "atlanta");
|
||||
|
||||
sc_map_foreach (&map, key, value) {
|
||||
printf("Key:[%s], Value:[%s] \n", key, value);
|
||||
}
|
||||
printf("Key:[%s], Value:[%s] \n", key, value);
|
||||
}
|
||||
|
||||
sc_map_term_str(&map);
|
||||
}
|
||||
|
||||
void example_int_to_str()
|
||||
void example_int_to_str(void)
|
||||
{
|
||||
uint32_t key;
|
||||
const char *value;
|
||||
@ -32,12 +32,12 @@ void example_int_to_str()
|
||||
sc_map_put_64s(&map, 200, "new york");
|
||||
sc_map_put_64s(&map, 300, "atlanta");
|
||||
|
||||
sc_map_del_64s(&map, 100, &value);
|
||||
value = sc_map_del_64s(&map, 100);
|
||||
printf("Deleted : %s \n", value);
|
||||
|
||||
sc_map_foreach (&map, key, value) {
|
||||
printf("Key:[%d], Value:[%s] \n", key, value);
|
||||
}
|
||||
printf("Key:[%d], Value:[%s] \n", key, value);
|
||||
}
|
||||
|
||||
sc_map_term_64s(&map);
|
||||
}
|
||||
|
782
map/map_test.c
782
map/map_test.c
File diff suppressed because it is too large
Load Diff
51
map/sc_map.c
51
map/sc_map.c
@ -205,20 +205,26 @@
|
||||
return true; \
|
||||
} \
|
||||
\
|
||||
bool sc_map_put_##name(struct sc_map_##name *m, K key, V value) \
|
||||
V sc_map_put_##name(struct sc_map_##name *m, K key, V value) \
|
||||
{ \
|
||||
V ret; \
|
||||
uint32_t pos, mod, h; \
|
||||
\
|
||||
m->oom = false; \
|
||||
\
|
||||
if (!sc_map_remap_##name(m)) { \
|
||||
return false; \
|
||||
m->oom = true; \
|
||||
return 0; \
|
||||
} \
|
||||
\
|
||||
if (key == 0) { \
|
||||
ret = (m->used) ? m->mem[-1].value : 0; \
|
||||
m->found = m->used; \
|
||||
m->size += !m->used; \
|
||||
m->used = true; \
|
||||
m->mem[-1].value = value; \
|
||||
\
|
||||
return true; \
|
||||
return ret; \
|
||||
} \
|
||||
\
|
||||
mod = m->cap - 1; \
|
||||
@ -233,20 +239,23 @@
|
||||
continue; \
|
||||
} \
|
||||
\
|
||||
m->found = m->mem[pos].key != 0; \
|
||||
ret = m->found ? m->mem[pos].value : 0; \
|
||||
sc_map_assign_##name(&m->mem[pos], key, value, h); \
|
||||
return true; \
|
||||
\
|
||||
return ret; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
/** NOLINTNEXTLINE */ \
|
||||
bool sc_map_get_##name(struct sc_map_##name *m, K key, V *value) \
|
||||
V sc_map_get_##name(struct sc_map_##name *m, K key) \
|
||||
{ \
|
||||
const uint32_t mod = m->cap - 1; \
|
||||
uint32_t h, pos; \
|
||||
\
|
||||
if (key == 0) { \
|
||||
*value = m->mem[-1].value; \
|
||||
return m->used; \
|
||||
m->found = m->used; \
|
||||
return m->used ? m->mem[-1].value : 0; \
|
||||
} \
|
||||
\
|
||||
h = hash_fn(key); \
|
||||
@ -254,33 +263,31 @@
|
||||
\
|
||||
while (true) { \
|
||||
if (m->mem[pos].key == 0) { \
|
||||
return false; \
|
||||
m->found = false; \
|
||||
return 0; \
|
||||
} else if (!sc_map_cmp_##name(&m->mem[pos], key, h)) { \
|
||||
pos = (pos + 1) & (mod); \
|
||||
continue; \
|
||||
} \
|
||||
\
|
||||
*value = m->mem[pos].value; \
|
||||
return true; \
|
||||
m->found = true; \
|
||||
return m->mem[pos].value; \
|
||||
} \
|
||||
} \
|
||||
\
|
||||
/** NOLINTNEXTLINE */ \
|
||||
bool sc_map_del_##name(struct sc_map_##name *m, K key, V *value) \
|
||||
V sc_map_del_##name(struct sc_map_##name *m, K key) \
|
||||
{ \
|
||||
const uint32_t mod = m->cap - 1; \
|
||||
uint32_t pos, prev, it, p, h; \
|
||||
V ret; \
|
||||
\
|
||||
if (key == 0) { \
|
||||
bool ret = m->used; \
|
||||
m->found = m->used; \
|
||||
m->size -= m->used; \
|
||||
m->used = false; \
|
||||
\
|
||||
if (value != NULL) { \
|
||||
*value = m->mem[-1].value; \
|
||||
} \
|
||||
\
|
||||
return ret; \
|
||||
return m->found ? m->mem[-1].value : 0; \
|
||||
} \
|
||||
\
|
||||
h = hash_fn(key); \
|
||||
@ -288,15 +295,15 @@
|
||||
\
|
||||
while (true) { \
|
||||
if (m->mem[pos].key == 0) { \
|
||||
return false; \
|
||||
m->found = false; \
|
||||
return 0; \
|
||||
} else if (!sc_map_cmp_##name(&m->mem[pos], key, h)) { \
|
||||
pos = (pos + 1) & (mod); \
|
||||
continue; \
|
||||
} \
|
||||
\
|
||||
if (value != NULL) { \
|
||||
*value = m->mem[pos].value; \
|
||||
} \
|
||||
m->found = true; \
|
||||
ret = m->mem[pos].value; \
|
||||
\
|
||||
m->size--; \
|
||||
m->mem[pos].key = 0; \
|
||||
@ -320,7 +327,7 @@
|
||||
} \
|
||||
} \
|
||||
\
|
||||
return true; \
|
||||
return ret; \
|
||||
} \
|
||||
}
|
||||
|
||||
|
28
map/sc_map.h
28
map/sc_map.h
@ -65,6 +65,8 @@
|
||||
uint32_t load_fac; \
|
||||
uint32_t remap; \
|
||||
bool used; \
|
||||
bool oom; \
|
||||
bool found; \
|
||||
}; \
|
||||
\
|
||||
/** \
|
||||
@ -123,9 +125,9 @@
|
||||
* @param map map \
|
||||
* @param K key \
|
||||
* @param V value \
|
||||
* @return 'true' on success, 'false' on out of memory. \
|
||||
* @return previous value if exists \
|
||||
*/ \
|
||||
bool sc_map_put_##name(struct sc_map_##name *map, K key, V val); \
|
||||
V sc_map_put_##name(struct sc_map_##name *map, K key, V val); \
|
||||
\
|
||||
/** \
|
||||
* Get element \
|
||||
@ -143,7 +145,7 @@
|
||||
* @return 'true' if key exists, 'false' otherwise \
|
||||
*/ \
|
||||
/** NOLINTNEXTLINE */ \
|
||||
bool sc_map_get_##name(struct sc_map_##name *map, K key, V *val); \
|
||||
V sc_map_get_##name(struct sc_map_##name *map, K key); \
|
||||
\
|
||||
/** \
|
||||
* Delete element \
|
||||
@ -156,13 +158,23 @@
|
||||
* \
|
||||
* @param map map \
|
||||
* @param K key \
|
||||
* @param V pointer to put current value \
|
||||
* - if key does not exist, value is undefined \
|
||||
* - Pass NULL if you don't want to get previous 'value' \
|
||||
* @return 'true' if key exists, 'false' otherwise \
|
||||
* @return current value if exists \
|
||||
*/ \
|
||||
/** NOLINTNEXTLINE */ \
|
||||
bool sc_map_del_##name(struct sc_map_##name *map, K key, V *val);
|
||||
V sc_map_del_##name(struct sc_map_##name *map, K key);
|
||||
|
||||
/**
|
||||
* @param map map
|
||||
* @return - if put operation overrides a value, returns true
|
||||
* - if del operation deletes a key, returns true
|
||||
*/
|
||||
#define sc_map_found(map) ((map)->found)
|
||||
|
||||
/**
|
||||
* @param map map
|
||||
* @return true if put operation failed with out of memory
|
||||
*/
|
||||
#define sc_map_oom(map) ((map)->oom)
|
||||
|
||||
/**
|
||||
* Foreach loop
|
||||
|
Loading…
x
Reference in New Issue
Block a user