summaryrefslogtreecommitdiff
path: root/src/value.h
diff options
context:
space:
mode:
authorMarius Kintel <marius@kintel.net>2013-11-15 20:34:42 (GMT)
committerMarius Kintel <marius@kintel.net>2013-11-15 20:34:42 (GMT)
commit022f80e87b1df5af489414a01bf2b5bcef1c3acd (patch)
treed20c8d9df56f129880df6426c0f885479ebc014a /src/value.h
parentaf8359993a6dfffed8f57bc5605f2ca77a353ffa (diff)
parentf46bd3788f75e691b65aaf0e4ffb1db1029ef717 (diff)
Merge pull request #542 from t-paul/issue500
Allow for statement with negative step value.
Diffstat (limited to 'src/value.h')
-rw-r--r--src/value.h75
1 files changed, 53 insertions, 22 deletions
diff --git a/src/value.h b/src/value.h
index 388b721..8197806 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 &range;
+ 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 uint32_ value if step is 0
+ uint32_t nbsteps() const;
+
+ friend class tostring_visitor;
+ friend class bracket_visitor;
};
typedef std::vector<Value> VectorType;
contact: Jan Huwald // Impressum