From e4e16764f3a3c359050760514cd9aee6058bd152 Mon Sep 17 00:00:00 2001 From: Andrey Date: Wed, 13 Apr 2022 11:22:27 +0300 Subject: [PATCH] PIP_DEBUG, PIVector sort and doc --- libs/main/containers/picontainers.h | 2 + libs/main/containers/pideque.h | 8 ++-- libs/main/containers/pimap.h | 2 +- libs/main/containers/pivector.h | 68 +++++++++++++++++++++++++---- libs/main/core/pibase.h | 7 +-- libs/main/core/piincludes.cpp | 5 --- libs/main/core/piincludes.h | 2 - libs/main/io_devices/piconfig.h | 3 +- libs/main/io_devices/pidir.cpp | 4 +- libs/main/math/pievaluator.cpp | 7 ++- main.cpp | 10 ++--- 11 files changed, 77 insertions(+), 41 deletions(-) diff --git a/libs/main/containers/picontainers.h b/libs/main/containers/picontainers.h index 78821cf8..26528c0f 100644 --- a/libs/main/containers/picontainers.h +++ b/libs/main/containers/picontainers.h @@ -45,6 +45,8 @@ #include #include #include +#include +#include template diff --git a/libs/main/containers/pideque.h b/libs/main/containers/pideque.h index a24b8a02..3dd152a9 100644 --- a/libs/main/containers/pideque.h +++ b/libs/main/containers/pideque.h @@ -440,7 +440,7 @@ public: } inline PIDeque & insert(size_t index, const PIDeque & 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 & append(const T & e) {return push_back(e);} inline PIDeque & append(T && e) {return push_back(std::move(e));} inline PIDeque & append(const PIDeque & 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> reshape(size_t rows, size_t cols, int order = byRow) const { PIDeque> 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)); } diff --git a/libs/main/containers/pimap.h b/libs/main/containers/pimap.h index 8b75a678..996374b5 100644 --- a/libs/main/containers/pimap.h +++ b/libs/main/containers/pimap.h @@ -208,7 +208,7 @@ public: const T at(const Key & key) const {return (*this)[key];} PIMap & operator <<(const PIMap & other) { -#ifdef USE_DEBUG +#ifdef PIP_DEBUG if (&other == this) { printf("error with PIMap<%s, %s>::<<\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T)); } diff --git a/libs/main/containers/pivector.h b/libs/main/containers/pivector.h index 879ce7bf..2e436891 100644 --- a/libs/main/containers/pivector.h +++ b/libs/main/containers/pivector.h @@ -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 & insert(size_t index, const PIVector & 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(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 & 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 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 comp) + inline PIVector & 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 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 & sort(std::function comp) { + std::sort(begin(), end(), comp); return *this; } @@ -1390,7 +1442,7 @@ public: } inline PIVector & push_back(const PIVector & 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> reshape(size_t rows, size_t cols, ReshapeOrder order = byRow) const { PIVector> 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)); } diff --git a/libs/main/core/pibase.h b/libs/main/core/pibase.h index befa525e..3cc2961a 100644 --- a/libs/main/core/pibase.h +++ b/libs/main/core/pibase.h @@ -38,7 +38,7 @@ #include "piplatform.h" #include "pip_export.h" #include "pip_defs.h" -#include "string.h" +#include //! \~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) diff --git a/libs/main/core/piincludes.cpp b/libs/main/core/piincludes.cpp index 5a84c558..242ba712 100644 --- a/libs/main/core/piincludes.cpp +++ b/libs/main/core/piincludes.cpp @@ -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); } diff --git a/libs/main/core/piincludes.h b/libs/main/core/piincludes.h index cefec9b0..71902e27 100644 --- a/libs/main/core/piincludes.h +++ b/libs/main/core/piincludes.h @@ -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(); diff --git a/libs/main/io_devices/piconfig.h b/libs/main/io_devices/piconfig.h index d5095446..6c415529 100644 --- a/libs/main/io_devices/piconfig.h +++ b/libs/main/io_devices/piconfig.h @@ -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;} diff --git a/libs/main/io_devices/pidir.cpp b/libs/main/io_devices/pidir.cpp index c4db1962..497f8dc1 100644 --- a/libs/main/io_devices/pidir.cpp +++ b/libs/main/io_devices/pidir.cpp @@ -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 diff --git a/libs/main/math/pievaluator.cpp b/libs/main/math/pievaluator.cpp index 6e8e4272..9fb5e9d3 100644 --- a/libs/main/math/pievaluator.cpp +++ b/libs/main/math/pievaluator.cpp @@ -476,11 +476,10 @@ bool PIEvaluator::fillElements() { cnum = 0; typedef PIPair PairII; PIVector 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; diff --git a/main.cpp b/main.cpp index 4f3d7c12..c5103f0a 100644 --- a/main.cpp +++ b/main.cpp @@ -2,12 +2,8 @@ #include int main(int argc, char * argv[]) { - PIVector 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 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; }