BinSearch.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. ===========================================================================
  3. Doom 3 BFG Edition GPL Source Code
  4. Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
  6. Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 BFG Edition Source Code 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. You should have received a copy of the GNU General Public License
  15. along with Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #ifndef __BINSEARCH_H__
  21. #define __BINSEARCH_H__
  22. /*
  23. ===============================================================================
  24. Binary Search templates
  25. The array elements have to be ordered in increasing order.
  26. ===============================================================================
  27. */
  28. /*
  29. ====================
  30. idBinSearch_GreaterEqual
  31. Finds the last array element which is smaller than the given value.
  32. ====================
  33. */
  34. template< class type >
  35. ID_INLINE int idBinSearch_Less( const type *array, const int arraySize, const type &value ) {
  36. int len = arraySize;
  37. int mid = len;
  38. int offset = 0;
  39. while( mid > 0 ) {
  40. mid = len >> 1;
  41. if ( array[offset+mid] < value ) {
  42. offset += mid;
  43. }
  44. len -= mid;
  45. }
  46. return offset;
  47. }
  48. /*
  49. ====================
  50. idBinSearch_GreaterEqual
  51. Finds the last array element which is smaller than or equal to the given value.
  52. ====================
  53. */
  54. template< class type >
  55. ID_INLINE int idBinSearch_LessEqual( const type *array, const int arraySize, const type &value ) {
  56. int len = arraySize;
  57. int mid = len;
  58. int offset = 0;
  59. while( mid > 0 ) {
  60. mid = len >> 1;
  61. if ( array[offset+mid] <= value ) {
  62. offset += mid;
  63. }
  64. len -= mid;
  65. }
  66. return offset;
  67. }
  68. /*
  69. ====================
  70. idBinSearch_Greater
  71. Finds the first array element which is greater than the given value.
  72. ====================
  73. */
  74. template< class type >
  75. ID_INLINE int idBinSearch_Greater( const type *array, const int arraySize, const type &value ) {
  76. int len = arraySize;
  77. int mid = len;
  78. int offset = 0;
  79. int res = 0;
  80. while( mid > 0 ) {
  81. mid = len >> 1;
  82. if ( array[offset+mid] > value ) {
  83. res = 0;
  84. } else {
  85. offset += mid;
  86. res = 1;
  87. }
  88. len -= mid;
  89. }
  90. return offset+res;
  91. }
  92. /*
  93. ====================
  94. idBinSearch_GreaterEqual
  95. Finds the first array element which is greater than or equal to the given value.
  96. ====================
  97. */
  98. template< class type >
  99. ID_INLINE int idBinSearch_GreaterEqual( const type *array, const int arraySize, const type &value ) {
  100. int len = arraySize;
  101. int mid = len;
  102. int offset = 0;
  103. int res = 0;
  104. while( mid > 0 ) {
  105. mid = len >> 1;
  106. if ( array[offset+mid] >= value ) {
  107. res = 0;
  108. } else {
  109. offset += mid;
  110. res = 1;
  111. }
  112. len -= mid;
  113. }
  114. return offset+res;
  115. }
  116. #endif /* !__BINSEARCH_H__ */