diff --git a/compiler/dynamic_array/const_iterator.cc b/compiler/dynamic_array/const_iterator.cc new file mode 100644 index 0000000..ad22d05 --- /dev/null +++ b/compiler/dynamic_array/const_iterator.cc @@ -0,0 +1,141 @@ +// 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/const_iterator.h" + +#include + +#include "debugger/debugger.h" + +namespace Aq { +template +Compiler::DynamicArray::const_iterator::const_iterator(DynamicArray* array, + std::size_t index) { + array_ = array; + index_ = index; + data_ = array_->at(index); +} +template +Compiler::DynamicArray::const_iterator::~const_iterator() = default; + +template +const T& Compiler::DynamicArray::const_iterator::operator*() const { + return data_; +} +template +const T* Compiler::DynamicArray::const_iterator::operator->() const { + return &data_; +} + +template +Compiler::DynamicArray::const_iterator& +Compiler::DynamicArray::const_iterator::operator++() { + return *this += 1; +} +template +Compiler::DynamicArray::const_iterator +Compiler::DynamicArray::const_iterator::operator++(int) { + const_iterator copy = *this; + *this += 1; + return copy; +} + +template +Compiler::DynamicArray::const_iterator& +Compiler::DynamicArray::const_iterator::operator--() { + return *this -= 1; +} +template +Compiler::DynamicArray::const_iterator +Compiler::DynamicArray::const_iterator::operator--(int) { + const_iterator copy = *this; + *this -= 1; + return copy; +} + +template +Compiler::DynamicArray::const_iterator +Compiler::DynamicArray::const_iterator::operator+(std::ptrdiff_t n) const { + const_iterator copy = *this; + copy += n; + return copy; +} +template +Compiler::DynamicArray::const_iterator& +Compiler::DynamicArray::const_iterator::operator+=(std::ptrdiff_t n) { + index_ += n; + if (index_ > array_->size()) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::const_iterator::operator++", + "operator++_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::const_iterator +Compiler::DynamicArray::const_iterator::operator-(std::ptrdiff_t n) const { + const_iterator copy = *this; + copy -= n; + return copy; +} +template +Compiler::DynamicArray::const_iterator +Compiler::DynamicArray::const_iterator::operator-=(std::ptrdiff_t n) { + index_ -= n; + if (index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::const_iterator::operator--", + "operator--_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::const_iterator& +Compiler::DynamicArray::const_iterator::operator[](std::size_t n) { + std::size_t original_index = index_; + index_ = n; + if (index_ > array_->size() || index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::const_iterator::operator[]", + "operator[]_IndexError", "Index out of range.", nullptr); + index_ = original_index; + return *this; + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +bool Compiler::DynamicArray::const_iterator::operator==( + const const_iterator& other) const { + return array_ == other.array_ && index_ == other.index_; +} +template +bool Compiler::DynamicArray::const_iterator::operator!=( + const const_iterator& other) const { + return !(*this == other); +} + +template +std::ptrdiff_t Compiler::DynamicArray::const_iterator::operator-( + const const_iterator& other) const { + return index_ - other.index_; +} +} // namespace Aq \ No newline at end of file diff --git a/compiler/dynamic_array/const_iterator.h b/compiler/dynamic_array/const_iterator.h new file mode 100644 index 0000000..2780745 --- /dev/null +++ b/compiler/dynamic_array/const_iterator.h @@ -0,0 +1,54 @@ +// 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_CONST_ITERATOR_H_ +#define AQ_COMPILER_DYNAMIC_ARRAY_CONST_ITERATOR_H_ + +#include "compiler/compiler.h" +#include "compiler/dynamic_array/dynamic_array.h" + +#include + +namespace Aq { +template +class Compiler::DynamicArray::const_iterator { + public: + const_iterator(DynamicArray* array, std::size_t index); + ~const_iterator(); + + const_iterator(const const_iterator&) = default; + const_iterator(const_iterator&&) noexcept = default; + const_iterator& operator=(const const_iterator&) = default; + const_iterator& operator=(const_iterator&&) noexcept = default; + + const T& operator*() const; + const T* operator->() const; + + const_iterator& operator++(); + const_iterator operator++(int); + + const_iterator& operator--(); + const_iterator operator--(int); + + const_iterator operator+(std::ptrdiff_t n) const; + const_iterator& operator+=(std::ptrdiff_t n); + + const_iterator operator-(std::ptrdiff_t n) const; + const_iterator operator-=(std::ptrdiff_t n); + + const_iterator& operator[](std::size_t n); + + bool operator==(const const_iterator& other) const; + bool operator!=(const const_iterator& other) const; + + std::ptrdiff_t operator-(const const_iterator& other) const; + + private: + DynamicArray* array_; + std::size_t index_; + const T& data_; +}; +} // namespace Aq + +#endif \ No newline at end of file diff --git a/compiler/dynamic_array/const_reverse_iterator.cc b/compiler/dynamic_array/const_reverse_iterator.cc new file mode 100644 index 0000000..8b5bcfa --- /dev/null +++ b/compiler/dynamic_array/const_reverse_iterator.cc @@ -0,0 +1,141 @@ +// 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/const_reverse_iterator.h" + +#include + +#include "debugger/debugger.h" + +namespace Aq { +template +Compiler::DynamicArray::const_reverse_iterator::const_reverse_iterator(DynamicArray* array, + std::size_t index) { + array_ = array; + index_ = index; + data_ = array_->at(index); +} +template +Compiler::DynamicArray::const_reverse_iterator::~const_reverse_iterator() = default; + +template +T& Compiler::DynamicArray::const_reverse_iterator::operator*() const { + return data_; +} +template +T* Compiler::DynamicArray::const_reverse_iterator::operator->() const { + return &data_; +} + +template +Compiler::DynamicArray::const_reverse_iterator& +Compiler::DynamicArray::const_reverse_iterator::operator++() { + return *this += 1; +} +template +Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::const_reverse_iterator::operator++(int) { + const_reverse_iterator copy = *this; + *this += 1; + return copy; +} + +template +Compiler::DynamicArray::const_reverse_iterator& +Compiler::DynamicArray::const_reverse_iterator::operator--() { + return *this -= 1; +} +template +Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::const_reverse_iterator::operator--(int) { + const_reverse_iterator copy = *this; + *this -= 1; + return copy; +} + +template +Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::const_reverse_iterator::operator+(std::ptrdiff_t n) const { + const_reverse_iterator copy = *this; + copy += n; + return copy; +} +template +Compiler::DynamicArray::const_reverse_iterator& +Compiler::DynamicArray::const_reverse_iterator::operator+=(std::ptrdiff_t n) { + index_ += n; + if (index_ > array_->size()) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::const_reverse_iterator::operator++", + "operator++_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::const_reverse_iterator::operator-(std::ptrdiff_t n) const { + const_reverse_iterator copy = *this; + copy -= n; + return copy; +} +template +Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::const_reverse_iterator::operator-=(std::ptrdiff_t n) { + index_ -= n; + if (index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::const_reverse_iterator::operator--", + "operator--_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::const_reverse_iterator& +Compiler::DynamicArray::const_reverse_iterator::operator[](std::size_t n) { + std::size_t original_index = index_; + index_ = n; + if (index_ > array_->size() || index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::const_reverse_iterator::operator[]", + "operator[]_IndexError", "Index out of range.", nullptr); + index_ = original_index; + return *this; + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +bool Compiler::DynamicArray::const_reverse_iterator::operator==( + const const_reverse_iterator& other) const { + return array_ == other.array_ && index_ == other.index_; +} +template +bool Compiler::DynamicArray::const_reverse_iterator::operator!=( + const const_reverse_iterator& other) const { + return !(*this == other); +} + +template +std::ptrdiff_t Compiler::DynamicArray::const_reverse_iterator::operator-( + const const_reverse_iterator& other) const { + return index_ - other.index_; +} +} // namespace Aq \ No newline at end of file diff --git a/compiler/dynamic_array/const_reverse_iterator.h b/compiler/dynamic_array/const_reverse_iterator.h new file mode 100644 index 0000000..332b28d --- /dev/null +++ b/compiler/dynamic_array/const_reverse_iterator.h @@ -0,0 +1,54 @@ +// 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_ITERATOR_H_ +#define AQ_COMPILER_DYNAMIC_ARRAY_ITERATOR_H_ + +#include "compiler/compiler.h" +#include "compiler/dynamic_array/dynamic_array.h" + +#include + +namespace Aq { +template +class Compiler::DynamicArray::const_reverse_iterator { + public: + const_reverse_iterator(DynamicArray* array, std::size_t index); + ~const_reverse_iterator(); + + const_reverse_iterator(const const_reverse_iterator&) = default; + const_reverse_iterator(const_reverse_iterator&&) noexcept = default; + const_reverse_iterator& operator=(const const_reverse_iterator&) = default; + const_reverse_iterator& operator=(const_reverse_iterator&&) noexcept = default; + + T& operator*() const; + T* operator->() const; + + const_reverse_iterator& operator++(); + const_reverse_iterator operator++(int); + + const_reverse_iterator& operator--(); + const_reverse_iterator operator--(int); + + const_reverse_iterator operator+(std::ptrdiff_t n) const; + const_reverse_iterator& operator+=(std::ptrdiff_t n); + + const_reverse_iterator operator-(std::ptrdiff_t n) const; + const_reverse_iterator operator-=(std::ptrdiff_t n); + + const_reverse_iterator& operator[](std::size_t n); + + bool operator==(const const_reverse_iterator& other) const; + bool operator!=(const const_reverse_iterator& other) const; + + std::ptrdiff_t operator-(const const_reverse_iterator& other) const; + + private: + DynamicArray* array_; + std::size_t index_; + T& data_; +}; +} // namespace Aq + +#endif \ No newline at end of file diff --git a/compiler/dynamic_array/dynamic_array.cc b/compiler/dynamic_array/dynamic_array.cc index b435697..3625298 100644 --- a/compiler/dynamic_array/dynamic_array.cc +++ b/compiler/dynamic_array/dynamic_array.cc @@ -4,5 +4,337 @@ #include "compiler/dynamic_array/dynamic_array.h" -// Wait development. -namespace Aq {} \ No newline at end of file +#include + +#include "compiler/dynamic_array/const_iterator.h" +#include "compiler/dynamic_array/const_reverse_iterator.h" +#include "compiler/dynamic_array/iterator.h" +#include "compiler/dynamic_array/reverse_iterator.h" +#include "debugger/debugger.h" + +namespace Aq { +template +Compiler::DynamicArray::DynamicArray() = default; +template +Compiler::DynamicArray::DynamicArray(std::size_t capacity, const T& value) { + array_ = new T[size]; + if (array_ == nullptr) { + Debugger error( + Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::DynamicArray", + "DynamicArray_InitError", "Size out of memory occurred.", nullptr); + return; + } + capacity_ = capacity; + fill(value); +} +template +Compiler::DynamicArray::~DynamicArray() { + delete[] array_; +} + +template +Compiler::DynamicArray::DynamicArray(const DynamicArray& other) { + *this = other; +} +template +Compiler::DynamicArray::DynamicArray(DynamicArray&& other) noexcept { + *this = other; +} +template +Compiler::DynamicArray& Compiler::DynamicArray::operator=( + const DynamicArray& other) { + if (this != &other) { + clear(); + array_ = new T[other.capacity_]; + if (array_ == nullptr) { + Debugger error( + Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::DynamicArray", + "DynamicArray_InitError", "Size out of memory occurred.", nullptr); + return *this; + } + capacity_ = other.capacity_; + size_ = other.size_; + for (size_t i = 0; i < size_; ++i) { + array_[i] = other.array_[i]; + } + } + return *this; +} +template +Compiler::DynamicArray& Compiler::DynamicArray::operator=( + DynamicArray&& other) noexcept { + if (this != &other) { + clear(); + array_ = other.array_; + size_ = other.size_; + capacity_ = other.capacity_; + other.array_ = nullptr; + other.size_ = 0; + other.capacity_ = 0; + } + return *this; +} + +template +typename Compiler::DynamicArray::iterator Compiler::DynamicArray::begin() { + return iterator(this, 0); +} +template +typename Compiler::DynamicArray::const_iterator Compiler::DynamicArray::begin() + const { + return const_iterator(this, 0); +} +template +typename Compiler::DynamicArray::const_iterator Compiler::DynamicArray::cbegin() + const { + return const_iterator(this, 0); +} +template +typename Compiler::DynamicArray::reverse_iterator +Compiler::DynamicArray::rbegin() { + return reverse_iterator(this, 0); +} +template +typename Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::rbegin() const { + return const_reverse_iterator(this, 0); +} +template +typename Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::crbegin() const { + return const_reverse_iterator(this, 0); +} +template +typename Compiler::DynamicArray::iterator Compiler::DynamicArray::end() { + return iterator(this, size_); +} +template +typename Compiler::DynamicArray::const_iterator Compiler::DynamicArray::end() + const { + return const_iterator(this, size_); +} +template +typename Compiler::DynamicArray::const_iterator Compiler::DynamicArray::cend() + const { + return const_iterator(this, size_); +} +template +typename Compiler::DynamicArray::reverse_iterator Compiler::DynamicArray::rend() { + return reverse_iterator(this, size_); +} +template +typename Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::rend() const { + return const_reverse_iterator(this, size_); +} +template +typename Compiler::DynamicArray::const_reverse_iterator +Compiler::DynamicArray::crend() const { + return const_reverse_iterator(this, size_); +} + +template +std::size_t Compiler::DynamicArray::size() const { + return size_; +} +template +bool Compiler::DynamicArray::empty() const { + if (size_ == 0) { + return true; + } + return false; +} +template +std::size_t Compiler::DynamicArray::capacity() const { + return capacity_; +} +template +void Compiler::DynamicArray::reserve(std::size_t count) { + if (count <= capacity_) { + return; + } + T* new_array = new T[count]; + if (new_array == nullptr) { + Debugger error( + Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::DynamicArray", + "DynamicArray_InitError", "Size out of memory occurred.", nullptr); + return; + } + for (size_t i = 0; i < size_; ++i) { + new_array[i] = array_[i]; + } + delete[] array_; + array_ = new_array; + capacity_ = count; +} +template +void Compiler::DynamicArray::shrink_to_fit() { + if (size_ == capacity_) { + return; + } + T* new_array = new T[size_]; + if (new_array == nullptr) { + Debugger error( + Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::DynamicArray", + "DynamicArray_InitError", "Size out of memory occurred.", nullptr); + return; + } + for (size_t i = 0; i < size_; ++i) { + new_array[i] = array_[i]; + } + delete[] array_; + array_ = new_array; + capacity_ = size_; +} + +template +T& Compiler::DynamicArray::operator[](std::size_t index) { + return at(index); +} +template +const T& Compiler::DynamicArray::operator[](std::size_t index) const { + return at(index); +} +template +T& Compiler::DynamicArray::at(std::size_t index) { + if (index >= size_) { + Debugger error(Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::at", + "IndexOutOfRangeError", "The index is out of range.", + nullptr); + return T(); + } + return at(index); +} +template +const T& Compiler::DynamicArray::at(std::size_t index) const { + if (index >= size_) { + Debugger error(Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::at", + "IndexOutOfRangeError", "The index is out of range.", + nullptr); + return T(); + } + return array_[index]; +} +template +T& Compiler::DynamicArray::front() { + return at(0); +} +template +const T& Compiler::DynamicArray::front() const { + return at(0); +} +template +T& Compiler::DynamicArray::back() { + return at(size_ - 1); +} +template +const T& Compiler::DynamicArray::back() const { + return at(size_ - 1); +} +template +T* Compiler::DynamicArray::data() { + return array_; +} +template +const T* Compiler::DynamicArray::data() const { + return array_; +} + +template +void Compiler::DynamicArray::push_back(const T& value) { + if (size_ > capacity_) { + Debugger error( + Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::push_back", + "PushBack_SizeError", "Size out of capacity occurred.", nullptr); + return; + } + if (size_ == capacity_) { + reserve(1); + reserve(capacity_ * 2); + } + array_[size_] = value; + ++size_; +} +template +void Compiler::DynamicArray::push_back(T&& value) { + if (size_ > capacity_) { + Debugger error( + Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::push_back", + "PushBack_SizeError", "Size out of capacity occurred.", nullptr); + return; + } + if (size_ == capacity_) { + reserve(1); + reserve(capacity_ * 2); + } + array_[size_] = value; + ++size_; +} +template +typename Compiler::DynamicArray::iterator Compiler::DynamicArray::insert( + typename Compiler::DynamicArray::const_iterator position, const T& value) { + if (size_ > capacity_) { + Debugger error(Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::insert", + "Insert_SizeError", "Size out of capacity occurred.", + nullptr); + return iterator(); + } + if (size_ == capacity_) { + reserve(1); + reserve(capacity_ * 2); + } + for (std::size_t i = size_; i > position.index_; --i) { + array_[i] = array_[i - 1]; + } + array_[position.index] = value; + ++size_; + return iterator(this, position.index); +} +template +typename Compiler::DynamicArray::iterator Compiler::DynamicArray::insert( + typename Compiler::DynamicArray::const_iterator position, T&& value) { + if (size_ > capacity_) { + Debugger error(Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::insert", + "Insert_SizeError", "Size out of capacity occurred.", + nullptr); + return iterator(); + } + if (size_ == capacity_) { + reserve(1); + reserve(capacity_ * 2); + } + for (std::size_t i = size_; i > position.index_; --i) { + array_[i] = array_[i - 1]; + } + array_[position.index] = value; + ++size_; + return iterator(this, position.index); +} +template +typename Compiler::DynamicArray::iterator Compiler::DynamicArray::insert( + typename Compiler::DynamicArray::const_iterator position, std::size_t count, + const T& value) { + if (size_ > capacity_) { + Debugger error(Debugger::Level::ERROR, "Aq::Compiler::DynamicArray::insert", + "Insert_SizeError", "Size out of capacity occurred.", + nullptr); + return iterator(); + } + while (size_ + count > capacity_) { + reserve(1); + reserve(capacity_ * 2); + } + for (std::size_t i = size_ + count - 1; i > position.index_; --i) { + array_[i] = array_[i - count]; + } + for (std::size_t i = 0; i < count; ++i) { + array_[position.index + i] = value; + } + size_ += count; + return iterator(this, position.index); +} +template +typename Compiler::DynamicArray::iterator Compiler::DynamicArray::insert( + typename Compiler::DynamicArray::const_iterator position, iterator first, + iterator last) {} +} // namespace Aq \ No newline at end of file diff --git a/compiler/dynamic_array/dynamic_array.h b/compiler/dynamic_array/dynamic_array.h index 0a3b169..9ef8621 100644 --- a/compiler/dynamic_array/dynamic_array.h +++ b/compiler/dynamic_array/dynamic_array.h @@ -13,7 +13,7 @@ template class Compiler::DynamicArray { public: DynamicArray(); - DynamicArray(size_t count, const T& value = T()); + DynamicArray(std::size_t capacity, const T& value = T()); ~DynamicArray(); DynamicArray(const DynamicArray&); @@ -22,24 +22,33 @@ class Compiler::DynamicArray { DynamicArray& operator=(DynamicArray&&) noexcept; class iterator; + class const_iterator; + class reverse_iterator; + class const_reverse_iterator; iterator begin(); - const iterator begin() const; + const_iterator begin() const; + const_iterator cbegin() const; + reverse_iterator rbegin(); + const_reverse_iterator rbegin() const; + const_reverse_iterator crbegin() const; iterator end(); - const iterator end() const; - iterator cbegin() const; - iterator cend() const; + const_iterator end() const; + const_iterator cend() const; + reverse_iterator rend(); + const_reverse_iterator rend() const; + const_reverse_iterator crend() const; - size_t size() const; + std::size_t size() const; bool empty() const; - void reserve(size_t new_cap); - size_t capacity() const; + void reserve(std::size_t count); + std::size_t capacity() const; void shrink_to_fit(); - T& operator[](size_t index); - const T& operator[](size_t index) const; - T& at(size_t index); - const T& at(size_t index) const; + T& operator[](std::size_t index); + const T& operator[](std::size_t index) const; + T& at(std::size_t index); + const T& at(std::size_t index) const; T& front(); const T& front() const; T& back(); @@ -49,23 +58,23 @@ class Compiler::DynamicArray { void push_back(const T& value); void push_back(T&& value); - iterator insert(const iterator pos, const T& value); - iterator insert(const iterator pos, T&& value); - iterator insert(const iterator pos, size_t count, const T& value); - iterator insert(const iterator pos, iterator first, iterator last); + iterator insert(const_iterator position, const T& value); + iterator insert(const_iterator position, T&& value); + iterator insert(const_iterator position, std::size_t count, const T& value); + iterator insert(const_iterator position, iterator first, iterator last); - iterator erase(const iterator pos); - iterator erase(const iterator first, const iterator last); + iterator erase(const_iterator position); + iterator erase(const_iterator first, const_iterator last); void pop_back(); void clear(); void swap(DynamicArray& other); - void resize(size_t count); - void resize(size_t count, const T& value); + void resize(std::size_t count); + void resize(std::size_t count, const T& value); void fill(const T& value); void assign(iterator first, iterator last); - void assign(size_t count, const T& value); + void assign(std::size_t count, const T& value); bool operator==(const DynamicArray& other) const; bool operator!=(const DynamicArray& other) const; bool operator<(const DynamicArray& other) const; @@ -74,9 +83,9 @@ class Compiler::DynamicArray { bool operator>=(const DynamicArray& other) const; private: - T* array_; - std::size_t capacity_; - std::size_t size_; + T* array_ = nullptr; + std::size_t capacity_ = 0; + std::size_t size_ = 0; }; } // namespace Aq diff --git a/compiler/dynamic_array/iterator.cc b/compiler/dynamic_array/iterator.cc index 1fccf2c..8e53951 100644 --- a/compiler/dynamic_array/iterator.cc +++ b/compiler/dynamic_array/iterator.cc @@ -1,3 +1,141 @@ // 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/iterator.h" + +#include + +#include "debugger/debugger.h" + +namespace Aq { +template +Compiler::DynamicArray::iterator::iterator(DynamicArray* array, + std::size_t index) { + array_ = array; + index_ = index; + data_ = array_->at(index); +} +template +Compiler::DynamicArray::iterator::~iterator() = default; + +template +T& Compiler::DynamicArray::iterator::operator*() const { + return data_; +} +template +T* Compiler::DynamicArray::iterator::operator->() const { + return &data_; +} + +template +Compiler::DynamicArray::iterator& +Compiler::DynamicArray::iterator::operator++() { + return *this += 1; +} +template +Compiler::DynamicArray::iterator +Compiler::DynamicArray::iterator::operator++(int) { + iterator copy = *this; + *this += 1; + return copy; +} + +template +Compiler::DynamicArray::iterator& +Compiler::DynamicArray::iterator::operator--() { + return *this -= 1; +} +template +Compiler::DynamicArray::iterator +Compiler::DynamicArray::iterator::operator--(int) { + iterator copy = *this; + *this -= 1; + return copy; +} + +template +Compiler::DynamicArray::iterator +Compiler::DynamicArray::iterator::operator+(std::ptrdiff_t n) const { + iterator copy = *this; + copy += n; + return copy; +} +template +Compiler::DynamicArray::iterator& +Compiler::DynamicArray::iterator::operator+=(std::ptrdiff_t n) { + index_ += n; + if (index_ > array_->size()) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::iterator::operator++", + "operator++_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::iterator +Compiler::DynamicArray::iterator::operator-(std::ptrdiff_t n) const { + iterator copy = *this; + copy -= n; + return copy; +} +template +Compiler::DynamicArray::iterator +Compiler::DynamicArray::iterator::operator-=(std::ptrdiff_t n) { + index_ -= n; + if (index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::iterator::operator--", + "operator--_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::iterator& +Compiler::DynamicArray::iterator::operator[](std::size_t n) { + std::size_t original_index = index_; + index_ = n; + if (index_ > array_->size() || index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::iterator::operator[]", + "operator[]_IndexError", "Index out of range.", nullptr); + index_ = original_index; + return *this; + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +bool Compiler::DynamicArray::iterator::operator==( + const iterator& other) const { + return array_ == other.array_ && index_ == other.index_; +} +template +bool Compiler::DynamicArray::iterator::operator!=( + const iterator& other) const { + return !(*this == other); +} + +template +std::ptrdiff_t Compiler::DynamicArray::iterator::operator-( + const iterator& other) const { + return index_ - other.index_; +} +} // namespace Aq \ No newline at end of file diff --git a/compiler/dynamic_array/iterator.h b/compiler/dynamic_array/iterator.h index 47c6d7b..48ac368 100644 --- a/compiler/dynamic_array/iterator.h +++ b/compiler/dynamic_array/iterator.h @@ -14,7 +14,7 @@ namespace Aq { template class Compiler::DynamicArray::iterator { public: - iterator(); + iterator(DynamicArray* array, std::size_t index); ~iterator(); iterator(const iterator&) = default; @@ -37,12 +37,17 @@ class Compiler::DynamicArray::iterator { iterator operator-(std::ptrdiff_t n) const; iterator operator-=(std::ptrdiff_t n); - iterator& operator[](std::ptrdiff_t n); + iterator& operator[](std::size_t n); bool operator==(const iterator& other) const; bool operator!=(const iterator& other) const; - std::ptrdiff_t operator-(const Iterator& other) const; + std::ptrdiff_t operator-(const iterator& other) const; + + private: + DynamicArray* array_; + std::size_t index_; + T& data_; }; } // namespace Aq diff --git a/compiler/dynamic_array/reverse_iterator.cc b/compiler/dynamic_array/reverse_iterator.cc new file mode 100644 index 0000000..eea4b86 --- /dev/null +++ b/compiler/dynamic_array/reverse_iterator.cc @@ -0,0 +1,141 @@ +// 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/reverse_iterator.h" + +#include + +#include "debugger/debugger.h" + +namespace Aq { +template +Compiler::DynamicArray::reverse_iterator::reverse_iterator(DynamicArray* array, + std::size_t index) { + array_ = array; + index_ = index; + data_ = array_->at(index); +} +template +Compiler::DynamicArray::reverse_iterator::~reverse_iterator() = default; + +template +T& Compiler::DynamicArray::reverse_iterator::operator*() const { + return data_; +} +template +T* Compiler::DynamicArray::reverse_iterator::operator->() const { + return &data_; +} + +template +Compiler::DynamicArray::reverse_iterator& +Compiler::DynamicArray::reverse_iterator::operator++() { + return *this += 1; +} +template +Compiler::DynamicArray::reverse_iterator +Compiler::DynamicArray::reverse_iterator::operator++(int) { + reverse_iterator copy = *this; + *this += 1; + return copy; +} + +template +Compiler::DynamicArray::reverse_iterator& +Compiler::DynamicArray::reverse_iterator::operator--() { + return *this -= 1; +} +template +Compiler::DynamicArray::reverse_iterator +Compiler::DynamicArray::reverse_iterator::operator--(int) { + reverse_iterator copy = *this; + *this -= 1; + return copy; +} + +template +Compiler::DynamicArray::reverse_iterator +Compiler::DynamicArray::reverse_iterator::operator+(std::ptrdiff_t n) const { + reverse_iterator copy = *this; + copy += n; + return copy; +} +template +Compiler::DynamicArray::reverse_iterator& +Compiler::DynamicArray::reverse_iterator::operator+=(std::ptrdiff_t n) { + index_ += n; + if (index_ > array_->size()) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::reverse_iterator::operator++", + "operator++_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::reverse_iterator +Compiler::DynamicArray::reverse_iterator::operator-(std::ptrdiff_t n) const { + reverse_iterator copy = *this; + copy -= n; + return copy; +} +template +Compiler::DynamicArray::reverse_iterator +Compiler::DynamicArray::reverse_iterator::operator-=(std::ptrdiff_t n) { + index_ -= n; + if (index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::reverse_iterator::operator--", + "operator--_IndexError", "Index out of range.", nullptr); + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +Compiler::DynamicArray::reverse_iterator& +Compiler::DynamicArray::reverse_iterator::operator[](std::size_t n) { + std::size_t original_index = index_; + index_ = n; + if (index_ > array_->size() || index_ < 0) { + Debugger error(Debugger::Level::ERROR, + "Aq::Compiler::DynamicArray::reverse_iterator::operator[]", + "operator[]_IndexError", "Index out of range.", nullptr); + index_ = original_index; + return *this; + } + if (index_ == array_->size()) { + data_ = T(); + return *this; + } + data_ = array_->at(index_); + return *this; +} + +template +bool Compiler::DynamicArray::reverse_iterator::operator==( + const reverse_iterator& other) const { + return array_ == other.array_ && index_ == other.index_; +} +template +bool Compiler::DynamicArray::reverse_iterator::operator!=( + const reverse_iterator& other) const { + return !(*this == other); +} + +template +std::ptrdiff_t Compiler::DynamicArray::reverse_iterator::operator-( + const reverse_iterator& other) const { + return index_ - other.index_; +} +} // namespace Aq \ No newline at end of file diff --git a/compiler/dynamic_array/reverse_iterator.h b/compiler/dynamic_array/reverse_iterator.h new file mode 100644 index 0000000..d2a4f41 --- /dev/null +++ b/compiler/dynamic_array/reverse_iterator.h @@ -0,0 +1,54 @@ +// 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_ITERATOR_H_ +#define AQ_COMPILER_DYNAMIC_ARRAY_ITERATOR_H_ + +#include "compiler/compiler.h" +#include "compiler/dynamic_array/dynamic_array.h" + +#include + +namespace Aq { +template +class Compiler::DynamicArray::reverse_iterator { + public: + reverse_iterator(DynamicArray* array, std::size_t index); + ~reverse_iterator(); + + reverse_iterator(const reverse_iterator&) = default; + reverse_iterator(reverse_iterator&&) noexcept = default; + reverse_iterator& operator=(const reverse_iterator&) = default; + reverse_iterator& operator=(reverse_iterator&&) noexcept = default; + + T& operator*() const; + T* operator->() const; + + reverse_iterator& operator++(); + reverse_iterator operator++(int); + + reverse_iterator& operator--(); + reverse_iterator operator--(int); + + reverse_iterator operator+(std::ptrdiff_t n) const; + reverse_iterator& operator+=(std::ptrdiff_t n); + + reverse_iterator operator-(std::ptrdiff_t n) const; + reverse_iterator operator-=(std::ptrdiff_t n); + + reverse_iterator& operator[](std::size_t n); + + bool operator==(const reverse_iterator& other) const; + bool operator!=(const reverse_iterator& other) const; + + std::ptrdiff_t operator-(const reverse_iterator& other) const; + + private: + DynamicArray* array_; + std::size_t index_; + T& data_; +}; +} // namespace Aq + +#endif \ No newline at end of file diff --git a/compiler/lexer/lexer.cc b/compiler/lexer/lexer.cc index 81ddaa6..f4c6e30 100644 --- a/compiler/lexer/lexer.cc +++ b/compiler/lexer/lexer.cc @@ -280,7 +280,7 @@ int Compiler::Lexer::Lex(Token& return_token) { goto LexStart; LexEnd: - /// \todo Wait development. +/* /// \todo Wait development. // Meaningless token. if (return_token.GetKind() == Tok::UNKNOWN || return_token.GetKind() == Tok::COMMENT) { @@ -339,7 +339,7 @@ int Compiler::Lexer::Lex(Token& return_token) { nullptr); return -2; } - } + }*/ return 0; }