X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=blobdiff_plain;f=ginac%2Fexpairseq.cpp;h=963cd23931b3a503c9edd323e55d55ce99e5bf92;hp=fe03ce899999d25e8f79c33c6a77b61fd5765877;hb=38e03cb00e07aa13d10ba75f79a5fee593b6f2cb;hpb=2afa71937b3c12cdc70f01213baa8a92be4b604a diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp index fe03ce89..963cd239 100644 --- a/ginac/expairseq.cpp +++ b/ginac/expairseq.cpp @@ -34,9 +34,6 @@ #include "indexed.h" #include -#if EXPAIRSEQ_USE_HASHTAB -#include -#endif // EXPAIRSEQ_USE_HASHTAB #include #include #include @@ -70,40 +67,10 @@ public: // public expairseq::expairseq() -#if EXPAIRSEQ_USE_HASHTAB - : hashtabsize(0) -#endif // EXPAIRSEQ_USE_HASHTAB {} // protected -#if 0 -/** For use by copy ctor and assignment operator. */ -void expairseq::copy(const expairseq &other) -{ - seq = other.seq; - overall_coeff = other.overall_coeff; -#if EXPAIRSEQ_USE_HASHTAB - // copy hashtab - hashtabsize = other.hashtabsize; - if (hashtabsize!=0) { - hashmask = other.hashmask; - hashtab.resize(hashtabsize); - epvector::const_iterator osb = other.seq.begin(); - for (unsigned i=0; i vp, const ex &oc, bool do_index_renaming) +expairseq::expairseq(epvector && vp, const ex &oc, bool do_index_renaming) : overall_coeff(oc) { - GINAC_ASSERT(vp.get()!=0); GINAC_ASSERT(is_a(oc)); - construct_from_epvector(*vp, do_index_renaming); + construct_from_epvector(std::move(vp), do_index_renaming); GINAC_ASSERT(is_canonical()); } @@ -208,59 +174,6 @@ void expairseq::do_print_tree(const print_tree & c, unsigned level) const overall_coeff.print(c, level + c.delta_indent); } c.s << std::string(level + c.delta_indent,' ') << "=====" << std::endl; -#if EXPAIRSEQ_USE_HASHTAB - c.s << std::string(level + c.delta_indent,' ') - << "hashtab size " << hashtabsize << std::endl; - if (hashtabsize == 0) return; -#define MAXCOUNT 5 - unsigned count[MAXCOUNT+1]; - for (int i=0; i 0) { - c.s << std::string(level + c.delta_indent, ' ') - << "bin " << i << " with entries "; - for (epplist::const_iterator it=hashtab[i].begin(); - it!=hashtab[i].end(); ++it) { - c.s << *it-seq.begin() << " "; - ++this_bin_fill; - } - c.s << std::endl; - cum_fill += this_bin_fill; - cum_fill_sq += this_bin_fill*this_bin_fill; - } - if (this_bin_fill0) - fact *= k; - double prob = std::pow(lambda,k)/fact * std::exp(-lambda); - cum_prob += prob; - c.s << std::string(level + c.delta_indent, ' ') << "bins with " << k << " entries: " - << int(1000.0*count[k]/hashtabsize)/10.0 << "% (expected: " - << int(prob*1000)/10.0 << ")" << std::endl; - } - c.s << std::string(level + c.delta_indent, ' ') << "bins with more entries: " - << int(1000.0*count[MAXCOUNT]/hashtabsize)/10.0 << "% (expected: " - << int((1-cum_prob)*1000)/10.0 << ")" << std::endl; - - c.s << std::string(level + c.delta_indent, ' ') << "variance: " - << 1.0/hashtabsize*cum_fill_sq-(1.0/hashtabsize*cum_fill)*(1.0/hashtabsize*cum_fill) - << std::endl; - c.s << std::string(level + c.delta_indent, ' ') << "average fill: " - << (1.0*cum_fill)/hashtabsize - << " (should be equal to " << (1.0*seq.size())/hashtabsize << ")" << std::endl; -#endif // EXPAIRSEQ_USE_HASHTAB } bool expairseq::info(unsigned inf) const @@ -306,24 +219,24 @@ ex expairseq::op(size_t i) const ex expairseq::map(map_function &f) const { - std::auto_ptr v(new epvector); - v->reserve(seq.size()+1); + epvector v; + v.reserve(seq.size()+1); epvector::const_iterator cit = seq.begin(), last = seq.end(); while (cit != last) { - v->push_back(split_ex_to_pair(f(recombine_pair_to_ex(*cit)))); + v.push_back(split_ex_to_pair(f(recombine_pair_to_ex(*cit)))); ++cit; } if (overall_coeff.is_equal(default_overall_coeff())) - return thisexpairseq(v, default_overall_coeff(), true); + return thisexpairseq(std::move(v), default_overall_coeff(), true); else { ex newcoeff = f(overall_coeff); if(is_a(newcoeff)) - return thisexpairseq(v, newcoeff, true); + return thisexpairseq(std::move(v), newcoeff, true); else { - v->push_back(split_ex_to_pair(newcoeff)); - return thisexpairseq(v, default_overall_coeff(), true); + v.push_back(split_ex_to_pair(newcoeff)); + return thisexpairseq(std::move(v), default_overall_coeff(), true); } } } @@ -333,12 +246,12 @@ ex expairseq::eval(int level) const { if ((level==1) && (flags &status_flags::evaluated)) return *this; - - std::auto_ptr vp = evalchildren(level); - if (vp.get() == 0) + + epvector evaled = evalchildren(level); + if (!evaled.empty()) + return (new expairseq(std::move(evaled), overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated); + else return this->hold(); - - return (new expairseq(vp, overall_coeff))->setflag(status_flags::dynallocated | status_flags::evaluated); } epvector* conjugateepvector(const epvector&epv) @@ -367,12 +280,15 @@ ex expairseq::conjugate() const { epvector* newepv = conjugateepvector(seq); ex x = overall_coeff.conjugate(); - if (!newepv && are_ex_trivially_equal(x, overall_coeff)) { + if (newepv) { + ex result = thisexpairseq(std::move(*newepv), x); + delete newepv; + return result; + } + if (are_ex_trivially_equal(x, overall_coeff)) { return *this; } - ex result = thisexpairseq(newepv ? *newepv : seq, x); - delete newepv; - return result; + return thisexpairseq(seq, x); } bool expairseq::match(const ex & pattern, exmap & repl_lst) const @@ -433,11 +349,11 @@ found: ; // it has already been matched before, in which case the matches // must be equal) size_t num = ops.size(); - std::auto_ptr vp(new epvector); - vp->reserve(num); + epvector vp; + vp.reserve(num); for (size_t i=0; ipush_back(split_ex_to_pair(ops[i])); - ex rest = thisexpairseq(vp, default_overall_coeff()); + vp.push_back(split_ex_to_pair(ops[i])); + ex rest = thisexpairseq(std::move(vp), default_overall_coeff()); for (exmap::const_iterator it = tmp_repl.begin(); it != tmp_repl.end(); ++it) { if (it->first.is_equal(global_wildcard)) { if (rest.is_equal(it->second)) { @@ -467,9 +383,9 @@ found: ; ex expairseq::subs(const exmap & m, unsigned options) const { - std::auto_ptr vp = subschildren(m, options); - if (vp.get()) - return ex_to(thisexpairseq(vp, overall_coeff, (options & subs_options::no_index_renaming) == 0)); + epvector subsed = subschildren(m, options); + if (!subsed.empty()) + return ex_to(thisexpairseq(std::move(subsed), overall_coeff, (options & subs_options::no_index_renaming) == 0)); else if ((options & subs_options::algebraic) && is_exactly_a(*this)) return static_cast(this)->algebraic_subs_mul(m, options); else @@ -494,54 +410,20 @@ int expairseq::compare_same_type(const basic &other) const if (cmpval!=0) return cmpval; -#if EXPAIRSEQ_USE_HASHTAB - GINAC_ASSERT(hashtabsize==o.hashtabsize); - if (hashtabsize==0) { -#endif // EXPAIRSEQ_USE_HASHTAB - epvector::const_iterator cit1 = seq.begin(); - epvector::const_iterator cit2 = o.seq.begin(); - epvector::const_iterator last1 = seq.end(); - epvector::const_iterator last2 = o.seq.end(); + epvector::const_iterator cit1 = seq.begin(); + epvector::const_iterator cit2 = o.seq.begin(); + epvector::const_iterator last1 = seq.end(); + epvector::const_iterator last2 = o.seq.end(); - for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) { - cmpval = (*cit1).compare(*cit2); - if (cmpval!=0) return cmpval; - } + for (; (cit1!=last1)&&(cit2!=last2); ++cit1, ++cit2) { + cmpval = (*cit1).compare(*cit2); + if (cmpval!=0) return cmpval; + } - GINAC_ASSERT(cit1==last1); - GINAC_ASSERT(cit2==last2); + GINAC_ASSERT(cit1==last1); + GINAC_ASSERT(cit2==last2); - return 0; -#if EXPAIRSEQ_USE_HASHTAB - } - - // compare number of elements in each hashtab entry - for (unsigned i=0; i0) { - const epplist &eppl1 = hashtab[i]; - const epplist &eppl2 = o.hashtab[i]; - epplist::const_iterator it1 = eppl1.begin(); - epplist::const_iterator it2 = eppl2.begin(); - while (it1!=eppl1.end()) { - cmpval = (*(*it1)).compare(*(*it2)); - if (cmpval!=0) - return cmpval; - ++it1; - ++it2; - } - } - } - - return 0; // equal -#endif // EXPAIRSEQ_USE_HASHTAB + return 0; } bool expairseq::is_equal_same_type(const basic &other) const @@ -556,56 +438,17 @@ bool expairseq::is_equal_same_type(const basic &other) const if (!overall_coeff.is_equal(o.overall_coeff)) return false; -#if EXPAIRSEQ_USE_HASHTAB - // compare number of elements in each hashtab entry - if (hashtabsize!=o.hashtabsize) { - std::cout << "this:" << std::endl; - print(print_tree(std::cout)); - std::cout << "other:" << std::endl; - other.print(print_tree(std::cout)); - } + epvector::const_iterator cit1 = seq.begin(); + epvector::const_iterator cit2 = o.seq.begin(); + epvector::const_iterator last1 = seq.end(); - GINAC_ASSERT(hashtabsize==o.hashtabsize); - - if (hashtabsize==0) { -#endif // EXPAIRSEQ_USE_HASHTAB - epvector::const_iterator cit1 = seq.begin(); - epvector::const_iterator cit2 = o.seq.begin(); - epvector::const_iterator last1 = seq.end(); - - while (cit1!=last1) { - if (!(*cit1).is_equal(*cit2)) return false; - ++cit1; - ++cit2; - } - - return true; -#if EXPAIRSEQ_USE_HASHTAB - } - - for (unsigned i=0; i0) { - const epplist &eppl1 = hashtab[i]; - const epplist &eppl2 = o.hashtab[i]; - epplist::const_iterator it1 = eppl1.begin(); - epplist::const_iterator it2 = eppl2.begin(); - while (it1!=eppl1.end()) { - if (!(*(*it1)).is_equal(*(*it2))) return false; - ++it1; - ++it2; - } - } - } - return true; -#endif // EXPAIRSEQ_USE_HASHTAB } unsigned expairseq::return_type() const @@ -620,11 +463,8 @@ unsigned expairseq::calchash() const const epvector::const_iterator end = seq.end(); while (i != end) { v ^= i->rest.gethash(); -#if !EXPAIRSEQ_USE_HASHTAB - // rotation spoils commutativity! v = rotate_left(v); v ^= i->coeff.gethash(); -#endif // !EXPAIRSEQ_USE_HASHTAB ++i; } @@ -641,13 +481,11 @@ unsigned expairseq::calchash() const ex expairseq::expand(unsigned options) const { - std::auto_ptr vp = expandchildren(options); - if (vp.get()) - return thisexpairseq(vp, overall_coeff); - else { - // The terms have not changed, so it is safe to declare this expanded - return (options == 0) ? setflag(status_flags::expanded) : *this; + epvector expanded = expandchildren(options); + if (!expanded.empty()) { + return thisexpairseq(std::move(expanded), overall_coeff); } + return (options == 0) ? setflag(status_flags::expanded) : *this; } ////////// @@ -661,7 +499,7 @@ ex expairseq::expand(unsigned options) const * has (at least) two possible different semantics but we want to inherit * methods thus avoiding code duplication. Sometimes a method in expairseq * has to create a new one of the same semantics, which cannot be done by a - * ctor because the name (add, mul,...) is unknown on the expaiseq level. In + * ctor because the name (add, mul,...) is unknown on the expairseq level. In * order for this trick to work a derived class must of course override this * definition. */ ex expairseq::thisexpairseq(const epvector &v, const ex &oc, bool do_index_renaming) const @@ -669,9 +507,9 @@ ex expairseq::thisexpairseq(const epvector &v, const ex &oc, bool do_index_renam return expairseq(v, oc, do_index_renaming); } -ex expairseq::thisexpairseq(std::auto_ptr vp, const ex &oc, bool do_index_renaming) const +ex expairseq::thisexpairseq(epvector && vp, const ex &oc, bool do_index_renaming) const { - return expairseq(vp, oc, do_index_renaming); + return expairseq(std::move(vp), oc, do_index_renaming); } void expairseq::printpair(const print_context & c, const expair & p, unsigned upper_precedence) const @@ -741,9 +579,6 @@ ex expairseq::recombine_pair_to_ex(const expair &p) const bool expairseq::expair_needs_further_processing(epp it) { -#if EXPAIRSEQ_USE_HASHTAB - //# error "FIXME: expair_needs_further_processing not yet implemented for hashtabs, sorry. A.F." -#endif // EXPAIRSEQ_USE_HASHTAB return false; } @@ -785,71 +620,31 @@ void expairseq::construct_from_2_ex_via_exvector(const ex &lh, const ex &rh) v.push_back(lh); v.push_back(rh); construct_from_exvector(v); -#if EXPAIRSEQ_USE_HASHTAB - GINAC_ASSERT((hashtabsize==0)||(hashtabsize>=minhashtabsize)); - GINAC_ASSERT(hashtabsize==calc_hashtabsize(seq.size())); -#endif // EXPAIRSEQ_USE_HASHTAB } void expairseq::construct_from_2_ex(const ex &lh, const ex &rh) { if (typeid(ex_to(lh)) == typeid(*this)) { if (typeid(ex_to(rh)) == typeid(*this)) { -#if EXPAIRSEQ_USE_HASHTAB - unsigned totalsize = ex_to(lh).seq.size() + - ex_to(rh).seq.size(); - if (calc_hashtabsize(totalsize)!=0) { - construct_from_2_ex_via_exvector(lh,rh); - } else { -#endif // EXPAIRSEQ_USE_HASHTAB - if (is_a(lh) && lh.info(info_flags::has_indices) && - rh.info(info_flags::has_indices)) { - ex newrh=rename_dummy_indices_uniquely(lh, rh); - construct_from_2_expairseq(ex_to(lh), - ex_to(newrh)); - } - else - construct_from_2_expairseq(ex_to(lh), - ex_to(rh)); -#if EXPAIRSEQ_USE_HASHTAB + if (is_a(lh) && lh.info(info_flags::has_indices) && + rh.info(info_flags::has_indices)) { + ex newrh=rename_dummy_indices_uniquely(lh, rh); + construct_from_2_expairseq(ex_to(lh), + ex_to(newrh)); } -#endif // EXPAIRSEQ_USE_HASHTAB + else + construct_from_2_expairseq(ex_to(lh), + ex_to(rh)); return; } else { -#if EXPAIRSEQ_USE_HASHTAB - unsigned totalsize = ex_to(lh).seq.size()+1; - if (calc_hashtabsize(totalsize)!=0) { - construct_from_2_ex_via_exvector(lh, rh); - } else { -#endif // EXPAIRSEQ_USE_HASHTAB - construct_from_expairseq_ex(ex_to(lh), rh); -#if EXPAIRSEQ_USE_HASHTAB - } -#endif // EXPAIRSEQ_USE_HASHTAB + construct_from_expairseq_ex(ex_to(lh), rh); return; } } else if (typeid(ex_to(rh)) == typeid(*this)) { -#if EXPAIRSEQ_USE_HASHTAB - unsigned totalsize=ex_to(rh).seq.size()+1; - if (calc_hashtabsize(totalsize)!=0) { - construct_from_2_ex_via_exvector(lh,rh); - } else { -#endif // EXPAIRSEQ_USE_HASHTAB - construct_from_expairseq_ex(ex_to(rh),lh); -#if EXPAIRSEQ_USE_HASHTAB - } -#endif // EXPAIRSEQ_USE_HASHTAB + construct_from_expairseq_ex(ex_to(rh),lh); return; } -#if EXPAIRSEQ_USE_HASHTAB - if (calc_hashtabsize(2)!=0) { - construct_from_2_ex_via_exvector(lh,rh); - return; - } - hashtabsize = 0; -#endif // EXPAIRSEQ_USE_HASHTAB - if (is_exactly_a(lh)) { if (is_exactly_a(rh)) { combine_overall_coeff(lh); @@ -939,7 +734,7 @@ void expairseq::construct_from_2_expairseq(const expairseq &s1, if (needs_further_processing) { epvector v = seq; seq.clear(); - construct_from_epvector(v); + construct_from_epvector(std::move(v)); } } @@ -1001,7 +796,7 @@ void expairseq::construct_from_expairseq_ex(const expairseq &s, if (needs_further_processing) { epvector v = seq; seq.clear(); - construct_from_epvector(v); + construct_from_epvector(std::move(v)); } } @@ -1013,12 +808,8 @@ void expairseq::construct_from_exvector(const exvector &v) // (same for (+,*) -> (*,^) make_flat(v); -#if EXPAIRSEQ_USE_HASHTAB - combine_same_terms(); -#else canonicalize(); combine_same_terms_sorted_seq(); -#endif // EXPAIRSEQ_USE_HASHTAB } void expairseq::construct_from_epvector(const epvector &v, bool do_index_renaming) @@ -1029,12 +820,20 @@ void expairseq::construct_from_epvector(const epvector &v, bool do_index_renamin // same for (+,*) -> (*,^) make_flat(v, do_index_renaming); -#if EXPAIRSEQ_USE_HASHTAB - combine_same_terms(); -#else canonicalize(); combine_same_terms_sorted_seq(); -#endif // EXPAIRSEQ_USE_HASHTAB +} + +void expairseq::construct_from_epvector(epvector &&v, bool do_index_renaming) +{ + // simplifications: +(a,+(b,c),d) -> +(a,b,c,d) (associativity) + // +(d,b,c,a) -> +(a,b,c,d) (canonicalization) + // +(...,x,*(x,c1),*(x,c2)) -> +(...,*(x,1+c1+c2)) (c1, c2 numeric) + // same for (+,*) -> (*,^) + + make_flat(std::move(v), do_index_renaming); + canonicalize(); + combine_same_terms_sorted_seq(); } /** Combine this expairseq with argument exvector. @@ -1126,13 +925,11 @@ void expairseq::make_flat(const epvector &v, bool do_index_renaming) ex newrest = mf.handle_factor(cit->rest, cit->coeff); const expairseq &subseqref = ex_to(newrest); combine_overall_coeff(ex_to(subseqref.overall_coeff), - ex_to(cit->coeff)); + ex_to(cit->coeff)); epvector::const_iterator cit_s = subseqref.seq.begin(); while (cit_s!=subseqref.seq.end()) { seq.push_back(expair(cit_s->rest, ex_to(cit_s->coeff).mul_dyn(ex_to(cit->coeff)))); - //seq.push_back(combine_pair_with_coeff_to_pair(*cit_s, - // (*cit).coeff)); ++cit_s; } } else { @@ -1203,319 +1000,10 @@ void expairseq::combine_same_terms_sorted_seq() if (needs_further_processing) { epvector v = seq; seq.clear(); - construct_from_epvector(v); + construct_from_epvector(std::move(v)); } } -#if EXPAIRSEQ_USE_HASHTAB - -unsigned expairseq::calc_hashtabsize(unsigned sz) const -{ - unsigned size; - unsigned nearest_power_of_2 = 1 << log2(sz); - // if (nearest_power_of_2 < maxhashtabsize/hashtabfactor) { - // size = nearest_power_of_2*hashtabfactor; - size = nearest_power_of_2/hashtabfactor; - if (size(e)) { - hashindex = hashmask; - } else { - hashindex = e.gethash() & hashmask; - // last hashtab entry is reserved for numerics - if (hashindex==hashmask) hashindex = 0; - } - GINAC_ASSERT((hashindexrest); - epplist &eppl = hashtab[hashindex]; - epplist::iterator epplit = eppl.begin(); - bool erased = false; - while (epplit!=eppl.end()) { - if (*epplit == element) { - eppl.erase(epplit); - erased = true; - break; - } - ++epplit; - } - GINAC_ASSERT(erased); - } - GINAC_ASSERT(erased); -} - -void expairseq::move_hashtab_entry(epvector::const_iterator oldpos, - epvector::iterator newpos) -{ - GINAC_ASSERT(hashtabsize!=0); - - // calculate hashindex of element which was moved - unsigned hashindex=calc_hashindex((*newpos).rest); - - // find it in hashtab and modify it - epplist &eppl = hashtab[hashindex]; - epplist::iterator epplit = eppl.begin(); - while (epplit!=eppl.end()) { - if (*epplit == oldpos) { - *epplit = newpos; - break; - } - ++epplit; - } - GINAC_ASSERT(epplit!=eppl.end()); -} - -void expairseq::sorted_insert(epplist &eppl, epvector::const_iterator elem) -{ - epplist::const_iterator current = eppl.begin(); - while ((current!=eppl.end()) && ((*current)->is_less(*elem))) { - ++current; - } - eppl.insert(current,elem); -} - -void expairseq::build_hashtab_and_combine(epvector::iterator &first_numeric, - epvector::iterator &last_non_zero, - std::vector &touched, - unsigned &number_of_zeroes) -{ - epp current = seq.begin(); - - while (current!=first_numeric) { - if (is_exactly_a(current->rest)) { - --first_numeric; - iter_swap(current,first_numeric); - } else { - // calculate hashindex - unsigned currenthashindex = calc_hashindex(current->rest); - - // test if there is already a matching expair in the hashtab-list - epplist &eppl=hashtab[currenthashindex]; - epplist::iterator epplit = eppl.begin(); - while (epplit!=eppl.end()) { - if (current->rest.is_equal((*epplit)->rest)) - break; - ++epplit; - } - if (epplit==eppl.end()) { - // no matching expair found, append this to end of list - sorted_insert(eppl,current); - ++current; - } else { - // epplit points to a matching expair, combine it with current - (*epplit)->coeff = ex_to((*epplit)->coeff). - add_dyn(ex_to(current->coeff)); - - // move obsolete current expair to end by swapping with last_non_zero element - // if this was a numeric, it is swapped with the expair before first_numeric - iter_swap(current,last_non_zero); - --first_numeric; - if (first_numeric!=last_non_zero) iter_swap(first_numeric,current); - --last_non_zero; - ++number_of_zeroes; - // test if combined term has coeff 0 and can be removed is done later - touched[(*epplit)-seq.begin()] = true; - } - } - } -} - -void expairseq::drop_coeff_0_terms(epvector::iterator &first_numeric, - epvector::iterator &last_non_zero, - std::vector &touched, - unsigned &number_of_zeroes) -{ - // move terms with coeff 0 to end and remove them from hashtab - // check only those elements which have been touched - epp current = seq.begin(); - size_t i = 0; - while (current!=first_numeric) { - if (!touched[i]) { - ++current; - ++i; - } else if (!ex_to((*current).coeff).is_zero()) { - ++current; - ++i; - } else { - remove_hashtab_entry(current); - - // move element to the end, unless it is already at the end - if (current!=last_non_zero) { - iter_swap(current,last_non_zero); - --first_numeric; - bool numeric_swapped = first_numeric!=last_non_zero; - if (numeric_swapped) - iter_swap(first_numeric,current); - epvector::iterator changed_entry; - - if (numeric_swapped) - changed_entry = first_numeric; - else - changed_entry = last_non_zero; - - --last_non_zero; - ++number_of_zeroes; - - if (first_numeric!=current) { - - // change entry in hashtab which referred to first_numeric or last_non_zero to current - move_hashtab_entry(changed_entry,current); - touched[current-seq.begin()] = touched[changed_entry-seq.begin()]; - } - } else { - --first_numeric; - --last_non_zero; - ++number_of_zeroes; - } - } - } - GINAC_ASSERT(i==current-seq.begin()); -} - -/** True if one of the coeffs vanishes, otherwise false. - * This would be an invariant violation, so this should only be used for - * debugging purposes. */ -bool expairseq::has_coeff_0() const -{ - epvector::const_iterator i = seq.begin(), end = seq.end(); - while (i != end) { - if (i->coeff.is_zero()) - return true; - ++i; - } - return false; -} - -void expairseq::add_numerics_to_hashtab(epvector::iterator first_numeric, - epvector::const_iterator last_non_zero) -{ - if (first_numeric == seq.end()) return; // no numerics - - epvector::const_iterator current = first_numeric, last = last_non_zero + 1; - while (current != last) { - sorted_insert(hashtab[hashmask], current); - ++current; - } -} - -void expairseq::combine_same_terms() -{ - // combine same terms, drop term with coeff 0, move numerics to end - - // calculate size of hashtab - hashtabsize = calc_hashtabsize(seq.size()); - - // hashtabsize is a power of 2 - hashmask = hashtabsize-1; - - // allocate hashtab - hashtab.clear(); - hashtab.resize(hashtabsize); - - if (hashtabsize==0) { - canonicalize(); - combine_same_terms_sorted_seq(); - GINAC_ASSERT(!has_coeff_0()); - return; - } - - // iterate through seq, move numerics to end, - // fill hashtab and combine same terms - epvector::iterator first_numeric = seq.end(); - epvector::iterator last_non_zero = seq.end()-1; - - size_t num = seq.size(); - std::vector touched(num); - - unsigned number_of_zeroes = 0; - - GINAC_ASSERT(!has_coeff_0()); - build_hashtab_and_combine(first_numeric,last_non_zero,touched,number_of_zeroes); - - // there should not be any terms with coeff 0 from the beginning, - // so it should be safe to skip this step - if (number_of_zeroes!=0) { - drop_coeff_0_terms(first_numeric,last_non_zero,touched,number_of_zeroes); - } - - add_numerics_to_hashtab(first_numeric,last_non_zero); - - // pop zero elements - for (unsigned i=0; i 0) return 1; // not canoncalized -#endif // EXPAIRSEQ_USE_HASHTAB - epvector::const_iterator it = seq.begin(), itend = seq.end(); epvector::const_iterator it_last = it; for (++it; it!=itend; it_last=it, ++it) { @@ -1554,13 +1038,12 @@ bool expairseq::is_canonical() const return 1; } - /** Member-wise expand the expairs in this sequence. * * @see expairseq::expand() - * @return pointer to epvector containing expanded pairs or zero pointer, - * if no members were changed. */ -std::auto_ptr expairseq::expandchildren(unsigned options) const + * @return epvector containing expanded pairs, empty if no members + * had to be changed. */ +epvector expairseq::expandchildren(unsigned options) const { const epvector::const_iterator last = seq.end(); epvector::const_iterator cit = seq.begin(); @@ -1569,25 +1052,25 @@ std::auto_ptr expairseq::expandchildren(unsigned options) const if (!are_ex_trivially_equal(cit->rest,expanded_ex)) { // something changed, copy seq, eval and return it - std::auto_ptr s(new epvector); - s->reserve(seq.size()); + epvector s; + s.reserve(seq.size()); // copy parts of seq which are known not to have changed epvector::const_iterator cit2 = seq.begin(); while (cit2!=cit) { - s->push_back(*cit2); + s.push_back(*cit2); ++cit2; } // copy first changed element - s->push_back(combine_ex_with_coeff_to_pair(expanded_ex, - cit2->coeff)); + s.push_back(combine_ex_with_coeff_to_pair(expanded_ex, + cit2->coeff)); ++cit2; // copy rest while (cit2!=last) { - s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.expand(options), - cit2->coeff)); + s.push_back(combine_ex_with_coeff_to_pair(cit2->rest.expand(options), + cit2->coeff)); ++cit2; } return s; @@ -1595,23 +1078,19 @@ std::auto_ptr expairseq::expandchildren(unsigned options) const ++cit; } - return std::auto_ptr(0); // signalling nothing has changed + return epvector(); // empty signalling nothing has changed } /** Member-wise evaluate the expairs in this sequence. * * @see expairseq::eval() - * @return pointer to epvector containing evaluated pairs or zero pointer, - * if no members were changed. */ -std::auto_ptr expairseq::evalchildren(int level) const + * @return epvector containing evaluated pairs, empty if no members + * had to be changed. */ +epvector expairseq::evalchildren(int level) const { - // returns a NULL pointer if nothing had to be evaluated - // returns a pointer to a newly created epvector otherwise - // (which has to be deleted somewhere else) - if (level==1) - return std::auto_ptr(0); + return epvector(); // nothing had to be evaluated if (level == -max_recursion_level) throw(std::runtime_error("max recursion level reached")); @@ -1620,45 +1099,45 @@ std::auto_ptr expairseq::evalchildren(int level) const epvector::const_iterator last = seq.end(); epvector::const_iterator cit = seq.begin(); while (cit!=last) { - const ex &evaled_ex = cit->rest.eval(level); + const ex evaled_ex = cit->rest.eval(level); if (!are_ex_trivially_equal(cit->rest,evaled_ex)) { // something changed, copy seq, eval and return it - std::auto_ptr s(new epvector); - s->reserve(seq.size()); + epvector s; + s.reserve(seq.size()); // copy parts of seq which are known not to have changed epvector::const_iterator cit2=seq.begin(); while (cit2!=cit) { - s->push_back(*cit2); + s.push_back(*cit2); ++cit2; } // copy first changed element - s->push_back(combine_ex_with_coeff_to_pair(evaled_ex, - cit2->coeff)); + s.push_back(combine_ex_with_coeff_to_pair(evaled_ex, + cit2->coeff)); ++cit2; // copy rest while (cit2!=last) { - s->push_back(combine_ex_with_coeff_to_pair(cit2->rest.eval(level), - cit2->coeff)); + s.push_back(combine_ex_with_coeff_to_pair(cit2->rest.eval(level), + cit2->coeff)); ++cit2; } - return s; + return std::move(s); } ++cit; } - - return std::auto_ptr(0); // signalling nothing has changed + + return epvector(); // signalling nothing has changed } /** Member-wise substitute in this sequence. * * @see expairseq::subs() - * @return pointer to epvector containing pairs after application of subs, - * or NULL pointer if no members were changed. */ -std::auto_ptr expairseq::subschildren(const exmap & m, unsigned options) const + * @return epvector containing expanded pairs, empty if no members + * had to be changed. */ +epvector expairseq::subschildren(const exmap & m, unsigned options) const { // When any of the objects to be substituted is a product or power // we have to recombine the pairs because the numeric coefficients may @@ -1687,19 +1166,19 @@ std::auto_ptr expairseq::subschildren(const exmap & m, unsigned option if (!are_ex_trivially_equal(orig_ex, subsed_ex)) { // Something changed, copy seq, subs and return it - std::auto_ptr s(new epvector); - s->reserve(seq.size()); + epvector s; + s.reserve(seq.size()); // Copy parts of seq which are known not to have changed - s->insert(s->begin(), seq.begin(), cit); + s.insert(s.begin(), seq.begin(), cit); // Copy first changed element - s->push_back(split_ex_to_pair(subsed_ex)); + s.push_back(split_ex_to_pair(subsed_ex)); ++cit; // Copy rest while (cit != last) { - s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(m, options))); + s.push_back(split_ex_to_pair(recombine_pair_to_ex(*cit).subs(m, options))); ++cit; } return s; @@ -1718,19 +1197,19 @@ std::auto_ptr expairseq::subschildren(const exmap & m, unsigned option if (!are_ex_trivially_equal(cit->rest, subsed_ex)) { // Something changed, copy seq, subs and return it - std::auto_ptr s(new epvector); - s->reserve(seq.size()); + epvector s; + s.reserve(seq.size()); // Copy parts of seq which are known not to have changed - s->insert(s->begin(), seq.begin(), cit); + s.insert(s.begin(), seq.begin(), cit); // Copy first changed element - s->push_back(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff)); + s.push_back(combine_ex_with_coeff_to_pair(subsed_ex, cit->coeff)); ++cit; // Copy rest while (cit != last) { - s->push_back(combine_ex_with_coeff_to_pair(cit->rest.subs(m, options), cit->coeff)); + s.push_back(combine_ex_with_coeff_to_pair(cit->rest.subs(m, options), cit->coeff)); ++cit; } return s; @@ -1741,17 +1220,11 @@ std::auto_ptr expairseq::subschildren(const exmap & m, unsigned option } // Nothing has changed - return std::auto_ptr(0); + return epvector(); } ////////// // static member variables ////////// -#if EXPAIRSEQ_USE_HASHTAB -unsigned expairseq::maxhashtabsize = 0x4000000U; -unsigned expairseq::minhashtabsize = 0x1000U; -unsigned expairseq::hashtabfactor = 1; -#endif // EXPAIRSEQ_USE_HASHTAB - } // namespace GiNaC