summaryrefslogtreecommitdiffstats
path: root/raul/ArrayStack.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2010-02-20 21:50:07 +0000
committerDavid Robillard <d@drobilla.net>2010-02-20 21:50:07 +0000
commit556f42229b596791b8eff00e8e0e668057845585 (patch)
tree289c1b510661bf84314ada8b0d4fa6f45a4315ca /raul/ArrayStack.hpp
parent94f4f2d501448dcdc7381cb0e0b94d337f0cf6f4 (diff)
downloadraul-556f42229b596791b8eff00e8e0e668057845585.tar.gz
raul-556f42229b596791b8eff00e8e0e668057845585.tar.bz2
raul-556f42229b596791b8eff00e8e0e668057845585.zip
Split stack stuff from Array.
git-svn-id: http://svn.drobilla.net/lad/trunk/raul@2467 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'raul/ArrayStack.hpp')
-rw-r--r--raul/ArrayStack.hpp74
1 files changed, 74 insertions, 0 deletions
diff --git a/raul/ArrayStack.hpp b/raul/ArrayStack.hpp
new file mode 100644
index 0000000..ca59b96
--- /dev/null
+++ b/raul/ArrayStack.hpp
@@ -0,0 +1,74 @@
+/* This file is part of Raul.
+ * Copyright (C) 2007-2009 Dave Robillard <http://drobilla.net>
+ *
+ * Raul 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.
+ *
+ * Raul 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 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 St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef RAUL_ARRAYSTACK_HPP
+#define RAUL_ARRAYSTACK_HPP
+
+#include "raul/Array.hpp"
+#include <cassert>
+#include <cstddef>
+#include <algorithm>
+#include "Deletable.hpp"
+
+namespace Raul {
+
+
+/** An array that can also be used as a stack (with a fixed maximum size).
+ */
+template <class T>
+class ArrayStack : public Array<T>
+{
+public:
+ ArrayStack(size_t size = 0) : Array<T>(size), _top(0) {}
+
+ ArrayStack(size_t size, T initial_value) : Array<T>(size, initial_value), _top(0) {}
+
+ ArrayStack(size_t size, const Array<T>& contents) : Array<T>(size, contents), _top(size + 1) {}
+
+ ~Array() {
+ delete[] _elems;
+ }
+
+ void alloc(size_t num_elems) {
+ Array<T>::alloc(num_elems);
+ _top = 0;
+ }
+
+ void alloc(size_t num_elems, T initial_value) {
+ Array<T>::alloc(num_elems, initial_value);
+ _top = 0;
+ }
+
+ void push_back(T n) {
+ assert(_top < _size);
+ _elems[_top++] = n;
+ }
+
+ inline size_t size() const { return _size; }
+
+ inline T& operator[](size_t i) const { assert(i < _size); return _elems[i]; }
+
+ inline T& at(size_t i) const { assert(i < _size); return _elems[i]; }
+
+private:
+ size_t _top; // Index of empty element following the top element
+};
+
+
+} // namespace Raul
+
+#endif // RAUL_ARRAY_HPP