]> www.ginac.de Git - ginac.git/blobdiff - ginac/utils.h
* Supplement some (now deprecated) macros by inlined template functions:
[ginac.git] / ginac / utils.h
index c0bc2efecf49c00ec10328bc5b51674b00341acb..3e8e17cae884c498773c5fb2860a8d3d1ee76795 100644 (file)
@@ -28,7 +28,6 @@
 
 #include <string>
 #include <stdexcept>
-#include <functional>
 #if defined(HAVE_SSTREAM)
 #include <sstream>
 #elif defined(HAVE_STRSTREAM)
@@ -131,7 +130,7 @@ inline unsigned golden_ratio_hash(unsigned n)
    explicitly supplied comparison function. If the sign returned is 1 or -1,
    the container is sorted after the operation. */
 template <class It>
-inline int permutation_sign(It first, It last)
+int permutation_sign(It first, It last)
 {
        if (first == last)
                return 0;
@@ -147,7 +146,7 @@ inline int permutation_sign(It first, It last)
                bool swapped = false;
                while (i != first) {
                        if (*i < *other) {
-                               iter_swap(other, i);
+                               std::iter_swap(other, i);
                                flag = other;
                                swapped = true;
                                sign = -sign;
@@ -166,7 +165,7 @@ inline int permutation_sign(It first, It last)
                swapped = false;
                while (i != last) {
                        if (*other < *i) {
-                               iter_swap(i, other);
+                               std::iter_swap(i, other);
                                flag = other;
                                swapped = true;
                                sign = -sign;
@@ -183,8 +182,8 @@ inline int permutation_sign(It first, It last)
        return sign;
 }
 
-template <class It, class Cmp>
-inline int permutation_sign(It first, It last, Cmp comp)
+template <class It, class Cmp, class Swap>
+int permutation_sign(It first, It last, Cmp comp, Swap swapit)
 {
        if (first == last)
                return 0;
@@ -200,7 +199,7 @@ inline int permutation_sign(It first, It last, Cmp comp)
                bool swapped = false;
                while (i != first) {
                        if (comp(*i, *other)) {
-                               iter_swap(other, i);
+                               swapit(*other, *i);
                                flag = other;
                                swapped = true;
                                sign = -sign;
@@ -219,7 +218,7 @@ inline int permutation_sign(It first, It last, Cmp comp)
                swapped = false;
                while (i != last) {
                        if (comp(*other, *i)) {
-                               iter_swap(i, other);
+                               swapit(*i, *other);
                                flag = other;
                                swapped = true;
                                sign = -sign;
@@ -237,8 +236,8 @@ inline int permutation_sign(It first, It last, Cmp comp)
 }
 
 /* Implementation of shaker sort, only compares adjacent elements. */
-template <class It, class Cmp>
-inline void shaker_sort(It first, It last, Cmp comp)
+template <class It, class Cmp, class Swap>
+void shaker_sort(It first, It last, Cmp comp, Swap swapit)
 {
        if (first == last)
                return;
@@ -253,7 +252,7 @@ inline void shaker_sort(It first, It last, Cmp comp)
                bool swapped = false;
                while (i != first) {
                        if (comp(*i, *other)) {
-                               iter_swap(other, i);
+                               swapit(*other, *i);
                                flag = other;
                                swapped = true;
                        }
@@ -270,7 +269,7 @@ inline void shaker_sort(It first, It last, Cmp comp)
                swapped = false;
                while (i != last) {
                        if (comp(*other, *i)) {
-                               iter_swap(i, other);
+                               swapit(*i, *other);
                                flag = other;
                                swapped = true;
                        }
@@ -283,14 +282,39 @@ inline void shaker_sort(It first, It last, Cmp comp)
        } while (first != last);
 }
 
-/* Function objects for STL sort() etc. */
-struct ex_is_less : public binary_function<ex, ex, bool> {
-       bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
-};
+/* In-place cyclic permutation of a container (no copying, only swapping). */
+template <class It, class Swap>
+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;
+       }
+}
 
-struct ex_is_equal : public binary_function<ex, ex, bool> {
-       bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
-};
 
 // Collection of `construct on first use' wrappers for safely avoiding
 // internal object replication without running into the `static
@@ -452,7 +476,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<print_tree>(c)) \
                inherited::print(c, level); \
        else \
                c.s << text; \
@@ -462,9 +486,9 @@ 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<print_tree>(c)) \
                inherited::print(c, level); \
-       else if (is_of_type(c, print_latex)) \
+       else if (is_a<print_latex>(c)) \
                c.s << latex; \
        else \
                c.s << text; \