Range.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef _ZT_RANGE_HPP
  28. #define _ZT_RANGE_HPP
  29. namespace ZeroTier {
  30. /**
  31. * A range of numeric values
  32. *
  33. * @tparam T Type, can be any numeric value (int, float, double, etc.)
  34. */
  35. template<typename T>
  36. class Range
  37. {
  38. public:
  39. /**
  40. * Construct an empty range
  41. */
  42. Range()
  43. throw() :
  44. start(0),
  45. end(0)
  46. {
  47. }
  48. /**
  49. * @param s Starting value (inclusive)
  50. * @param e Ending value (exclusive)
  51. */
  52. Range(T s,T e)
  53. throw() :
  54. start(s),
  55. end(e)
  56. {
  57. }
  58. /**
  59. * Construct a range containing from n to n+1 (thus only n for integers)
  60. *
  61. * @param n Number to contain
  62. */
  63. Range(T n)
  64. throw() :
  65. start(n),
  66. end(n+1)
  67. {
  68. }
  69. /**
  70. * @return end - start
  71. */
  72. inline T magnitude() const
  73. throw()
  74. {
  75. return (end - start);
  76. }
  77. /**
  78. * @return True if range contains something (magnitude is nonzero)
  79. */
  80. inline operator bool() const
  81. throw()
  82. {
  83. return (end > start);
  84. }
  85. /**
  86. * @param v Value to test
  87. * @return True if value is between start (inclusive) and end (exclusive)
  88. */
  89. inline bool operator()(const T &v) const
  90. throw()
  91. {
  92. return ((v >= start)&&(v < end));
  93. }
  94. inline bool operator==(const Range &r) const throw() { return ((start == r.start)&&(end == r.end)); }
  95. inline bool operator!=(const Range &r) const throw() { return (!(*this == r)); }
  96. inline bool operator<(const Range &r) const throw() { return ((start < r.start) ? true : ((start == r.start) ? (end < r.end) : false)); }
  97. inline bool operator>(const Range &r) const throw() { return (r < *this); }
  98. inline bool operator<=(const Range &r) const throw() { return !(r < *this); }
  99. inline bool operator>=(const Range &r) const throw() { return !(*this < r); }
  100. /**
  101. * Start of range (may be modified directly)
  102. */
  103. T start;
  104. /**
  105. * End of range (may be modified directly)
  106. */
  107. T end;
  108. };
  109. } // namespace ZeroTier
  110. #endif