12#ifndef CCGL_UTILS_ARRAY_H
13#define CCGL_UTILS_ARRAY_H
32namespace utils_array {
40template <
typename T,
typename INI_T>
50template <
typename T,
typename INI_T>
53template <
typename T,
typename INI_T>
54bool Initialize1DArray4ItpWeight(
int row, T*& data, INI_T* init_data,
int itp_weight_data_length);
71template <
typename T,
typename INI_T>
83template <
typename T,
typename INI_T>
94template <
typename T1,
typename T2>
234 T** create2DArray(vuint32_t nrows, vuint32_t ncols,
const T& val = T()) {
238 ptr =
new(nothrow) T*[nrows];
239 pool =
new(nothrow) T[nrows*ncols];
240 for (vuint32_t i = 0; i < nrows * ncols; i++) {
244 for (vuint32_t i = 0; i < nrows; ++i, pool += ncols) {
248 }
catch (std::bad_alloc& ex) {
255 typedef T value_type;
260 vuint32_t get_rows()
const {
return m_rows; }
262 vuint32_t get_cols()
const {
return m_cols; }
264 Array2D() : data_ptr(
nullptr), m_rows(0), m_cols(0) {}
265 Array2D(vuint32_t rows, vuint32_t cols,
const T& val = T()) {
267 throw std::invalid_argument(
"number of rows is 0");
269 throw std::invalid_argument(
"number of columns is 0");
270 data_ptr = create2DArray(rows, cols, val);
277 delete[] data_ptr[0];
282 Array2D(
const Array2D& rhs) : m_rows(rhs.m_rows), m_cols(rhs.m_cols) {
283 data_ptr = create2DArray(m_rows, m_cols);
284 std::copy(&rhs.data_ptr[0][0], &rhs.data_ptr[m_rows - 1][m_cols], &data_ptr[0][0]);
288 data_ptr = rhs.data_ptr;
291 rhs.data_ptr =
nullptr;
302 std::swap(left.data_ptr, right.data_ptr);
303 std::swap(left.m_cols, right.m_cols);
304 std::swap(left.m_rows, right.m_rows);
315 T* operator[](vuint32_t row) {
316 return data_ptr[row];
319 const T* operator[](vuint32_t row)
const {
320 return data_ptr[row];
323 void create(vuint32_t rows, vuint32_t cols,
const T& val = T()) {
324 *
this =
Array2D(rows, cols, val);
330template <
typename T,
typename INI_T>
332 if (
nullptr != data) {
338 cout <<
"The data length MUST be greater than 0!" << endl;
342 data =
new(nothrow)T[row];
343 if (
nullptr == data) {
345 cout <<
"Bad memory allocated during 1D array initialization!" << endl;
349 T init =
static_cast<T
>(init_value);
350#pragma omp parallel for
351 for (
int i = 0; i < row; i++) {
357template <
typename T,
typename INI_T>
359 if (
nullptr != data) {
360 cout <<
"The input 1D array pointer is not nullptr. No initialization performed!" << endl;
363 data =
new(nothrow) T[row];
364 if (
nullptr == data) {
366 cout <<
"Bad memory allocated during 1D array initialization!" << endl;
369 if (
nullptr == init_data) {
370 cout <<
"The input parameter init_data MUST NOT be nullptr!" << endl;
373#pragma omp parallel for
374 for (
int i = 0; i < row; i++) {
375 data[i] =
static_cast<T
>(init_data[i]);
380template <
typename T,
typename INI_T>
382 const INI_T init_value) {
383 if (
nullptr != data) {
384 cout <<
"The input 2D array pointer is not nullptr. No initialization performed!" << endl;
387 data =
new(nothrow) T*[row];
388 if (
nullptr == data) {
390 cout <<
"Bad memory allocated during initialize rows of the 2D array!" << endl;
394 pool =
new(nothrow) T[row * col];
395 if (
nullptr == pool) {
397 cout <<
"Bad memory allocated during initialize data pool of the 2D array!" << endl;
401 T init =
static_cast<T
>(init_value);
402#pragma omp parallel for
403 for (
int i = 0; i < row * col; i++) {
407 for (
int i = 0; i < row; ++i, pool += col) {
413template <
typename T,
typename INI_T>
415 INI_T**
const init_data) {
417 if (!flag) {
return false; }
418#pragma omp parallel for
419 for (
int i = 0; i < row; i++) {
420 for (
int j = 0; j < col; j++) {
421 data[i][j] =
static_cast<T
>(init_data[i][j]);
427template <
typename T1,
typename T2>
430 rows =
CVT_INT(init_data[idx++]);
431 data =
new(nothrow) T2* [rows];
432 if (
nullptr == data) {
434 cout <<
"Bad memory allocated during initialize rows of the 2D array!" << endl;
439 int* cols =
new int[rows];
441 for (
int i = 0; i < rows; i++) {
442 cols[i] =
CVT_INT(init_data[idx]);
444 if (cols[i] > max_cols) { max_cols = cols[i]; }
446 int length = idx - 1;
451 for (
int i = 0; i < rows; ++i) {
452 data[i] = pool + pos;
461 if (
nullptr != data) {
469 if (
nullptr == data) {
480 va_start(arg_ptr, data);
482 T* arg_value = va_arg(arg_ptr, T*);
483 while (
nullptr != arg_value) {
485 arg_value = va_arg(arg_ptr, T*);
493 va_start(arg_ptr, data);
495 T** arg_value = va_arg(arg_ptr, T**);
496 while (
nullptr != arg_value) {
498 arg_value = va_arg(arg_ptr, T**);
508 if (find(vec.begin(), vec.end(), val) == vec.end()) {
516 for (
auto iter = vec.begin(); iter != vec.end();) {
518 iter = vec.erase(iter);
#define NOEXCEPT
A compatible reference to noexcept or throw() if not supported by the compiler.
Definition: basic.h:153
#define CVT_INT(param)
A reference to the postfix of executable file for RELWITHDEBINFO mode.
Definition: basic.h:325
Rudimentary RAII class of 2D Array which occupy successive memory.
Definition: utils_array.h:229
void Release1DArray(T *&data)
Release DT_Array1D data.
Definition: utils_array.h:460
bool Initialize1DArray(int row, T *&data, INI_T init_value)
Initialize DT_Array1D data.
Definition: utils_array.h:331
bool Initialize2DArray(int row, int col, T **&data, INI_T init_value)
Initialize DT_Array2D data.
Definition: utils_array.h:381
void Read2DArrayFromTxtFile(const char *filename, int &rows, T **&data)
Read 2D array from file The input file should follow the format: a 2D array sized rows * rows.
Definition: utils_array.cpp:40
void RemoveValueInVector(T val, vector< T > &vec)
Remove value in vector container.
Definition: utils_array.h:515
void Output2DArrayToTxtFile(const int rows, const int cols, const float **data, const char *filename)
Write 2D array to a file.
Definition: utils_array.cpp:16
void BatchRelease1DArray(T *&data,...)
Batch release of 1D array Variable arguments with the end of nullptr.
Definition: utils_array.h:478
bool ValueInVector(T val, const vector< T > &vec)
If value in vector container.
Definition: utils_array.h:504
void Read2DArrayFromString(const char *s, int &rows, T **&data)
Read 2D array from string The input string should follow the format: float value, total number is row...
Definition: utils_array.cpp:58
void Output1DArrayToTxtFile(const int n, const float *data, const char *filename)
Write 1D array to a file.
Definition: utils_array.cpp:8
void BatchRelease2DArray(int nrows, T **&data,...)
Batch release of 2D array,.
Definition: utils_array.h:491
void Read1DArrayFromTxtFile(const char *filename, int &rows, T *&data)
Read 1D array from file The input file should follow the format: a 1D array sized rows * 1.
Definition: utils_array.cpp:28
void Release2DArray(T **&data)
Release DT_Array2D data.
Definition: utils_array.h:468
Common Cross-platform Geographic Library (CCGL)
Definition: basic.cpp:17