Compile with float/double again.
[ipdf/code.git] / src / paranoidnumber.cpp
index 53729a3..60802eb 100644 (file)
@@ -3,17 +3,46 @@
 #include <sstream>
 #include <fenv.h>
 #include "log.h"
+#include <cassert>
 #include <iostream>
 
+// here be many copy paste bugs
+
 using namespace std;
 namespace IPDF
 {
-int64_t ParanoidNumber::g_count = 0;
 
-ParanoidNumber::ParanoidNumber(const char * str) : m_value(0), m_op(ADD), m_next_term(NULL), m_next_factor(NULL)
+
+#ifdef PARANOID_USE_ARENA
+ParanoidNumber::Arena ParanoidNumber::g_arena;
+#endif //PARANOID_USE_ARENA
+
+ParanoidNumber::~ParanoidNumber()
+{
+       for (int i = 0; i < NOP; ++i)
+       {
+               for (auto n : m_next[i])
+                       delete n;
+       }
+}
+
+ParanoidNumber::ParanoidNumber(const string & str) : m_value(0), m_next()
 {
+       #ifdef PARANOID_SIZE_LIMIT
+               m_size = 1;
+       #endif
+       #ifdef PARANOID_CACHE_RESULTS
+       m_cache_valid = false;
+       #endif
+       
        int dp = 0;
        int end = 0;
+       bool negate = str[0] == '-';
+       if (negate)
+       {
+               dp++;
+               end++;
+       }
        while (str[dp] != '\0' && str[dp] != '.')
        {
                ++dp;
@@ -21,16 +50,12 @@ ParanoidNumber::ParanoidNumber(const char * str) : m_value(0), m_op(ADD), m_next
        }
        while (str[end] != '\0')
                ++end;
-               
        ParanoidNumber m(1);
-       for (int i = dp-1; i >= 0; --i)
+       for (int i = dp-1; i >= negate; --i)
        {
                ParanoidNumber b(str[i]-'0');
                b*=m;
-               //Debug("m is %s", m.Str().c_str());
-               //Debug("Add %s", b.Str().c_str());
                this->operator+=(b);
-               //Debug("Now at %s", Str().c_str());
                m*=10;
        }
        ParanoidNumber n(1);
@@ -38,414 +63,943 @@ ParanoidNumber::ParanoidNumber(const char * str) : m_value(0), m_op(ADD), m_next
        {
                n/=10;
                ParanoidNumber b(str[i]-'0');
-               //Debug("%s * %s", b.Str().c_str(), n.Str().c_str());
                b*=n;
-               //Debug("b -> %s", b.Str().c_str());
-               //Debug("Add %s", b.Str().c_str());
                this->operator+=(b);
-               //Debug("Now at %s", Str().c_str());
-
        }
-       //Debug("Constructed {%s} from %s (%f)", Str().c_str(), str, ToDouble());       
+       
+       if (negate)
+               Negate();
+       
+       #ifdef PARANOID_COMPARE_EPSILON
+               double d = strtod(str.c_str(), NULL);
+               CompareForSanity(d, d);
+       #endif
 }
 
 ParanoidNumber & ParanoidNumber::operator=(const ParanoidNumber & a)
 {
-       //TODO: Optimise
-       delete m_next_term;
-       delete m_next_factor;
-       m_op = a.m_op;
-       if (a.m_next_term != NULL)
-       {
-               m_next_term = new ParanoidNumber(*(a.m_next_term));
-       }
-       if (a.m_next_factor != NULL)
+       //assert(this != NULL);
+       
+       #ifdef PARANOID_SIZE_LIMIT
+               m_size = a.m_size;
+       #endif
+       
+       m_value = a.m_value;
+       #ifdef PARANOID_CACHE_RESULTS
+       m_cached_result = a.m_cached_result;
+       m_cache_valid = a.m_cache_valid;
+       #endif
+       for (int i = 0; i < NOP; ++i)
        {
-               m_next_factor = new ParanoidNumber(*(a.m_next_factor));
+               for (auto n : m_next[i])
+                       delete n;
+               m_next[i].clear();
+               for (auto n : a.m_next[i])
+                       m_next[i].push_back(new ParanoidNumber(*n));
        }
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(a.Digit(),a.Digit());
+       #endif
        return *this;
 }
 
-ParanoidNumber & ParanoidNumber::operator+=(const ParanoidNumber & a)
+
+string ParanoidNumber::Str() const
 {
-       if (m_next_factor == NULL && a.Floating())
+       
+       //assert(this != NULL);
+       string result("");
+       stringstream s;
+       s << (double)m_value;
+       result += s.str();
+       for (auto mul : m_next[MULTIPLY])
        {
-               if (ParanoidOp<digit_t>(m_value, a.m_value, ADD))
-               {
-                       Simplify();
-                       return *this;
-               }
+               result += "*";
+               if (!mul->Floating())
+                       result += "(" + mul->Str() + ")";
+               else
+                       result += mul->Str();
        }
-       ParanoidNumber * nt = m_next_term;
-       ParanoidNumber * nf = m_next_factor;
+       for (auto div : m_next[DIVIDE])
+       {
+               result += "/";
+               if (!div->Floating())
+                       result += "(" + div->Str() + ")";
+               else
+                       result += div->Str();
+       }       
        
-       ParanoidNumber ca(a);
-       if (m_next_factor != NULL)
+       for (auto add : m_next[ADD])
        {
-               if (m_next_factor->m_op == MULTIPLY)
-                       ca /= (*m_next_factor);
+               result += "+";
+               if (!add->Floating())
+                       result += "(" + add->Str() + ")";
                else
-                       ca *= (*m_next_factor);
-                       
-               if (ca.Floating())
-               {
-                       m_next_factor = NULL;
-                       m_next_term = NULL;
-                       operator+=(ca);
-                       m_next_factor = nf;
-                       m_next_term = nt;
-                       Simplify();
-                       return *this;
-               }
-               
+                       result += add->Str();
+       }
+       for (auto sub : m_next[SUBTRACT])
+       {
+               result += "-";
+               if (!sub->Floating())
+                       result += "(" + sub->Str() + ")";
+               else
+                       result += sub->Str();
        }
        
-       m_next_term = new ParanoidNumber(a, ADD);
-       ParanoidNumber * t = m_next_term;
-       while (t->m_next_term != NULL)
-               t = t->m_next_term;
-       t->m_next_term = nt;
-       //Debug("Simplify {%s} after add", Str().c_str());
-       Simplify();
-       return *this;
+
+       return result;
 }
 
-ParanoidNumber & ParanoidNumber::operator-=(const ParanoidNumber & a)
+template <>
+bool TrustingOp<float>(float & a, const float & b, Optype op)
 {
-       // this = v + t + (a)
-       // -> v + (a) + t
-       if (m_next_factor == NULL && a.Floating())
+
+       
+       feclearexcept(FE_ALL_EXCEPT);
+       switch (op)
        {
-               if (ParanoidOp<digit_t>(m_value, a.m_value, ADD))
-               {
-                       Simplify();
-                       return *this;
-               }
+               case ADD:
+                       a += b;
+                       break;
+               case SUBTRACT:
+                       a -= b;
+                       break;
+               case MULTIPLY:
+                       a *= b;
+                       break;
+               case DIVIDE:
+                       if (b == 0)
+                       {
+                               a = (a >= 0) ? INFINITY : -INFINITY;
+                               return false;
+                       }
+                       
+                       a /= b;
+                       break;
+               case NOP:
+                       break;
        }
+       return !fetestexcept(FE_ALL_EXCEPT);
+}
+
+template <>
+bool TrustingOp<double>(double & a, const double & b, Optype op)
+{
+       feclearexcept(FE_ALL_EXCEPT);
+       switch (op)
+       {
+               case ADD:
+                       a += b;
+                       break;
+               case SUBTRACT:
+                       a -= b;
+                       break;
+               case MULTIPLY:
+                       a *= b;
+                       break;
+               case DIVIDE:
+                       if (b == 0)
+                       {
+                               a = (a >= 0) ? INFINITY : -INFINITY;
+                               return false;
+                       }
+                       a /= b;
+                       break;
+               case NOP:
+                       break;
+       }
+       return !fetestexcept(FE_ALL_EXCEPT);
+}
+
+
+
+template <>
+bool TrustingOp<long double>(long double & a, const long double & b, Optype op)
+{
 
-       ParanoidNumber * nt = m_next_term;
-       ParanoidNumber * nf = m_next_factor;
        
-       ParanoidNumber ca(a, SUBTRACT);
-       if (m_next_factor != NULL)
+       feclearexcept(FE_ALL_EXCEPT);
+       switch (op)
        {
-               if (m_next_factor->m_op == MULTIPLY)
-                       ca /= (*m_next_factor);
-               else
-                       ca *= (*m_next_factor);
+               case ADD:
+                       a += b;
+                       break;
+               case SUBTRACT:
+                       a -= b;
+                       break;
+               case MULTIPLY:
+                       a *= b;
+                       break;
+               case DIVIDE:
+                       if (b == 0)
+                       {
+                               a = (a >= 0) ? INFINITY : -INFINITY;
+                               return false;
+                       }
                        
-               if (ca.Floating())
-               {
-                       m_next_factor = NULL;
-                       m_next_term = NULL;
-                       operator-=(ca);
-                       m_next_factor = nf;
-                       m_next_term = nt;
-                       Simplify();
-                       return *this;
-               }
-               
+                       a /= b;
+                       break;
+               case NOP:
+                       break;
        }
-       
-       m_next_term = new ParanoidNumber(a,SUBTRACT);
-       ParanoidNumber * t = m_next_term;
-       while (t->m_next_term != NULL)
+       return !fetestexcept(FE_ALL_EXCEPT);
+}
+
+
+template <>
+bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op)
+{
+       int16_t sa(a);
+       bool exact = true;
+       switch (op)
        {
-               t->m_op = SUBTRACT;
-               t = t->m_next_term;
+               case ADD:
+                       sa += b;
+                       exact = (abs(sa) <= 127);
+                       break;
+               case SUBTRACT:
+                       sa -= b;
+                       exact = (abs(sa) <= 127);
+                       break;
+               case MULTIPLY:
+                       sa *= b;
+                       exact = (abs(sa) <= 127);
+                       break;
+               case DIVIDE:
+                       exact = (b != 0 && sa > b && sa % b == 0);
+                       sa /= b;
+                       break;
+               case NOP:
+                       break;
        }
-       t->m_op = SUBTRACT;
-       //Debug("next term {%s}", m_next_term->Str().c_str());
-       t->m_next_term = nt;
-       //Debug("Simplify {%s} after sub", Str().c_str());
-       Simplify();
+       a = (int8_t)(sa);
+       return exact;
+}
+
+
+ParanoidNumber & ParanoidNumber::operator+=(const digit_t & a)
+{
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare += a;
+       #endif
+       delete Operation(new ParanoidNumber(a), ADD);
+       Simplify(SUBTRACT);
+       Simplify(ADD);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a);
+       #endif
        return *this;
 }
 
