Newer
Older

Christian Godenschwager
committed
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//======================================================================================================================
//
// This file is part of waLBerla. waLBerla 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.
//
// waLBerla 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 waLBerla (see COPYING.txt). If not, see <http://www.gnu.org/licenses/>.
//
//! \file Array.h
//! \ingroup core
//! \author Florian Schornbaum <florian.schornbaum@fau.de>
//
//======================================================================================================================
#pragma once
#include "DataTypes.h"
#include "core/debug/Debug.h"
#include <iostream>
#include <sstream>
#include <vector>
namespace walberla {
/// Fixed size, dynamically allocated array
template< typename T >
class Array {
public:
inline Array() : array_( NULL ), size_( uint_c(0) ) {}
inline Array( const uint_t n, const T& t = T() );
inline Array( const std::vector<T>& vector );
inline Array( const Array& array );
~Array() { if( array_ != NULL ) delete[] array_; }
uint_t size() const { return size_; }
bool empty() const { return size_ == 0; }
inline bool operator==( const Array& array ) const;
inline bool operator!=( const Array& array ) const { return !operator==( array );}
const T& operator[]( const uint_t index ) const { WALBERLA_ASSERT_LESS( index, size_ ); return array_[ index ]; }
T& operator[]( const uint_t index ) { WALBERLA_ASSERT_LESS( index, size_ ); return array_[ index ]; }
inline const T* begin() const { return array_; }
inline T* begin() { return array_; }
inline const T* end() const { return array_ + size_; }
inline T* end() { return array_ + size_; }
inline void swap( Array& array );
void toStream( std::ostream& os ) const;
inline std::string toString() const;
protected:
T* array_;
uint_t size_;
}; // class Array
template< typename T >
inline Array<T>::Array( const uint_t n, const T& t ) : array_( n == 0 ? NULL : new T[n] ), size_( n )
{
for( uint_t i = 0; i != n; ++i )
array_[i] = t;
}
template< typename T >
inline Array<T>::Array( const std::vector<T>& vector ) :
array_( vector.size() == 0 ? NULL : new T[ vector.size() ] ), size_( vector.size() )
{
for( uint_t i = 0; i != size_; ++i )
array_[i] = vector[i];
}
template< typename T >
inline Array<T>::Array( const Array& array ) :
array_( array.size_ == 0 ? NULL : new T[ array.size_ ] ), size_( array.size_ )
{
for( uint_t i = 0; i != size_; ++i )
array_[i] = array.array_[i];
}
template< typename T >
inline bool Array<T>::operator==( const Array& array ) const
{
if( size_ != array.size_ )
return false;
for( uint_t i = 0; i != size_; ++i )
if( array_[i] != array.array_[i] ) return false;
return true;
}
template< typename T >
inline void Array<T>::swap( Array& array )
{
T* ptr = array_;
array_ = array.array_;
array.array_ = ptr;
uint_t s = size_;
size_ = array.size_;
array.size_ = s;
}
template< typename T >
void Array<T>::toStream( std::ostream& os ) const {
os << "{ ";
for( auto it = begin(); it != end(); ++it ) {
auto next = it;
os << (*it) << ( ( ++next == end() ) ? " " : ", " );
}
os << "}";
}
template< typename T >
inline std::string Array<T>::toString() const {
std::ostringstream oss;
toStream( oss );
return oss.str();
}
//////////////////////
// Global Functions //
//////////////////////
template< typename T >
inline std::ostream& operator<<( std::ostream& os, const Array<T>& array ) {
array.toStream( os );
return os;
}
} // namespace walberla