ArrayBase.hpp
Go to the documentation of this file.
1 // This code is based on Jet framework.
2 // Copyright (c) 2018 Doyub Kim
3 // CubbyFlow is voxel-based fluid simulation engine for computer games.
4 // Copyright (c) 2020 CubbyFlow Team
5 // Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6 // AI Part: Dongheon Cho, Minseo Kim
7 // We are making my contributions/submissions to this project solely in our
8 // personal capacity and are not conveying any rights to any intellectual
9 // property of any third parties.
10 
11 #ifndef CUBBYFLOW_ARRAY_BASE_HPP
12 #define CUBBYFLOW_ARRAY_BASE_HPP
13 
14 #include <Core/Matrix/Matrix.hpp>
15 
16 namespace CubbyFlow
17 {
18 template <typename T, size_t N, typename DerivedArray>
19 class ArrayBase
20 {
21  public:
22  using Derived = DerivedArray;
23  using ValueType = T;
24  using Reference = T&;
25  using ConstReference = const T&;
26  using Pointer = T*;
27  using ConstPointer = const T*;
28  using Iterator = T*;
29  using ConstIterator = const T*;
30 
31  [[nodiscard]] size_t Index(size_t i) const;
32 
33  template <typename... Args>
34  [[nodiscard]] size_t Index(size_t i, Args... args) const;
35 
36  template <size_t... I>
37  [[nodiscard]] size_t Index(const Vector<size_t, N>& idx) const;
38 
39  [[nodiscard]] Pointer data();
40 
41  [[nodiscard]] ConstPointer data() const;
42 
43  [[nodiscard]] const Vector<size_t, N>& Size() const;
44 
45  template <size_t M = N>
46  std::enable_if_t<(M > 0), size_t> Width() const;
47 
48  template <size_t M = N>
49  std::enable_if_t<(M > 1), size_t> Height() const;
50 
51  template <size_t M = N>
52  std::enable_if_t<(M > 2), size_t> Depth() const;
53 
54  [[nodiscard]] bool IsEmpty() const;
55 
56  [[nodiscard]] size_t Length() const;
57 
58  [[nodiscard]] Iterator begin();
59 
60  [[nodiscard]] ConstIterator begin() const;
61 
62  [[nodiscard]] Iterator end();
63 
64  [[nodiscard]] ConstIterator end() const;
65 
66  [[nodiscard]] Iterator rbegin();
67 
68  [[nodiscard]] ConstIterator rbegin() const;
69 
70  [[nodiscard]] Iterator rend();
71 
72  [[nodiscard]] ConstIterator rend() const;
73 
74  [[nodiscard]] Reference At(size_t i);
75 
76  [[nodiscard]] ConstReference At(size_t i) const;
77 
78  template <typename... Args>
79  [[nodiscard]] Reference At(size_t i, Args... args);
80 
81  template <typename... Args>
82  [[nodiscard]] ConstReference At(size_t i, Args... args) const;
83 
84  [[nodiscard]] Reference At(const Vector<size_t, N>& idx);
85 
86  [[nodiscard]] ConstReference At(const Vector<size_t, N>& idx) const;
87 
88  Reference operator[](size_t i);
89 
90  ConstReference operator[](size_t i) const;
91 
92  template <typename... Args>
93  Reference operator()(size_t i, Args... args);
94 
95  template <typename... Args>
96  ConstReference operator()(size_t i, Args... args) const;
97 
99 
100  ConstReference operator()(const Vector<size_t, N>& idx) const;
101 
102  protected:
103  ArrayBase();
104 
105  virtual ~ArrayBase() = default;
106 
107  ArrayBase(const ArrayBase& other);
108 
109  ArrayBase(ArrayBase&& other) noexcept;
110 
111  ArrayBase& operator=(const ArrayBase& other);
112 
113  ArrayBase& operator=(ArrayBase&& other) noexcept;
114 
115  template <typename... Args>
116  void SetPtrAndSize(Pointer ptr, size_t ni, Args... args);
117 
119 
120  void SwapPtrAndSize(ArrayBase& other);
121 
122  void ClearPtrAndSize();
123 
124  Pointer m_ptr = nullptr;
126 
127  private:
128  template <typename... Args>
129  [[nodiscard]] size_t IndexInternal(size_t d, size_t i, Args... args) const;
130 
131  [[nodiscard]] size_t IndexInternal(size_t, size_t i) const;
132 
133  template <size_t... I>
134  [[nodiscard]] size_t IndexInternal(const Vector<size_t, N>& idx,
135  std::index_sequence<I...>) const;
136 };
137 } // namespace CubbyFlow
138 
140 
141 #endif
T ValueType
Definition: ArrayBase.hpp:23
Reference operator[](size_t i)
Definition: ArrayBase-Impl.hpp:176
void SwapPtrAndSize(ArrayBase &other)
Definition: ArrayBase-Impl.hpp:269
ArrayBase & operator=(const ArrayBase &other)
Definition: ArrayBase-Impl.hpp:232
std::enable_if_t<(M > 2), size_t > Depth() const
void ClearPtrAndSize()
Definition: ArrayBase-Impl.hpp:263
virtual ~ArrayBase()=default
Definition: ArrayBase.hpp:19
Iterator end()
Definition: ArrayBase-Impl.hpp:102
Pointer m_ptr
Definition: ArrayBase.hpp:124
T & Reference
Definition: ArrayBase.hpp:24
Vector< size_t, N > m_size
Definition: ArrayBase.hpp:125
const T & ConstReference
Definition: ArrayBase.hpp:25
Definition: Matrix.hpp:27
const T * ConstPointer
Definition: ArrayBase.hpp:27
Definition: pybind11Utils.hpp:20
std::enable_if_t<(M > 1), size_t > Height() const
Iterator begin()
Definition: ArrayBase-Impl.hpp:90
size_t Length() const
Definition: ArrayBase-Impl.hpp:84
std::enable_if_t<(M > 0), size_t > Width() const
Generic N-dimensional array class interface.
Definition: Array.hpp:32
Reference operator()(size_t i, Args... args)
Iterator rbegin()
Definition: ArrayBase-Impl.hpp:114
Pointer data()
Definition: ArrayBase-Impl.hpp:39
ArrayBase()
Definition: ArrayBase-Impl.hpp:214
bool IsEmpty() const
Definition: ArrayBase-Impl.hpp:78
const T * ConstIterator
Definition: ArrayBase.hpp:29
const Vector< size_t, N > & Size() const
Definition: ArrayBase-Impl.hpp:51
size_t Index(size_t i) const
Definition: ArrayBase-Impl.hpp:17
Iterator rend()
Definition: ArrayBase-Impl.hpp:126
T * Pointer
Definition: ArrayBase.hpp:26
Reference At(size_t i)
Definition: ArrayBase-Impl.hpp:138
T * Iterator
Definition: ArrayBase.hpp:28
void SetPtrAndSize(Pointer ptr, size_t ni, Args... args)
Definition: ArrayBase-Impl.hpp:250