Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra Credit Robinhood hashing -- Naman #36

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ file(GLOB_RECURSE INCLUDE_H
set(
INCLUDE_H
${CMAKE_SOURCE_DIR}/src/include
${CMAKE_SOURCE_DIR}/test/unit/include
# ${CMAKE_SOURCE_DIR}/test/unit/include
${CMAKE_SOURCE_DIR}
)

Expand Down
23 changes: 22 additions & 1 deletion src/include/utils/file.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@

#include <cstdint>
#include <memory>
#include <vector>
#include <optional>

using namespace std;
namespace buzzdb{
namespace utils{
class RHT{
public:
RHT(int table_size);
int table_size;
int keys_inserted;
struct data {
int key;
// optional <int> val;
int val;
};
vector<data> data_items;
vector<int> probe_seq_len;
int key_hasher(int key);
bool insert(int key, int val);
pair<int, bool> search(int key);
bool erase(int key);
};



void hello_world();

} // namespace utils
} // namespace buzzdb
91 changes: 89 additions & 2 deletions src/utils/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,102 @@
#include <algorithm>
#include <fstream>
#include <iostream>
#include <utility>
#include "../include/utils/file.h"

namespace buzzdb {
namespace utils {

#define UNUSED(p) ((void)(p))

void hello_world(){
printf("Hello world");
utils::RHT::RHT(int table_size){
cout << "coming here" << endl;
this->table_size = table_size;
this->keys_inserted = 0;
this->data_items.reserve(table_size);
this->probe_seq_len.reserve(table_size);
for(int i=0;i<table_size;i++){
this->data_items[i].val = -1;
this->probe_seq_len[i] = 0;
}
}

int utils::RHT::key_hasher(int key){
return (key % table_size);
}

bool utils::RHT::insert(int key, int val){
bool inserted = false;
if(keys_inserted == table_size)
return inserted;

int hash_key = key_hasher(key);
int key_probe = 0;
int curr_key = hash_key;
while(true){
if(data_items[hash_key].val == -1){
data_items[hash_key].key = key;
data_items[hash_key].val = val;
keys_inserted++;
inserted = true;
break;
}
else if(key_probe > probe_seq_len[curr_key]){
data tmp = data_items[curr_key];
data_items[curr_key] = data_items[hash_key];
key = tmp.key;
val = tmp.val;
key_probe = 0;
}
key_probe++;
curr_key = (curr_key + 1) % table_size;
}
return inserted;
}

pair<int, bool> utils::RHT::search(int key) {
bool found = false;
int found_val = -1;
int hash_key = key_hasher(key);
int key_probe = 0;
int curr_key = hash_key;
while(true){
if(data_items[hash_key].val != -1 && data_items[hash_key].key == key){
found = true;
found_val = data_items[hash_key].val;
break;
}
else if(data_items[hash_key].val == -1 || key_probe > probe_seq_len[curr_key]){
break;
}
key_probe++;
curr_key = (curr_key + 1) % table_size;
}
return make_pair(found_val, found);
}

bool utils::RHT::erase(int key){
bool deleted = false;
int hash_key = key_hasher(key);
int key_probe = 0;
int curr_key = hash_key;
while(true){
if(data_items[hash_key].val == -1 || key_probe > probe_seq_len[curr_key]){
break;
}
else if(data_items[hash_key].key == key){
data_items[hash_key].val = -1; // try to make the val nullable | maybe using optional? | int when set to NULL defaults to 0
probe_seq_len[hash_key] = -1;
keys_inserted--;
deleted = true;
break;
}
key_probe++;
curr_key = (curr_key + 1) % table_size;
}
return deleted;
}


} // namespace utils
} // namespace buzzdb
43 changes: 41 additions & 2 deletions test/unit/utils/file_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,47 @@

namespace {

TEST(FileTest, HelloWorld) {
buzzdb::utils::hello_world();
TEST(FileTest, InsertTest) {
auto RH_table = buzzdb::utils::RHT(5);

std::cout << "Table created" << std::endl;
for (int i = 0; i < RH_table.table_size; i++) {
EXPECT_EQ(RH_table.insert(i, i), true);
}
std::cout << "out of loop" << std::endl;
EXPECT_NE(RH_table.insert(6, 6), true);
}
TEST(FileTest, SearchKeyFoundTest) {
auto RH_table = buzzdb::utils::RHT(5);

std::cout << "Table created" << std::endl;
RH_table.insert(2, 2);
// Key found
pair<int, bool> search_result = RH_table.search(2);
EXPECT_EQ(search_result.first, 2);
EXPECT_EQ(search_result.second, true);
}
TEST(FileTest, SearchKeyNotFoundTest) {
auto RH_table = buzzdb::utils::RHT(5);

std::cout << "Table created" << std::endl;
RH_table.insert(1, 1);
// Key not found
pair<int, bool> search_result = RH_table.search(2);
EXPECT_EQ(search_result.first, -1);
EXPECT_EQ(search_result.second, false);
}
TEST(FileTest, DeleteTest) {
auto RH_table = buzzdb::utils::RHT(5);

std::cout << "Table created" << std::endl;
RH_table.insert(1, 1);
// erase the key
RH_table.erase(1);
// Key not found
pair<int, bool> search_result = RH_table.search(1);
EXPECT_EQ(search_result.first, -1);
EXPECT_EQ(search_result.second, false);
}

} // namespace
Expand Down