/src/lib/storage/internal/hash_table_size.e

http://github.com/tybor/Liberty · Specman e · 178 lines · 142 code · 5 blank · 31 comment · 27 complexity · b4405a919ac6ff8389925e1d8dc8edeb MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. deferred class HASH_TABLE_SIZE
  5. --
  6. -- Some useful features to deal with prime INTEGER values in order to
  7. -- select an appropriate size for some hash table (used for example
  8. -- by the DICTIONARY class as well as by the SET class).
  9. --
  10. insert
  11. PLATFORM
  12. undefine copy, is_equal end
  13. feature {}
  14. prime_number_ceiling (integer: INTEGER_32): INTEGER_32
  15. -- A good prime number, large enough, and no smaller than `integer'.
  16. require
  17. is_positive: integer >= 0
  18. do
  19. if integer <= 98317 then
  20. if integer <= 769 then
  21. if integer <= 53 then
  22. if integer <= 11 then
  23. if integer <= 3 then
  24. Result := 3
  25. else
  26. Result := 11
  27. end
  28. elseif integer <= 23 then
  29. Result := 23
  30. else
  31. Result := 53
  32. end
  33. else
  34. if integer <= 193 then
  35. if integer <= 97 then
  36. Result := 97
  37. else
  38. Result := 193
  39. end
  40. else
  41. if integer <= 389 then
  42. Result := 389
  43. else
  44. Result := 769
  45. end
  46. end
  47. end
  48. else
  49. if integer <= 12289 then
  50. if integer <= 3079 then
  51. if integer <= 1543 then
  52. Result := 1543
  53. else
  54. Result := 3079
  55. end
  56. else
  57. if integer <= 6151 then
  58. Result := 6151
  59. else
  60. Result := 12289
  61. end
  62. end
  63. else
  64. if integer <= 24593 then
  65. Result := 24593
  66. elseif integer <= 49157 then
  67. Result := 49157
  68. else
  69. Result := 98317
  70. end
  71. end
  72. end
  73. else
  74. if integer <= 12582917 then
  75. if integer <= 1572869 then
  76. if integer <= 393241 then
  77. if integer <= 196613 then
  78. Result := 196613
  79. else
  80. Result := 393241
  81. end
  82. else
  83. if integer <= 786433 then
  84. Result := 786433
  85. else
  86. Result := 1572869
  87. end
  88. end
  89. else
  90. if integer <= 3145739 then
  91. Result := 3145739
  92. elseif integer <= 6291469 then
  93. Result := 6291469
  94. else
  95. Result := 12582917
  96. end
  97. end
  98. else
  99. if integer <= 201326611 then
  100. if integer <= 50331653 then
  101. if integer <= 25165843 then
  102. Result := 25165843
  103. else
  104. Result := 50331653
  105. end
  106. else
  107. if integer <= 100663319 then
  108. Result := 100663319
  109. else
  110. Result := 201326611
  111. end
  112. end
  113. else
  114. if integer <= 805306457 then
  115. if integer <= 402653189 then
  116. Result := 402653189
  117. else
  118. Result := 805306457
  119. end
  120. else
  121. if integer <= 1610612741 then
  122. Result := 1610612741
  123. else
  124. -- Oddly enough 2^31-1 *is* a prime (it is a "Marsenne prime")
  125. -- http://en.wikipedia.org/wiki/Mersenne_prime
  126. Result := 2147483647
  127. end
  128. end
  129. end
  130. end
  131. end
  132. ensure
  133. Result >= integer.max(1)
  134. end
  135. feature {} -- Capacity management: ideally we try to keep the dictionary less than 2/3rd filled
  136. prime_capacity (a_capacity: INTEGER_32): INTEGER_32
  137. require
  138. a_capacity >= 0
  139. do
  140. Result := a_capacity #+ (a_capacity #// 2)
  141. if Result >= 0 then
  142. Result := prime_number_ceiling(Result)
  143. else
  144. Result := prime_number_ceiling(Maximum_integer)
  145. end
  146. ensure
  147. Result >= a_capacity
  148. end
  149. should_increase_capacity (a_capacity, a_count: INTEGER_32): BOOLEAN
  150. do
  151. Result := a_count > ((a_capacity #// 3) #* 2) and then a_capacity < Maximum_integer
  152. end
  153. end -- class HASH_TABLE_SIZE
  154. --
  155. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  156. --
  157. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  158. -- of this software and associated documentation files (the "Software"), to deal
  159. -- in the Software without restriction, including without limitation the rights
  160. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  161. -- copies of the Software, and to permit persons to whom the Software is
  162. -- furnished to do so, subject to the following conditions:
  163. --
  164. -- The above copyright notice and this permission notice shall be included in
  165. -- all copies or substantial portions of the Software.
  166. --
  167. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  168. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  169. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  170. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  171. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  172. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  173. -- THE SOFTWARE.