diff options
author | Jacques Lucke <mail@jlucke.com> | 2019-09-12 15:23:21 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2019-09-12 15:23:21 +0300 |
commit | 369d5e8ad2bb7c249c6b941779066b6aa99f9ea0 (patch) | |
tree | b0aece159472bfdc863832d8dc94666eba9a6327 /source/blender/blenlib/BLI_stack_cxx.h | |
parent | 19547236355d59bc3888635ecbe4a9e35c1f86cb (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_stack_cxx.h')
-rw-r--r-- | source/blender/blenlib/BLI_stack_cxx.h | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_stack_cxx.h b/source/blender/blenlib/BLI_stack_cxx.h new file mode 100644 index 00000000000..095f98608b7 --- /dev/null +++ b/source/blender/blenlib/BLI_stack_cxx.h @@ -0,0 +1,142 @@ +/* + * 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 + * + * Basic stack implementation with support for small object optimization. + */ + +#pragma once + +#include "BLI_vector.h" + +namespace BLI { + +template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Stack { + private: + Vector<T, N, Allocator> m_elements; + + public: + Stack() = default; + + /** + * Construct a stack from an array ref. The elements will be pushed in the same order they are in + * the array. + */ + Stack(ArrayRef<T> values) : m_elements(values) + { + } + + operator ArrayRef<T>() + { + return m_elements; + } + + /** + * Return the number of elements in the stack. + */ + uint size() const + { + return m_elements.size(); + } + + /** + * Return true when the stack is empty, otherwise false. + */ + bool empty() const + { + return this->size() == 0; + } + + /** + * Add a new element to the top of the stack. + */ + void push(const T &value) + { + m_elements.append(value); + } + + void push(T &&value) + { + m_elements.append(std::forward<T>(value)); + } + + /** + * Remove the element from the top of the stack and return it. + * This will assert when the stack is empty. + */ + T pop() + { + return m_elements.pop_last(); + } + + /** + * Return a reference to the value a the top of the stack. + * This will assert when the stack is empty. + */ + T &peek() + { + BLI_assert(!this->empty()); + return m_elements[this->size() - 1]; + } + + T *begin() + { + return m_elements.begin(); + } + + T *end() + { + return m_elements.end(); + } + + const T *begin() const + { + return m_elements.begin(); + } + + const T *end() const + { + return m_elements.end(); + } + + /** + * Remove all elements from the stack but keep the memory. + */ + void clear() + { + m_elements.clear(); + } + + /** + * Remove all elements and free any allocated memory. + */ + void clear_and_make_small() + { + m_elements.clear_and_make_small(); + } + + /** + * Does a linear search to check if the value is in the stack. + */ + bool contains(const T &value) + { + return m_elements.contains(value); + } +}; + +} /* namespace BLI */ |