• Main Page
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

ArrayStack.hpp

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 <algorithm>
00022 #include <cassert>
00023 #include <cstddef>
00024 
00025 #include "raul/Array.hpp"
00026 #include "raul/Deletable.hpp"
00027 
00028 namespace Raul {
00029 
00030 
00034 template <class T>
00035 class ArrayStack : public Array<T>
00036 {
00037 public:
00038     explicit ArrayStack(size_t size = 0) : Array<T>(size), _top(0) {}
00039 
00040     ArrayStack(size_t size, T initial_value) : Array<T>(size, initial_value), _top(0) {}
00041 
00042     ArrayStack(size_t size, const Array<T>& contents) : Array<T>(size, contents), _top(size + 1) {}
00043 
00044     ~Array() {
00045         delete[] _elems;
00046     }
00047 
00048     void alloc(size_t num_elems) {
00049         Array<T>::alloc(num_elems);
00050         _top = 0;
00051     }
00052 
00053     void alloc(size_t num_elems, T initial_value) {
00054         Array<T>::alloc(num_elems, initial_value);
00055         _top = 0;
00056     }
00057 
00058     void push_back(T n) {
00059         assert(_top < _size);
00060         _elems[_top++] = n;
00061     }
00062 
00063     inline size_t size() const  { return _size; }
00064 
00065     inline T& operator[](size_t i) const { assert(i < _size); return _elems[i]; }
00066 
00067     inline T& at(size_t i) const { assert(i < _size); return _elems[i]; }
00068 
00069 private:
00070     size_t _top; // Index of empty element following the top element
00071 };
00072 
00073 
00074 } // namespace Raul
00075 
00076 #endif // RAUL_ARRAY_HPP

Generated on Tue Jan 11 2011 18:26:17 for RAUL by  doxygen 1.7.1