RAUL 0.7.0
|
00001 /* This file is part of Raul. 00002 * Copyright (C) 2007-2009 David Robillard <http://drobilla.net> 00003 * 00004 * Raul is free software; you can redistribute it and/or modify it under the 00005 * terms of the GNU General Public License as published by the Free Software 00006 * Foundation; either version 2 of the License, or (at your option) any later 00007 * version. 00008 * 00009 * Raul is distributed in the hope that it will be useful, but WITHOUT ANY 00010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 00011 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. 00012 * 00013 * You should have received a copy of the GNU General Public License along 00014 * with this program; if not, write to the Free Software Foundation, Inc., 00015 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00016 */ 00017 00018 #ifndef RAUL_ARRAYSTACK_HPP 00019 #define RAUL_ARRAYSTACK_HPP 00020 00021 #include "raul/Array.hpp" 00022 #include <cassert> 00023 #include <cstddef> 00024 #include <algorithm> 00025 #include "Deletable.hpp" 00026 00027 namespace Raul { 00028 00029 00032 template <class T> 00033 class ArrayStack : public Array<T> 00034 { 00035 public: 00036 ArrayStack(size_t size = 0) : Array<T>(size), _top(0) {} 00037 00038 ArrayStack(size_t size, T initial_value) : Array<T>(size, initial_value), _top(0) {} 00039 00040 ArrayStack(size_t size, const Array<T>& contents) : Array<T>(size, contents), _top(size + 1) {} 00041 00042 ~Array() { 00043 delete[] _elems; 00044 } 00045 00046 void alloc(size_t num_elems) { 00047 Array<T>::alloc(num_elems); 00048 _top = 0; 00049 } 00050 00051 void alloc(size_t num_elems, T initial_value) { 00052 Array<T>::alloc(num_elems, initial_value); 00053 _top = 0; 00054 } 00055 00056 void push_back(T n) { 00057 assert(_top < _size); 00058 _elems[_top++] = n; 00059 } 00060 00061 inline size_t size() const { return _size; } 00062 00063 inline T& operator[](size_t i) const { assert(i < _size); return _elems[i]; } 00064 00065 inline T& at(size_t i) const { assert(i < _size); return _elems[i]; } 00066 00067 private: 00068 size_t _top; // Index of empty element following the top element 00069 }; 00070 00071 00072 } // namespace Raul 00073 00074 #endif // RAUL_ARRAY_HPP