Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <mail@jlucke.com>2019-09-12 15:23:21 +0300
committerJacques Lucke <mail@jlucke.com>2019-09-12 15:23:21 +0300
commit369d5e8ad2bb7c249c6b941779066b6aa99f9ea0 (patch)
treeb0aece159472bfdc863832d8dc94666eba9a6327 /source/blender/blenlib/BLI_array_cxx.h
parent19547236355d59bc3888635ecbe4a9e35c1f86cb (diff)
BLI: new C++ ArrayRef, Vector, Stack, ... data structures
Many generic C++ data structures have been developed in the functions branch. This commit merges a first chunk of them into master. The following new data structures are included: Array: Owns a memory buffer with a fixed size. It is different from std::array in that the size is not part of the type. ArrayRef: References an array owned by someone else. All elements in the referenced array are considered to be const. This should be the preferred parameter type for functions that take arrays as input. MutableArrayRef: References an array owned by someone else. The elements in the referenced array can be changed. IndexRange: Specifies a continuous range of integers with a start and end index. IntrusiveListBaseWrapper: A utility class that allows iterating over ListBase instances where the prev and next pointer are stored in the objects directly. Stack: A stack implemented on top of a vector. Vector: An array that can grow dynamically. Allocators: Three allocator types are included that can be used by the container types to support different use cases. The Stack and Vector support small object optimization. So when the amount of elements in them is below a certain threshold, no memory allocation is performed. Additionally, most methods have unit tests. I'm merging this without normal code review, after I checked the code roughly with Sergey, and after we talked about it with Brecht.
Diffstat (limited to 'source/blender/blenlib/BLI_array_cxx.h')
-rw-r--r--source/blender/blenlib/BLI_array_cxx.h195
1 files changed, 195 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_array_cxx.h b/source/blender/blenlib/BLI_array_cxx.h
new file mode 100644
index 00000000000..f48ba05842e
--- /dev/null
+++ b/source/blender/blenlib/BLI_array_cxx.h
@@ -0,0 +1,195 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/** \file
+ * \ingroup bli
+ *
+ * This is a container that contains a fixed size array. Note however, the size of the array is not
+ * a template argument. Instead it can be specified at the construction time.
+ */
+
+#pragma once
+
+#include "BLI_utildefines.h"
+#include "BLI_allocator.h"
+#include "BLI_array_ref.h"
+#include "BLI_memory_utils_cxx.h"
+
+namespace BLI {
+
+template<typename T, typename Allocator = GuardedAllocator> class Array {
+ private:
+ T *m_data;
+ uint m_size;
+ Allocator m_allocator;
+
+ public:
+ Array()
+ {
+ m_data = nullptr;
+ m_size = 0;
+ }
+
+ Array(ArrayRef<T> values)
+ {
+ m_size = values.size();
+ m_data = this->allocate(m_size);
+ uninitialized_copy_n(values.begin(), m_size, m_data);
+ }
+
+ Array(const std::initializer_list<T> &values) : Array(ArrayRef<T>(values))
+ {
+ }
+
+ explicit Array(uint size)
+ {
+ m_data = this->allocate(size);
+ m_size = size;
+
+ for (uint i = 0; i < m_size; i++) {
+ new (m_data + i) T();
+ }
+ }
+
+ Array(uint size, const T &value)
+ {
+ m_data = this->allocate(size);
+ m_size = size;
+ uninitialized_fill_n(m_data, m_size, value);
+ }
+
+ Array(const Array &other)
+ {
+ m_size = other.size();
+ m_allocator = other.m_allocator;
+
+ if (m_size == 0) {
+ m_data = nullptr;
+ return;
+ }
+ else {
+ m_data = this->allocate(m_size);
+ copy_n(other.begin(), m_size, m_data);
+ }
+ }
+
+ Array(Array &&other) noexcept
+ {
+ m_data = other.m_data;
+ m_size = other.m_size;
+ m_allocator = other.m_allocator;
+
+ other.m_data = nullptr;
+ other.m_size = 0;
+ }
+
+ ~Array()
+ {
+ destruct_n(m_data, m_size);
+ if (m_data != nullptr) {
+ m_allocator.deallocate((void *)m_data);
+ }
+ }
+
+ Array &operator=(const Array &other)
+ {
+ if (this == &other) {
+ return *this;
+ }
+
+ this->~Array();
+ new (this) Array(other);
+ return *this;
+ }
+
+ Array &operator=(Array &&other)
+ {
+ if (this == &other) {
+ return *this;
+ }
+
+ this->~Array();
+ new (this) Array(std::move(other));
+ return *this;
+ }
+
+ operator ArrayRef<T>() const
+ {
+ return ArrayRef<T>(m_data, m_size);
+ }
+
+ operator MutableArrayRef<T>()
+ {
+ return MutableArrayRef<T>(m_data, m_size);
+ }
+
+ ArrayRef<T> as_ref() const
+ {
+ return *this;
+ }
+
+ T &operator[](uint index)
+ {
+ BLI_assert(index < m_size);
+ return m_data[index];
+ }
+
+ uint size() const
+ {
+ return m_size;
+ }
+
+ void fill(const T &value)
+ {
+ MutableArrayRef<T>(*this).fill(value);
+ }
+
+ void fill_indices(ArrayRef<uint> indices, const T &value)
+ {
+ MutableArrayRef<T>(*this).fill_indices(indices, value);
+ }
+
+ const T *begin() const
+ {
+ return m_data;
+ }
+
+ const T *end() const
+ {
+ return m_data + m_size;
+ }
+
+ T *begin()
+ {
+ return m_data;
+ }
+
+ T *end()
+ {
+ return m_data + m_size;
+ }
+
+ private:
+ T *allocate(uint size)
+ {
+ return (T *)m_allocator.allocate_aligned(
+ size * sizeof(T), std::alignment_of<T>::value, __func__);
+ }
+};
+
+template<typename T> using TemporaryArray = Array<T, TemporaryAllocator>;
+
+} // namespace BLI