diff options
author | Torsten Paul <Torsten.Paul@gmx.de> | 2013-11-10 01:42:58 (GMT) |
---|---|---|
committer | Torsten Paul <Torsten.Paul@gmx.de> | 2013-11-11 00:31:44 (GMT) |
commit | 00a329f0bd4ab940c1063106ee6ba7db7811a090 (patch) | |
tree | 26872901c9cbc603e7a62b14975e306b0e146885 /src/value.h | |
parent | e5d535e900ae5bc5923fc76c80fb3b3f9153a80e (diff) |
Add support for handling negative step values in ranges (fixes #500).
Diffstat (limited to 'src/value.h')
-rw-r--r-- | src/value.h | 75 |
1 files changed, 53 insertions, 22 deletions
diff --git a/src/value.h b/src/value.h index 388b721..4671cdc 100644 --- a/src/value.h +++ b/src/value.h @@ -31,33 +31,64 @@ std::ostream &operator<<(std::ostream &stream, const Filename &filename); class Value { public: - struct RangeType { + class RangeType { + private: + double begin_val; + double step_val; + double end_val; + + /// inverse begin/end if begin is upper than end + void normalize(); + + public: + typedef enum { RANGE_TYPE_BEGIN, RANGE_TYPE_RUNNING, RANGE_TYPE_END } type_t; + + class iterator { + public: + typedef iterator self_type; + typedef double value_type; + typedef double& reference; + typedef double* pointer; + typedef std::forward_iterator_tag iterator_category; + typedef double difference_type; + iterator(RangeType &range, type_t type); + self_type operator++(); + self_type operator++(int junk); + reference operator*(); + pointer operator->(); + bool operator==(const self_type& other) const; + bool operator!=(const self_type& other) const; + private: + RangeType ⦥ + double val; + type_t type; + + void update_type(); + }; + + RangeType(double begin, double end) + : begin_val(begin), step_val(1.0), end_val(end) + { + normalize(); + } + RangeType(double begin, double step, double end) - : begin(begin), step(step), end(end) {} + : begin_val(begin), step_val(step), end_val(end) {} bool operator==(const RangeType &other) const { - return this->begin == other.begin && - this->step == other.step && - this->end == other.end; + return this->begin_val == other.begin_val && + this->step_val == other.step_val && + this->end_val == other.end_val; } - /// inverse begin/end if begin is upper than end - void normalize() { - if ((step>0) && (end < begin)) { - std::swap(begin,end); - } - } - /// return number of steps, max int value if step is null - int nbsteps() const { - if (step<=0) { - return std::numeric_limits<int>::max(); - } - return (int)((begin-end)/step); - } - - double begin; - double step; - double end; + iterator begin() { return iterator(*this, RANGE_TYPE_BEGIN); } + iterator end() { return iterator(*this, RANGE_TYPE_END); } + + /// return number of steps, max int value if step is null + unsigned long nbsteps() const; + + friend class tostring_visitor; + friend class bracket_visitor; }; typedef std::vector<Value> VectorType; |