-ParanoidNumber & ParanoidNumber::operator*=(const ParanoidNumber & a)
+
+ParanoidNumber & ParanoidNumber::operator-=(const digit_t & a)
 {
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare -= a;
+       #endif
+       delete Operation(new ParanoidNumber(a), SUBTRACT);
+       Simplify(ADD);
+       Simplify(SUBTRACT);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a);
+       #endif
+       return *this;
+}
 
-       //if (m_value == 0)
-       //              return *this;
-       //Debug("{%s} *= {%s}", Str().c_str(), a.Str().c_str());
-       // this = (vf + t) * (a)
-       if (a.Floating() && ParanoidOp<digit_t>(m_value, a.m_value, MULTIPLY))
-       {
-               if (m_next_term != NULL)
-                       m_next_term->operator*=(a);
-               Simplify();
-               return *this;
-       }
-       
-       ParanoidNumber * t = this;
-       while (t->m_next_factor != NULL)
-               t = t->m_next_factor;
-       t->m_next_factor = new ParanoidNumber(a, MULTIPLY);
+ParanoidNumber & ParanoidNumber::operator*=(const digit_t & a)
+{
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare *= a;
+       #endif
+       delete Operation(new ParanoidNumber(a), MULTIPLY);
+       Simplify(DIVIDE);
+       Simplify(MULTIPLY);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a);
+       #endif
+       return *this;
+}
 
