PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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