Skip to content

Commit

Permalink
added more tpht functions
Browse files Browse the repository at this point in the history
  • Loading branch information
EinfachAndy committed Feb 22, 2021
1 parent 3dbf031 commit 3d582e3
Show file tree
Hide file tree
Showing 6 changed files with 566 additions and 51 deletions.
45 changes: 27 additions & 18 deletions License
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
Copyright (c) 2020 Andy Günther
This software is provided under the BSD license. The text of this license
is provided below:

--------------------------------------------------------------------------

Copyright (C) 2010-2021 Andy Guenther
All rights reserved.

BSD 2-Clause License (https://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
3. Neither the name of the author nor the names of any contributors
may be used to endorse or promote products derived from this
software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
7 changes: 5 additions & 2 deletions include/oha.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ struct oha_bh_config {
bool resizable;
};
#define OHA_BH_NOT_FOUND INT64_MIN
#define OHA_BH_MIN_VALUE (OHA_BH_NOT_FOUND + 1)
struct oha_bh;
struct oha_bh * oha_bh_create(const struct oha_bh_config * config);
void oha_bh_destroy(struct oha_bh * heap);
Expand All @@ -81,6 +82,7 @@ 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);
void * oha_bh_remove(struct oha_bh * const heap, void * const value, int64_t * out_key);

/**********************************************************************************************************************
* tpht (temporal prioritized hash table)
Expand All @@ -95,13 +97,14 @@ struct oha_tpht_config {

struct oha_tpht * oha_tpht_create(const struct oha_tpht_config * config);
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);
bool oha_tpht_increase_global_time(struct oha_tpht * tpht, int64_t timestamp);
void * oha_tpht_insert(struct oha_tpht * tpht, const void * key, 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);
size_t oha_tpht_next_timeout_entries(struct oha_tpht * tpht, struct oha_key_value_pair * next_pair, size_t num_pairs);
void * oha_tpht_update_time_for_entry(struct oha_tpht * tpht, const void * key, int64_t new_timestamp);

#ifdef __cplusplus
Expand Down
44 changes: 40 additions & 4 deletions src/binary_heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ struct oha_bh {
struct key_bucket * keys;
};

static inline struct value_bucket * get_value_bucket(void * value)
{
struct value_bucket * value_bucket =
(struct value_bucket *)((uint8_t *)value - offsetof(struct value_bucket, value_buffer));
assert(value_bucket->key->value == value_bucket);
return value_bucket;
}

static inline uint_fast32_t parent(uint_fast32_t i)
{
return (i - 1) / 2;
Expand Down Expand Up @@ -192,6 +200,10 @@ int64_t oha_bh_find_min(const struct oha_bh * const heap)
if (heap == NULL || heap->elems == 0) {
return OHA_BH_NOT_FOUND;
}
if (heap->elems == 0) {
return OHA_BH_NOT_FOUND;
}

return heap->keys[0].key;
}

Expand All @@ -216,15 +228,39 @@ void * oha_bh_delete_min(struct oha_bh * const heap)
return heap->keys[heap->elems].value->value_buffer;
}

void * oha_bh_remove(struct oha_bh * const heap, void * const value, int64_t * out_key)
{
if (heap == NULL || value == NULL) {
return NULL;
}

struct value_bucket * value_bucket = get_value_bucket(value);
struct key_bucket * key = value_bucket->key;
int64_t key_value = key->key;

// move to top
if (OHA_BH_MIN_VALUE != oha_bh_change_key(heap, value, OHA_BH_MIN_VALUE)) {
return NULL;
}
// delete the swapped element
if (oha_bh_delete_min(heap) != value_bucket->value_buffer) {
return NULL;
}

if (out_key != NULL) {
*out_key = key_value;
}

return value_bucket->value_buffer;
}

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;
return OHA_BH_NOT_FOUND;
}

struct value_bucket * value_bucket =
(struct value_bucket *)((uint8_t *)value - offsetof(struct value_bucket, value_buffer));
assert(value_bucket->key->value == value_bucket);
struct value_bucket * value_bucket = get_value_bucket(value);

enum mode {
UNCHANGED_KEY,
Expand Down
6 changes: 2 additions & 4 deletions src/linear_probing_hash_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

#include "utils.h"

#define XXHASH_SEED 0xc800c831bc63dff8

#ifdef OHA_FIX_KEY_SIZE_IN_BYTES
#if OHA_FIX_KEY_SIZE_IN_BYTES == 0
#error "unsupported compile time key size"
Expand Down Expand Up @@ -88,9 +86,9 @@ static uint64_t hash_key(const struct oha_lpht * const table, const void * const
{
#ifdef OHA_FIX_KEY_SIZE_IN_BYTES
(void)table;
return XXH3_64bits_withSeed(key, OHA_FIX_KEY_SIZE_IN_BYTES, XXHASH_SEED);
return XXH3_64bits(key, OHA_FIX_KEY_SIZE_IN_BYTES);
#else
return XXH3_64bits_withSeed(key, table->config.key_size, XXHASH_SEED);
return XXH3_64bits(key, table->config.key_size);
#endif
}

Expand Down
Loading

0 comments on commit 3d582e3

Please sign in to comment.