2019-05-30 04:40:02 +08:00
|
|
|
/*
|
|
|
|
Copyright (c) 2008-2019 Jan W. Krieger (<jan@jkrieger.de>)
|
|
|
|
|
|
|
|
last modification: $LastChangedDate$ (revision $Rev$)
|
|
|
|
|
|
|
|
This software is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License (LGPL) as published by
|
|
|
|
the Free Software Foundation, either version 2.1 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 Lesser General Public License (LGPL) for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License (LGPL)
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef JKQTPARRAYTOOLS_H_INCLUDED
|
|
|
|
#define JKQTPARRAYTOOLS_H_INCLUDED
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <cmath>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits>
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
#include <cfloat>
|
|
|
|
#include <ostream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <sstream>
|
2019-06-22 20:21:32 +08:00
|
|
|
#include "jkqtcommon/jkqtcommon_imexport.h"
|
2019-05-30 04:40:02 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef _OPENMP
|
|
|
|
# include <omp.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __LINUX__
|
|
|
|
# if defined(linux)
|
|
|
|
# define __LINUX__
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __LINUX__
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef JKQTP_ALIGNMENT_BYTES
|
|
|
|
#define JKQTP_ALIGNMENT_BYTES 32
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*! \brief swap two elements \a l and \a r in an array \a a
|
|
|
|
\ingroup jkqtptools_math_array
|
|
|
|
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
inline void jkqtpArraySwap(T* a, long long l, long long r){
|
|
|
|
const T tmp=a[l];
|
|
|
|
a[l]=a[r];
|
|
|
|
a[r]=tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*! \brief swap two elements \a l and \a r in an array \a a
|
|
|
|
\ingroup jkqtptools_math_array
|
|
|
|
|
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
inline void jkqtpArraySwapV(std::vector<T>& a, long long l, long long r){
|
|
|
|
const T tmp=a[l];
|
|
|
|
a[l]=a[r];
|
|
|
|
a[r]=tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*! \brief duplicate an array of data
|
|
|
|
\ingroup jkqtptools_math_array
|
|
|
|
|
2019-11-18 23:15:49 +08:00
|
|
|
\note use free() to free the memory!!!
|
2019-05-30 04:40:02 +08:00
|
|
|
*/
|
|
|
|
template <class T>
|
|
|
|
inline T* jkqtpArrayDuplicate(const T* dataIn, long long N) {
|
|
|
|
// std::cout<<"statisticsDuplicateArray("<<dataIn<<", "<<N<<")\n";
|
|
|
|
if (N<=0 || !dataIn) return nullptr;
|
2019-11-18 23:15:49 +08:00
|
|
|
T* out=static_cast<T*>(malloc(N*sizeof(T)));
|
2019-05-30 04:40:02 +08:00
|
|
|
if (out) memcpy(out, dataIn, N*sizeof(T));
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2019-11-18 23:15:49 +08:00
|
|
|
/*! \brief this class ensures that the given pointer is freed when the class is destroyed.
|
2019-05-30 04:40:02 +08:00
|
|
|
\ingroup jkqtptools_math_array
|
|
|
|
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class JKQTPArrayScopedPointer {
|
|
|
|
public:
|
|
|
|
inline explicit JKQTPArrayScopedPointer(T* pnt) {
|
|
|
|
pntr=pnt;
|
|
|
|
}
|
|
|
|
inline explicit JKQTPArrayScopedPointer() {
|
|
|
|
pntr=nullptr;
|
|
|
|
}
|
|
|
|
inline explicit JKQTPArrayScopedPointer(const JKQTPArrayScopedPointer& other) {
|
|
|
|
pntr=other.pntr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
~JKQTPArrayScopedPointer() {
|
2019-11-18 23:15:49 +08:00
|
|
|
if (pntr) free(pntr);
|
2019-05-30 04:40:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline JKQTPArrayScopedPointer<T>& operator=(T* p) {
|
|
|
|
pntr=p;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline JKQTPArrayScopedPointer<T>& operator=(const JKQTPArrayScopedPointer<T>& p) {
|
|
|
|
pntr=p.pntr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline T& operator*() const { return *pntr; }
|
|
|
|
inline T* operator->() const { return pntr; }
|
|
|
|
inline T* data() const { return pntr; }
|
|
|
|
inline T& operator[](long long i) {
|
|
|
|
return pntr[i];
|
|
|
|
}
|
|
|
|
inline const T& operator[](long long i) const {
|
|
|
|
return pntr[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
T* pntr;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // JKQTPARRAYTOOLS_H_INCLUDED
|
|
|
|
|
|
|
|
|