Skip to content

Commit

Permalink
Change HahMap to HashTable to make the description more accurate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Apr 6, 2024
1 parent 33e5d95 commit 5048475
Show file tree
Hide file tree
Showing 10 changed files with 2,777 additions and 40 deletions.
2,737 changes: 2,737 additions & 0 deletions Doxyfile

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ add_subdirectory(ast)
add_subdirectory(pair)
add_subdirectory(linked_list)
add_subdirectory(dyn_array)
add_subdirectory(hash_map)
add_subdirectory(hash_table)

add_library(AqCompiler STATIC ${SOURCES})

Expand All @@ -27,5 +27,5 @@ target_link_libraries(AqCompiler PRIVATE AqCompilerAst)
target_link_libraries(AqCompiler PRIVATE AqCompilerPair)
target_link_libraries(AqCompiler PRIVATE AqCompilerLinkedList)
target_link_libraries(AqCompiler PRIVATE AqCompilerDynArray)
target_link_libraries(AqCompiler PRIVATE AqCompilerHashMap)
target_link_libraries(AqCompiler PRIVATE AqCompilerHashTable)
target_link_libraries(AqCompiler PRIVATE AqDebugger)
2 changes: 1 addition & 1 deletion compiler/compiler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Compiler::Compiler(const char* filename) {
Lexer lexer(buffer_ptr_, code.size());
Token token;
while (true) {
lexer.LexToken(token);
lexer.Lex(token);
if (lexer.IsReadEnd()) {
break;
}
Expand Down
18 changes: 9 additions & 9 deletions compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,32 @@ class Compiler {
template <typename T1, typename T2>
struct Pair;

/// \class HashMap
/// \brief Stores ValueType in hash map
/// \class HashTable
/// \brief Stores ValueType in hash table
/// \details
/// - **Key Type**: The key is of type `std::string`, allowing string-based
/// lookup for stored elements.
/// - **Value Type**: The value associated with each key is of the template
/// parameter type `ValueType`.
/// Users can instantiate `HashMap` with any desired value type to suit
/// Users can instantiate `HashTable` with any desired value type to suit
/// their specific use case.
/// - **Hash-Based Storage**: Internally, the class employs a hash map
/// implementation (e.g., `std::unordered_map`)
/// - **Hash-Based Storage**: Internally, the class employs a hash table
/// implementation
/// to store and organize key-value pairs. Hashing ensures constant-time
/// average complexity for most operations, making `HashMap` particularly
/// average complexity for most operations, making `HashTable` particularly
/// suitable for large datasets where fast access is crucial.
/// - **Operations**:
/// - **Insertion**: New key-value pairs can be inserted into the map using
/// - **Insertion**: New key-value pairs can be inserted into the table using
/// appropriate member functions.
/// - **Retrieval**: Given a key, users can efficiently retrieve the
/// corresponding value.
/// - **Deletion**: Individual entries or entire ranges of entries can be
/// removed from the map.
/// removed from the table.
/// - **Iteration**: The container supports iteration over its elements,
/// enabling traversal or modification of all stored key-value pairs.
/// \tparam ValueType
template <typename ValueType>
class HashMap;
class HashTable;

/// \class LinkedList
/// \brief The `LinkedList` template class implements a linear collection of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ cmake_minimum_required(VERSION 3.10)

include_directories(${PROJECT_SOURCE_DIR})

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hash_map.cc)
set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/hash_table.cc)

add_library(AqCompilerHashMap STATIC ${SOURCES})
add_library(AqCompilerHashTable STATIC ${SOURCES})

target_link_libraries(AqCompilerHashMap PRIVATE AqDebugger)
target_link_libraries(AqCompilerHashTable PRIVATE AqDebugger)
20 changes: 10 additions & 10 deletions compiler/hash_map/hash_map.cc → compiler/hash_table/hash_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include "compiler/hash_map/hash_map.h"
#include "compiler/hash_table/hash_table.h"

#include <cstddef>

Expand All @@ -12,23 +12,23 @@

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

template <typename ValueType>
void Compiler::HashMap<ValueType>::Insert(std::string key, ValueType value) {
void Compiler::HashTable<ValueType>::Insert(std::string key, ValueType value) {
auto hash = static_cast<std::size_t>(Hash(key));
++size_;
if (size_ / capacity_ > 0.8) {
Expand All @@ -39,7 +39,7 @@ void Compiler::HashMap<ValueType>::Insert(std::string key, ValueType value) {
}

template <typename ValueType>
bool Compiler::HashMap<ValueType>::Find(std::string key, ValueType& value) {
bool Compiler::HashTable<ValueType>::Find(std::string key, ValueType& value) {
auto hash = static_cast<std::size_t>(Hash(key));
LinkedList<Pair<std::string, std::string>> find_list = pair_list_[hash];
typename LinkedList<Pair<std::string, ValueType>>::Iterator temp_node =
Expand All @@ -56,7 +56,7 @@ bool Compiler::HashMap<ValueType>::Find(std::string key, ValueType& value) {
}

template <typename ValueType>
unsigned int Compiler::HashMap<ValueType>::Hash(std::string key) const {
unsigned int Compiler::HashTable<ValueType>::Hash(std::string key) const {
unsigned int hash = 5381;
for (char character : key) {
hash = ((hash << 5) + hash) + static_cast<unsigned int>(character);
Expand All @@ -66,14 +66,14 @@ unsigned int Compiler::HashMap<ValueType>::Hash(std::string key) const {
};

template <typename ValueType>
int Compiler::HashMap<ValueType>::Resize() {
int Compiler::HashTable<ValueType>::Resize() {
DynArray<LinkedList<Pair<std::string, std::string>>>* temp = pair_list_;
std::size_t new_capacity = capacity_ * 1.5;
pair_list_ =
new DynArray<LinkedList<Pair<std::string, std::string>>>[new_capacity];
if (!pair_list_) {
Debugger error(Debugger::Level::ERROR,
"Aq::Compiler::Lexer::HashMap::Resize", "Resize_MemoryError",
"Aq::Compiler::Lexer::HashTable::Resize", "Resize_MemoryError",
"Memory allocation failed.", nullptr);
return -1;
}
Expand Down
20 changes: 10 additions & 10 deletions compiler/hash_map/hash_map.h → compiler/hash_table/hash_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#ifndef AQ_COMPILER_HASH_MAP_HASH_MAP_H_
#define AQ_COMPILER_HASH_MAP_HASH_MAP_H_
#ifndef AQ_COMPILER_HASH_TABLE_HASH_TABLE_H_
#define AQ_COMPILER_HASH_TABLE_HASH_TABLE_H_

#include <cstddef>

Expand All @@ -14,20 +14,20 @@

namespace Aq {
template <typename ValueType>
class Compiler::HashMap {
class Compiler::HashTable {
public:
/// \fn HashMap
/// \fn HashTable
/// \brief Creates and initialize a hash table.
/// \param init_capacity std::size_t Type, optional, default value is 1024
HashMap(std::size_t init_capacity = 1024);
~HashMap();
HashTable(std::size_t init_capacity = 1024);
~HashTable();

/// \bug These functions have many bugs when called.
/// \todo Fix these bugs.
HashMap(const HashMap&) = default;
HashMap(HashMap&&) noexcept = default;
HashMap& operator=(const HashMap&) = default;
HashMap& operator=(HashMap&&) noexcept = default;
HashTable(const HashTable&) = default;
HashTable(HashTable&&) noexcept = default;
HashTable& operator=(const HashTable&) = default;
HashTable& operator=(HashTable&&) noexcept = default;

/// \fn Insert
/// \brief Insert a new value into the hash table.
Expand Down
2 changes: 1 addition & 1 deletion compiler/lexer/lexer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Compiler::Lexer::Lexer(char* source_code, size_t length)
: buffer_ptr_(source_code), buffer_end_(source_code + length - 1){};
Compiler::Lexer::~Lexer() = default;

int Compiler::Lexer::LexToken(Token& return_token) {
int Compiler::Lexer::Lex(Token& return_token) {
using Tok = Token::Kind;
return_token.SetKind(Tok::UNKNOWN);
char* read_ptr = buffer_ptr_;
Expand Down
4 changes: 2 additions & 2 deletions compiler/lexer/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Compiler::Lexer {
Lexer& operator=(const Lexer&) = delete;
Lexer& operator=(Lexer&&) noexcept = delete;

/// \fn LexToken
/// \fn Lex
/// \brief Lexically analyze `buffer_ptr_` and store the analyzed token to
/// `return_token`.
/// \details Reads one character at a time and analyzes the token for
Expand All @@ -35,7 +35,7 @@ class Compiler::Lexer {
/// token.
/// \param return_token Token& Type.
/// \return A normal read returns `0`, and a read error returns `-1`.
int LexToken(Token& return_token);
int Lex(Token& return_token);

/// \fn IsReadEnd
/// \brief Returns whether the source code has finished reading.
Expand Down
4 changes: 2 additions & 2 deletions compiler/lexer/token_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#define AQ_COMPILER_LEXER_TOKEN_MAP_H_

#include "compiler/compiler.h"
#include "compiler/hash_map/hash_map.h"
#include "compiler/hash_table/hash_table.h"
#include "compiler/token/token.h"

namespace Aq {
Expand All @@ -30,7 +30,7 @@ class Compiler::TokenMap {

private:
/// \brief Maps tokens to kinds.
HashMap<Token::Kind> token_map_;
HashTable<Token::Kind> token_map_;
};
} // namespace Aq

Expand Down

0 comments on commit 5048475

Please sign in to comment.