TF++, Miscellaneous classes and modules in C++:
range.h
Go to the documentation of this file.
1 
37 // include guard
38 #ifndef TFXX_RANGE_H_VERSION
39 
40 #define TFXX_RANGE_H_VERSION \
41  "TFXX_RANGE_H V1.2"
42 
43 namespace tfxx {
44 
49 template<class T=int>
50  class Range {
51  public:
53  typedef T Tvalue;
55  Range(): Mfirst(0), Mlast(0) { }
57  Range(const Tvalue& index): Mfirst(index), Mlast(index) { }
59  Range(const Tvalue& first, const Tvalue& last):
61  Mlast(last>first ? last:first) { }
63  Tvalue& first() { return Mfirst; }
65  Tvalue& last() { return Mlast; }
67  const Tvalue& first() const { return Mfirst; }
69  const Tvalue& last() const { return Mlast; }
71  bool isinside(const Range& other) const
72  { return ((Mfirst >= other.first()) && (Mlast <= other.last())); }
74  bool contains(const Range& other) const
75  { return ((Mfirst <= other.first()) && (Mlast >= other.last())); }
77  bool contains(const Tvalue& v) const
78  { return ((Mfirst <= v) && (Mlast >= v)); }
80  Range& shrink(const Range& other)
81  {
82  Mfirst=Mfirst>other.first() ? Mfirst:other.first();
83  Mlast=Mlast<other.last() ? Mlast:other.last();
84  return *this;
85  }
87  Range& expand(const Range& other)
88  {
89  Mfirst=Mfirst<other.first() ? Mfirst:other.first();
90  Mlast=Mlast>other.last() ? Mlast:other.last();
91  return *this;
92  }
94  Range& shift(const Tvalue& n)
95  { Mfirst+=n; Mlast+=n; return *this; }
97  Range& operator+=(const Tvalue& n)
98  { return this->shift(n); }
101  { return this->shift(-n); }
102  private:
105  }; // class Range
106 
107 /*----------------------------------------------------------------------*/
108 
113  template<class T=int>
114  class RangeStepper {
115  public:
116  typedef Range<T> Trange;
117  typedef typename Trange::Tvalue Tvalue;
119  const Tvalue& stepsize=1):
120  Mrange(range), Mstepsize(stepsize), Mcurrent(Mrange.first()) { }
122  operator Tvalue() const { return(this->current()); }
124  Tvalue current() const { return(Mcurrent); }
128  bool valid() const { return(Mcurrent<=Mrange.last()); }
130  bool more() const { return(Mcurrent<=(Mrange.last()-Mstepsize)); }
133  {
134  if (this->valid()) { Mcurrent += Mstepsize; }
135  return(this->current());
136  }
138  Tvalue operator++() { return(this->next()); }
139  private:
140  Trange Mrange;
143  };// class RangeStepper
144 
145 } // namespace tfxx
146 
147 #endif // TFXX_RANGE_H_VERSION (includeguard)
148 
149 /* ----- END OF range.h ----- */
Tvalue operator++()
advance to next value and return current value
Definition: range.h:138
Tvalue Mlast
end of range.
Definition: range.h:104
Range()
empty range
Definition: range.h:55
Tvalue & last()
access end of range
Definition: range.h:65
const Tvalue & last() const
read end of range
Definition: range.h:69
Range(const Tvalue &first, const Tvalue &last)
set range
Definition: range.h:59
const Tvalue & first() const
read start of range
Definition: range.h:67
Tvalue Mcurrent
Definition: range.h:142
tfxx::Range< T > range(const std::string &s)
Definition: rangestring.h:61
bool contains(const Tvalue &v) const
does this range contain then value v
Definition: range.h:77
Tvalue next()
advance to next value and return current value
Definition: range.h:132
Trange::Tvalue Tvalue
Definition: range.h:117
Range & shift(const Tvalue &n)
shift by n
Definition: range.h:94
bool contains(const Range &other) const
does this range contain the other
Definition: range.h:74
Tvalue & first()
access start of range
Definition: range.h:63
Range & shrink(const Range &other)
shrink to smaller of this and the other
Definition: range.h:80
Trange Mrange
Definition: range.h:140
A class to deal with numerical ranges.
Definition: range.h:50
RangeStepper(const Trange &range, const Tvalue &stepsize=1)
Definition: range.h:118
Tvalue current() const
return current value
Definition: range.h:124
Tvalue Mstepsize
Definition: range.h:141
bool more() const
true if stepper will still be in range after next advance
Definition: range.h:130
Range & expand(const Range &other)
expand to larger of this and the other
Definition: range.h:87
Range & operator+=(const Tvalue &n)
shift by n
Definition: range.h:97
T Tvalue
element type
Definition: range.h:53
bool valid() const
true if stepper is still in range and can return a current value
Definition: range.h:128
Range & operator-=(const Tvalue &n)
shift by -n
Definition: range.h:100
Range< T > Trange
Definition: range.h:116
bool isinside(const Range &other) const
is this range inside the other
Definition: range.h:71
Namespace containing all code of library libtfxx.
Tvalue Mfirst
start of range.
Definition: range.h:103
Range(const Tvalue &index)
range containing exactly one element
Definition: range.h:57