RAUL 0.8.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List

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