29 typedef typename std::vector<T>::iterator Iterator;
30 typedef typename std::vector<T>::const_iterator ConstIterator;
31 typedef typename std::vector<T>::reference Reference;
32 typedef typename std::vector<T>::const_reference ConstReference;
41 const T&
operator[](
int index)
const {
return _data[index]; }
47 Iterator
begin() {
return _data.begin(); }
48 Iterator
end() {
return _data.end(); }
50 ConstIterator
begin()
const {
return _data.begin(); }
51 ConstIterator
end()
const {
return _data.end(); }
53 Reference
front() {
return _data.front(); }
54 ConstReference
front()
const {
return _data.front(); }
56 Reference
back() {
return _data.back(); }
57 ConstReference
back()
const {
return _data.back(); }
59 const std::vector<T>&
getData()
const {
return _data; }
61 int size()
const {
return _data.size(); }
62 bool empty()
const {
return _data.empty(); }
63 size_t capacity()
const {
return _data.capacity(); }
65 void resize(
int n) { _data.resize(n); }
66 void resize(
int n, T value) { _data.resize(n, value); }
67 void resize_with(
int n,
const T& val) { _data.resize(n, val); }
69 void assign(
int n, T value) { _data.assign(n, value); }
74 if (
size() + nplanned > capacity()) {
75 reserveAdd(nplanned + ntoallocated);
80 _data.reserve(capacity() + ntoallocated);
83 void push_back(
const T& val) { _data.push_back(val); }
85 void push_front(
const T& val) { _data.insert(_data.begin(), val); }
88 _data.insert(_data.end(), arr->
getData().begin(), arr->
getData().end());
92 _data.insert(_data.end(), arr.
getData().begin(), arr.
getData().end());
95 void remove(
int i) { _data.erase(_data.begin() + i); }
99 if (
id == -1) _data.push_back(val);
104 T val = _data.back();
110 const auto it = std::find(_data.begin(), _data.end(), value);
111 return it != _data.end() ? std::distance(_data.begin(), it) : -1;
118 auto it = std::lower_bound(_data.begin(), _data.end(), value);
119 if (it == _data.end())
return -1;
121 if (it != _data.begin()) {
124 auto prevIt = std::prev(it);
125 it = (value - *prevIt) < (*it - value) ? prevIt : it;
127 return std::distance(_data.begin(), it);
131 if (_data.empty())
return -1;
132 return std::distance(_data.begin(),
133 std::min_element(_data.begin(), _data.end()));
137 if (_data.empty())
return -1;
138 return std::distance(_data.begin(),
139 std::max_element(_data.begin(), _data.end()));
149 if (a ==
nullptr)
return 0;
155 if (a.
empty())
return 0;
161 int isthereindex = -1;
164 while ((i < n) && (isthereindex == -1)) {
165 if (arr[i] == what) {
178 FILE* f = fopen(fileName.c_str(),
"wb");
180 fwrite(&n,
sizeof(
int), 1, f);
181 for (
int i = 0; i < n; i++) {
185 fwrite(&m,
sizeof(
int), 1, f);
188 fwrite(&m,
sizeof(
int), 1, f);
190 fwrite(&(*a)[0],
sizeof(T), m, f);
201 FILE* f = fopen(fileName.c_str(),
"wb");
203 fwrite(&n,
sizeof(
int), 1, f);
204 for (
int i = 0; i < n; i++) {
208 fwrite(&m,
sizeof(
int), 1, f);
211 fwrite(&m,
sizeof(
int), 1, f);
213 fwrite(&a[0],
sizeof(T), m, f);
222 const std::string& fileName) {
224 FILE* f = fopen(fileName.c_str(),
"rb");
226 CVLog::Error(
"[IO] loadArrayOfArraysFromFile: can't open file ",
231 size_t retval = fread(&n,
sizeof(
int), 1, f);
235 "[IO] loadArrayOfArraysFromFile: can't read outer array size");
240 for (
int i = 0; i < n; i++) {
242 retval = fread(&m,
sizeof(
int), 1, f);
246 "[IO] loadArrayOfArraysFromFile: can't read inner array "
252 retval = fread(&(*a)[0],
sizeof(T), m, f);
253 if (retval !=
static_cast<std::size_t
>(m)) {
256 "[IO] loadArrayOfArraysFromFile: can't read vector "
269 const std::string& fileName) {
271 FILE* f = fopen(fileName.c_str(),
"rb");
273 CVLog::Error(
"[IO] loadArrayOfArraysFromFile: can't open file ",
278 size_t retval = fread(&n,
sizeof(
int), 1, f);
282 "[IO] loadArrayOfArraysFromFile: can't read outer array size");
287 for (
int i = 0; i < n; i++) {
289 retval = fread(&m,
sizeof(
int), 1, f);
293 "[IO] loadArrayOfArraysFromFile: can't read inner array "
299 retval = fread(&a[0],
sizeof(T), m, f);
300 if (retval !=
static_cast<std::size_t
>(m)) {
303 "[IO] loadArrayOfArraysFromFile: can't read vector "
314 bool docompress =
true) {
321 bool docompress =
true) {
324 const std::string parent_dir =
332 "[IO] saveArrayToFile called with NULL static vector");
336 if (a->
size() == 0) {
338 "[IO] saveArrayToFile called with 0-sized static vector");
342 if ((docompress ==
false) || (a->
size() < 1000)) {
343 FILE* f = fopen(fileName.c_str(),
"wb");
346 " could not be opened, msg: ", strerror(errno));
353 auto items = fwrite(&n,
sizeof(
int), 1, f);
354 if (items < 1 && ferror(f) != 0) {
356 CVLog::Error(
"[IO] failed to write 1 int to ", fileName.c_str(),
357 ", msg: ", strerror(errno));
359 items = fwrite(&(*a)[0],
sizeof(T), n, f);
360 if (items <
static_cast<std::size_t
>(n) && ferror(f) != 0) {
362 CVLog::Error(
"[IO] failed to write n items to ", fileName.c_str(),
363 ", msg: ", strerror(errno));
382 uLong(
static_cast<double>(
sizeof(T) * a->
size()) * 1.02) + 12;
384 static_cast<Byte*
>(calloc(
static_cast<uInt
>(comprLen), 1));
386 compress(compr, &comprLen,
static_cast<const Bytef*
>(&(*a)[0]),
387 sizeof(T) * a->
size());
390 CVLog::Error(QString(
"compress error %1 : 2% -> %3, n %4")
391 .arg(err, (
sizeof(T) * a->
size()), comprLen,
394 FILE* f = fopen(fileName.c_str(),
"wb");
398 " could not be opened, msg: ", strerror(errno));
402 auto items = fwrite(&n,
sizeof(
int), 1, f);
403 if (items < 1 && ferror(f) != 0) {
407 fileName.c_str(),
", msg: ", strerror(errno));
409 items = fwrite(&(*a)[0],
sizeof(T), n, f);
410 if (items < 1 && ferror(f) != 0) {
413 CVLog::Error(QString(
"[IO] failed to write %1 items to %2, "
415 .arg(QString::number(n),
422 FILE* f = fopen(fileName.c_str(),
"wb");
426 " could not be opened, msg: ", strerror(errno));
429 auto items = fwrite(&n,
sizeof(
int), 1, f);
430 if (items < 1 && ferror(f) != 0) {
433 CVLog::Error(
"[IO] failed to write 1 int to ", fileName.c_str(),
434 ", msg: ", strerror(errno));
437 items = fwrite(&n,
sizeof(
int), 1, f);
438 if (items < 1 && ferror(f) != 0) {
441 CVLog::Error(
"[IO] failed to write 1 int to ", fileName.c_str(),
442 ", msg: ", strerror(errno));
444 items = fwrite(&comprLen,
sizeof(uLong), 1, f);
445 if (items < 1 && ferror(f) != 0) {
449 fileName.c_str(),
", msg: ", strerror(errno));
451 items = fwrite(compr,
sizeof(Byte), comprLen, f);
452 if (items < 1 && ferror(f) != 0) {
456 QString(
"[IO] failed to write %1 items to %2, msg: %3")
457 .arg(QString::number(comprLen),
458 fileName.c_str(), strerror(errno)));
469 bool printfWarning =
false) {
470 Q_UNUSED(printfWarning);
473 FILE* f = fopen(fileName.c_str(),
"rb");
475 CVLog::Error(
"loadArrayFromFile : can't open file ", fileName.c_str());
478 size_t retval = fread(&n,
sizeof(
int), 1, f);
482 "[IO] loadArrayFromFile: can't read array size (1) from ",
488 retval = fread(&n,
sizeof(
int), 1, f);
492 "[IO] loadArrayFromFile: can't read array size (2)");
498 retval = fread(&comprLen,
sizeof(uLong), 1, f);
503 "[IO] loadArrayFromFile: can't read ulong elem size");
506 static_cast<Byte*
>(calloc(
static_cast<uInt
>(comprLen), 1));
507 retval = fread(compr,
sizeof(Byte), comprLen, f);
508 if (retval != comprLen) {
511 CVLog::Error(
"[IO] loadArrayFromFile: can't read blob");
514 uLong uncomprLen =
sizeof(T) * n;
515 int err = uncompress((Bytef*)(&(*a)[0]), &uncomprLen, compr,
521 CVLog::Error(QString(
"uncompress error %1 : 2% -> %3, n %4")
522 .arg(QString::number(err),
523 QString::number(
sizeof(T) * n),
524 QString::number(uncomprLen),
525 QString::number(n)));
528 if (uncomprLen !=
sizeof(T) * n) {
532 "loadArrayFromFile: uncompression failed "
533 "uncomprLen!=sizeof(T)*n");
540 size_t retval = fread(&(*a)[0],
sizeof(T), n, f);
541 if (retval !=
static_cast<std::size_t
>(n)) {
544 CVLog::Error(
"[IO] loadArrayFromFile: can't read n elements");
556 const std::string& fileName,
557 bool printfWarning =
false) {
558 Q_UNUSED(printfWarning);
561 FILE* f = fopen(fileName.c_str(),
"rb");
563 throw std::runtime_error(
"loadArrayFromFile : can't open file " +
567 size_t retval = fread(&n,
sizeof(
int), 1, f);
570 "[IO] loadArrayFromFile: can't read array size (1) from ",
575 retval = fread(&n,
sizeof(
int), 1, f);
578 "[IO] loadArrayFromFile: can't read array size (2)");
582 retval = fread(&comprLen,
sizeof(uLong), 1, f);
585 "[IO] loadArrayFromFile: can't read ulong elem size");
587 static_cast<Byte*
>(calloc(
static_cast<uInt
>(comprLen), 1));
588 retval = fread(compr,
sizeof(Byte), comprLen, f);
589 if (retval != comprLen)
592 uLong uncomprLen =
sizeof(T) * n;
593 int err = uncompress(
594 static_cast<Bytef*
>(out.getDataWritable().data()),
595 &uncomprLen, compr, comprLen);
598 CVLog::Error(QString(
"uncompress error %1 : 2% -> %3, n %4")
599 .arg(QString::number(err),
600 QString::number(
sizeof(T) * n),
601 QString::number(uncomprLen),
602 QString::number(n)));
605 if (uncomprLen !=
sizeof(T) * n) {
607 throw std::runtime_error(
608 "loadArrayFromFile: uncompression failed "
609 "uncomprLen!=sizeof(T)*n");
616 fread(out.getDataWritable().data(),
sizeof(T), n, f);
617 if (retval !=
static_cast<std::size_t
>(n))
629 const std::string& fileName,
630 bool printfWarning =
false) {
631 Q_UNUSED(printfWarning);
634 FILE* f = fopen(fileName.c_str(),
"rb");
636 CVLog::Error(
"loadArrayFromFileIntoArray: can not open file: ",
640 fread(&n,
sizeof(
int), 1, f);
643 fread(&n,
sizeof(
int), 1, f);
644 if (a->
size() != n) {
646 CVLog::Error(
"loadArrayFromFileIntoArray: expected length "
647 << a->
size() <<
" loaded length " << n);
651 fread(&comprLen,
sizeof(uLong), 1, f);
653 static_cast<Byte*
>(calloc(
static_cast<uInt
>(comprLen), 1));
654 fread(compr,
sizeof(Byte), comprLen, f);
656 uLong uncomprLen =
sizeof(T) * n;
657 int err = uncompress(
static_cast<Bytef*
>(&(*a)[0]), &uncomprLen, compr,
662 CVLog::Error(QString(
"uncompress error %1 : 2% -> %3, n %4")
663 .arg(QString::number(err),
664 QString::number(
sizeof(T) * n),
665 QString::number(uncomprLen),
666 QString::number(n)));
669 if (uncomprLen !=
sizeof(T) * n) {
672 "loadArrayFromFileIntoArray: uncompression failed "
673 "uncomprLen!=sizeof(T)*n");
678 if (a->
size() != n) {
680 CVLog::Error(
"loadArrayFromFileIntoArray: expected length "
681 << a->
size() <<
" loaded length " << n);
683 fread(&(*a)[0],
sizeof(T), n, f);
693 for (
int i = 0; i < (*aa)->size(); i++) {
694 if ((*(*aa))[i] !=
nullptr) {
696 (*(*aa))[i] =
nullptr;
704 for (
int i = 0; i < aa.size(); i++) {
705 if (aa[i] !=
nullptr) {
718 outAOA->
reserve(inAOA->size());
720 for (
int i = 0; i < inAOA->size(); i++) {
721 if ((*inAOA)[i] ==
NULL) {
static bool PrintDebug(const char *format,...)
Same as Print, but works only in Debug mode.
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
static bool Error(const char *format,...)
Display an error dialog with formatted message.
void reserveAddIfNeeded(int nplanned, int ntoallocated)
int indexOf(const T &value) const
void assign(int n, T value)
T & operator[](int index)
const T & operator[](int index) const
int indexOfNearestSorted(const T &value) const
ConstIterator end() const
void push_back_arr(StaticVector< T > &arr)
void resize_with(int n, const T &val)
void swap(StaticVector &other)
int indexOfSorted(const T &value) const
const std::vector< T > & getData() const
void push_front(const T &val)
std::vector< T > & getDataWritable()
StaticVector(int n, const T &value)
ConstReference front() const
ConstIterator begin() const
void push_back_arr(StaticVector< T > *arr)
ConstReference back() const
void push_back(const T &val)
void reserveAdd(int ntoallocated)
void resize(int n, T value)
int push_back_distinct(const T &val)
bool MakeDirectoryHierarchy(const std::string &directory)
std::string GetFileParentDirectory(const std::string &filename)
bool DirectoryExists(const std::string &directory)
Generic file read and write utility for python interface.
void deleteArrayOfArrays(StaticVector< StaticVector< T > * > **aa)
void saveArrayOfArraysToFile(std::string fileName, StaticVector< StaticVector< T > * > *aa)
int getArrayLengthFromFile(std::string fileName)
StaticVector< StaticVector< T > * > * loadArrayOfArraysFromFile(const std::string &fileName)
int indexOf(T *arr, int n, const T &what)
int sizeOfStaticVector(const StaticVector< T > *a)
StaticVector< T > * loadArrayFromFile(const std::string &fileName, bool printfWarning=false)
void loadArrayFromFileIntoArray(StaticVector< T > *a, const std::string &fileName, bool printfWarning=false)
void saveArrayToFile(const std::string &fileName, const StaticVector< T > &a, bool docompress=true)
StaticVector< StaticVector< T > * > * cloneArrayOfArrays(StaticVector< StaticVector< T > * > *inAOA)