00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00043 #ifndef RANGELIB_INCL_RANGELIB_HPP_RANDOM_RANGE
00044 #define RANGELIB_INCL_RANGELIB_HPP_RANDOM_RANGE
00045
00046 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00047 # define RANGELIB_VER_RANGELIB_HPP_RANDOM_RANGE_MAJOR 2
00048 # define RANGELIB_VER_RANGELIB_HPP_RANDOM_RANGE_MINOR 2
00049 # define RANGELIB_VER_RANGELIB_HPP_RANDOM_RANGE_REVISION 3
00050 # define RANGELIB_VER_RANGELIB_HPP_RANDOM_RANGE_EDIT 30
00051 #endif
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 #ifndef RANGELIB_INCL_RANGELIB_HPP_RANGELIB
00069 # include <rangelib/rangelib.hpp>
00070 #endif
00071 #ifndef RANGELIB_INCL_RANGELIB_HPP_RANGE_CATEGORIES
00072 # include <rangelib/range_categories.hpp>
00073 #endif
00074 #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_OPERATOR_BOOL
00075 # include <stlsoft/util/operator_bool.hpp>
00076 #endif
00077
00078 #ifndef STLSOFT_INCL_H_STDLIB
00079 # define STLSOFT_INCL_H_STDLIB
00080 # include <stdlib.h>
00081 #endif
00082
00083
00084
00085
00086
00087 #ifndef RANGELIB_NO_NAMESPACE
00088 # if defined(_STLSOFT_NO_NAMESPACE) || \
00089 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00090
00091 namespace rangelib
00092 {
00093 # else
00094
00095
00096 namespace stlsoft
00097 {
00098
00099 namespace rangelib_project
00100 {
00101
00102 # endif
00103 #endif
00104
00105
00106
00107
00108
00124 class random_range
00125 : public notional_range_tag
00126 {
00129 public:
00130 typedef int value_type;
00131 typedef notional_range_tag range_tag_type;
00132 typedef random_range class_type;
00134
00137 public:
00143 random_range(ss_size_t numValues, value_type minValue = 0, value_type maxValue = RAND_MAX)
00144 : m_numValues(numValues)
00145 , m_minValue(minValue)
00146 , m_maxValue(maxValue)
00147 , m_position(0)
00148 , m_value(next_value_(minValue, maxValue))
00149 {
00150 STLSOFT_ASSERT(minValue <= maxValue);
00151 STLSOFT_ASSERT((maxValue - minValue) <= RAND_MAX);
00152 }
00154
00157 private:
00158 STLSOFT_DEFINE_OPERATOR_BOOL_TYPES(class_type, operator_bool_generator_type, operator_bool_type);
00159 public:
00161 ss_bool_t is_open() const
00162 {
00163 return m_position != m_numValues;
00164 }
00166 value_type current() const
00167 {
00168 STLSOFT_MESSAGE_ASSERT("Attempting to access the value of a closed range", is_open());
00169
00170 return m_value;
00171 }
00173 class_type& advance()
00174 {
00175 STLSOFT_MESSAGE_ASSERT("Attempting to advance a closed range", is_open());
00176
00177 ++m_position;
00178 m_value = next_value_(m_minValue, m_maxValue);
00179
00180 return *this;
00181 }
00182
00184 operator operator_bool_type() const
00185 {
00186 return operator_bool_generator_type::translate(is_open());
00187 }
00189 value_type operator *() const
00190 {
00191 return current();
00192 }
00194 class_type& operator ++()
00195 {
00196 return advance();
00197 }
00200 class_type operator ++(int)
00201 {
00202 class_type ret(*this);
00203
00204 operator ++();
00205
00206 return ret;
00207 }
00209
00212 public:
00214 value_type minimum() const
00215 {
00216 return m_minValue;
00217 }
00219 value_type maximum() const
00220 {
00221 return m_maxValue;
00222 }
00224
00227 public:
00229 bool operator ==(class_type const& ) const
00230 {
00231 return false;
00232 }
00234 bool operator !=(class_type const& rhs) const
00235 {
00236 return ! operator ==(rhs);
00237 }
00239
00242 private:
00243 static value_type next_value_(value_type minValue, value_type maxValue)
00244 {
00245 return (maxValue - minValue) ? minValue + (rand() % (maxValue - minValue)) : 0;
00246 }
00248
00249
00250 private:
00251 const ss_size_t m_numValues;
00252 const int m_minValue;
00253 const int m_maxValue;
00254 ss_size_t m_position;
00255 int m_value;
00256
00257
00258 private:
00259 class_type& operator =(class_type const&);
00260 };
00261
00263
00264
00265 #ifdef STLSOFT_UNITTEST
00266 # include "./unittest/random_range_unittest_.h"
00267 #endif
00268
00269
00270
00271 #ifndef RANGELIB_NO_NAMESPACE
00272 # if defined(_STLSOFT_NO_NAMESPACE) || \
00273 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00274 }
00275 # else
00276 }
00277 }
00278 # endif
00279 #endif
00280
00281
00282
00283 #endif
00284
00285