Skip to content

Commit

Permalink
The HashMap class is temporarily stored and has not been fully develo…
Browse files Browse the repository at this point in the history
…ped yet.
  • Loading branch information
ax-6 committed Mar 22, 2024
1 parent c54f95b commit 97d85d7
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 140 deletions.
2 changes: 1 addition & 1 deletion compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Compiler {

template <typename ValueType>
class HashMap;
template <typename ArrayType, std::size_t InitCapacity>
template <typename ArrayType>
class DynArray;
class TokenMap;
class Token;
Expand Down
32 changes: 18 additions & 14 deletions compiler/dyn_array/dyn_array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "debugger/debugger.h"

namespace Aq {
template <typename ArrayType, std::size_t InitCapacity>
Compiler::DynArray<ArrayType, InitCapacity>::DynArray() {
template <typename ArrayType>
Compiler::DynArray<ArrayType>::DynArray(std::size_t InitCapacity) {
data_ = new ArrayType[InitCapacity];
if (data_ != nullptr) {
capacity_ = InitCapacity;
Expand All @@ -32,13 +32,13 @@ Compiler::DynArray<ArrayType, InitCapacity>::DynArray() {
size_ = 0;
}

template <typename ArrayType, std::size_t InitCapacity>
Compiler::DynArray<ArrayType, InitCapacity>::~DynArray() {
template <typename ArrayType>
Compiler::DynArray<ArrayType>::~DynArray() {
delete[] data_;
}

template <typename ArrayType, std::size_t InitCapacity>
void Compiler::DynArray<ArrayType, InitCapacity>::PushBack(ArrayType data) {
template <typename ArrayType>
void Compiler::DynArray<ArrayType>::PushBack(ArrayType data) {
if (capacity_ == 0 && Resize(1) == -1) {
return;
}
Expand All @@ -54,9 +54,8 @@ void Compiler::DynArray<ArrayType, InitCapacity>::PushBack(ArrayType data) {
size_++;
}

template <typename ArrayType, std::size_t InitCapacity>
int Compiler::DynArray<ArrayType, InitCapacity>::Resize(
std::size_t new_capacity) {
template <typename ArrayType>
int Compiler::DynArray<ArrayType>::Resize(std::size_t new_capacity) {
if (new_capacity == 0) {
capacity_ *= 2;
} else {
Expand All @@ -77,9 +76,14 @@ int Compiler::DynArray<ArrayType, InitCapacity>::Resize(
return 0;
}

template <typename ArrayType, std::size_t InitCapacity>
Compiler::DynArray<ArrayType, InitCapacity>::Iterator::Iterator(
DynArray<ArrayType, InitCapacity>* array, std::size_t index)
template <typename ArrayType>
std::size_t Compiler::DynArray<ArrayType>::Size() const {
return size_;
}

template <typename ArrayType>
Compiler::DynArray<ArrayType>::Iterator::Iterator(DynArray<ArrayType>* array,
std::size_t index)
: array_(array), index_(index) {
if (index_ >= array_->size_) {
Debugger error_info(Debugger::Level::ERROR,
Expand All @@ -88,7 +92,7 @@ Compiler::DynArray<ArrayType, InitCapacity>::Iterator::Iterator(
}
}

template <typename ArrayType, std::size_t InitCapacity>
Compiler::DynArray<ArrayType, InitCapacity>::Iterator::~Iterator() = default;
template <typename ArrayType>
Compiler::DynArray<ArrayType>::Iterator::~Iterator() = default;

} // namespace Aq
11 changes: 7 additions & 4 deletions compiler/dyn_array/dyn_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include "debugger/debugger.h"

namespace Aq {
template <typename ArrayType, std::size_t InitCapacity>
template <typename ArrayType>
class Compiler::DynArray {
public:
DynArray();
DynArray(std::size_t InitCapacity = 1);
~DynArray();

DynArray(const DynArray&) = default;
Expand Down Expand Up @@ -42,11 +42,14 @@ class Compiler::DynArray {
// an error in the allocation.
int Resize(std::size_t new_capacity = 0);


std::size_t Size() const;

class Iterator {
public:
using iterator_category = std::random_access_iterator_tag;

Iterator(DynArray<ArrayType, InitCapacity>* array, std::size_t index = 0);
Iterator(DynArray<ArrayType>* array, std::size_t index = 0);

~Iterator();

Expand Down Expand Up @@ -155,7 +158,7 @@ class Compiler::DynArray {
}

private:
DynArray<ArrayType, InitCapacity>* array_;
DynArray<ArrayType>* array_;
std::size_t index_;
};

Expand Down
150 changes: 149 additions & 1 deletion compiler/hash_map/hash_map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,152 @@

#include "compiler/hash_map/hash_map.h"

// TODO: Hash Map
#include <cstddef>

#include "compiler/compiler.h"
#include "compiler/dyn_array/dyn_array.h"
#include "debugger/debugger.h"

namespace Aq {
template <typename ValueType>
Compiler::HashMap<ValueType>::HashMap(std::size_t init_capacity) {
pair_list_ = new DynArray<ValueType>(init_capacity);
if (!pair_list_) {
Debugger error_info(
Debugger::Level::ERROR, "Aq::Compiler::Lexer::HashMap::HashMap",
"HashMap_MemoryError", "Memory allocation failed.", nullptr);
return;
}
capacity_ = init_capacity;
}
template <typename ValueType>
Compiler::HashMap<ValueType>::~HashMap() {
delete pair_list_;
}

template <typename ValueType>
void Compiler::HashMap<ValueType>::Insert(std::string key, ValueType value) {
unsigned int hash = Hash(key);

// Increase the size of the hash table.
size_++;
if (size_ / capacity_ > 0.8) {
Resize();
}

// Create key-value pairs and insert them into the linked list.
Pair pair;
pair.key = key;
pair.value = value;
pair_list_[hash].Prepend(pair);
}

template <typename ValueType>
ValueType Compiler::HashMap<ValueType>::Find(std::string key) {
unsigned int hash = Hash(key);
return pair_list_[hash].Find(key);
}

template <typename ValueType>
Compiler::HashMap<ValueType>::PairList::PairList() = default;

template <typename ValueType>
Compiler::HashMap<ValueType>::PairList::~PairList() {
while (head_ptr_ != nullptr) {
Node* temp = head_ptr_;
head_ptr_ = head_ptr_->next;
delete temp;
}
}

template <typename ValueType>
void Compiler::HashMap<ValueType>::PairList::Prepend(Pair value) {
Node* new_node = new Node(value);
new_node->next = head_ptr_;
head_ptr_ = new_node;
}

template <typename ValueType>
void Compiler::HashMap<ValueType>::PairList::CopyDataToNewList(
PairList* new_list, size_t new_capacity) {
PairList::Node* temp_node = head_ptr_;
while (temp_node != nullptr) {
unsigned int hash = 5381;
for (char character : temp_node->data.key) {
// hash = hash * 33 + static_cast<unsigned int>(character)
hash = ((hash << 5) + hash) + static_cast<unsigned int>(character);
}
hash = hash % new_capacity;
new_list[hash].Append(temp_node->data);
temp_node = temp_node->next;
}
}

template <typename ValueType>
void Compiler::HashMap<ValueType>::PairList::Append(Pair value) {
if (head_ptr_ == nullptr) {
head_ptr_ = new Node(value);
} else {
// Find the last node and append the new node.
Node* temp = head_ptr_;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = new Node(value);
}
}

template <typename ValueType>
ValueType Compiler::HashMap<ValueType>::PairList::Find(std::string key) {
Node* temp = head_ptr_;

// Compare keys one by one to find the corresponding value.
while (temp != nullptr) {
if (key == temp->data.key) {
return temp->data.value;
};
temp = temp->next;
}

// Key not found, return nullptr.
return static_cast<ValueType>(0);
}

template <typename ValueType>
unsigned int Compiler::HashMap<ValueType>::Hash(std::string key) const {
unsigned int hash = 5381;
for (char character : key) {
// hash = hash * 33 + static_cast<unsigned int>(character)
hash = ((hash << 5) + hash) + static_cast<unsigned int>(character);
}
hash = hash % capacity_;
return hash;
};

template <typename ValueType>
int Compiler::HashMap<ValueType>::Resize() {
PairList* temp = pair_list_;
std::size_t new_capacity = capacity_ * 1.5;
pair_list_ = new PairList[new_capacity];

// Memory allocation failed.
if (!pair_list_) {
Debugger error_info(
Debugger::Level::ERROR, "Aq::Compiler::Lexer::HashMap::Resize",
"Resize_MemoryError", "Memory allocation failed.", nullptr);
return -1;
}

// Copy data.
for (int i = 0; i < capacity_; i++) {
temp[i].CopyDataToNewList(pair_list_, new_capacity);
}

// Release the memory of the original linked list.
delete[] temp;

capacity_ = new_capacity;
return 0;
}

} // namespace Aq
Loading

0 comments on commit 97d85d7

Please sign in to comment.