PIP_DEBUG, PIVector sort and doc

This commit is contained in:
Andrey
2022-04-13 11:22:27 +03:00
parent 00830958df
commit e4e16764f3
11 changed files with 77 additions and 41 deletions

View File

@@ -45,6 +45,8 @@
#include <type_traits>
#include <string.h>
#include <new>
#include <algorithm>
#include <functional>
template <typename C>

View File

@@ -440,7 +440,7 @@ public:
}
inline PIDeque<T> & insert(size_t index, const PIDeque<T> & other) {
if (other.isEmpty()) return *this;
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (&other == this) {
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(T && e) {return push_back(std::move(e));}
inline PIDeque<T> & append(const PIDeque<T> & v) {
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (&v == this) {
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 {
PIDeque<PIDeque<T>> ret;
if (isEmpty()) return ret;
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (rows*cols != pid_size) {
printf("error with PIDeque<%s>::reshape\n", __PIP_TYPENAME__(T));
}
@@ -777,7 +777,7 @@ private:
if (as != pid_rsize) {
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-pid_rsize))
T * p_d = (T*)(realloc((void*)(pid_data), as*sizeof(T)));
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (!p_d) {
printf("error with PIDeque<%s>::alloc\n", __PIP_TYPENAME__(T));
}

View File

@@ -208,7 +208,7 @@ public:
const T at(const Key & key) const {return (*this)[key];}
PIMap<Key, T> & operator <<(const PIMap<Key, T> & other) {
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (&other == this) {
printf("error with PIMap<%s, %s>::<<\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T));
}

View File

@@ -36,6 +36,7 @@
#include "picontainers.h"
//! \addtogroup Containers
//! \{
//! \class PIVector
@@ -1199,7 +1200,7 @@ public:
//! \~\sa \a append(), \a prepend(), \a remove()
inline PIVector<T> & insert(size_t index, const PIVector<T> & v) {
if (v.isEmpty()) return *this;
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (&v == this) {
printf("error with PIVector<%s>::insert\n", __PIP_TYPENAME__(T));
}
@@ -1249,10 +1250,61 @@ public:
piSwap<size_t>(piv_rsize, v.piv_rsize);
}
typedef int (*CompareFunc)(const T * , const T * );
static int compare_func(const T * t0, const T * t1) {return (*t0) < (*t1) ? -1 : ((*t0) == (*t1) ? 0 : 1);}
inline PIVector<T> & sort(CompareFunc compare = compare_func) {
piqsort(piv_data, piv_size, sizeof(T), (int(*)(const void * , const void * ))compare);
//! \~\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 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;
}
@@ -1390,7 +1442,7 @@ public:
}
inline PIVector<T> & push_back(const PIVector<T> & v) {
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (&v == this) {
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 {
PIVector<PIVector<T>> ret;
if (isEmpty()) return ret;
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (rows*cols != piv_size) {
printf("error with PIVector<%s>::reshape\n", __PIP_TYPENAME__(T));
}
@@ -1624,7 +1676,7 @@ private:
if (as == piv_rsize) return;
PIINTROSPECTION_CONTAINER_ALLOC(T, (as-piv_rsize))
T * p_d = (T*)(realloc((void*)(piv_data), as*sizeof(T)));
#ifdef USE_DEBUG
#ifdef PIP_DEBUG
if (!p_d) {
printf("error with PIVector<%s>::alloc\n", __PIP_TYPENAME__(T));
}

View File

@@ -38,7 +38,7 @@
#include "piplatform.h"
#include "pip_export.h"
#include "pip_defs.h"
#include "string.h"
#include <string.h>
//! \~english
//! Meta-information section for any entity.
@@ -246,11 +246,6 @@
extern char ** environ;
#endif
#ifdef NDEBUG
# undef NDEBUG
#else
# define USE_DEBUG
#endif
#ifndef assert
# define assert(x)
# define assertm(exp, msg)

View File

@@ -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() {
srand(PISystemTime::current(true).nanoseconds);
}

View File

@@ -64,8 +64,6 @@ PIP_EXPORT PIString errorString();
//! Сброс последней ошибки
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 int randomi();

View File

@@ -303,8 +303,7 @@ public:
PIStringList toStringList() const {return _value.split("%|%");}
private:
typedef PIConfig::Entry * EntryPtr;
static int compare(const EntryPtr * f, const EntryPtr * s) {return (*f)->_line == (*s)->_line ? 0 : (*f)->_line < (*s)->_line ? -1 : 1;}
static bool compare(PIConfig::Entry * const & f, PIConfig::Entry * const & s) {return f->_line < s->_line;}
bool entryExists(const Entry * e, const PIString & name) const;
void buildLine() {_all = _tab + _full_name + " = " + _value + " #" + _type + " " + _comment;}
void clear() {_children.clear(); _name = _value = _type = _comment = _all = PIString(); _line = 0; _parent = 0;}

View File

@@ -237,8 +237,8 @@ bool PIDir::make(bool withParents) {
#ifdef WINDOWS
int sort_compare(const PIFile::FileInfo * v0, const PIFile::FileInfo * v1) {
return strcoll(v0->path.data(), v1->path.data());
bool sort_compare(const PIFile::FileInfo & v0, const PIFile::FileInfo & v1) {
return strcoll(v0.path.data(), v1.path.data()) < 0;
}
#endif

View File

@@ -476,11 +476,10 @@ bool PIEvaluator::fillElements() {
cnum = 0;
typedef PIPair<int, int> PairII;
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.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++) {
int vind = var_index[i].first;
curfind = content.variables[vind].name;

View File

@@ -2,12 +2,8 @@
#include <algorithm>
int main(int argc, char * argv[]) {
PIVector <PIString> v(5, [](size_t i){return PIString::fromNumber(i*2);});
v.prepend({"1", "02", "3"});
piCout << v;
std::sort(v.begin(), v.end());
piCout << v;
std::sort(v.rbegin(), v.rend());
piCout << v;
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
return 0;
}