-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.h
112 lines (94 loc) · 2.75 KB
/
array.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
* Copyright (C) 2007 Simon Perreault
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef ARRAY_H
#define ARRAY_H
#include "shareddata.h"
#include "tinyvector.h"
template< typename T, int N >
class Array
{
public:
Array();
Array( const TinyVector<int,N>& sizes );
const TinyVector<int,N>& sizes() const;
const T& at( const TinyVector<int,N>& indices ) const;
const T& operator() ( const TinyVector<int,N>& indices ) const;
T& operator() ( const TinyVector<int,N>& indices );
Array<T,N> subarray( const TinyVector<int,N>& begin,
const TinyVector<int,N>& end );
private:
int dataIndex( const TinyVector<int,N>& indices ) const;
private:
SharedData<T> data_;
int offset_;
TinyVector<int,N> strides_;
TinyVector<int,N> sizes_;
};
template< typename T, int N >
Array<T,N>::Array()
: data_(0)
, sizes_(0)
{
}
template< typename T, int N >
Array<T,N>::Array( const TinyVector<int,N>& sizes )
: data_( new T[prod(sizes)] )
, offset_(0)
, strides_( cumprod(sizes)/sizes(0) )
, sizes_(sizes)
{
}
template< typename T, int N >
const TinyVector<int,N>& Array<T,N>::sizes() const
{
return sizes_;
}
template< typename T, int N >
const T& Array<T,N>::at( const TinyVector<int,N>& indices ) const
{
return data_[ dataIndex(indices) ];
}
template< typename T, int N >
const T& Array<T,N>::operator() ( const TinyVector<int,N>& indices ) const
{
return at(indices);
}
template< typename T, int N >
T& Array<T,N>::operator() ( const TinyVector<int,N>& indices )
{
return data_[ dataIndex(indices) ];
}
template< typename T, int N >
int Array<T,N>::dataIndex( const TinyVector<int,N>& indices ) const
{
for ( int i = 0; i < N; ++i ) {
assert( indices(i) >= 0 && indices(i) < sizes_(i) );
}
return offset_ + sum( indices * strides_ );
}
template< typename T, int N >
Array<T,N> Array<T,N>::subarray( const TinyVector<int,N>& begin,
const TinyVector<int,N>& end )
{
Array<T,N> sub;
sub.data_ = data_;
sub.offset_ = dataIndex(begin);
sub.strides_ = strides_;
sub.sizes_ = end - begin;
return sub;
}
#endif