-       if (m_next_term != NULL)
-               m_next_term->operator*=(a);
 
-       //Debug("Simplify {%s}", Str().c_str());
-       Simplify();
-       //Debug("Simplified to {%s}", Str().c_str());
+ParanoidNumber & ParanoidNumber::operator/=(const digit_t & a)
+{
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare /= a;
+       #endif
+       delete Operation(new ParanoidNumber(a), DIVIDE);
+       Simplify(MULTIPLY);
+       Simplify(DIVIDE);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a);
+       #endif
+       return *this;
+}
+
+
+ParanoidNumber & ParanoidNumber::operator+=(const ParanoidNumber & a)
+{
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare += a.Digit();
+       #endif
+       delete Operation(new ParanoidNumber(a), ADD);
+       Simplify(SUBTRACT);
+       Simplify(ADD);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a.Digit());
+       #endif
+       return *this;
+}
+
+
+ParanoidNumber & ParanoidNumber::operator-=(const ParanoidNumber & a)
+{
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare -= a.Digit();
+       #endif
+       delete Operation(new ParanoidNumber(a), SUBTRACT);
+       Simplify(ADD);
+       Simplify(SUBTRACT);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a.Digit());
+       #endif
+       return *this;
+}
+
+ParanoidNumber & ParanoidNumber::operator*=(const ParanoidNumber & a)
+{
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare *= a.Digit();
+       #endif
+       delete Operation(new ParanoidNumber(a), MULTIPLY);
+       Simplify(DIVIDE);
+       Simplify(MULTIPLY);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a.Digit());
+       #endif
        return *this;
 }
 
 
 ParanoidNumber & ParanoidNumber::operator/=(const ParanoidNumber & a)
 {
+       #ifdef PARANOID_COMPARE_EPSILON
+               digit_t compare = Digit();
+               compare /= a.Digit();
+       #endif
+       delete Operation(new ParanoidNumber(a), DIVIDE);
+       Simplify(MULTIPLY);
+       Simplify(DIVIDE);
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(compare, a.Digit());
+       #endif
+       return *this;
+}
+
+ParanoidNumber & ParanoidNumber::operator=(const digit_t & a)
+{
+
+       for (int i = 0; i < NOP; ++i)
+       {
+               for (auto n : m_next[i])
+                       delete n;
+               m_next[i].clear();
+       }
+       m_value = a;
+       #ifdef PARANOID_CACHE_RESULTS
+       m_cached_result = a;
+       m_cache_valid = true;
+       #endif
+
+       #ifdef PARANOID_COMPARE_EPSILON
+               CompareForSanity(a,a);
+       #endif
+       
+       return *this;
+}
+
+// a + b
+ParanoidNumber * ParanoidNumber::OperationTerm(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point, Optype * merge_op)
+{
+       ////assert(b->SanityCheck());
+       #ifdef PARANOID_CACHE_RESULTS
+       m_cache_valid = false;
+       #endif
+       #ifdef PARANOID_SIZE_LIMIT
+               if (m_size + b->m_size >= PARANOID_SIZE_LIMIT)
+               {
+                       this->operator=(this->Digit());
+                       if (op == ADD)
+                               m_value += b->Digit();
+                       else
+                               m_value -= b->Digit();
+                       m_size = 1;
+                       #ifdef PARANOID_CACHE_RESULTS
+                               m_cached_result = m_value;
+                               m_cache_valid = true;
+                       #endif
+                       return b;
+               }
+               //Debug("At size limit %d", m_size);
+       #endif 
+       
+
+       if (Floating() && m_value == 0) // 0 + b = b
+       {
+               m_value = b->m_value;
+               if (op == SUBTRACT)
+               {
+                       m_value = -m_value;
+                       swap(b->m_next[ADD], b->m_next[SUBTRACT]);
+               }
+               
+               for (int i = 0; i < NOP; ++i)
+               {
+                       m_next[i] = b->m_next[i];
+                       b->m_next[i].clear();
+               }
+               
+               //assert(SanityCheck());
+               return b;
+       }
+       if (b->Floating() && b->m_value == 0) // a + 0 = a
+               return b;
                
 
+       
+       if ((NoFactors() && b->NoFactors())
+               || (GetFactors() == b->GetFactors()))
+       {
+               if (ParanoidOp<digit_t>(m_value, b->m_value, op))
+               {
+                       Optype addop = (op == ADD) ? ADD : SUBTRACT;
+                       for (auto add : b->m_next[ADD])
+                       {
+                               delete (OperationTerm(add, addop));
+                       }
+                       Optype subop = (op == ADD) ? SUBTRACT : ADD;
+                       for (auto sub : b->m_next[SUBTRACT])
+                               delete (OperationTerm(sub, subop));
+                               
+                       b->m_next[ADD].clear();
+                       b->m_next[SUBTRACT].clear();
+                       //assert(SanityCheck());
+                       return b;
+               }
+       }
+
+       
+       
+       bool parent = (merge_point == NULL);
+       ParanoidNumber * merge = this;
+       Optype mop = op;
+       //assert(mop != NOP); // silence compiler warning
+       if (parent)
+       {
+               merge_point = &merge;
+               merge_op = &mop;
+       }
+       else
+       {
+               merge = *merge_point;
+               mop = *merge_op;
+       }
                
-       if (a.Floating() && ParanoidOp<digit_t>(m_value, a.m_value, DIVIDE))
+       Optype invop = InverseOp(op); // inverse of p
+       Optype fwd = op;
+       Optype rev = invop;
+       if (op == SUBTRACT)
        {
-               if (m_next_term != NULL)
-                       m_next_term->operator/=(a);
-               Simplify();
-               return *this;
+               fwd = ADD;
+               rev = SUBTRACT;
        }
        
-       //Debug("Called %s /= %s", Str().c_str(), a.Str().c_str());
-       // this = (vf + t) * (a)
-       ParanoidNumber * t = this;
-       while (t->m_next_factor != NULL)
+       for (auto prev : m_next[invop])
        {
-               t = t->m_next_factor;
+               if (prev->OperationTerm(b, rev, merge_point, merge_op) == b)
+               {
+                       //assert(SanityCheck());
+                       return b;
+               }
+               
        }
-       t->m_next_factor = new ParanoidNumber(a, DIVIDE);
-
-       if (m_next_term != NULL)
-               m_next_term->operator/=(a);
+       for (auto next : m_next[op])
+       {
+               if (next->OperationTerm(b, fwd, merge_point, merge_op) == b)
+               {
+                       //assert(SanityCheck());
+                       return b;
+               }
+       }
+       
 
-       Simplify();
-       return *this;
-}
+       
+       
+       if (parent)
+       {
+               //merge->m_next[*merge_op].push_back(b);
+               m_next[op].push_back(b);
+               #ifdef PARANOID_SIZE_LIMIT
+                       m_size += b->m_size;
+               #endif  
+       }
+       else
+       {
+               if (m_next[op].size() == 0)
+               {
+                       *merge_point = this;
+                       *merge_op = op;
+               }
+       }
 
