Frobby 0.10.0
Loading...
Searching...
No Matches
LocalArray.h
1/* Frobby: Software for monomial ideal computations.
2 Copyright (C) 2011 University of Aarhus
3 Contact Bjarke Hammersholt Roune for license information (www.broune.com)
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see http://www.gnu.org/licenses/.
17*/
18#ifndef LOCAL_ARRAY_GUARD
19#define LOCAL_ARRAY_GUARD
20
21#include "Arena.h"
22#include <utility>
23
35template<class T, class ArenaSource = Arena>
36class LocalArray {
37 public:
38 LocalArray(const size_t sizeParam):
39 _size(sizeParam),
40 _range(ArenaSource::getArena().template allocArray<T>(sizeParam)) {
41 ASSERT(this->size() == static_cast<size_t>(end() - begin()));
42 }
43
44 ~LocalArray() {
45 ASSERT(size() == static_cast<size_t>(end() - begin()));
46 ArenaSource::getArena().freeTopArray(begin(), end());
47 }
48
49 T& operator[](const size_t i) const {
50 ASSERT(i < size());
51 return begin()[i];
52 }
53
54 T* begin() const {return _range.first;}
55 T* end() const {return _range.second;}
56 size_t size() const {return _size;}
57
58 private:
59 // Everything is inline and should be on the stack and be unlikely
60 // to have its address taken. So the compiler should be able to
61 // eliminate any of these member variables that are not used.
62 const size_t _size;
63 const pair<T*, T*> _range;
64};
65
66#endif