From 0b54fbca384e3c29b2fc70ad1756141e9383b0cb Mon Sep 17 00:00:00 2001 From: Louis Rubet Date: Sun, 18 Jun 2017 22:11:15 +0200 Subject: [PATCH] #139: stack::pop_back() has a new parameter to pop several levels --- src/rpn-branch.hpp | 18 +++++------------- src/rpn-complex.hpp | 3 +-- src/rpn-real.hpp | 17 +++++++---------- src/rpn-stack.hpp | 29 +++++++++-------------------- src/rpn-string.hpp | 6 ++---- src/stack.hpp | 12 ++++++++---- 6 files changed, 32 insertions(+), 53 deletions(-) diff --git a/src/rpn-branch.hpp b/src/rpn-branch.hpp index 2012e49..5b93bfc 100644 --- a/src/rpn-branch.hpp +++ b/src/rpn-branch.hpp @@ -89,17 +89,13 @@ void rpn_ift(void) if (!mpfr_zero_p(testee->_value.mpfr)) { CHECK_MPFR(stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack)); - (void)_stack->pop_back(); - (void)_stack->pop_back(); + (void)_stack->pop_back(2); CHECK_MPFR(stack::copy_and_push_back(_calc_stack, _calc_stack.size()-1, *_stack)); (void)_calc_stack.pop_back(); } else - { - (void)_stack->pop_back(); - (void)_stack->pop_back(); - } + (void)_stack->pop_back(2); } void rpn_ifte(void) @@ -116,9 +112,7 @@ void rpn_ifte(void) else CHECK_MPFR(stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack)); - (void)_stack->pop_back(); - (void)_stack->pop_back(); - (void)_stack->pop_back(); + (void)_stack->pop_back(3); CHECK_MPFR(stack::copy_and_push_back(_calc_stack, _calc_stack.size()-1, *_stack)); (void)_calc_stack.pop_back(); @@ -242,8 +236,7 @@ int rpn_next(branch& myobj) { // end of loop myobj.arg_bool = false;// init again next time - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(2); return -1; } else @@ -298,8 +291,7 @@ int rpn_step(branch& myobj) { // end of loop myobj.arg_bool = false;// init again next time - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(2); ret = -1; } else diff --git a/src/rpn-complex.hpp b/src/rpn-complex.hpp index e84c995..f7ed82c 100644 --- a/src/rpn-complex.hpp +++ b/src/rpn-complex.hpp @@ -62,8 +62,7 @@ void r2c() complex* cplx = (complex*)_stack->allocate_back(complex::calc_size(), cmd_complex); CHECK_MPFR(mpfr_set(cplx->re()->mpfr, ((number*)_calc_stack.get_obj(1))->_value.mpfr, floating_t::s_mpfr_rnd)); CHECK_MPFR(mpfr_set(cplx->im()->mpfr, ((number*)_calc_stack.get_obj(0))->_value.mpfr, floating_t::s_mpfr_rnd)); - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(2); } void c2r() diff --git a/src/rpn-real.hpp b/src/rpn-real.hpp index c3a854b..0088210 100644 --- a/src/rpn-real.hpp +++ b/src/rpn-real.hpp @@ -182,10 +182,7 @@ void do_divide_complexes() CHECK_MPFR(mpfr_div(left->im()->mpfr, left->im()->mpfr, ex->_value.mpfr, floating_t::s_mpfr_rnd)); _stack->pop_back(); - _calc_stack.pop_back(); - _calc_stack.pop_back(); - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(4); } void div() @@ -216,17 +213,18 @@ void div() //1. copy out stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack);//complex stack::copy_and_push_back(*_stack, _stack->size()-2, _calc_stack);//number - _stack->pop_back(); - _stack->pop_back(); + _stack->pop_back(2); + //2. copy back (2 complexes on stack) stack::copy_and_push_back(_calc_stack, _calc_stack.size()-2, *_stack);//complex back to stack stack::copy_and_push_back(_calc_stack, _calc_stack.size()-2, *_stack);//complex back to stack + //3. set complex level 2 to (number,0) complex* new_cplx = (complex*)_stack->get_obj(1); CHECK_MPFR(mpfr_set(new_cplx->re()->mpfr, ((number*)_calc_stack.get_obj(0))->_value.mpfr, floating_t::s_mpfr_rnd)); CHECK_MPFR(mpfr_set_ui(new_cplx->im()->mpfr, 0UL, floating_t::s_mpfr_rnd)); - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(2); + //4. divide do_divide_complexes(); } @@ -501,8 +499,7 @@ void mant() while (mpfr_less_p(left->_value.mpfr, one->_value.mpfr)) mpfr_mul(left->_value.mpfr, left->_value.mpfr, ten->_value.mpfr, floating_t::s_mpfr_rnd); - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(2); } } else diff --git a/src/rpn-stack.hpp b/src/rpn-stack.hpp index bda1a41..0eefc6b 100644 --- a/src/rpn-stack.hpp +++ b/src/rpn-stack.hpp @@ -4,12 +4,10 @@ void swap(void) MIN_ARGUMENTS(2); stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack); stack::copy_and_push_back(*_stack, _stack->size()-2, _calc_stack); - (void)_stack->pop_back(); - (void)_stack->pop_back(); + (void)_stack->pop_back(2); stack::copy_and_push_back(_calc_stack, _calc_stack.size()-2, *_stack); stack::copy_and_push_back(_calc_stack, _calc_stack.size()-1, *_stack); - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(2); } void drop(void) @@ -21,8 +19,7 @@ void drop(void) void drop2(void) { MIN_ARGUMENTS(2); - (void)_stack->pop_back(); - (void)_stack->pop_back(); + (void)_stack->pop_back(2); } void dropn(void) @@ -33,14 +30,12 @@ void dropn(void) int args = (int)mpfr_get_si(((number*)_stack->back())->_value.mpfr, floating_t::s_mpfr_rnd); MIN_ARGUMENTS(args+1); - for(int i=0;ipop_back(); + (void)_stack->pop_back(args+1); } void erase(void) { - while(_stack->size()>0) - (void)_stack->pop_back(); + (void)_stack->pop_back(_stack->size()); } void dup(void) @@ -93,15 +88,11 @@ void rot(void) stack::copy_and_push_back(*_stack, _stack->size()-3, _calc_stack); stack::copy_and_push_back(*_stack, _stack->size()-2, _calc_stack); stack::copy_and_push_back(*_stack, _stack->size()-1, _calc_stack); - (void)_stack->pop_back(); - (void)_stack->pop_back(); - (void)_stack->pop_back(); + (void)_stack->pop_back(3); stack::copy_and_push_back(_calc_stack, _calc_stack.size()-2, *_stack); stack::copy_and_push_back(_calc_stack, _calc_stack.size()-1, *_stack); stack::copy_and_push_back(_calc_stack, _calc_stack.size()-3, *_stack); - _calc_stack.pop_back(); - _calc_stack.pop_back(); - _calc_stack.pop_back(); + _calc_stack.pop_back(3); } void depth(void) @@ -130,8 +121,7 @@ void roll(void) stack::copy_and_push_back(_calc_stack, args-1-i, *_stack); stack::copy_and_push_back(_calc_stack, args-1, *_stack); - for(int i=0;ipop_back(); - _stack->pop_back(); + _stack->pop_back(2); number* numb = (number*)_stack->allocate_back(number::calc_size(), cmd_number); numb->_value = pos; @@ -112,8 +111,7 @@ void strsub() long len = ((ostring*)_stack->get_obj(2))->_len; bool result_is_void = false; - _stack->pop_back(); - _stack->pop_back(); + _stack->pop_back(2); if (first < 0) first = 0; diff --git a/src/stack.hpp b/src/stack.hpp index 9d30a70..99603c0 100644 --- a/src/stack.hpp +++ b/src/stack.hpp @@ -109,14 +109,18 @@ public: return allocated; } - object* pop_back() + object* pop_back(int pop_count = 1) { object* back = NULL; - if (_count > 0) + // pop several entries, return the last + while (pop_count-- > 0) { - _current = (char*)_base_pointer[--_count]; - back = (object*)_current; + if (_count > 0) + { + _current = (char*)_base_pointer[--_count]; + back = (object*)_current; + } } return back;