wrapping_int.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef _DATE_TIME_WRAPPING_INT_HPP__
  2. #define _DATE_TIME_WRAPPING_INT_HPP__
  3. /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include "boost/config.hpp"
  11. namespace boost {
  12. namespace date_time {
  13. //! A wrapping integer used to support time durations (WARNING: only instantiate with a signed type)
  14. /*! In composite date and time types this type is used to
  15. * wrap at the day boundary.
  16. * Ex:
  17. * A wrapping_int<short, 10> will roll over after nine, and
  18. * roll under below zero. This gives a range of [0,9]
  19. *
  20. * NOTE: it is strongly recommended that wrapping_int2 be used
  21. * instead of wrapping_int as wrapping_int is to be depricated
  22. * at some point soon.
  23. *
  24. * Also Note that warnings will occur if instantiated with an
  25. * unsigned type. Only a signed type should be used!
  26. */
  27. template<typename int_type_, int_type_ wrap_val>
  28. class wrapping_int {
  29. public:
  30. typedef int_type_ int_type;
  31. //typedef overflow_type_ overflow_type;
  32. static BOOST_CONSTEXPR int_type wrap_value() {return wrap_val;}
  33. //!Add, return true if wrapped
  34. BOOST_CXX14_CONSTEXPR wrapping_int(int_type v) : value_(v) {}
  35. //! Explicit converion method
  36. BOOST_CONSTEXPR int_type as_int() const {return value_;}
  37. BOOST_CONSTEXPR operator int_type() const {return value_;}
  38. //!Add, return number of wraps performed
  39. /*! The sign of the returned value will indicate which direction the
  40. * wraps went. Ex: add a negative number and wrapping under could occur,
  41. * this would be indicated by a negative return value. If wrapping over
  42. * took place, a positive value would be returned */
  43. template< typename IntT >
  44. BOOST_CXX14_CONSTEXPR IntT add(IntT v)
  45. {
  46. int_type remainder = static_cast<int_type>(v % (wrap_val));
  47. IntT overflow = static_cast<IntT>(v / (wrap_val));
  48. value_ = static_cast<int_type>(value_ + remainder);
  49. return calculate_wrap(overflow);
  50. }
  51. //! Subtract will return '+d' if wrapping under took place ('d' is the number of wraps)
  52. /*! The sign of the returned value will indicate which direction the
  53. * wraps went (positive indicates wrap under, negative indicates wrap over).
  54. * Ex: subtract a negative number and wrapping over could
  55. * occur, this would be indicated by a negative return value. If
  56. * wrapping under took place, a positive value would be returned. */
  57. template< typename IntT >
  58. BOOST_CXX14_CONSTEXPR IntT subtract(IntT v)
  59. {
  60. int_type remainder = static_cast<int_type>(v % (wrap_val));
  61. IntT underflow = static_cast<IntT>(-(v / (wrap_val)));
  62. value_ = static_cast<int_type>(value_ - remainder);
  63. return calculate_wrap(underflow) * -1;
  64. }
  65. private:
  66. int_type value_;
  67. template< typename IntT >
  68. BOOST_CXX14_CONSTEXPR IntT calculate_wrap(IntT wrap)
  69. {
  70. if ((value_) >= wrap_val)
  71. {
  72. ++wrap;
  73. value_ -= (wrap_val);
  74. }
  75. else if(value_ < 0)
  76. {
  77. --wrap;
  78. value_ += (wrap_val);
  79. }
  80. return wrap;
  81. }
  82. };
  83. //! A wrapping integer used to wrap around at the top (WARNING: only instantiate with a signed type)
  84. /*! Bad name, quick impl to fix a bug -- fix later!!
  85. * This allows the wrap to restart at a value other than 0.
  86. */
  87. template<typename int_type_, int_type_ wrap_min, int_type_ wrap_max>
  88. class wrapping_int2 {
  89. public:
  90. typedef int_type_ int_type;
  91. static BOOST_CONSTEXPR int_type wrap_value() {return wrap_max;}
  92. static BOOST_CONSTEXPR int_type min_value() {return wrap_min;}
  93. /*! If initializing value is out of range of [wrap_min, wrap_max],
  94. * value will be initialized to closest of min or max */
  95. BOOST_CXX14_CONSTEXPR wrapping_int2(int_type v) : value_(v) {
  96. if(value_ < wrap_min)
  97. {
  98. value_ = wrap_min;
  99. }
  100. if(value_ > wrap_max)
  101. {
  102. value_ = wrap_max;
  103. }
  104. }
  105. //! Explicit converion method
  106. BOOST_CONSTEXPR int_type as_int() const {return value_;}
  107. BOOST_CONSTEXPR operator int_type() const {return value_;}
  108. //!Add, return number of wraps performed
  109. /*! The sign of the returned value will indicate which direction the
  110. * wraps went. Ex: add a negative number and wrapping under could occur,
  111. * this would be indicated by a negative return value. If wrapping over
  112. * took place, a positive value would be returned */
  113. template< typename IntT >
  114. BOOST_CXX14_CONSTEXPR IntT add(IntT v)
  115. {
  116. int_type remainder = static_cast<int_type>(v % (wrap_max - wrap_min + 1));
  117. IntT overflow = static_cast<IntT>(v / (wrap_max - wrap_min + 1));
  118. value_ = static_cast<int_type>(value_ + remainder);
  119. return calculate_wrap(overflow);
  120. }
  121. //! Subtract will return '-d' if wrapping under took place ('d' is the number of wraps)
  122. /*! The sign of the returned value will indicate which direction the
  123. * wraps went. Ex: subtract a negative number and wrapping over could
  124. * occur, this would be indicated by a positive return value. If
  125. * wrapping under took place, a negative value would be returned */
  126. template< typename IntT >
  127. BOOST_CXX14_CONSTEXPR IntT subtract(IntT v)
  128. {
  129. int_type remainder = static_cast<int_type>(v % (wrap_max - wrap_min + 1));
  130. IntT underflow = static_cast<IntT>(-(v / (wrap_max - wrap_min + 1)));
  131. value_ = static_cast<int_type>(value_ - remainder);
  132. return calculate_wrap(underflow);
  133. }
  134. private:
  135. int_type value_;
  136. template< typename IntT >
  137. BOOST_CXX14_CONSTEXPR IntT calculate_wrap(IntT wrap)
  138. {
  139. if ((value_) > wrap_max)
  140. {
  141. ++wrap;
  142. value_ -= (wrap_max - wrap_min + 1);
  143. }
  144. else if((value_) < wrap_min)
  145. {
  146. --wrap;
  147. value_ += (wrap_max - wrap_min + 1);
  148. }
  149. return wrap;
  150. }
  151. };
  152. } } //namespace date_time
  153. #endif