/builddir/build/BUILD/raul-0.4.0/raul/Array.hpp

00001 /* This file is part of Raul.
00002  * Copyright (C) 2007 Dave 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_ARRAY_HPP
00019 #define RAUL_ARRAY_HPP
00020 
00021 #include <cassert>
00022 #include <cstdlib>
00023 #include "types.hpp"
00024 #include "Deletable.hpp"
00025 
00026 namespace Raul {
00027 
00028 
00035 template <class T>
00036 class Array : public Deletable
00037 {
00038 public:
00039     Array(size_t size = 0) : _size(size), _top(0), _elems(NULL) {
00040         if (size > 0)
00041             _elems = new T[size];
00042     }
00043     
00044     Array(size_t size, T initial_value) : _size(size), _top(0), _elems(NULL) {
00045         if (size > 0) {
00046             _elems = new T[size];
00047             for (size_t i=0; i < size; ++i)
00048                 _elems[i] = initial_value;
00049         }
00050     }
00051     
00052     Array(size_t size, const Array<T>& contents) : _size(size), _top(size+1) {
00053         _elems = new T[size];
00054         if (size <= contents.size())
00055             memcpy(_elems, contents._elems, size * sizeof(T));
00056         else
00057             memcpy(_elems, contents._elems, contents.size() * sizeof(T));
00058     }
00059 
00060     ~Array() {
00061         delete[] _elems;
00062     }
00063 
00064     void alloc(size_t num_elems) {
00065         assert(num_elems > 0);
00066         
00067         delete[] _elems;
00068         _size = num_elems;
00069         _top = 0;
00070         
00071         _elems = new T[num_elems];
00072     }
00073     
00074     void alloc(size_t num_elems, T initial_value) {
00075         assert(num_elems > 0);
00076         
00077         delete[] _elems;
00078         _size = num_elems;
00079         _top = 0;
00080 
00081         _elems = new T[num_elems];
00082         for (size_t i=0; i < _size; ++i)
00083             _elems[i] = initial_value;
00084     }
00085     
00086     void push_back(T n) {
00087         assert(_top < _size);
00088         _elems[_top++] = n;
00089     }
00090     
00091     inline size_t size() const  { return _size; }
00092 
00093     inline T& operator[](size_t i) const { assert(i < _size); return _elems[i]; }
00094     
00095     inline T& at(size_t i) const { assert(i < _size); return _elems[i]; }
00096 
00097 private:
00098     size_t _size;
00099     size_t _top; // points to empty element above "top" element
00100     T*     _elems;
00101 };
00102 
00103 
00104 } // namespace Raul
00105 
00106 #endif // RAUL_ARRAY_HPP

Generated on Wed Apr 9 08:14:41 2008 for RAUL by  doxygen 1.5.1