Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2004-05-14 16:08:50


There has been some discussion previously on this list for a ranged type. My
personal desire was to have new double types in the ranges [0.0, 1.0) and
[0.0, 1.0] among other things. There are several other uses that I can
imagine. Feel free to rip this apart or do whatever you want.

#include <float.h>
#include <assert.h>

namespace boost
{
  // a simple range class
  template<class policy>
  class range {
    public:
      typedef range<policy> self;
      typedef typename policy::value value;
      range() : m(min()) { }
      range(const self& x) : m(x) { }
      range(const value& x) { m = rangeAssert(x); }
      static const value min() { return policy::min(); }
      static const value max() { return policy::max(); }
      operator value() { return m; }
      self& operator=(const self& x) { m = x; return *this; }
      self& operator=(const value& x) { m = rangeAssert(x); return *this; }
      const value& rangeAssert(const value& x) const { assert(x <= max());
assert(x >= min()); return x; };
    private:
      value m;
  };

  // some range policies
  struct RangePolicyZeroToOne {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 1.0; };
  };

  struct RangePolicyZeroToBelowOne {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 1.0 - DBL_EPSILON; };
  };

  struct RangePolicyPercent {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 100.0; };
  };

  struct RangePolicyDegrees {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return 360.0 - DBL_EPSILON; };
  };

  struct RangePolicyRadians {
    typedef double value;
    static const value min() { return 0.0; };
    static const value max() { return (2 * PI) - DBL_EPSILON; };
  };

  template <typename T, T min_T, T max_T>
  struct StaticRange {
    typedef T value;
    static const value min() { return min_T; };
    static const value max() { return max_T; };
  }

  // some sample types made with ranges
  typedef range<RangePolicyZeroToOne> Probability;
  typedef range<RangePolicyZeroToBelowOne> Distribution;
  typedef range<RangePolicyPercent> Percent;
  typedef range<RangePolicyDegrees> Degrees;
  typedef range<RangePolicyRadians> Radians;
  typedef range<RangePolicyStatic<int, 0, 11> > Month;
  typedef range<RangePolicyStatic<int, 0, 364> > DayOfTheYear;
  typedef range<RangePolicyStatic<int, 0, 6> > DayOfTheWeek;
  typedef range<RangePolicyStatic<char, 'a', 'z'> > LowerCaseLetter;
  typedef range<RangePolicyStatic<char, 'A', 'Z'> > UpperCaseLetter;
  typedef range<RangePolicyStatic<char, '0', '9'> > NumberChar;
};

by Christopher Diggins
licensed under the Boost software license

http://www.cdiggins.com
http://www.heron-language.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk