1 /* ***** BEGIN LICENSE BLOCK *****
7 * Copyright (c) 2008 BBC Research
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * ***** END LICENSE BLOCK ***** */
36 * Some housekeeping around a 1d array of elements, but no
37 * accessors to this, you need to subclass Allocatable to
38 * give it some structure.
40 template<class T
> class Allocatable
{
43 Allocatable(int len
) :
44 _data(new T
[len
]), _len(len
), _allocated(true)
48 Allocatable(int len
, T
* data
) :
49 _data(data
), _len(len
), _allocated(false)
59 /* assign a single value to every element */
60 Allocatable
<T
>& assign(const T
& v
)
62 for (int i
= 0; i
< _len
; i
++)
68 /* copy elements in one array to another */
69 Allocatable
<T
>& operator=(const Allocatable
& v
)
74 for (int i
= 0; i
< _len
; i
++)
75 _data
[i
] = v
._data
[i
];
83 /* disable inadvertant copying */
84 Allocatable
<T
>(const Allocatable
<T
>&);
87 const bool _allocated
;
91 * Non resizable, One dimensional array.
93 template<class T
> class Array1d
: public Allocatable
<T
> {
96 Allocatable
<T
>(len
), _len(len
)
101 Array1d(int len
, T
* data
) :
102 Allocatable
<T
>(len
, data
), _len(len
)
113 T
& operator[](const int i
)
119 const T
& operator[](const int i
) const
126 /* disable inadvertant copying */
127 Array1d
<T
>(const Array1d
<T
>&);
131 using Allocatable
<T
>::_data
;
135 * Non resizable, Two dimensional array.
137 template<class T
> class Array2d
: public Allocatable
<T
> {
139 Array2d(int width
, int height
) :
140 Allocatable
<T
>(width
* height
), _width(width
), _height(height
)
142 _rows
= (Array1d
<T
>*) malloc(sizeof(Array1d
<T
>) * _height
);
143 for (int i
= 0; i
< _height
; i
++)
145 ::new(static_cast<void*>(&_rows
[i
]))
146 Array1d
<T
>(_width
, _data
+ i
*width
);
150 /* clean up at destruction too -- this does not call destructors
151 * which it probably should. */
169 Array1d
<T
>& operator[](const int j
)
175 const Array1d
<T
>& operator[](const int j
) const
181 Array2d
<T
>& operator=(const Array2d
<T
>& v
)
183 if (v
.width() != _width
|| v
.height() != _height
)
186 this->Allocatable
<T
>::operator=(v
);
192 /* disable inadvertant copying */
193 Array2d
<T
>(const Array2d
<T
>&);
198 /* an array of rowpointers, _height long. This allows us to
199 * do more efficient row access. */
202 /* gain access to _data from allocatable */
203 using Allocatable
<T
>::_data
;
206 #endif /*ARRAY_DF_H_*/