]> www.ginac.de Git - ginac.git/blobdiff - ginac/container.pl
- added an optional algorithm-switch to lsolve().
[ginac.git] / ginac / container.pl
index 7b8304bd64d8a65e324ec6086b40125dea11c095..8374b9b9065cc8c5563d57b2f9b33938603fe214 100755 (executable)
@@ -23,6 +23,7 @@ if ($type eq 'exprseq') {
        $STLHEADER="vector";
        $reserve=1;
        $prepend=0;
+       $sort=0;
        $let_op=0;
        $open_bracket='(';
        $close_bracket=')';
@@ -34,6 +35,7 @@ if ($type eq 'exprseq') {
        $STLHEADER="list";
        $reserve=0;
        $prepend=1;
+       $sort=1;
        $let_op=1;
        $open_bracket='{';
        $close_bracket='}';
@@ -64,6 +66,7 @@ ${CONTAINER} & ${CONTAINER}::prepend(const ex & b)
        seq.push_front(b);
        return *this;
 }
+
 ${CONTAINER} & ${CONTAINER}::remove_first(void)
 {
        ensure_if_modifiable();
@@ -76,6 +79,32 @@ END_OF_PREPEND_IMPLEMENTATION
        $PREPEND_IMPLEMENTATION="";
 }
 
+if ($sort) {
+       $SORT_INTERFACE=<<END_OF_SORT_INTERFACE;
+       virtual ${CONTAINER} & sort(void);
+       virtual ${CONTAINER} & unique(void);
+END_OF_SORT_INTERFACE
+
+       $SORT_IMPLEMENTATION=<<END_OF_SORT_IMPLEMENTATION;
+${CONTAINER} & ${CONTAINER}::sort(void)
+{
+       ensure_if_modifiable();
+       seq.sort(ex_is_less());
+       return *this;
+}
+
+${CONTAINER} & ${CONTAINER}::unique(void)
+{
+       ensure_if_modifiable();
+       seq.unique(ex_is_equal());
+       return *this;
+}
+END_OF_SORT_IMPLEMENTATION
+} else {
+       $SORT_INTERFACE="    // no sort possible for ${CONTAINER}";
+       $SORT_IMPLEMENTATION="";
+}
+
 if ($let_op) {
        $LET_OP_IMPLEMENTATION=<<END_OF_LET_OP_IMPLEMENTATION
 ex & ${CONTAINER}::let_op(int i)
@@ -157,6 +186,7 @@ $interface=<<END_OF_INTERFACE;
  *                        \$STLHEADER=${STLHEADER}
  *                        \$reserve=${reserve}
  *                        \$prepend=${prepend}
+ *                        \$sort=${sort}
  *                        \$let_op=${let_op}
  *                        \$open_bracket=${open_bracket}
  *                        \$close_bracket=${close_bracket}
@@ -200,7 +230,7 @@ class ${CONTAINER} : public basic
        GINAC_DECLARE_REGISTERED_CLASS(${CONTAINER}, basic)
 
 public:
-       ${CONTAINER}(${STLT} const & s, bool discardable=0);
+       ${CONTAINER}(${STLT} const & s, bool discardable = false);
        ${CONTAINER}(${STLT} * vp); // vp will be deleted
 ${constructors_interface}
 
@@ -222,6 +252,7 @@ public:
        virtual ${CONTAINER} & append(const ex & b);
        virtual ${CONTAINER} & remove_last(void);
 ${PREPEND_INTERFACE}
+${SORT_INTERFACE}
 protected:
        virtual void printseq(const print_context & c, char openbracket, char delim,
                              char closebracket, unsigned this_precedence,
@@ -391,15 +422,15 @@ ex ${CONTAINER}::unarchive(const archive_node &n, const lst &sym_lst)
 void ${CONTAINER}::archive(archive_node &n) const
 {
        inherited::archive(n);
-       ${STLT}::const_iterator i = seq.begin(), iend = seq.end();
-       while (i != iend) {
+       ${STLT}::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
                n.add_ex("seq", *i);
-               i++;
+               ++i;
        }
 }
 
 //////////
-// functions overriding virtual functions from bases classes
+// functions overriding virtual functions from base classes
 //////////
 
 // public
@@ -415,8 +446,11 @@ void ${CONTAINER}::print(const print_context & c, unsigned level) const
                    << ", nops=" << nops()
                    << std::endl;
                unsigned delta_indent = static_cast<const print_tree &>(c).delta_indent;
-               for (${STLT}::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit)
-                       cit->print(c, level + delta_indent);
+               ${STLT}::const_iterator i = seq.begin(), end = seq.end();
+               while (i != end) {
+                       i->print(c, level + delta_indent);
+                       ++i;
+               }
                c.s << std::string(level + delta_indent,' ') << "=====" << std::endl;
 
        } else {
@@ -436,10 +470,14 @@ ${LET_OP_IMPLEMENTATION}
 
 ex ${CONTAINER}::map(map_function & f) const
 {
+       // This implementation is here because basic::map() uses let_op()
+       // which is not defined for all containers
        ${STLT} s;
        RESERVE(s,seq.size());
-       for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
-               s.push_back(f(*it));
+       ${STLT}::const_iterator i = seq.begin(), end = seq.end();
+       while (i != end) {
+               s.push_back(f(*i));
+               ++i;
        }
 
        return this${CONTAINER}(s);
@@ -467,35 +505,36 @@ ex ${CONTAINER}::subs(const lst & ls, const lst & lr, bool no_pattern) const
 int ${CONTAINER}::compare_same_type(const basic & other) const
 {
        GINAC_ASSERT(is_a<${CONTAINER}>(other));
-       ${CONTAINER} const & o=static_cast<${CONTAINER} const &>
-                                                                       (const_cast<basic &>(other));
-       int cmpval;
-       ${STLT}::const_iterator it1=seq.begin();
-       ${STLT}::const_iterator it2=o.seq.begin();
-
-       for (; (it1!=seq.end())&&(it2!=o.seq.end()); ++it1, ++it2) {
-               cmpval=(*it1).compare(*it2);
-               if (cmpval!=0) return cmpval;
-       }
+       ${CONTAINER} const & o = static_cast<const ${CONTAINER} &>(other);
+
+       ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
+                               it2 = o.seq.begin(), it2end = o.seq.end();
 
-       if (it1==seq.end()) {
-               return (it2==o.seq.end() ? 0 : -1);
+       while (it1 != it1end && it2 != it2end) {
+               int cmpval = it1->compare(*it2);
+               if (cmpval)
+                       return cmpval;
+               ++it1; ++it2;
        }
 
-       return 1;
+       return (it1 == it1end) ? (it2 == it2end ? 0 : -1) : 1;
 }
 
 bool ${CONTAINER}::is_equal_same_type(const basic & other) const
 {
        GINAC_ASSERT(is_a<${CONTAINER}>(other));
-       ${CONTAINER} const & o = static_cast<${CONTAINER} const &>(const_cast<basic &>(other));
-       if (seq.size()!=o.seq.size()) return false;
+       ${CONTAINER} const &o = static_cast<const ${CONTAINER} &>(other);
 
-       ${STLT}::const_iterator it1 = seq.begin();
-       ${STLT}::const_iterator it2 = o.seq.begin();
+       if (seq.size() != o.seq.size())
+               return false;
 
-       for (; it1!=seq.end(); ++it1, ++it2) {
-               if (!(*it1).is_equal(*it2)) return false;
+       ${STLT}::const_iterator it1 = seq.begin(), it1end = seq.end(),
+                               it2 = o.seq.begin();
+
+       while (it1 != it1end) {
+               if (!it1->is_equal(*it2))
+                       return false;
+               ++it1; ++it2;
        }
 
        return true;
@@ -528,6 +567,8 @@ ${CONTAINER} & ${CONTAINER}::remove_last(void)
 
 ${PREPEND_IMPLEMENTATION}
 
+${SORT_IMPLEMENTATION}
+
 // protected
 
 void ${CONTAINER}::printseq(const print_context & c, char openbracket, char delim,
@@ -537,13 +578,13 @@ void ${CONTAINER}::printseq(const print_context & c, char openbracket, char deli
        if (this_precedence <= upper_precedence)
                c.s << openbracket;
 
-       if (seq.size() != 0) {
+       if (!seq.empty()) {
                ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
                --itend;
                while (it != itend) {
                        it->print(c, this_precedence);
                        c.s << delim;
-                       it++;
+                       ++it;
                }
                it->print(c, this_precedence);
        }
@@ -576,11 +617,11 @@ bool ${CONTAINER}::is_canonical() const
 {
        if (seq.size()<=1) { return 1; }
 
-       ${STLT}::const_iterator it=seq.begin();
+       ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
        ${STLT}::const_iterator it_last=it;
-       for (++it; it!=seq.end(); it_last=it, ++it) {
-               if ((*it_last).compare(*it)>0) {
-                       if ((*it_last).compare(*it)>0) {
+       for (++it; it!=itend; it_last=it, ++it) {
+               if (it_last->compare(*it)>0) {
+                       if (it_last->compare(*it)>0) {
                                std::cout << *it_last << ">" << *it << "\\n";
                                return 0;
                        }
@@ -602,8 +643,10 @@ ${STLT} ${CONTAINER}::evalchildren(int level) const
                throw(std::runtime_error("max recursion level reached"));
        }
        --level;
-       for (${STLT}::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
-               s.push_back((*it).eval(level));
+       ${STLT}::const_iterator it = seq.begin(), itend = seq.end();
+       while (it != itend) {
+               s.push_back(it->eval(level));
+               ++it;
        }
        return s;
 }
@@ -614,28 +657,29 @@ ${STLT} * ${CONTAINER}::subschildren(const lst & ls, const lst & lr, bool no_pat
        // returns a pointer to a newly created epvector otherwise
        // (which has to be deleted somewhere else)
 
-       ${STLT}::const_iterator last=seq.end();
-       ${STLT}::const_iterator cit=seq.begin();
-       while (cit!=last) {
-               const ex & subsed_ex=(*cit).subs(ls,lr,no_pattern);
-               if (!are_ex_trivially_equal(*cit,subsed_ex)) {
+       ${STLT}::const_iterator cit = seq.begin(), end = seq.end();
+       while (cit != end) {
+               const ex & subsed_ex = cit->subs(ls, lr, no_pattern);
+               if (!are_ex_trivially_equal(*cit, subsed_ex)) {
 
                        // something changed, copy seq, subs and return it
                        ${STLT} *s=new ${STLT};
-                       RESERVE(*s,seq.size());
+                       RESERVE(*s, seq.size());
 
                        // copy parts of seq which are known not to have changed
-                       ${STLT}::const_iterator cit2=seq.begin();
-                       while (cit2!=cit) {
+                       ${STLT}::const_iterator cit2 = seq.begin();
+                       while (cit2 != cit) {
                                s->push_back(*cit2);
                                ++cit2;
                        }
+
                        // copy first changed element
                        s->push_back(subsed_ex);
                        ++cit2;
+
                        // copy rest
-                       while (cit2!=last) {
-                               s->push_back((*cit2).subs(ls,lr,no_pattern));
+                       while (cit2 != end) {
+                               s->push_back(cit2->subs(ls, lr, no_pattern));
                                ++cit2;
                        }
                        return s;