X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Futils.h;h=78334b17d25ac4047b5a7d35eef4e83841b0edb1;hp=6edea866c16f22c2e0d533f0a42f2513b88d31df;hb=dc2510946d9ce577aab2bd3e5d2f62c50d3faa30;hpb=7ece13ddcc174e28b7b333e5d09900bc90f3dd78 diff --git a/ginac/utils.h b/ginac/utils.h index 6edea866..78334b17 100644 --- a/ginac/utils.h +++ b/ginac/utils.h @@ -28,36 +28,18 @@ #include #include -#if defined(HAVE_SSTREAM) -#include -#elif defined(HAVE_STRSTREAM) -#include -#else -#error Need either sstream or strstream -#endif #include "assertion.h" namespace GiNaC { -// This should be obsoleted once is widely deployed. -template -std::string ToString(const T & t) -{ -#if defined(HAVE_SSTREAM) - std::ostringstream buf; - buf << t << std::ends; - return buf.str(); -#else - char buf[256]; - std::ostrstream(buf,sizeof(buf)) << t << std::ends; - return buf; -#endif -} - /** Exception class thrown by classes which provide their own series expansion * to signal that ordinary Taylor expansion is safe. */ class do_taylor {}; +/** Exception class thrown by functions to signal unimplemented functionality + * so the expression may just be .hold() */ +class dunno {}; + /** Exception class thrown when a singularity is encountered. */ class pole_error : public std::domain_error { public: @@ -126,27 +108,195 @@ inline unsigned golden_ratio_hash(unsigned n) #endif } -// Compute the sign of a permutation of a vector of things. -template -int permutation_sign(std::vector s) +/* Compute the sign of a permutation of a container, with and without an + explicitly supplied comparison function. If the sign returned is 1 or -1, + the container is sorted after the operation. */ +template +int permutation_sign(It first, It last) +{ + if (first == last) + return 0; + --last; + if (first == last) + return 0; + It flag = first; + int sign = 1; + + do { + It i = last, other = last; + --other; + bool swapped = false; + while (i != first) { + if (*i < *other) { + std::iter_swap(other, i); + flag = other; + swapped = true; + sign = -sign; + } else if (!(*other < *i)) + return 0; + --i; --other; + } + if (!swapped) + return sign; + ++flag; + if (flag == last) + return sign; + first = flag; + i = first; other = first; + ++other; + swapped = false; + while (i != last) { + if (*other < *i) { + std::iter_swap(i, other); + flag = other; + swapped = true; + sign = -sign; + } else if (!(*i < *other)) + return 0; + ++i; ++other; + } + if (!swapped) + return sign; + last = flag; + --last; + } while (first != last); + + return sign; +} + +template +int permutation_sign(It first, It last, Cmp comp, Swap swapit) { - if (s.size() < 2) + if (first == last) + return 0; + --last; + if (first == last) return 0; - int sigma = 1; - for (typename std::vector::iterator i=s.begin(); i!=s.end()-1; ++i) { - for (typename std::vector::iterator j=i+1; j!=s.end(); ++j) { - if (*i == *j) + It flag = first; + int sign = 1; + + do { + It i = last, other = last; + --other; + bool swapped = false; + while (i != first) { + if (comp(*i, *other)) { + swapit(*other, *i); + flag = other; + swapped = true; + sign = -sign; + } else if (!comp(*other, *i)) return 0; - if (*i > *j) { - iter_swap(i,j); - sigma = -sigma; + --i; --other; + } + if (!swapped) + return sign; + ++flag; + if (flag == last) + return sign; + first = flag; + i = first; other = first; + ++other; + swapped = false; + while (i != last) { + if (comp(*other, *i)) { + swapit(*i, *other); + flag = other; + swapped = true; + sign = -sign; + } else if (!comp(*i, *other)) + return 0; + ++i; ++other; + } + if (!swapped) + return sign; + last = flag; + --last; + } while (first != last); + + return sign; +} + +/* Implementation of shaker sort, only compares adjacent elements. */ +template +void shaker_sort(It first, It last, Cmp comp, Swap swapit) +{ + if (first == last) + return; + --last; + if (first == last) + return; + It flag = first; + + do { + It i = last, other = last; + --other; + bool swapped = false; + while (i != first) { + if (comp(*i, *other)) { + swapit(*other, *i); + flag = other; + swapped = true; + } + --i; --other; + } + if (!swapped) + return; + ++flag; + if (flag == last) + return; + first = flag; + i = first; other = first; + ++other; + swapped = false; + while (i != last) { + if (comp(*other, *i)) { + swapit(*i, *other); + flag = other; + swapped = true; } + ++i; ++other; } + if (!swapped) + return; + last = flag; + --last; + } while (first != last); +} + +/* In-place cyclic permutation of a container (no copying, only swapping). */ +template +void cyclic_permutation(It first, It last, It new_first, Swap swapit) +{ + unsigned num = last - first; +again: + if (first == new_first || num < 2) + return; + + unsigned num1 = new_first - first, num2 = last - new_first; + if (num1 >= num2) { + It a = first, b = new_first; + while (b != last) { + swapit(*a, *b); + ++a; ++b; + } + if (num1 > num2) { + first += num2; + num = num1; + goto again; + } + } else { + It a = new_first, b = last; + do { + --a; --b; + swapit(*a, *b); + } while (a != first); + last -= num1; + num = num2; + goto again; } - return sigma; } -void append_exvector_to_exvector(exvector & dest, const exvector & source); // Collection of `construct on first use' wrappers for safely avoiding // internal object replication without running into the `static @@ -308,7 +458,7 @@ int classname::compare_same_type(const basic & other) const \ void classname::print(const print_context & c, unsigned level) const \ { \ debugmsg(#classname " print", LOGLEVEL_PRINT); \ - if (is_of_type(c, print_tree)) \ + if (is_a(c)) \ inherited::print(c, level); \ else \ c.s << text; \ @@ -318,14 +468,30 @@ void classname::print(const print_context & c, unsigned level) const \ void classname::print(const print_context & c, unsigned level) const \ { \ debugmsg(#classname " print", LOGLEVEL_PRINT); \ - if (is_of_type(c, print_tree)) \ + if (is_a(c)) \ inherited::print(c, level); \ - else if (is_of_type(c, print_latex)) \ + else if (is_a(c)) \ c.s << latex; \ else \ c.s << text; \ } +// Obsolete convenience macros. TO BE PHASED OUT SOON! +// Use the inlined template functions in basic.h instead. (FIXME: remove them) + +#define is_of_type(OBJ,TYPE) \ + (dynamic_cast(&OBJ)!=0) + +#define is_exactly_of_type(OBJ,TYPE) \ + ((OBJ).tinfo()==GiNaC::TINFO_##TYPE) + +#define is_ex_of_type(OBJ,TYPE) \ + (dynamic_cast((OBJ).bp)!=0) + +#define is_ex_exactly_of_type(OBJ,TYPE) \ + ((*(OBJ).bp).tinfo()==GiNaC::TINFO_##TYPE) + } // namespace GiNaC + #endif // ndef __GINAC_UTILS_H__