2
* Copyright (C) 2002,2003 Daniel Heck
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License
6
* as published by the Free Software Foundation; either version 2
7
* of the License, or (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free Software Foundation, Inc.,
16
* 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18
* $Id: array2.hh,v 1.6 2004/04/24 11:46:01 dheck Exp $
24
** This file implements templated two-dimensional arrays.
31
template <class T, class A = std::allocator<T> >
34
T *first, *last; // start/end of allocated space
36
Array2Base(const A &a, typename A::size_type n)
37
: alloc(a), first(alloc.allocate(n)), last(first+n)
41
// allocate(0) returns 0 on GCC 2.95 -- standard?
43
alloc.deallocate(first, last-first);
45
void resize (typename A::size_type n)
48
alloc.deallocate(first, last-first);
49
first = alloc.allocate(n);
54
template <class T, class A=std::allocator<T> >
55
class Array2 : private Array2Base<T,A> {
60
typedef const T * const_iterator;
61
typedef T & reference;
62
typedef const T & const_reference;
63
typedef typename A::size_type size_type;
65
// Construction / Copying
66
explicit Array2(int ww=0, int hh=0, const T& val=T(), const A& a=A());
67
Array2(const Array2<T,A> &a);
68
Array2<T,A> &operator=(Array2<T,A> a2); // call by value!
71
~Array2() { destroy_elements(); }
73
iterator begin() { return this->first; }
74
iterator end() { return this->last; }
75
const_iterator begin() const { return this->first; }
76
const_iterator end() const { return this->last; }
77
iterator row_begin(size_type y) { return this->first + y*w; }
78
iterator row_end(size_type y) { return this->first + y*w + w; }
79
const_iterator row_begin(size_type y) const { return this->first + y*w; }
80
const_iterator row_end(size_type y) const { return this->first + y*w + w; }
83
void swap(Array2<T,A> &a2);
85
size_type width() const { return w; }
86
size_type height()const { return h; }
89
T& get(size_type x, size_type y) { return this->first[y*w+x]; }
90
const T& get(size_type x, size_type y) const { return this->first[y*w+x]; }
91
T& operator()(size_type x, size_type y) { return get(x,y); }
92
const T& operator()(size_type x, size_type y) const { return get(x,y); }
94
void set(size_type x, size_type y, const T& val) {
95
this->first[y*w+x] = val;
98
/*! Fill the array with some value or the default value. */
99
void fill (const T& val=T());
101
/*! Resize the array in place, but discard any old array
103
void resize (int w, int h, const T& val=T());
106
void destroy_elements();
111
template <class T, class A>
112
Array2<T,A>::Array2(int ww, int hh, const T& val, const A& a)
113
: Array2Base<T,A>(a, ww*hh), w(ww), h(hh)
115
std::uninitialized_fill(this->first, this->last, val);
118
template <class T, class A>
119
Array2<T,A>::Array2(const Array2<T,A> &a)
120
: Array2Base<T,A>(a.alloc, a.last-a.first)
122
std::uninitialized_copy(a.begin(), a.end(), this->first);
125
template <class T, class A>
126
void Array2<T,A>::destroy_elements() {
127
for (T* p=this->first; p!=this->last; ++p)
131
template <class T, class A>
132
void Array2<T,A>::fill (const T& val)
135
std::uninitialized_fill(this->first, this->last, val);
138
/*! Resize the array in place, but discard any old array
140
template <class T, class A>
141
void Array2<T,A>::resize (int w_, int h_, const T& val)
144
Array2Base<T,A>::resize(w_*h_);
145
std::uninitialized_fill(this->first, this->last, val);
150
template <class T, class A>
151
void Array2<T,A>::swap(Array2<T,A> &a2)
153
std::swap(this->first, a2.first);
154
std::swap(this->last, a2.last);
159
template <class T, class A>
160
void swap (Array2<T,A> &a, Array2<T,A> &b)
165
template <class T, class A> Array2<T,A>&
166
Array2<T,A>::operator= (Array2<T,A> a2)