#ifndef __GINAC_UTILS_H__
#define __GINAC_UTILS_H__
-#include <strstream>
+#include "config.h"
+
#include <string>
#include <stdexcept>
-#include "config.h"
#include "assertion.h"
namespace GiNaC {
-// This should be obsoleted once <sstream> is widely deployed.
-template<class T>
-std::string ToString(const T & t)
-{
- char buf[256];
- std::ostrstream(buf,sizeof(buf)) << t << std::ends;
- return buf;
-}
-
/** 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:
#endif
}
-// Compute the sign of a permutation of a vector of things.
-template <typename T>
-int permutation_sign(std::vector<T> 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 <class It>
+int permutation_sign(It first, It last)
{
- if (s.size() < 2)
+ if (first == last)
+ return 0;
+ --last;
+ if (first == last)
return 0;
- int sigma = 1;
- for (typename std::vector<T>::iterator i=s.begin(); i!=s.end()-1; ++i) {
- for (typename std::vector<T>::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 (*i < *other) {
+ std::iter_swap(other, i);
+ flag = other;
+ swapped = true;
+ sign = -sign;
+ } else if (!(*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 (*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 <class It, class Cmp, class Swap>
+int permutation_sign(It first, It last, Cmp comp, Swap swapit)
+{
+ 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 (comp(*i, *other)) {
+ swapit(*other, *i);
+ flag = other;
+ swapped = true;
+ sign = -sign;
+ } else if (!comp(*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 (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 <class It, class Cmp, class Swap>
+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 <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;
}
- 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
}
#define DEFAULT_PRINT(classname, text) \
-void classname::print(std::ostream & os, unsigned upper_precedence) const \
+void classname::print(const print_context & c, unsigned level) const \
+{ \
+ debugmsg(#classname " print", LOGLEVEL_PRINT); \
+ if (is_a<print_tree>(c)) \
+ inherited::print(c, level); \
+ else \
+ c.s << text; \
+}
+
+#define DEFAULT_PRINT_LATEX(classname, text, latex) \
+void classname::print(const print_context & c, unsigned level) const \
{ \
debugmsg(#classname " print", LOGLEVEL_PRINT); \
- os << text; \
+ if (is_a<print_tree>(c)) \
+ inherited::print(c, level); \
+ else if (is_a<print_latex>(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<const TYPE *>(&OBJ)!=0)
+
+#define is_exactly_of_type(OBJ,TYPE) \
+ ((OBJ).tinfo()==GiNaC::TINFO_##TYPE)
+
+#define is_ex_of_type(OBJ,TYPE) \
+ (dynamic_cast<const TYPE *>((OBJ).bp)!=0)
+
+#define is_ex_exactly_of_type(OBJ,TYPE) \
+ ((*(OBJ).bp).tinfo()==GiNaC::TINFO_##TYPE)
+
} // namespace GiNaC
+
#endif // ndef __GINAC_UTILS_H__