From 556f42229b596791b8eff00e8e0e668057845585 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 20 Feb 2010 21:50:07 +0000 Subject: Split stack stuff from Array. git-svn-id: http://svn.drobilla.net/lad/trunk/raul@2467 a436a847-0d15-0410-975c-d299462d15a1 --- raul/Array.hpp | 28 ++++++++++---------- raul/ArrayStack.hpp | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 raul/ArrayStack.hpp (limited to 'raul') diff --git a/raul/Array.hpp b/raul/Array.hpp index 1d15dcd..72d2c31 100644 --- a/raul/Array.hpp +++ b/raul/Array.hpp @@ -36,12 +36,12 @@ template class Array : public Deletable { public: - Array(size_t size = 0) : _size(size), _top(0), _elems(NULL) { + Array(size_t size = 0) : _size(size), _elems(NULL) { if (size > 0) _elems = new T[size]; } - Array(size_t size, T initial_value) : _size(size), _top(0), _elems(NULL) { + Array(size_t size, T initial_value) : _size(size), _elems(NULL) { if (size > 0) { _elems = new T[size]; for (size_t i = 0; i < size; ++i) @@ -49,43 +49,46 @@ public: } } - Array(size_t size, const Array& contents) : _size(size), _top(size + 1) { + Array(size_t size, const Array& contents) : _size(size) { + assert(contents.size() >= size); _elems = new T[size]; for (size_t i = 0; i < std::min(size, contents.size()); ++i) _elems[i] = contents[i]; } + Array(size_t size, const Array& contents, T initial_value=T()) : _size(size) { + _elems = new T[size]; + const size_t end = std::min(size, contents.size()); + for (size_t i = 0; i < end; ++i) + _elems[i] = contents[i]; + for (size_t i = end; i < size; ++i) + _elems[i] = initial_value; + } + ~Array() { delete[] _elems; } - void alloc(size_t num_elems) { + virtual void alloc(size_t num_elems) { assert(num_elems > 0); delete[] _elems; _size = num_elems; - _top = 0; _elems = new T[num_elems]; } - void alloc(size_t num_elems, T initial_value) { + virtual void alloc(size_t num_elems, T initial_value) { assert(num_elems > 0); delete[] _elems; _size = num_elems; - _top = 0; _elems = new T[num_elems]; for (size_t i = 0; i < _size; ++i) _elems[i] = initial_value; } - 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]; } @@ -94,7 +97,6 @@ public: private: size_t _size; - size_t _top; // points to empty element above "top" element T* _elems; }; 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 + * + * 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 +#include +#include +#include "Deletable.hpp" + +namespace Raul { + + +/** An array that can also be used as a stack (with a fixed maximum size). + */ +template +class ArrayStack : public Array +{ +public: + ArrayStack(size_t size = 0) : Array(size), _top(0) {} + + ArrayStack(size_t size, T initial_value) : Array(size, initial_value), _top(0) {} + + ArrayStack(size_t size, const Array& contents) : Array(size, contents), _top(size + 1) {} + + ~Array() { + delete[] _elems; + } + + void alloc(size_t num_elems) { + Array::alloc(num_elems); + _top = 0; + } + + void alloc(size_t num_elems, T initial_value) { + Array::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 -- cgit v1.2.1