+       //assert(SanityCheck());
 
+       return NULL;
+}
 
-void ParanoidNumber::SimplifyTerms()
-{ 
+ParanoidNumber * ParanoidNumber::OperationFactor(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point, Optype * merge_op)
+{
+       #ifdef PARANOID_CACHE_RESULTS
+       m_cache_valid = false;
+       #endif
+       #ifdef PARANOID_SIZE_LIMIT
+               if (m_size + b->m_size >= PARANOID_SIZE_LIMIT)
+               {
+                       this->operator=(this->Digit());
+                       if (op == MULTIPLY)
+                               m_value *= b->Digit();
+                       else
+                               m_value /= b->Digit();
+                       m_size = 1;
+                       #ifdef PARANOID_CACHE_RESULTS
+                               m_cached_result = m_value;
+                               m_cache_valid = true;
+                       #endif
+                       //Debug("Cut off %p", this);
+                       return b;
+                       
+               }
+       #endif  
 
-       //Debug("Simplify {%s}", Str().c_str()); 
-       if (m_next_term == NULL)
+       if (Floating() && m_value == 0)
        {
-               //Debug("No terms!");
-               return;
+               return b;
        }
-
-       for (ParanoidNumber * a = this; a != NULL; a = a->m_next_term)
+       
+       if (Floating() && m_value == 1 && op == MULTIPLY)
        {
-               ParanoidNumber * b = a->m_next_term;
-               if (a->m_next_factor != NULL && !a->m_next_factor->Floating())
+               m_value = b->m_value;
+               for (int i = 0; i < NOP; ++i)
                {
-                       continue;
+                       for (auto n : m_next[i])
+                               delete (n);
+                       m_next[i].clear();
+                       swap(m_next[i], b->m_next[i]);
                }
+               //assert(SanityCheck());
+               return b;
+       }
+       if (b->Floating() && b->m_value == 1)
+               return b;
+       if (b->Floating() && b->m_value == 0 && op == MULTIPLY)
+       {
+               operator=(*b);
+               return b;
+       }
+
                
-               ParanoidNumber * bprev = a;
-               while (b != NULL)
+       if (NoTerms() && b->NoTerms())
+       {
+               if (ParanoidOp<digit_t>(m_value, b->m_value, op))
                {
-                       //Debug("Simplify factors of %s", b->Str().c_str());
-                       b->SimplifyFactors();
-                       if (b->m_next_factor != NULL && !b->m_next_factor->Floating())
-                       {
-                               bprev = b;
-                               b = b->m_next_term;
-                               continue;
-                       }
-                       
-                       bool simplify = false;
-                       if (a->m_next_factor != NULL || b->m_next_factor != NULL)
+                       Optype mulop = (op == MULTIPLY) ? MULTIPLY : DIVIDE;
+                       for (auto mul : b->m_next[MULTIPLY])
                        {
-                               digit_t aa(a->Head<digit_t>());
-                               digit_t ab = (a->m_next_factor != NULL) ? a->m_next_factor->Head<digit_t>() : 1;
-                               digit_t bc(b->Head<digit_t>());
-                               digit_t bd = (b->m_next_factor != NULL) ? b->m_next_factor->Head<digit_t>() : 1;
-                               Optype aop = (a->m_next_factor != NULL) ? a->m_next_factor->m_op : DIVIDE;
-                               Optype cop = (b->m_next_factor != NULL) ? b->m_next_factor->m_op : DIVIDE;
-                               simplify = CombineTerms<digit_t>(aa, aop, ab, bc, cop, bd);
-                               if (simplify)
-                               {
-                                       a->m_value = aa;
-                                       if (a->m_next_factor != NULL)
-                                               a->m_next_factor->m_value = ab;
-                                       else if (ab != 1)
-                                       {
-                                               a->m_next_factor = b->m_next_factor;
-                                               b->m_next_factor = NULL;
-                                               a->m_next_factor->m_value = ab;
-                                       }
-                               }
+                               delete(OperationFactor(mul, mulop));
                        }
-                       else
+                       Optype divop = (op == MULTIPLY) ? DIVIDE : MULTIPLY;
+                       for (auto div : b->m_next[DIVIDE])
+                               delete(OperationFactor(div, divop));
+                               
+                       b->m_next[DIVIDE].clear();
+                       b->m_next[MULTIPLY].clear();
+                       return b;               
+               }
+       }
+       
+               
+       bool parent = (merge_point == NULL);
+       ParanoidNumber * merge = this;
+       Optype mop = op;
+       if (parent)
+       {
+               merge_point = &merge;
+               merge_op = &mop;        
+       }
+       else
+       {
+               merge = *merge_point;
+               mop = *merge_op;
+       }
+               
+       Optype invop = InverseOp(op); // inverse of p
+       Optype fwd = op;
+       Optype rev = invop;
+       if (op == DIVIDE)
+       {
+               fwd = MULTIPLY;
+               rev = DIVIDE;
+       }
+
+       ParanoidNumber * cpy_b = new ParanoidNumber(*b);
+       for (auto prev : m_next[invop])
+       {
+               if (prev->OperationFactor(b, rev, merge_point, merge_op) == b)
+               {
+                       for (auto add : m_next[ADD])
+                               delete(add->OperationFactor(new ParanoidNumber(*cpy_b), op));
+                       for (auto sub : m_next[SUBTRACT])
+                               delete(sub->OperationFactor(new ParanoidNumber(*cpy_b), op));
+                               
+                       delete(cpy_b);
+                       return b;
+               }
+       }
+       for (auto next : m_next[op])
+       {
+               if (next->OperationFactor(b, fwd, merge_point, merge_op) == b)
+               {
+                       for (auto add : m_next[ADD])
                        {
-                               simplify = ParanoidOp<digit_t>(a->m_value, b->Head<digit_t>(), ADD);
+                               delete(add->OperationFactor(new ParanoidNumber(*cpy_b), op));
                        }
-                       if (simplify)
+                       for (auto sub : m_next[SUBTRACT])
                        {
-                               bprev->m_next_term = b->m_next_term;
-                               b->m_next_term = NULL;
-                               delete b;
-                               b = bprev;
+                               delete(sub->OperationFactor(new ParanoidNumber(*cpy_b), op));
                        }
-                       
-                       bprev = b;
-                       b = b->m_next_term;
+                       delete(cpy_b);
+                       return b;
                }
        }
+       
+       if (parent)
+       {
+               //assert(b != NULL);
+               m_next[op].push_back(b);
+               for (auto add : m_next[ADD])
+                       delete(add->OperationFactor(new ParanoidNumber(*cpy_b), op));
+               for (auto sub : m_next[SUBTRACT])
+                       delete(sub->OperationFactor(new ParanoidNumber(*cpy_b), op));
+                       
+               #ifdef PARANOID_SIZE_LIMIT
+                       m_size += b->m_size;
+               #endif  
+       }
+       //assert(SanityCheck());
+
+
+       
+       return NULL;    
+}
+
+
+
+/**
+ * Performs the operation on a with argument b (a += b, a -= b, a *= b, a /= b)
+ * @returns b if b can safely be deleted
+ * @returns NULL if b has been merged with a
+ * append indicates that b should be merged
+ */
+ParanoidNumber * ParanoidNumber::Operation(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point, Optype * merge_op)
+{
+
+       if (b == NULL)
+               return NULL;
+
+       
+       if (op == SUBTRACT || op == ADD)
+               return OperationTerm(b, op, merge_point, merge_op);
+       if (op == MULTIPLY || op == DIVIDE)
+               return OperationFactor(b, op, merge_point, merge_op);
+       return b;
 }
 
-void ParanoidNumber::SimplifyFactors()
-{ 
 
-       //Debug("Simplify {%s}", Str().c_str()); 
-       if (m_next_factor == NULL)
+
+string ParanoidNumber::PStr() const
+{
+       stringstream s;
+       for (int i = 0; i < NOP; ++i)
        {
-               //Debug("No factors!");
-               return;
+               Optype f = Optype(i);
+               s << this;
+               for (auto n : m_next[f])
+               {
+                       s << OpChar(f) << n->PStr();
+               }
        }
+       return s.str();
+}
 
-       for (ParanoidNumber * a = this; a != NULL; a = a->m_next_factor)
+bool ParanoidNumber::Simplify(Optype op)
+{
+       
+       if (Floating())
+               return false;
+               
+       //assert(SanityCheck());
+       vector<ParanoidNumber*> next;
+       next.clear();
+       swap(m_next[op], next);
+       m_next[op].clear();
+       //assert(m_next[op].size() == 0);
+       //assert(SanityCheck());
+       Optype fwd = op;
+       if (op == DIVIDE)
+               fwd = MULTIPLY;
+       else if (op == SUBTRACT)
+               fwd = ADD;
+               
+               
+       vector<ParanoidNumber*> hold[2];
+       if (op == MULTIPLY || op == DIVIDE)
        {
-               if ((a->m_op != ADD || a->m_op != SUBTRACT) && a->m_next_term != NULL)
+               swap(m_next[ADD], hold[0]);
+               swap(m_next[SUBTRACT], hold[1]);
+       }
+       
+       for (vector<ParanoidNumber*>::iterator n = next.begin(); n != next.end(); ++n)
+       {
+               if (*n == NULL)
                        continue;
-                       
-               ParanoidNumber * bprev = a;
-               ParanoidNumber * b = a->m_next_factor;
-               while (b != NULL)
+               for (vector<ParanoidNumber*>::iterator m = n; m != next.end(); ++m)
                {
-                       b->SimplifyTerms();
-                       if (b->m_next_term != NULL)
-                       {
-                               bprev = b;
-                               b = b->m_next_factor;
+                       if ((*m) == (*n))
                                continue;
-                       }
-               
-                       Optype op = b->m_op;
-                       if (a->m_op == DIVIDE)
+                       if (*m == NULL)
+                               continue;
+                               
+                       ParanoidNumber * parent = this;
+                       Optype mop = op;
+                       ParanoidNumber * result = (*n)->Operation(*m, fwd, &parent, &mop);
+                       if (result != NULL)
                        {
-                               op = (b->m_op == DIVIDE) ? MULTIPLY : DIVIDE;
+                               #ifdef PARANOID_SIZE_LIMIT
+                                       m_size -= result->m_size;
+                               #endif
+                               *m = NULL;
+                               delete(result);
                        }
-                       
-                       if (ParanoidOp<digit_t>(a->m_value, b->m_value, op))
-                       {       
-
-                               bprev->m_next_factor = b->m_next_factor;
-                               b->m_next_factor = NULL;
-                               delete b;
-                               b = bprev;
-                       }
-                       bprev = b;
-                       b = b->m_next_factor;
                }
        }
+       
+       
+       
+       for (auto n : next)
+       {
+               if (n != NULL)
+               {               
+                       #ifdef PARANOID_SIZE_LIMIT
+                               if (Operation(n, op) == n)
+                               {
+                                       m_size -= n->m_size;
+                                       delete n;
+                               }
+                       #else   
+                               delete(Operation(n, op));
+                       #endif 
+               }
+       }
+       
+       if (op == MULTIPLY || op == DIVIDE)
+       {
+               swap(m_next[ADD], hold[0]);
+               swap(m_next[SUBTRACT], hold[1]);
+       }
+       
+       set<ParanoidNumber*> s;
+       //if (!SanityCheck(s))
+       //{
+       //      Error("Simplify broke Sanity");
+       //}
+       return (next.size() > m_next[op].size());
 }
 
-void ParanoidNumber::Simplify()
+bool ParanoidNumber::FullSimplify()
 {
-       SimplifyFactors();
-       SimplifyTerms();
+       bool result = false;
+       result |= Simplify(MULTIPLY);
+       result |= Simplify(DIVIDE);
+       result |= Simplify(ADD);
+       result |= Simplify(SUBTRACT);
+       return result;
 }
 
-string ParanoidNumber::Str() const
+ParanoidNumber::digit_t ParanoidNumber::Digit() const
 {
-       string result("");
-       stringstream s;
-       s << (double)m_value;
+
+       // Get around the absurd requirement that const correctness be observed.
+       #ifdef PARANOID_CACHE_RESULTS
+       if (m_cache_valid) // le sigh ambiguous function compiler warnings
+               return m_cached_result;
+               
+       digit_t & result = ((ParanoidNumber*)(this))->m_cached_result;
        
-       if (m_next_factor != NULL)
+       #else
+               digit_t result;
+       #endif
+       result = m_value;
+       for (auto mul : m_next[MULTIPLY])
        {
-               result += s.str();
-               result += OpChar(m_next_factor->m_op);
-               if (m_next_factor->m_next_term != NULL)
-                       result += "(" + m_next_factor->Str() + ")";
-               else
-                       result += m_next_factor->Str();
+               result *= mul->Digit();
        }
-       else
+       for (auto div : m_next[DIVIDE])
        {
-               result += s.str();
+               result /= div->Digit();
        }
+       for (auto add : m_next[ADD])
+               result += add->Digit();
+       for (auto sub : m_next[SUBTRACT])
+               result -= sub->Digit();
                
-       if (m_next_term != NULL)
-       {
-               result += " ";
-               result += OpChar(m_next_term->m_op);
-               result += m_next_term->Str();
-       }
+       #ifdef PARANOID_CACHE_RESULTS
+               ((ParanoidNumber*)(this))->m_cache_valid = true;
+       #endif
        return result;
+               
 }
 
-template <>
-bool TrustingOp<float>(float & a, const float & b, Optype op)
+ParanoidNumber::digit_t ParanoidNumber::GetFactors() const
 {
-       feclearexcept(FE_ALL_EXCEPT);
-       switch (op)
+       digit_t value = 1;
+       for (auto mul : m_next[MULTIPLY])
+               value *= mul->Digit();
+       for (auto div : m_next[DIVIDE])
+               value /= div->Digit();
+       return value;
+}
+
+
+ParanoidNumber::digit_t ParanoidNumber::GetTerms() const
+{
+       digit_t value = 0;
+       for (auto add : m_next[ADD])
+               value += add->Digit();
+       for (auto sub : m_next[SUBTRACT])
+               value -= sub->Digit();
+       return value;
+}
+
+bool ParanoidNumber::SanityCheck(set<ParanoidNumber*> & visited) const
+{
+       if (this == NULL)
        {
-               case ADD:
-                       a += b;
-                       break;
-               case SUBTRACT:
-                       a -= b;
-                       break;
-               case MULTIPLY:
-                       a *= b;
-                       break;
-               case DIVIDE:
-                       a /= b;
-                       break;
+               Error("NULL pointer in tree");
+               return false;
        }
-       return !fetestexcept(FE_ALL_EXCEPT);
+               
+       if (visited.find((ParanoidNumber*)this) != visited.end())
+       {
+               Error("I think I've seen this tree before...");
+               return false;
+       }
+       
+       visited.insert((ParanoidNumber*)this);
+               
+       for (auto add : m_next[ADD])
+       {
+               if (!add->SanityCheck(visited))
+                       return false;
+       }
+       for (auto sub : m_next[SUBTRACT])
+       {
+               if (!sub->SanityCheck(visited))
+                       return false;
+       }
+       for (auto mul : m_next[MULTIPLY])
+       {
+               if (!mul->SanityCheck(visited))
+                       return false;
+       }
+       
+       for (auto div : m_next[DIVIDE])
+       {
+               if (!div->SanityCheck(visited))
+                       return false;
+               if (div->Digit() == 0)
+               {
+                       Error("Divide by zero");
+                       return false;
+               }
+       }
+       return true;
 }
 
-template <>
-bool TrustingOp<double>(double & a, const double & b, Optype op)
+void ParanoidNumber::Negate()
 {
-       feclearexcept(FE_ALL_EXCEPT);
-       switch (op)
+       swap(m_next[ADD], m_next[SUBTRACT]);
+       m_value = -m_value;
+       #ifdef PARANOID_CACHE_RESULTS
+               m_cached_result = -m_cached_result;
+       #endif
+}
+
+#ifdef PARANOID_USE_ARENA
+
+void * ParanoidNumber::operator new(size_t s)
+{
+       return g_arena.allocate(s);
+}
+
+void ParanoidNumber::operator delete(void * p)
+{
+       g_arena.deallocate(p);
+}
+
+ParanoidNumber::Arena::Arena(int64_t block_size) : m_block_size(block_size), m_spare(NULL)
+{
+       m_blocks.push_back({malloc(block_size*sizeof(ParanoidNumber)),0});
+}
+
+ParanoidNumber::Arena::~Arena()
+{
+       for (auto block : m_blocks)
        {
-               case ADD:
-                       a += b;
-                       break;
-               case SUBTRACT:
-                       a -= b;
-                       break;
-               case MULTIPLY:
-                       a *= b;
-                       break;
-               case DIVIDE:
-                       a /= b;
-                       break;
+               free(block.memory);
        }
-       return !fetestexcept(FE_ALL_EXCEPT);
+       m_blocks.clear();
 }
 
-template <>
-bool TrustingOp<int8_t>(int8_t & a, const int8_t & b, Optype op)
+void * ParanoidNumber::Arena::allocate(size_t s)
 {
-       int16_t sa(a);
-       bool exact = true;
-       switch (op)
+       if (m_spare != NULL)
        {
-               case ADD:
-                       sa += b;
-                       exact = (abs(sa) <= 127);
-                       break;
-               case SUBTRACT:
-                       sa -= b;
-                       exact = (abs(sa) <= 127);
-                       break;
-               case MULTIPLY:
-                       sa *= b;
-                       exact = (abs(sa) <= 127);
-                       break;
-               case DIVIDE:
-                       exact = (b != 0 && sa > b && sa % b == 0);
-                       sa /= b;
-                       break;
+               void * result = m_spare;
+               m_spare = NULL;
+               return result;
        }
-       a = (int8_t)(sa);
-       return exact;
+               
+       Block & b = m_blocks.back();
+       void * result = (ParanoidNumber*)(b.memory) + (b.used++);
+       if (b.used >= m_block_size)
+       {
+               m_block_size *= 2;
+               Debug("Add block of size %d", m_block_size);
+               m_blocks.push_back({malloc(m_block_size*sizeof(ParanoidNumber)), 0});
+       }
+       return result;
+}
+
+void ParanoidNumber::Arena::deallocate(void * p)
+{
+       m_spare = p;
 }
+#endif //PARANOID_USE_ARENA
 
 }

UCC git Repository :: git.ucc.asn.au