Skip to content

Commit

Permalink
add new tpht functions
Browse files Browse the repository at this point in the history
 * refactor/clean up code
  • Loading branch information
EinfachAndy committed Dec 23, 2020
1 parent 077859e commit 3dbf031
Show file tree
Hide file tree
Showing 13 changed files with 370 additions and 172 deletions.
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ cmake_minimum_required (VERSION 3.0)

project(oha C CXX)

# global vaiables
set(LIBNAME "oha")
set(PROJECT_COMPILE_OPTIONS -O3 -std=c11 -Wall -Wextra -Wpedantic)

option(ASAN "use adress sanitizer" OFF)
if(ASAN)
message(STATUS "Enable ASAN.")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
set(PROJECT_COMPILE_OPTIONS -fsanitize=address -fno-omit-frame-pointer ${PROJECT_COMPILE_OPTIONS})
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fsanitize=address -fno-omit-frame-pointer")
endif()

option(WITH_KEY_FROM_VALUE_FUNC "enabled the function: 'oha_lpht_get_key_from_value()'" OFF)

set(LIBNAME "oha")
set(PROJECT_COMPILE_OPTIONS -O3 -std=c11 -Wall -Wextra -Wpedantic)

add_subdirectory(src)
include(CTest)
add_subdirectory(test)
30 changes: 16 additions & 14 deletions include/oha.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ struct oha_lpht_status {

struct oha_lpht * oha_lpht_create(const struct oha_lpht_config * config);
void oha_lpht_destroy(struct oha_lpht * table);
void * oha_lpht_look_up(struct oha_lpht * table, const void * key);
__attribute__((pure))
void * oha_lpht_look_up(const struct oha_lpht * table, const void * key);
void * oha_lpht_insert(struct oha_lpht * table, const void * key);
__attribute__((pure))
void * oha_lpht_get_key_from_value(const void * value);
void * oha_lpht_remove(struct oha_lpht * table, const void * key);
bool oha_lpht_get_status(struct oha_lpht * table, struct oha_lpht_status * status);
bool oha_lpht_get_status(const struct oha_lpht * table, struct oha_lpht_status * status);
void oha_lpht_clear(struct oha_lpht * table);
bool oha_lpht_get_next_element_to_remove(struct oha_lpht * table, struct oha_key_value_pair * pair);

Expand All @@ -65,7 +67,7 @@ bool oha_lpht_get_next_element_to_remove(struct oha_lpht * table, struct oha_key
*
**********************************************************************************************************************/
struct oha_bh_config {
struct oha_memory_fp memory;
struct oha_memory_fp memory;
size_t value_size;
uint32_t max_elems;
bool resizable;
Expand All @@ -74,7 +76,8 @@ struct oha_bh_config {
struct oha_bh;
struct oha_bh * oha_bh_create(const struct oha_bh_config * config);
void oha_bh_destroy(struct oha_bh * heap);
int64_t oha_bh_find_min(struct oha_bh * heap);
__attribute__((pure))
int64_t oha_bh_find_min(const struct oha_bh * heap);
void * oha_bh_delete_min(struct oha_bh * heap);
void * oha_bh_insert(struct oha_bh * heap, int64_t key);
int64_t oha_bh_change_key(struct oha_bh * heap, void * value, int64_t new_val);
Expand All @@ -86,21 +89,20 @@ int64_t oha_bh_change_key(struct oha_bh * heap, void * value, int64_t new_val);
*
**********************************************************************************************************************/
struct oha_tpht;
#define OHA_MAX_TIMEOUT_SLOTS 10
struct oha_tpht_config {
struct oha_lpht_config lpht_config;
int64_t timeout_slots[OHA_MAX_TIMEOUT_SLOTS];
size_t number_of_timeout_slots;
};

struct oha_tpht * oha_tpht_create(const struct oha_tpht_config * config);
void oha_tpht_destroy(struct oha_tpht * pht);
void * oha_tpht_insert(struct oha_tpht * pht, void * key, int64_t timestamp);
void * oha_tpht_look_up(struct oha_tpht * pht, void * key);
void * oha_tpht_remove(struct oha_tpht * pht, void * key);
void * oha_tpht_set_timeout_slot(struct oha_tpht * pht, void * key, size_t timeout_slot_id);
bool oha_tpht_next_timeout_entry(struct oha_tpht * pht, struct oha_key_value_pair * next_pair);
void * oha_tpht_update_time_for_entry(struct oha_tpht * pht, void * key, int64_t new_timestamp);
void oha_tpht_destroy(struct oha_tpht * tpht);
void * oha_tpht_insert(struct oha_tpht * tpht, const void * key, int64_t timestamp, uint8_t timeout_slot_id);
__attribute__((pure))
void * oha_tpht_look_up(const struct oha_tpht * tpht, const void * key);
void * oha_tpht_remove(struct oha_tpht * tpht, const void * key);
int8_t oha_tpht_add_timeout_slot(struct oha_tpht * tpht, int64_t timeout, uint32_t num_elements, bool resizable);
void * oha_tpht_set_timeout_slot(struct oha_tpht * tpht, const void * key, uint8_t timeout_slot_id);
bool oha_tpht_next_timeout_entry(struct oha_tpht * tpht, struct oha_key_value_pair * next_pair);
void * oha_tpht_update_time_for_entry(struct oha_tpht * tpht, const void * key, int64_t new_timestamp);

#ifdef __cplusplus
}
Expand Down
4 changes: 3 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
set(SOURCE_FILES linear_probing_hash_table.c binary_heap.c prioritized_hash_table.c)
set(SOURCE_FILES linear_probing_hash_table.c
binary_heap.c
temporal_prioritized_hash_table.c)

if(WITH_KEY_FROM_VALUE_FUNC)
add_definitions(-DOHA_WITH_KEY_FROM_VALUE_SUPPORT)
Expand Down
32 changes: 17 additions & 15 deletions src/binary_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static inline uint_fast32_t right(uint_fast32_t i)
return (2 * i + 2);
}

static inline void swap_keys(struct key_bucket * restrict a, struct key_bucket * restrict b)
static inline void swap_keys(struct key_bucket * const restrict a, struct key_bucket * const restrict b)
{
a->value->key = b;
b->value->key = a;
Expand All @@ -49,7 +49,7 @@ static inline void swap_keys(struct key_bucket * restrict a, struct key_bucket *
*b = tmp_a;
}

static inline void connect_keys_values(struct oha_bh * heap)
static inline void connect_keys_values(struct oha_bh * const heap)
{
// connect keys and values
struct value_bucket * tmp_value = heap->values;
Expand All @@ -60,7 +60,7 @@ static inline void connect_keys_values(struct oha_bh * heap)
}
}

static bool resize_table(struct oha_bh * heap)
static bool resize_table(struct oha_bh * const heap)
{
if (!heap->config.resizable) {
return false;
Expand All @@ -69,15 +69,17 @@ static bool resize_table(struct oha_bh * heap)
struct oha_memory_fp * memory = &heap->config.memory;
struct oha_bh tmp_heap = {0};
tmp_heap.config = heap->config;
// TODO add 32 bit overflow check
tmp_heap.config.max_elems *= 2;
const struct oha_bh_config * config = &tmp_heap.config;

tmp_heap.keys = oha_calloc(memory, tmp_heap.config.max_elems * sizeof(struct key_bucket));
tmp_heap.keys = oha_calloc(memory, config->max_elems * sizeof(struct key_bucket));
if (tmp_heap.keys == NULL) {
return false;
}

tmp_heap.value_size = add_alignment(sizeof(struct value_bucket) + tmp_heap.config.value_size);
tmp_heap.values = oha_malloc(memory, tmp_heap.config.max_elems * tmp_heap.value_size);
tmp_heap.value_size = add_alignment(sizeof(struct value_bucket) + config->value_size);
tmp_heap.values = oha_malloc(memory, config->max_elems * tmp_heap.value_size);
if (tmp_heap.values == NULL) {
oha_free(memory, tmp_heap.keys);
return false;
Expand All @@ -89,7 +91,7 @@ static bool resize_table(struct oha_bh * heap)
while (OHA_BH_NOT_FOUND != (tmp_key = oha_bh_find_min(heap))) {
void * value = oha_bh_delete_min(heap);
void * new_value = oha_bh_insert(&tmp_heap, tmp_key);
memcpy(new_value, value, heap->value_size);
memcpy(new_value, value, config->value_size);
}

// destroy old table buffers
Expand All @@ -102,7 +104,7 @@ static bool resize_table(struct oha_bh * heap)
return true;
}

static void heapify(struct oha_bh * heap, uint_fast32_t i)
static void heapify(struct oha_bh * const heap, uint_fast32_t i)
{
uint_fast32_t l = left(i);
uint_fast32_t r = right(i);
Expand All @@ -117,7 +119,7 @@ static void heapify(struct oha_bh * heap, uint_fast32_t i)
}
}

void oha_bh_destroy(struct oha_bh * heap)
void oha_bh_destroy(struct oha_bh * const heap)
{
if (heap == NULL) {
return;
Expand All @@ -128,14 +130,14 @@ void oha_bh_destroy(struct oha_bh * heap)
oha_free(memory, heap);
}

struct oha_bh * oha_bh_create(const struct oha_bh_config * config)
struct oha_bh * oha_bh_create(const struct oha_bh_config * const config)
{
if (config == NULL) {
return NULL;
}
const struct oha_memory_fp * memory = &config->memory;

struct oha_bh * heap = oha_calloc(memory, sizeof(struct oha_bh));
struct oha_bh * const heap = oha_calloc(memory, sizeof(struct oha_bh));
if (heap == NULL) {
return NULL;
}
Expand All @@ -160,7 +162,7 @@ struct oha_bh * oha_bh_create(const struct oha_bh_config * config)
return heap;
}

void * oha_bh_insert(struct oha_bh * heap, int64_t key)
void * oha_bh_insert(struct oha_bh * const heap, int64_t key)
{
if (heap == NULL) {
return NULL;
Expand All @@ -185,15 +187,15 @@ void * oha_bh_insert(struct oha_bh * heap, int64_t key)
return heap->keys[i].value->value_buffer;
}

int64_t oha_bh_find_min(struct oha_bh * heap)
int64_t oha_bh_find_min(const struct oha_bh * const heap)
{
if (heap == NULL || heap->elems == 0) {
return OHA_BH_NOT_FOUND;
}
return heap->keys[0].key;
}

void * oha_bh_delete_min(struct oha_bh * heap)
void * oha_bh_delete_min(struct oha_bh * const heap)
{
if (heap == NULL) {
return NULL;
Expand All @@ -214,7 +216,7 @@ void * oha_bh_delete_min(struct oha_bh * heap)
return heap->keys[heap->elems].value->value_buffer;
}

int64_t oha_bh_change_key(struct oha_bh * heap, void * value, int64_t new_val)
int64_t oha_bh_change_key(struct oha_bh * const heap, void * const value, int64_t new_val)
{
if (heap == NULL || value == NULL) {
return 0;
Expand Down
Loading

0 comments on commit 3dbf031

Please sign in to comment.