From 26f83b9efbb420fd859402620c56cd1e7dc1ae13 Mon Sep 17 00:00:00 2001 From: q66 Date: Fri, 29 May 2015 20:02:40 +0100 Subject: [PATCH] pop_first() and pop_last() now return bool instead of void (true if popped, false if not) --- octa/algorithm.h | 9 +++++---- octa/range.h | 52 +++++++++++++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 25 deletions(-) diff --git a/octa/algorithm.h b/octa/algorithm.h index 056b60d..816a3b3 100644 --- a/octa/algorithm.h +++ b/octa/algorithm.h @@ -440,8 +440,8 @@ namespace octa { bool empty() const { return p_range.empty(); } RangeSize size() const { return p_range.size(); } - void pop_first() { p_range.pop_first(); } - void pop_last() { p_range.pop_last(); } + bool pop_first() { return p_range.pop_first(); } + bool pop_last() { return p_range.pop_last(); } bool push_first() { return p_range.pop_first(); } bool push_last() { return p_range.push_last(); } @@ -530,9 +530,10 @@ namespace octa { bool empty() const { return p_range.empty(); } - void pop_first() { - p_range.pop_first(); + bool pop_first() { + bool ret = p_range.pop_first(); advance_valid(); + return ret; } bool push_first() { T tmp = p_range; diff --git a/octa/range.h b/octa/range.h index cd57faa..6d746e2 100644 --- a/octa/range.h +++ b/octa/range.h @@ -120,19 +120,15 @@ namespace octa { template RangeSize __octa_pop_first_n(R &range, RangeSize n) { - for (RangeSize i = 0; i < n; ++i) { - if (range.empty()) return i; - range.pop_first(); - } + for (RangeSize i = 0; i < n; ++i) + if (!range.pop_first()) return i; return n; } template RangeSize __octa_pop_last_n(R &range, RangeSize n) { - for (RangeSize i = 0; i < n; ++i) { - if (range.empty()) return i; - range.pop_last(); - } + for (RangeSize i = 0; i < n; ++i) + if (!range.pop_last()) return i; return n; } @@ -237,8 +233,8 @@ namespace octa { bool empty() const { return p_range.empty(); } r_size size() const { return p_range.size(); } - void pop_first() { p_range.pop_last(); } - void pop_last() { p_range.pop_first(); } + bool pop_first() { return p_range.pop_last(); } + bool pop_last() { return p_range.pop_first(); } bool push_first() { return p_range.push_last(); } bool push_last() { return p_range.push_first(); } @@ -316,8 +312,8 @@ namespace octa { bool empty() const { return p_range.empty(); } r_size size() const { return p_range.size(); } - void pop_first() { p_range.pop_first(); } - void pop_last() { p_range.pop_last(); } + bool pop_first() { return p_range.pop_first(); } + bool pop_last() { return p_range.pop_last(); } bool push_first() { return p_range.push_first(); } bool push_last() { return p_range.push_last(); } @@ -370,7 +366,7 @@ namespace octa { } bool empty() const { return p_a * p_step >= p_b * p_step; } - void pop_first() { p_a += p_step; } + bool pop_first() { p_a += p_step; return true; } bool push_first() { p_a -= p_step; return true; } T &first() { return p_a; } @@ -412,9 +408,10 @@ namespace octa { /* satisfy InputRange / ForwardRange */ bool empty() const { return p_beg == p_end; } - void pop_first() { - if (p_beg == p_end) return; + bool pop_first() { + if (p_beg == p_end) return false; ++p_beg; + return true; } bool push_first() { --p_beg; return true; @@ -439,9 +436,10 @@ namespace octa { const T &first() const { return *p_beg; } /* satisfy BidirectionalRange */ - void pop_last() { - if (p_end == p_beg) return; + bool pop_last() { + if (p_end == p_beg) return false; --p_end; + return true; } bool push_last() { ++p_end; return true; @@ -540,7 +538,13 @@ namespace octa { bool empty() const { return p_range.empty(); } - void pop_first() { ++p_index; p_range.pop_first(); } + bool pop_first() { + if (p_range.pop_first()) { + ++p_index; + return true; + } + return false; + } r_size pop_first_n(r_size n) { r_size ret = p_range.pop_first_n(n); @@ -597,7 +601,13 @@ namespace octa { bool empty() const { return (p_remaining <= 0) || p_range.empty(); } - void pop_first() { --p_remaining; p_range.pop_first(); } + bool pop_first() { + if (p_range.pop_first()) { + --p_remaining; + return true; + } + return false; + } bool push_first() { if (p_range.push_first()) { ++p_remaining; @@ -632,7 +642,7 @@ namespace octa { void pop_last() { static_assert(IsRandomAccessRange::value, "pop_last() only available for random access ranges"); - --p_remaining; + return --p_remaining >= 0; } RangeSize pop_last_n(RangeSize n) { static_assert(IsRandomAccessRange::value, @@ -706,7 +716,7 @@ namespace octa { } bool empty() const { return p_range.empty(); } - void pop_first() { p_range.pop_first_n(p_chunksize); } + bool pop_first() { return p_range.pop_first_n(p_chunksize) > 0; } bool push_first() { T tmp = p_range; RangeSize an = tmp.push_first_n(p_chunksize);