Created
August 23, 2022 20:57
-
-
Save MisaghM/f14887e38224c6c21d07d0cac025341a to your computer and use it in GitHub Desktop.
Simple single allocation multidimensional arrays in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ARRAY2D_HPP_INCLUDE | |
#define ARRAY2D_HPP_INCLUDE | |
#include <algorithm> | |
#include <cstddef> | |
template <class T> | |
class Array2D { | |
public: | |
// clang-format off | |
using value_type = T; | |
using reference = T&; | |
using const_reference = const T&; | |
using pointer = T*; | |
using const_pointer = const T*; | |
using size_type = std::size_t; | |
using difference_type = std::ptrdiff_t; | |
// clang-format on | |
Array2D(size_type row, size_type col) | |
: arr_(new value_type[row * col]()), | |
row_(row), | |
col_(col) {} | |
explicit Array2D(size_type n) | |
: Array2D(n, n) {} | |
~Array2D() { | |
delete[] arr_; | |
} | |
Array2D(const Array2D& other) | |
: Array2D(other.row_, other.col_) { | |
std::copy(other.arr_, other.arr_ + other.size(), arr_); | |
} | |
Array2D(Array2D&& other) noexcept { | |
swap(*this, other); | |
} | |
Array2D& operator=(const Array2D& rhs) { | |
Array2D temp(rhs); | |
swap(*this, temp); | |
return *this; | |
} | |
Array2D& operator=(Array2D&& rhs) noexcept { | |
swap(*this, rhs); | |
return *this; | |
} | |
friend void swap(Array2D& a, Array2D& b) noexcept { | |
using std::swap; | |
swap(a.arr_, b.arr_); | |
swap(a.row_, b.row_); | |
swap(a.col_, b.col_); | |
} | |
void fill(const_reference x) { | |
std::fill(arr_, arr_ + size(), x); | |
} | |
size_type rows() const noexcept { return row_; } | |
size_type cols() const noexcept { return col_; } | |
size_type size() const noexcept { return row_ * col_; } | |
// clang-format off | |
pointer operator[](size_type i) noexcept { return arr_ + i * col_; } | |
const_pointer operator[](size_type i) const noexcept { return arr_ + i * col_; } | |
// clang-format on | |
private: | |
pointer arr_ = nullptr; | |
size_type row_; | |
size_type col_; | |
}; | |
#endif // ARRAY2D_HPP_INCLUDE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ARRAY3D_HPP_INCLUDE | |
#define ARRAY3D_HPP_INCLUDE | |
#include <algorithm> | |
#include <cstddef> | |
template <class T> | |
class Array3D { | |
public: | |
// clang-format off | |
using value_type = T; | |
using reference = T&; | |
using const_reference = const T&; | |
using pointer = T*; | |
using const_pointer = const T*; | |
using size_type = std::size_t; | |
using difference_type = std::ptrdiff_t; | |
// clang-format on | |
Array3D(size_type row, size_type col, size_type depth) | |
: arr_(new value_type[row * col * depth]()), | |
row_(row), | |
col_(col), | |
depth_(depth) {} | |
explicit Array3D(size_type n) | |
: Array3D(n, n, n) {} | |
~Array3D() { | |
delete[] arr_; | |
} | |
Array3D(const Array3D& other) | |
: Array3D(other.row_, other.col_, other_.depth_) { | |
std::copy(other.arr_, other.arr_ + other.size(), arr_); | |
} | |
Array3D(Array3D&& other) noexcept { | |
swap(*this, other); | |
} | |
Array3D& operator=(const Array3D& rhs) { | |
Array3D temp(rhs); | |
swap(*this, temp); | |
return *this; | |
} | |
Array3D& operator=(Array3D&& rhs) noexcept { | |
swap(*this, rhs); | |
return *this; | |
} | |
friend void swap(Array3D& a, Array3D& b) noexcept { | |
using std::swap; | |
swap(a.arr_, b.arr_); | |
swap(a.row_, b.row_); | |
swap(a.col_, b.col_); | |
swap(a.depth_, b.depth_); | |
} | |
void fill(const_reference x) { | |
std::fill(arr_, arr_ + size(), x); | |
} | |
size_type rows() const noexcept { return row_; } | |
size_type cols() const noexcept { return col_; } | |
size_type depth() const noexcept { return depth_; } | |
size_type size() const noexcept { return row_ * col_ * depth_; } | |
reference operator()(size_type i, size_type j, size_type k) noexcept { | |
return arr_[i * col_ * depth_ + j * depth_ + k]; | |
}; | |
const_reference operator()(size_type i, size_type j, size_type k) const noexcept { | |
return arr_[i * col_ * depth_ + j * depth_ + k]; | |
}; | |
private: | |
pointer arr_ = nullptr; | |
size_type row_; | |
size_type col_; | |
size_type depth_; | |
}; | |
#endif // ARRAY3D_HPP_INCLUDE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment