This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move hashtable and utils to new files
- Loading branch information
Showing
9 changed files
with
205 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#ifndef BSONNUMPY_H | ||
#define BSONNUMPY_H | ||
|
||
#include <Python.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION | ||
#include <numpy/ndarrayobject.h> | ||
|
||
#include "bson/bson.h" | ||
|
||
typedef struct | ||
{ | ||
const char *s; | ||
size_t len; | ||
} bsnp_string_t; | ||
|
||
|
||
ssize_t | ||
bsnp_next_power_of_two(ssize_t v); | ||
|
||
void | ||
bsnp_string_init(bsnp_string_t *string, const char *s); | ||
|
||
|
||
#endif //BSONNUMPY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#include "bsonnumpy_hashtable.h" | ||
|
||
|
||
/* how much larger the table is than the number of entries */ | ||
const ssize_t TABLE_MULTIPLE = 4; | ||
|
||
|
||
void | ||
table_init(hash_table_t *table, ssize_t n_entries) | ||
{ | ||
ssize_t i; | ||
|
||
table->size = bsnp_next_power_of_two(n_entries * TABLE_MULTIPLE); | ||
table->entries = bson_malloc0(table->size * sizeof(hash_table_entry_t)); | ||
|
||
for (i = 0; i < table->size; i++) { | ||
table->entries[i].value = EMPTY; | ||
} | ||
} | ||
|
||
|
||
/* simple insertion w/ robin hood hashing. keys are always unique. no resize. */ | ||
void | ||
table_insert(hash_table_t *table, const char *key, ssize_t value) | ||
{ | ||
ssize_t mask = table->size - 1; | ||
ssize_t dist_key = 0; | ||
Py_hash_t hash; | ||
ssize_t i; | ||
|
||
hash_table_entry_t entry; | ||
|
||
bsnp_string_init(&entry.key, key); | ||
entry.value = value; | ||
|
||
hash = _Py_HashBytes(key, entry.key.len); | ||
|
||
/* table size is power of 2, hash & (size-1) is faster than hash % size */ | ||
i = entry.ideal_pos = hash & mask; | ||
|
||
while (true) { | ||
hash_table_entry_t *inplace; | ||
ssize_t dist_inplace; | ||
|
||
inplace = &table->entries[i]; | ||
if (inplace->value == EMPTY) { | ||
memcpy(inplace, &entry, sizeof(hash_table_entry_t)); | ||
table->used++; | ||
return; | ||
} | ||
|
||
/* this spot is taken. if this entry is closer to its ideal spot than | ||
* the input is, swap them and find a new place for this entry. */ | ||
dist_inplace = (i - inplace->ideal_pos) & mask; | ||
if (dist_inplace < dist_key) { | ||
hash_table_entry_t tmp; | ||
|
||
/* swap with input, start searching for place for swapped entry */ | ||
memcpy(&tmp, inplace, sizeof(hash_table_entry_t)); | ||
memcpy(inplace, &entry, sizeof(hash_table_entry_t)); | ||
memcpy(&entry, &tmp, sizeof(hash_table_entry_t)); | ||
|
||
dist_key = dist_inplace; | ||
} | ||
|
||
dist_key++; | ||
i++; | ||
i &= mask; | ||
} | ||
} | ||
|
||
|
||
ssize_t | ||
table_lookup(hash_table_t *table, const char *key) | ||
{ | ||
ssize_t mask = table->size - 1; | ||
Py_hash_t hash; | ||
ssize_t i; | ||
ssize_t dist_key = 0; | ||
|
||
hash = _Py_HashBytes(key, strlen(key)); | ||
i = hash & mask; | ||
|
||
while (true) { | ||
hash_table_entry_t *entry = &table->entries[i]; | ||
|
||
if (entry->value == EMPTY || !strcmp(entry->key.s, key)) { | ||
return entry->value; | ||
} | ||
|
||
/* we haven't yet found the key in the table, and this entry is farther | ||
* from its ideal spot than key would be if it were here, so we know | ||
* the key is absent */ | ||
if (dist_key > ((i - entry->ideal_pos) & mask)) { | ||
return EMPTY; | ||
} | ||
|
||
dist_key++; | ||
i++; | ||
i &= mask; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#ifndef BSONNUMPY_HASHTABLE_H | ||
#define BSONNUMPY_HASHTABLE_H | ||
|
||
#include "bsonnumpy.h" | ||
|
||
|
||
typedef struct | ||
{ | ||
bsnp_string_t key; | ||
ssize_t ideal_pos; | ||
ssize_t value; | ||
} hash_table_entry_t; | ||
|
||
typedef struct | ||
{ | ||
hash_table_entry_t *entries; | ||
ssize_t size; | ||
ssize_t used; | ||
} hash_table_t; | ||
|
||
static const ssize_t EMPTY = -1; | ||
|
||
void | ||
table_init(hash_table_t *table, ssize_t n_entries); | ||
|
||
void | ||
table_insert(hash_table_t *table, const char *key, ssize_t value); | ||
|
||
ssize_t | ||
table_lookup(hash_table_t *table, const char *key); | ||
|
||
#endif //BSONNUMPY_HASHTABLE_H |
Oops, something went wrong.