Skip to content

Commit

Permalink
Added the DynamicArray class to replace the original DynArray class. …
Browse files Browse the repository at this point in the history
…undone.
  • Loading branch information
ax-6 committed Apr 12, 2024
1 parent 37f4b06 commit 443291c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_subdirectory(token)
add_subdirectory(ast)
add_subdirectory(pair)
add_subdirectory(linked_list)
add_subdirectory(dynamic_array)
add_subdirectory(dyn_array)
add_subdirectory(hash_table)
add_subdirectory(symbol_table)
Expand Down
24 changes: 24 additions & 0 deletions compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ class Compiler {
template <typename DataType>
class LinkedList;

/// \class DynamicArray
/// \brief The `DynamicArray` template class provides a contiguous memory
/// \details
/// - **Template Parameter**:
/// - `ArrayType`: The type of the data stored in the dynamic array.
/// - **Memory Management**:
/// - The array internally manages its own memory, automatically growing or
/// shrinking as needed to accommodate changes in the number of stored
/// elements.
/// - **Methods**:
/// - **Construction/Destruction**: The array can be created with an initial
/// capacity or default-constructed with zero elements. Destruction releases
/// the underlying memory.
/// - **Size**:
/// - `size()`: Returns the current number of elements in the array.
/// - **Element Access**:
/// - `operator[]`: Allows direct read/write access to elements by index
/// (bounds-checked in debug builds).
/// - **Modifiers**:
/// - `insert()`, `remove()`: Inserts/Removes elements at arbitrary
/// positions, shifting subsequent elements as needed.
template <typename ArrayType>
class DynamicArray;

/// \class DynArray
/// \brief The `DynArray` template class provides a contiguous memory buffer
/// capable of holding elements of type `ArrayType`. It combines the
Expand Down
13 changes: 13 additions & 0 deletions compiler/dynamic_array/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright 2024 AQ authors, All Rights Reserved.
# This program is licensed under the AQ License. You can find the AQ license in
# the root directory.

cmake_minimum_required(VERSION 3.10)

include_directories(${PROJECT_SOURCE_DIR})

set(SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/dynamic_array.cc)

add_library(AqCompilerDynamicArray STATIC ${SOURCES})

target_link_libraries(AqCompilerDynamicArray PRIVATE AqDebugger)
8 changes: 8 additions & 0 deletions compiler/dynamic_array/dynamic_array.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright 2024 AQ authors, All Rights Reserved.
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#include "compiler/dynamic_array/dynamic_array.h"

// Wait development.
namespace Aq {}
51 changes: 51 additions & 0 deletions compiler/dynamic_array/dynamic_array.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2024 AQ authors, All Rights Reserved.
// This program is licensed under the AQ License. You can find the AQ license in
// the root directory.

#ifndef AQ_COMPILER_DYNAMIC_ARRAY_DYNAMIC_ARRAY_H_
#define AQ_COMPILER_DYNAMIC_ARRAY_DYNAMIC_ARRAY_H_

#include "compiler/compiler.h"

// Wait development.
namespace Aq {
template <typename ArrayType>
class Compiler::DynamicArray {
public:
DynamicArray();
~DynamicArray();

DynamicArray(const DynamicArray&);
DynamicArray(DynamicArray&&) noexcept;
DynamicArray& operator=(const DynamicArray&);
DynamicArray& operator=(DynamicArray&&) noexcept;

ArrayType& operator[](std::size_t index);
const ArrayType& operator[](std::size_t index) const;

ArrayType& at(std::size_t index);
const ArrayType& at(std::size_t index) const;

void push_back(const ArrayType& value);
void push_back(ArrayType&& value);
void insert(std::size_t index, const ArrayType& value);
void insert(std::size_t index, ArrayType&& value);

void pop_back();
void erase(std::size_t index);
void clear();

std::size_t size() const;
std::size_t capacity() const;

void resize(std::size_t new_size);
void reserve(std::size_t new_capacity);

private:
ArrayType* array_;
std::size_t capacity_;
std::size_t size_;
};
}

#endif

0 comments on commit 443291c

Please sign in to comment.