PIP_DEBUG, PIVector sort and doc
This commit is contained in:
@@ -45,6 +45,8 @@
|
|||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <new>
|
#include <new>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
|
|
||||||
template <typename C>
|
template <typename C>
|
||||||
|
|||||||
@@ -440,7 +440,7 @@ public:
|
|||||||
}
|
}
|
||||||
inline PIDeque<T> & insert(size_t index, const PIDeque<T> & other) {
|
inline PIDeque<T> & insert(size_t index, const PIDeque<T> & other) {
|
||||||
if (other.isEmpty()) return *this;
|
if (other.isEmpty()) return *this;
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (&other == this) {
|
if (&other == this) {
|
||||||
printf("error with PIDeque<%s>::insert\n", __PIP_TYPENAME__(T));
|
printf("error with PIDeque<%s>::insert\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
@@ -549,7 +549,7 @@ public:
|
|||||||
inline PIDeque<T> & append(const T & e) {return push_back(e);}
|
inline PIDeque<T> & append(const T & e) {return push_back(e);}
|
||||||
inline PIDeque<T> & append(T && e) {return push_back(std::move(e));}
|
inline PIDeque<T> & append(T && e) {return push_back(std::move(e));}
|
||||||
inline PIDeque<T> & append(const PIDeque<T> & v) {
|
inline PIDeque<T> & append(const PIDeque<T> & v) {
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (&v == this) {
|
if (&v == this) {
|
||||||
printf("error with PIDeque<%s>::append\n", __PIP_TYPENAME__(T));
|
printf("error with PIDeque<%s>::append\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
@@ -625,7 +625,7 @@ public:
|
|||||||
inline PIDeque<PIDeque<T>> reshape(size_t rows, size_t cols, int order = byRow) const {
|
inline PIDeque<PIDeque<T>> reshape(size_t rows, size_t cols, int order = byRow) const {
|
||||||
PIDeque<PIDeque<T>> ret;
|
PIDeque<PIDeque<T>> ret;
|
||||||
if (isEmpty()) return ret;
|
if (isEmpty()) return ret;
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (rows*cols != pid_size) {
|
if (rows*cols != pid_size) {
|
||||||
printf("error with PIDeque<%s>::reshape\n", __PIP_TYPENAME__(T));
|
printf("error with PIDeque<%s>::reshape\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
@@ -777,7 +777,7 @@ private:
|
|||||||
if (as != pid_rsize) {
|
if (as != pid_rsize) {
|
||||||
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize))
|
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize))
|
||||||
T * p_d = (T*)(realloc((void*)(pid_data), as*sizeof(T)));
|
T * p_d = (T*)(realloc((void*)(pid_data), as*sizeof(T)));
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (!p_d) {
|
if (!p_d) {
|
||||||
printf("error with PIDeque<%s>::alloc\n", __PIP_TYPENAME__(T));
|
printf("error with PIDeque<%s>::alloc\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -208,7 +208,7 @@ public:
|
|||||||
const T at(const Key & key) const {return (*this)[key];}
|
const T at(const Key & key) const {return (*this)[key];}
|
||||||
|
|
||||||
PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
|
PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (&other == this) {
|
if (&other == this) {
|
||||||
printf("error with PIMap<%s, %s>::<<\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T));
|
printf("error with PIMap<%s, %s>::<<\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
|
|
||||||
#include "picontainers.h"
|
#include "picontainers.h"
|
||||||
|
|
||||||
|
|
||||||
//! \addtogroup Containers
|
//! \addtogroup Containers
|
||||||
//! \{
|
//! \{
|
||||||
//! \class PIVector
|
//! \class PIVector
|
||||||
@@ -1199,7 +1200,7 @@ public:
|
|||||||
//! \~\sa \a append(), \a prepend(), \a remove()
|
//! \~\sa \a append(), \a prepend(), \a remove()
|
||||||
inline PIVector<T> & insert(size_t index, const PIVector<T> & v) {
|
inline PIVector<T> & insert(size_t index, const PIVector<T> & v) {
|
||||||
if (v.isEmpty()) return *this;
|
if (v.isEmpty()) return *this;
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (&v == this) {
|
if (&v == this) {
|
||||||
printf("error with PIVector<%s>::insert\n", __PIP_TYPENAME__(T));
|
printf("error with PIVector<%s>::insert\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
@@ -1249,10 +1250,61 @@ public:
|
|||||||
piSwap<size_t>(piv_rsize, v.piv_rsize);
|
piSwap<size_t>(piv_rsize, v.piv_rsize);
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef int (*CompareFunc)(const T * , const T * );
|
//! \~\brief
|
||||||
static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}
|
//! \~english Sorts the elements in non-descending order.
|
||||||
inline PIVector<T> & sort(CompareFunc compare = compare_func) {
|
//! \~russian Сортировка элементов в порядке возрастания.
|
||||||
piqsort(piv_data, piv_size, sizeof(T), (int(*)(const void * , const void * ))compare);
|
//! \~\details
|
||||||
|
//! \~english The order of equal elements is not guaranteed to be preserved.
|
||||||
|
//! Elements are compared using operator<.
|
||||||
|
//! Sorting provided by [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort).
|
||||||
|
//! Complexity `O(N·log(N))`.
|
||||||
|
//! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется.
|
||||||
|
//! Для сравнения элементов используется оператор `operator<`.
|
||||||
|
//! Для сортировки используется функция [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort).
|
||||||
|
//! Сложность сортировки `O(N·log(N))`.
|
||||||
|
//! \~\code
|
||||||
|
//! PIVector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
|
||||||
|
//! v.sort();
|
||||||
|
//! piCout << v; // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
|
||||||
|
//! \endcode
|
||||||
|
//! \~\sa \a sort(std::function<bool(const T &a, const T &b)> comp)
|
||||||
|
inline PIVector<T> & sort() {
|
||||||
|
std::sort(begin(), end());
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \~\brief
|
||||||
|
//! \~english Sorts the elements in non-descending order.
|
||||||
|
//! \~russian Сортировка элементов в порядке возрастания.
|
||||||
|
//! \~\details
|
||||||
|
//! \~english The order of equal elements is not guaranteed to be preserved.
|
||||||
|
//! Elements are compared using the given binary comparison function `comp`.
|
||||||
|
//! which returns `true` if the first argument is less than (i.e. is ordered before) the second.
|
||||||
|
//! The signature of the comparison function should be equivalent to the following:
|
||||||
|
//! \code
|
||||||
|
//! bool comp(const T &a, const T &b);
|
||||||
|
//! \endcode
|
||||||
|
//! While the signature does not need to have const &, the function must not modify the objects passed to it.
|
||||||
|
//! Sorting provided by [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort).
|
||||||
|
//! Complexity `O(N·log(N))`.
|
||||||
|
//! \~russian Сохранность порядка элементов, имеющих одинаковое значение, не гарантируется.
|
||||||
|
//! Для сравнения элементов используется функция сравнения `comp`.
|
||||||
|
//! Функция сравнения, возвращает `true` если первый аргумент меньше второго.
|
||||||
|
//! Сигнатура функции сравнения должна быть эквивалентна следующей:
|
||||||
|
//! \code
|
||||||
|
//! bool comp(const T &a, const T &b);
|
||||||
|
//! \endcode
|
||||||
|
//! Сигнатура не обязана содержать const &, однако, функция не может изменять переданные объекты.
|
||||||
|
//! Для сортировки используется функция [std::sort](https://ru.cppreference.com/w/cpp/algorithm/sort).
|
||||||
|
//! Сложность сортировки `O(N·log(N))`.
|
||||||
|
//! \~\code
|
||||||
|
//! PIVector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
|
||||||
|
//! v.sort([](const int & a, const int & b){return a > b;});
|
||||||
|
//! piCout << v; // 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
|
||||||
|
//! \endcode
|
||||||
|
//! \~\sa \a sort()
|
||||||
|
inline PIVector<T> & sort(std::function<bool(const T &a, const T &b)> comp) {
|
||||||
|
std::sort(begin(), end(), comp);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1390,7 +1442,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline PIVector<T> & push_back(const PIVector<T> & v) {
|
inline PIVector<T> & push_back(const PIVector<T> & v) {
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (&v == this) {
|
if (&v == this) {
|
||||||
printf("error with PIVector<%s>::push_back\n", __PIP_TYPENAME__(T));
|
printf("error with PIVector<%s>::push_back\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
@@ -1495,7 +1547,7 @@ public:
|
|||||||
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, ReshapeOrder order = byRow) const {
|
inline PIVector<PIVector<T>> reshape(size_t rows, size_t cols, ReshapeOrder order = byRow) const {
|
||||||
PIVector<PIVector<T>> ret;
|
PIVector<PIVector<T>> ret;
|
||||||
if (isEmpty()) return ret;
|
if (isEmpty()) return ret;
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (rows*cols != piv_size) {
|
if (rows*cols != piv_size) {
|
||||||
printf("error with PIVector<%s>::reshape\n", __PIP_TYPENAME__(T));
|
printf("error with PIVector<%s>::reshape\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
@@ -1624,7 +1676,7 @@ private:
|
|||||||
if (as == piv_rsize) return;
|
if (as == piv_rsize) return;
|
||||||
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-piv_rsize))
|
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-piv_rsize))
|
||||||
T * p_d = (T*)(realloc((void*)(piv_data), as*sizeof(T)));
|
T * p_d = (T*)(realloc((void*)(piv_data), as*sizeof(T)));
|
||||||
#ifdef USE_DEBUG
|
#ifdef PIP_DEBUG
|
||||||
if (!p_d) {
|
if (!p_d) {
|
||||||
printf("error with PIVector<%s>::alloc\n", __PIP_TYPENAME__(T));
|
printf("error with PIVector<%s>::alloc\n", __PIP_TYPENAME__(T));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@
|
|||||||
#include "piplatform.h"
|
#include "piplatform.h"
|
||||||
#include "pip_export.h"
|
#include "pip_export.h"
|
||||||
#include "pip_defs.h"
|
#include "pip_defs.h"
|
||||||
#include "string.h"
|
#include <string.h>
|
||||||
|
|
||||||
//! \~english
|
//! \~english
|
||||||
//! Meta-information section for any entity.
|
//! Meta-information section for any entity.
|
||||||
@@ -246,11 +246,6 @@
|
|||||||
extern char ** environ;
|
extern char ** environ;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef NDEBUG
|
|
||||||
# undef NDEBUG
|
|
||||||
#else
|
|
||||||
# define USE_DEBUG
|
|
||||||
#endif
|
|
||||||
#ifndef assert
|
#ifndef assert
|
||||||
# define assert(x)
|
# define assert(x)
|
||||||
# define assertm(exp, msg)
|
# define assertm(exp, msg)
|
||||||
|
|||||||
@@ -80,11 +80,6 @@ PIString PIPVersion() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void piqsort(void * base, size_t num, size_t size, int (*compar)(const void *, const void *)) {
|
|
||||||
qsort(base, num, size, compar);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void randomize() {
|
void randomize() {
|
||||||
srand(PISystemTime::current(true).nanoseconds);
|
srand(PISystemTime::current(true).nanoseconds);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,8 +64,6 @@ PIP_EXPORT PIString errorString();
|
|||||||
//! Сброс последней ошибки
|
//! Сброс последней ошибки
|
||||||
PIP_EXPORT void errorClear();
|
PIP_EXPORT void errorClear();
|
||||||
|
|
||||||
PIP_EXPORT void piqsort(void* base, size_t num, size_t size, int (*compar)(const void*,const void*));
|
|
||||||
|
|
||||||
PIP_EXPORT void randomize();
|
PIP_EXPORT void randomize();
|
||||||
PIP_EXPORT int randomi();
|
PIP_EXPORT int randomi();
|
||||||
|
|
||||||
|
|||||||
@@ -303,8 +303,7 @@ public:
|
|||||||
PIStringList toStringList() const {return _value.split("%|%");}
|
PIStringList toStringList() const {return _value.split("%|%");}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
typedef PIConfig::Entry * EntryPtr;
|
static bool compare(PIConfig::Entry * const & f, PIConfig::Entry * const & s) {return f->_line < s->_line;}
|
||||||
static int compare(const EntryPtr * f, const EntryPtr * s) {return (*f)->_line == (*s)->_line ? 0 : (*f)->_line < (*s)->_line ? -1 : 1;}
|
|
||||||
bool entryExists(const Entry * e, const PIString & name) const;
|
bool entryExists(const Entry * e, const PIString & name) const;
|
||||||
void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
|
void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
|
||||||
void clear() {_children.clear(); _name = _value = _type = _comment = _all = PIString(); _line = 0; _parent = 0;}
|
void clear() {_children.clear(); _name = _value = _type = _comment = _all = PIString(); _line = 0; _parent = 0;}
|
||||||
|
|||||||
@@ -237,8 +237,8 @@ bool PIDir::make(bool withParents) {
|
|||||||
|
|
||||||
|
|
||||||
#ifdef WINDOWS
|
#ifdef WINDOWS
|
||||||
int sort_compare(const PIFile::FileInfo * v0, const PIFile::FileInfo * v1) {
|
bool sort_compare(const PIFile::FileInfo & v0, const PIFile::FileInfo & v1) {
|
||||||
return strcoll(v0->path.data(), v1->path.data());
|
return strcoll(v0.path.data(), v1.path.data()) < 0;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -476,11 +476,10 @@ bool PIEvaluator::fillElements() {
|
|||||||
cnum = 0;
|
cnum = 0;
|
||||||
typedef PIPair<int, int> PairII;
|
typedef PIPair<int, int> PairII;
|
||||||
PIVector<PairII> var_index;
|
PIVector<PairII> var_index;
|
||||||
for (int i = 0; i < content.variables.size_s(); ++i)
|
for (int i = 0; i < content.variables.size_s(); ++i) {
|
||||||
var_index << PairII(i, content.variables[i].name.length());
|
var_index << PairII(i, content.variables[i].name.length());
|
||||||
var_index.sort([](const PairII * v1, const PairII * v2) -> int {
|
}
|
||||||
return v1->second > v2->second ? -1 : (v1->second == v2->second ? 0 : 1);
|
var_index.sort([](const PairII & v1, const PairII & v2){return v1.second < v2.second;});
|
||||||
});
|
|
||||||
for (int i = 0; i < var_index.size_s(); i++) {
|
for (int i = 0; i < var_index.size_s(); i++) {
|
||||||
int vind = var_index[i].first;
|
int vind = var_index[i].first;
|
||||||
curfind = content.variables[vind].name;
|
curfind = content.variables[vind].name;
|
||||||
|
|||||||
10
main.cpp
10
main.cpp
@@ -2,12 +2,8 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
|
||||||
int main(int argc, char * argv[]) {
|
int main(int argc, char * argv[]) {
|
||||||
PIVector <PIString> v(5, [](size_t i){return PIString::fromNumber(i*2);});
|
PIVector<int> v{5, 7, 4, 2, 8, 6, 1, 9, 0, 3};
|
||||||
v.prepend({"1", "02", "3"});
|
v.sort([](const int & a, const int & b){return a > b;});
|
||||||
piCout << v;
|
piCout << v; // 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
|
||||||
std::sort(v.begin(), v.end());
|
|
||||||
piCout << v;
|
|
||||||
std::sort(v.rbegin(), v.rend());
|
|
||||||
piCout << v;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user