12#ifndef CCGL_UTILS_ARRAY_H
13#define CCGL_UTILS_ARRAY_H
33namespace utils_array {
41template <
typename T,
typename INI_T>
51template <
typename T,
typename INI_T>
54template <
typename T,
typename INI_T>
55bool Initialize1DArray4ItpWeight(
int row, T*& data, INI_T* init_data,
int itp_weight_data_length);
72template <
typename T,
typename INI_T>
84template <
typename T,
typename INI_T>
95template <
typename T1,
typename T2>
200bool Read2DArrayFromCsvFile(
const char* filename,
const vector<string>* header, std::vector<std::vector<T>>& data);
247 T** create2DArray(vuint32_t nrows, vuint32_t ncols,
const T& val = T()) {
251 ptr =
new(nothrow) T*[nrows];
252 pool =
new(nothrow) T[nrows*ncols];
253 for (vuint32_t i = 0; i < nrows * ncols; i++) {
257 for (vuint32_t i = 0; i < nrows; ++i, pool += ncols) {
261 }
catch (std::bad_alloc& ex) {
268 typedef T value_type;
273 vuint32_t get_rows()
const {
return m_rows; }
275 vuint32_t get_cols()
const {
return m_cols; }
277 Array2D() : data_ptr(
nullptr), m_rows(0), m_cols(0) {}
278 Array2D(vuint32_t rows, vuint32_t cols,
const T& val = T()) {
280 throw std::invalid_argument(
"number of rows is 0");
282 throw std::invalid_argument(
"number of columns is 0");
283 data_ptr = create2DArray(rows, cols, val);
290 delete[] data_ptr[0];
295 Array2D(
const Array2D& rhs) : m_rows(rhs.m_rows), m_cols(rhs.m_cols) {
296 data_ptr = create2DArray(m_rows, m_cols);
297 std::copy(&rhs.data_ptr[0][0], &rhs.data_ptr[m_rows - 1][m_cols], &data_ptr[0][0]);
301 data_ptr = rhs.data_ptr;
304 rhs.data_ptr =
nullptr;
315 std::swap(left.data_ptr, right.data_ptr);
316 std::swap(left.m_cols, right.m_cols);
317 std::swap(left.m_rows, right.m_rows);
328 T* operator[](vuint32_t row) {
329 return data_ptr[row];
332 const T* operator[](vuint32_t row)
const {
333 return data_ptr[row];
336 void create(vuint32_t rows, vuint32_t cols,
const T& val = T()) {
337 *
this =
Array2D(rows, cols, val);
343template <
typename T,
typename INI_T>
345 if (
nullptr != data) {
351 cout <<
"The data length MUST be greater than 0!" << endl;
355 data =
new(nothrow)T[row];
356 if (
nullptr == data) {
358 cout <<
"Bad memory allocated during 1D array initialization!" << endl;
362 T init =
static_cast<T
>(init_value);
363#pragma omp parallel for
364 for (
int i = 0; i < row; i++) {
370template <
typename T,
typename INI_T>
372 if (
nullptr != data) {
376 data =
new(nothrow) T[row];
377 if (
nullptr == data) {
379 cout <<
"Bad memory allocated during 1D array initialization!" << endl;
382 if (
nullptr == init_data) {
383 cout <<
"The input parameter init_data MUST NOT be nullptr!" << endl;
386#pragma omp parallel for
387 for (
int i = 0; i < row; i++) {
388 data[i] =
static_cast<T
>(init_data[i]);
393template <
typename T,
typename INI_T>
395 if (row <= 0 || col <= 0) {
396 cout <<
"The row and col should not be less or equal to ZERO!" << endl;
399 if (
nullptr != data) {
403 data =
new(nothrow) T*[row];
404 if (
nullptr == data) {
406 cout <<
"Bad memory allocated during initialize rows of the 2D array!" << endl;
410 pool =
new(nothrow) T[row * col];
411 if (
nullptr == pool) {
413 cout <<
"Bad memory allocated during initialize data pool of the 2D array!" << endl;
417 T init =
static_cast<T
>(init_value);
418#pragma omp parallel for
419 for (
int i = 0; i < row * col; i++) {
423 for (
int i = 0; i < row; ++i, pool += col) {
429template <
typename T,
typename INI_T>
431 INI_T**
const init_data) {
433 if (!flag) {
return false; }
434#pragma omp parallel for
435 for (
int i = 0; i < row; i++) {
436 for (
int j = 0; j < col; j++) {
437 data[i][j] =
static_cast<T
>(init_data[i][j]);
443template <
typename T1,
typename T2>
446 rows =
CVT_INT(init_data[idx++]);
447 data =
new(nothrow) T2* [rows];
448 if (
nullptr == data) {
450 cout <<
"Bad memory allocated during initialize rows of the 2D array!" << endl;
455 int* cols =
new int[rows];
457 for (
int i = 0; i < rows; i++) {
458 cols[i] =
CVT_INT(init_data[idx]);
460 if (cols[i] > max_cols) { max_cols = cols[i]; }
462 int length = idx - 1;
467 for (
int i = 0; i < rows; ++i) {
468 data[i] = pool + pos;
477 if (
nullptr != data) {
485 if (
nullptr == data) {
496 va_start(arg_ptr, data);
498 T* arg_value = va_arg(arg_ptr, T*);
499 while (
nullptr != arg_value) {
501 arg_value = va_arg(arg_ptr, T*);
509 va_start(arg_ptr, data);
511 T** arg_value = va_arg(arg_ptr, T**);
512 while (
nullptr != arg_value) {
514 arg_value = va_arg(arg_ptr, T**);
524 if (find(vec.begin(), vec.end(), val) == vec.end()) {
532 for (
auto iter = vec.begin(); iter != vec.end();) {
534 iter = vec.erase(iter);
543 std::ifstream ifs(filename);
545 if (!ifs.is_open()) {
546 std::cerr <<
"Error: Open file " << filename <<
" failed!" << std::endl;
550 if (header !=
nullptr) {
552 std::getline(ifs, line);
553 std::stringstream ss(line);
555 while (getline(ss, value,
',')) {
557 col_idx.push_back(i);
562 while (std::getline(ifs, line)) {
565 std::stringstream ss(line);
567 while (getline(ss, value,
',')) {
569 row.push_back(
static_cast<T
>(std::stod(value)));
#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:242
void Release1DArray(T *&data)
Release DT_Array1D data.
Definition: utils_array.h:476
bool Initialize1DArray(int row, T *&data, INI_T init_value)
Initialize DT_Array1D data.
Definition: utils_array.h:344
bool Initialize2DArray(int row, int col, T **&data, INI_T init_value)
Initialize DT_Array2D data.
Definition: utils_array.h:394
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:531
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:494
bool ValueInVector(T val, const vector< T > &vec)
If value in vector container.
Definition: utils_array.h:520
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
bool Read2DArrayFromCsvFile(const char *filename, const vector< string > *header, std::vector< std::vector< T > > &data)
Read csv.
Definition: utils_array.h:542
void BatchRelease2DArray(int nrows, T **&data,...)
Batch release of 2D array,.
Definition: utils_array.h:507
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:484
Common Cross-platform Geographic Library (CCGL)
Definition: basic.cpp:17