Skip to content

Commit

Permalink
Updated the definition of the DynamicArray class. Basic completion. S…
Browse files Browse the repository at this point in the history
…ome of the iterators were not completed correctly.
  • Loading branch information
ax-6 committed Apr 14, 2024
1 parent 9da2ba0 commit 02d7c83
Show file tree
Hide file tree
Showing 11 changed files with 1,100 additions and 31 deletions.
141 changes: 141 additions & 0 deletions compiler/dynamic_array/const_iterator.cc
Original file line number Diff line number Diff line change
@@ -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 <cstddef>

#include "debugger/debugger.h"

namespace Aq {
template <typename T>
Compiler::DynamicArray<T>::const_iterator::const_iterator(DynamicArray<T>* array,
std::size_t index) {
array_ = array;
index_ = index;
data_ = array_->at(index);
}
template <typename T>
Compiler::DynamicArray<T>::const_iterator::~const_iterator() = default;

template <typename T>
const T& Compiler::DynamicArray<T>::const_iterator::operator*() const {
return data_;
}
template <typename T>
const T* Compiler::DynamicArray<T>::const_iterator::operator->() const {
return &data_;
}

template <typename T>
Compiler::DynamicArray<T>::const_iterator&
Compiler::DynamicArray<T>::const_iterator::operator++() {
return *this += 1;
}
template <typename T>
Compiler::DynamicArray<T>::const_iterator
Compiler::DynamicArray<T>::const_iterator::operator++(int) {
const_iterator copy = *this;
*this += 1;
return copy;
}

template <typename T>
Compiler::DynamicArray<T>::const_iterator&
Compiler::DynamicArray<T>::const_iterator::operator--() {
return *this -= 1;
}
template <typename T>
Compiler::DynamicArray<T>::const_iterator
Compiler::DynamicArray<T>::const_iterator::operator--(int) {
const_iterator copy = *this;
*this -= 1;
return copy;
}

template <typename T>
Compiler::DynamicArray<T>::const_iterator
Compiler::DynamicArray<T>::const_iterator::operator+(std::ptrdiff_t n) const {
const_iterator copy = *this;
copy += n;
return copy;
}
template <typename T>
Compiler::DynamicArray<T>::const_iterator&
Compiler::DynamicArray<T>::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 <typename T>
Compiler::DynamicArray<T>::const_iterator
Compiler::DynamicArray<T>::const_iterator::operator-(std::ptrdiff_t n) const {
const_iterator copy = *this;
copy -= n;
return copy;
}
template <typename T>
Compiler::DynamicArray<T>::const_iterator
Compiler::DynamicArray<T>::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 <typename T>
Compiler::DynamicArray<T>::const_iterator&
Compiler::DynamicArray<T>::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 <typename T>
bool Compiler::DynamicArray<T>::const_iterator::operator==(
const const_iterator& other) const {
return array_ == other.array_ && index_ == other.index_;
}
template <typename T>
bool Compiler::DynamicArray<T>::const_iterator::operator!=(
const const_iterator& other) const {
return !(*this == other);
}

template <typename T>
std::ptrdiff_t Compiler::DynamicArray<T>::const_iterator::operator-(
const const_iterator& other) const {
return index_ - other.index_;
}
} // namespace Aq
54 changes: 54 additions & 0 deletions compiler/dynamic_array/const_iterator.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>

namespace Aq {
template <typename T>
class Compiler::DynamicArray<T>::const_iterator {
public:
const_iterator(DynamicArray<T>* 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<T>* array_;
std::size_t index_;
const T& data_;
};
} // namespace Aq

#endif
141 changes: 141 additions & 0 deletions compiler/dynamic_array/const_reverse_iterator.cc
Original file line number Diff line number Diff line change
@@ -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 <cstddef>

#include "debugger/debugger.h"

namespace Aq {
template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator::const_reverse_iterator(DynamicArray<T>* array,
std::size_t index) {
array_ = array;
index_ = index;
data_ = array_->at(index);
}
template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator::~const_reverse_iterator() = default;

template <typename T>
T& Compiler::DynamicArray<T>::const_reverse_iterator::operator*() const {
return data_;
}
template <typename T>
T* Compiler::DynamicArray<T>::const_reverse_iterator::operator->() const {
return &data_;
}

template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator&
Compiler::DynamicArray<T>::const_reverse_iterator::operator++() {
return *this += 1;
}
template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator
Compiler::DynamicArray<T>::const_reverse_iterator::operator++(int) {
const_reverse_iterator copy = *this;
*this += 1;
return copy;
}

template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator&
Compiler::DynamicArray<T>::const_reverse_iterator::operator--() {
return *this -= 1;
}
template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator
Compiler::DynamicArray<T>::const_reverse_iterator::operator--(int) {
const_reverse_iterator copy = *this;
*this -= 1;
return copy;
}

template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator
Compiler::DynamicArray<T>::const_reverse_iterator::operator+(std::ptrdiff_t n) const {
const_reverse_iterator copy = *this;
copy += n;
return copy;
}
template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator&
Compiler::DynamicArray<T>::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 <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator
Compiler::DynamicArray<T>::const_reverse_iterator::operator-(std::ptrdiff_t n) const {
const_reverse_iterator copy = *this;
copy -= n;
return copy;
}
template <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator
Compiler::DynamicArray<T>::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 <typename T>
Compiler::DynamicArray<T>::const_reverse_iterator&
Compiler::DynamicArray<T>::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 <typename T>
bool Compiler::DynamicArray<T>::const_reverse_iterator::operator==(
const const_reverse_iterator& other) const {
return array_ == other.array_ && index_ == other.index_;
}
template <typename T>
bool Compiler::DynamicArray<T>::const_reverse_iterator::operator!=(
const const_reverse_iterator& other) const {
return !(*this == other);
}

template <typename T>
std::ptrdiff_t Compiler::DynamicArray<T>::const_reverse_iterator::operator-(
const const_reverse_iterator& other) const {
return index_ - other.index_;
}
} // namespace Aq
54 changes: 54 additions & 0 deletions compiler/dynamic_array/const_reverse_iterator.h
Original file line number Diff line number Diff line change
@@ -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 <cstddef>

namespace Aq {
template <typename T>
class Compiler::DynamicArray<T>::const_reverse_iterator {
public:
const_reverse_iterator(DynamicArray<T>* 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<T>* array_;
std::size_t index_;
T& data_;
};
} // namespace Aq

#endif
Loading

0 comments on commit 02d7c83

Please sign in to comment.