18 #ifndef __safe_array__
19 #define __safe_array__
26 #include "nec_exception.h"
28 #ifdef NEC_ERROR_CHECK
33 BoundsViol(
const char* message, int64_t index, int64_t bound)
36 m_message <<
"array index: " << index <<
" exceeds " << bound << std::endl;
51 : len_(0), rows_(0), cols_(0), resize_chunk_(2), data_(NULL), data_size_(0), own_data_(
true)
55 : len_(0), rows_(0), cols_(0), resize_chunk_(2), data_(NULL), data_size_(0), own_data_(
true)
61 : len_(0), rows_(0), cols_(0), resize_chunk_(2), data_(NULL), data_size_(0), own_data_(
true)
74 int64_t size()
const {
78 int32_t rows()
const {
82 int32_t cols()
const {
86 int64_t capacity()
const {
90 void resize(int32_t n_rows, int32_t n_cols) {
94 resize(rows_ * cols_);
100 if (in_array.rows_ == 0)
101 resize(in_array.len_);
103 resize(in_array.rows_,in_array.cols_);
105 for (int64_t i=0; i<len_; i++)
106 data_[i] = in_array[i];
110 void resize(int64_t new_length) {
111 #ifdef NEC_ERROR_CHECK
113 throw new nec_exception(
"attempt to resize data we do not own");
115 if (new_length > data_size_) {
118 data_size_ = new_length + resize_chunk_;
120 T* new_data_ =
new T[data_size_];
122 std::memcpy(new_data_, data_, len_ *
sizeof(T));
126 }
catch (std::bad_alloc& ba) {
138 T ret = data_[check(0)];
140 for (int64_t i = 1; i < len_; i++ ) {
141 if ( data_[check(i)] > ret)
142 ret = data_[check(i)];
149 if (int64_t(0) == len_)
152 T ret = data_[check(0)];
154 for (int64_t i = 1; i < len_; i++ ) {
155 if ( data_[check(i)] < ret)
156 ret = data_[check(i)];
162 T
sum(int64_t start_index, int64_t stop_index) {
163 T ret = data_[check(start_index)];
165 for (int64_t i = start_index+1; i < stop_index; i++ ) {
166 ret += data_[check(i)];
177 void fill(int64_t start, int64_t N,
const T& x) {
178 int64_t stop = start + N;
179 for (int64_t i = start; i < stop; i++ ) {
185 void setConstant(
const T& x) {
191 void set_col_major(int32_t col_dim, int32_t col, int32_t row,
const T& val) {
192 data_[check(row*col_dim + col)] = val;
198 return data_[check(row*col_dim + col)];
206 return data_[check(row,col)];
217 const T&
operator()(int32_t row, int32_t col)
const {
218 return data_[check(row,col)];
225 return data_[check(i)];
228 const T& operator[](int64_t i)
const {
229 return data_[check(i)];
232 T& operator[](int64_t i) {
241 if (int64_t(-1) == end_index)
251 int64_t end_index = start_index + n;
270 int64_t resize_chunk_;
284 resize_chunk_ = in_array.resize_chunk_;
285 len_ = (end_index - start_index)+1;
293 for (int64_t i=0; i<len_; i++)
294 data_[check(i)] = in_array[start_index + i];
298 data_ = in_array.data() + start_index;
304 inline int64_t check(int64_t i)
const {
305 #ifdef NEC_ERROR_CHECK
306 if (i < 0 || i >= len_)
307 throw new BoundsViol(
"safe_array: ", i, len_);
312 inline int64_t check(int32_t row, int32_t col)
const {
313 #ifdef NEC_ERROR_CHECK
314 if (row < 0 || row >= rows_)
315 throw new BoundsViol(
"safe_array: ", row, rows_);
316 if (col < 0 || col >= cols_)
317 throw new BoundsViol(
"safe_array: ", col, cols_);
319 return check(int64_t(col)*rows_ + row);
328 this->resize(_rows, _cols);
T & operator()(int32_t row, int32_t col)
Definition: safe_array.h:213
T sum(int64_t start_index, int64_t stop_index)
return the sum of the specified elements in the array
Definition: safe_array.h:162
T minCoeff() const
return the largest element of the array
Definition: safe_array.h:148
Definition: safe_array.h:325
Definition: nec_exception.h:28
safe_array(const safe_array< T > &in_array, int64_t start_index, int64_t end_index, bool in_copy_data)
Constructor only used to construct segment.
Definition: safe_array.h:282
T & get_col_major(int32_t col_dim, int32_t col, int32_t row)
Get an element assuming that the data is stored in column major form.
Definition: safe_array.h:197
safe_array< T > segment(int64_t start_index, int64_t end_index)
Return a representation of a subset of this array.
Definition: safe_array.h:240
T maxCoeff() const
return the largest element of the array
Definition: safe_array.h:134
safe_array< T > eigen_segment(int64_t start_index, int64_t n)
Return a representation of a subset of this array.
Definition: safe_array.h:250
A Safe Array class for nec2++ that performs bounds checking.
Definition: safe_array.h:48
T & getItem(int32_t row, int32_t col)
Definition: safe_array.h:205
void set_col_major(int32_t col_dim, int32_t col, int32_t row, const T &val)
Set an element assuming that the data is stored in column major form.
Definition: safe_array.h:191
T sum()
return the sum of all elements in the array
Definition: safe_array.h:172