+
+ParanoidNumber & ParanoidNumber::operator+=(const ParanoidNumber & a)
+{
+ delete Operation(new ParanoidNumber(a), ADD);
+ Simplify(ADD);
+ Simplify(SUBTRACT);
+ return *this;
+}
+
+
+ParanoidNumber & ParanoidNumber::operator-=(const ParanoidNumber & a)
+{
+ delete Operation(new ParanoidNumber(a), SUBTRACT);
+ //Simplify(SUBTRACT);
+ //Simplify(ADD);
+ return *this;
+}
+
+ParanoidNumber & ParanoidNumber::operator*=(const ParanoidNumber & a)
+{
+ delete Operation(new ParanoidNumber(a), MULTIPLY);
+ return *this;
+}
+
+
+ParanoidNumber & ParanoidNumber::operator/=(const ParanoidNumber & a)
+{
+ delete Operation(new ParanoidNumber(a), DIVIDE);
+ return *this;
+}
+
+// a + b
+ParanoidNumber * ParanoidNumber::OperationTerm(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point, Optype * merge_op)
+{
+ m_cached_result = nan("");
+ 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();
+ }
+ 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();
+ 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;
+ }
+
+ Optype invop = InverseOp(op); // inverse of p
+ Optype fwd = op;
+ Optype rev = invop;
+ if (op == SUBTRACT)
+ {
+ fwd = ADD;
+ rev = SUBTRACT;
+ }
+
+ for (auto prev : m_next[invop])
+ {
+ if (prev->OperationTerm(b, rev, merge_point, merge_op) == b)
+ return b;
+
+ }
+ for (auto next : m_next[op])
+ {
+ if (next->OperationTerm(b, fwd, merge_point, merge_op) == b)
+ return b;
+ }
+
+
+
+
+ if (parent)
+ {
+ //merge->m_next[*merge_op].push_back(b);
+ m_next[op].push_back(b);
+ }
+ else
+ {
+ if (m_next[op].size() == 0)
+ {
+ *merge_point = this;
+ *merge_op = op;
+ }
+ }
+ return NULL;
+}
+
+ParanoidNumber * ParanoidNumber::OperationFactor(ParanoidNumber * b, Optype op, ParanoidNumber ** merge_point, Optype * merge_op)
+{
+ m_cached_result = nan("");
+ if (Floating() && m_value == 0)
+ {
+ return b;
+ }
+
+ if (Floating() && m_value == 1 && op == MULTIPLY)
+ {
+ m_value = b->m_value;
+ for (int i = 0; i < NOP; ++i)
+ {
+ for (auto n : m_next[i])
+ delete n;
+ m_next[i].clear();
+ swap(m_next[i], b->m_next[i]);
+ }
+ return b;
+ }
+ if (b->Floating() && b->m_value == 1)
+ return b;
+
+
+
+ if (NoTerms() && b->NoTerms())
+ {
+ if (ParanoidOp<digit_t>(m_value, b->m_value, op))
+ {
+ Optype mulop = (op == MULTIPLY) ? MULTIPLY : DIVIDE;
+ for (auto mul : b->m_next[MULTIPLY])
+ {
+ delete OperationFactor(mul, mulop);
+ }
+ 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 = NULL;
+
+ if (m_next[ADD].size() > 0 || m_next[SUBTRACT].size() > 0)
+ {
+ 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])
+ 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;
+ }
+ }
+
+ if (parent)
+ {
+ 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);
+ }
+ 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;
+}
+
+
+
+string ParanoidNumber::PStr() const
+{
+ stringstream s;
+ for (int i = 0; i < NOP; ++i)
+ {
+ Optype f = Optype(i);
+ s << this;
+ for (auto n : m_next[f])
+ {
+ s << OpChar(f) << n->PStr();
+ }
+ }
+ return s.str();
+}
+
+bool ParanoidNumber::Simplify(Optype op)
+{
+ vector<ParanoidNumber*> next(0);
+ swap(m_next[op], next);
+ for (auto n : next)
+ {
+ delete Operation(n, op);
+ }
+ return (next.size() > m_next[op].size());
+}
+
+bool ParanoidNumber::FullSimplify()
+{
+ bool result = false;
+ result |= Simplify(MULTIPLY);
+ result |= Simplify(DIVIDE);
+ result |= Simplify(ADD);
+ result |= Simplify(SUBTRACT);
+ return result;
+}
+
+
+ParanoidNumber::digit_t ParanoidNumber::Digit()
+{
+ if (!isnan(m_cached_result))
+ return m_cached_result;
+ m_cached_result = m_value;
+ for (auto mul : m_next[MULTIPLY])
+ {
+ m_cached_result *= mul->Digit();
+ }
+ for (auto div : m_next[DIVIDE])
+ {
+ m_cached_result /= div->Digit();
+ }
+ for (auto add : m_next[ADD])
+ m_cached_result += add->Digit();
+ for (auto sub : m_next[SUBTRACT])
+ m_cached_result -= sub->Digit();
+ return m_cached_result;
+
+}
+
+ParanoidNumber::digit_t ParanoidNumber::GetFactors()
+{
+ 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()
+{
+ digit_t value = 0;
+ for (auto add : m_next[ADD])
+ value += add->Digit();
+ for (auto sub : m_next[SUBTRACT])
+ value -= sub->Digit();
+ return value;
+}
+
+