PODInterval.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (C) 2010 Google Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #ifndef PODInterval_h
  26. #define PODInterval_h
  27. #ifndef NDEBUG
  28. #include <wtf/text/StringBuilder.h>
  29. #endif
  30. namespace WebCore {
  31. // Class representing a closed interval which can hold an arbitrary
  32. // Plain Old Datatype (POD) as its endpoints and a piece of user
  33. // data. An important characteristic for the algorithms we use is that
  34. // if two intervals have identical endpoints but different user data,
  35. // they are not considered to be equal. This situation can arise when
  36. // representing the vertical extents of bounding boxes of overlapping
  37. // triangles, where the pointer to the triangle is the user data of
  38. // the interval.
  39. //
  40. // *Note* that the destructors of type T and UserData will *not* be
  41. // called by this class. They must not allocate any memory that is
  42. // required to be cleaned up in their destructors.
  43. //
  44. // The following constructors and operators must be implemented on
  45. // type T:
  46. //
  47. // - Copy constructor (if user data is desired)
  48. // - operator<
  49. // - operator==
  50. // - operator=
  51. //
  52. // If the UserData type is specified, it must support a copy
  53. // constructor and assignment operator.
  54. //
  55. // In debug mode, printing of intervals and the data they contain is
  56. // enabled. This requires the following template specializations to be
  57. // available:
  58. //
  59. // template<> struct WebCore::ValueToString<T> {
  60. // static String string(const T& t);
  61. // };
  62. // template<> struct WebCore::ValueToString<UserData> {
  63. // static String string(const UserData& t);
  64. // };
  65. //
  66. // Note that this class requires a copy constructor and assignment
  67. // operator in order to be stored in the red-black tree.
  68. #ifndef NDEBUG
  69. template<class T>
  70. struct ValueToString;
  71. #endif
  72. template<class T, class UserData = void*>
  73. class PODInterval {
  74. public:
  75. // Constructor from endpoints. This constructor only works when the
  76. // UserData type is a pointer or other type which can be initialized
  77. // with 0.
  78. PODInterval(const T& low, const T& high)
  79. : m_low(low)
  80. , m_high(high)
  81. , m_data(0)
  82. , m_maxHigh(high)
  83. {
  84. }
  85. // Constructor from two endpoints plus explicit user data.
  86. PODInterval(const T& low, const T& high, const UserData data)
  87. : m_low(low)
  88. , m_high(high)
  89. , m_data(data)
  90. , m_maxHigh(high)
  91. {
  92. }
  93. const T& low() const { return m_low; }
  94. const T& high() const { return m_high; }
  95. const UserData& data() const { return m_data; }
  96. bool overlaps(const T& low, const T& high) const
  97. {
  98. if (this->high() < low)
  99. return false;
  100. if (high < this->low())
  101. return false;
  102. return true;
  103. }
  104. bool overlaps(const PODInterval& other) const
  105. {
  106. return overlaps(other.low(), other.high());
  107. }
  108. // Returns true if this interval is "less" than the other. The
  109. // comparison is performed on the low endpoints of the intervals.
  110. bool operator<(const PODInterval& other) const
  111. {
  112. return low() < other.low();
  113. }
  114. // Returns true if this interval is strictly equal to the other,
  115. // including comparison of the user data.
  116. bool operator==(const PODInterval& other) const
  117. {
  118. return (low() == other.low()
  119. && high() == other.high()
  120. && data() == other.data());
  121. }
  122. const T& maxHigh() const { return m_maxHigh; }
  123. void setMaxHigh(const T& maxHigh) { m_maxHigh = maxHigh; }
  124. #ifndef NDEBUG
  125. // Support for printing PODIntervals.
  126. String toString() const
  127. {
  128. StringBuilder builder;
  129. builder.append("[PODInterval (");
  130. builder.append(ValueToString<T>::string(low()));
  131. builder.append(", ");
  132. builder.append(ValueToString<T>::string(high()));
  133. builder.append("), data=");
  134. builder.append(ValueToString<UserData>::string(data()));
  135. builder.append(", maxHigh=");
  136. builder.append(ValueToString<T>::string(maxHigh()));
  137. builder.append("]");
  138. return builder.toString();
  139. }
  140. #endif
  141. private:
  142. T m_low;
  143. T m_high;
  144. UserData m_data;
  145. T m_maxHigh;
  146. };
  147. } // namespace WebCore
  148. #endif // PODInterval_h