generics.memoryexpanders.pas 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. {
  2. This file is part of the Free Pascal/NewPascal run time library.
  3. Copyright (c) 2014 by Maciej Izak (hnb)
  4. member of the NewPascal development team (http://newpascal.org)
  5. Copyright(c) 2004-2018 DaThoX
  6. It contains the generics collections library
  7. See the file COPYING.FPC, included in this distribution,
  8. for details about the copyright.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. **********************************************************************}
  13. {$IFNDEF FPC_DOTTEDUNITS}
  14. unit Generics.MemoryExpanders;
  15. {$ENDIF FPC_DOTTEDUNITS}
  16. // Memory expanders
  17. {$mode delphi}
  18. {$MACRO ON}
  19. {$OVERFLOWCHECKS OFF}
  20. {$RANGECHECKS OFF}
  21. {.$WARN 5024 OFF}
  22. {.$WARN 4079 OFF}
  23. interface
  24. {$IFDEF FPC_DOTTEDUNITS}
  25. uses
  26. System.Classes, System.SysUtils;
  27. {$ELSE FPC_DOTTEDUNITS}
  28. uses
  29. Classes, SysUtils;
  30. {$ENDIF FPC_DOTTEDUNITS}
  31. type
  32. TProbeSequence = class
  33. public
  34. end;
  35. { TLinearProbing }
  36. TLinearProbing = class(TProbeSequence)
  37. public
  38. class function Probe(I, Hash: UInt32): UInt32; static; inline;
  39. const MAX_LOAD_FACTOR = 1;
  40. const DEFAULT_LOAD_FACTOR = 0.75;
  41. end;
  42. { TQuadraticProbing }
  43. TQuadraticProbing = class(TProbeSequence)
  44. public
  45. class function Probe(I, Hash: UInt32): UInt32; static; inline;
  46. const MAX_LOAD_FACTOR = 0.5;
  47. const DEFAULT_LOAD_FACTOR = 0.5;
  48. end;
  49. { TDoubleHashing }
  50. TDoubleHashing = class(TProbeSequence)
  51. public
  52. class function Probe(I, Hash1: UInt32; Hash2: UInt32 = 1): UInt32; static; inline;
  53. const MAX_LOAD_FACTOR = 1;
  54. const DEFAULT_LOAD_FACTOR = 0.85;
  55. end;
  56. const
  57. // http://stackoverflow.com/questions/757059/position-of-least-significant-bit-that-is-set
  58. // MultiplyDeBruijnBitPosition[uint32(((numberInt32 and -numberInt32) * $077CB531)) shr 27]
  59. MultiplyDeBruijnBitPosition: array[0..31] of Int32 =
  60. (
  61. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  62. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  63. );
  64. // http://primes.utm.edu/lists/2small/0bit.html
  65. // http://www.math.niu.edu/~rusin/known-math/98/pi_x
  66. // http://oeis.org/A014234/
  67. PrimaryNumbersJustLessThanPowerOfTwo: array[0..31] of UInt32 =
  68. (
  69. 0, 1, 3, 7, 13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071,
  70. 262139, 524287, 1048573, 2097143, 4194301, 8388593, 16777213, 33554393, 67108859,
  71. 134217689, 268435399, 536870909, 1073741789, 2147483647
  72. );
  73. // http://oeis.org/A014210
  74. // http://oeis.org/A203074
  75. PrimaryNumbersJustBiggerThanPowerOfTwo: array[0..31] of UInt32 = (
  76. 2,3,5,11,17,37,67,131,257,521,1031,2053,4099,
  77. 8209,16411,32771,65537,131101,262147,524309,
  78. 1048583,2097169,4194319,8388617,16777259,33554467,
  79. 67108879,134217757,268435459,536870923,1073741827,
  80. 2147483659);
  81. // Fibonacci numbers
  82. FibonacciNumbers: array[0..44] of UInt32 = (
  83. {0,1,1,2,3,}0,5,8,13,21,34,55,89,144,233,377,610,987,
  84. 1597,2584,4181,6765,10946,17711,28657,46368,75025,
  85. 121393,196418,317811,514229,832040,1346269,
  86. 2178309,3524578,5702887,9227465,14930352,24157817,
  87. 39088169, 63245986, 102334155, 165580141, 267914296,
  88. 433494437, 701408733, 1134903170, 1836311903, 2971215073,
  89. {! not fib number - this is memory limit} 4294967295);
  90. // Largest prime not exceeding Fibonacci(n)
  91. // http://oeis.org/A138184/list
  92. // http://www.numberempire.com/primenumbers.php
  93. PrimaryNumbersJustLessThanFibonacciNumbers: array[0..44] of UInt32 = (
  94. {! not correlated to fib number. For empty table} 0,
  95. 5,7,13,19,31,53,89,139,233,373,607,983,1597,
  96. 2579,4177,6763,10939,17707,28657,46351,75017,
  97. 121379,196387,317797,514229,832003,1346249,
  98. 2178283,3524569,5702867,9227443,14930341,24157811,
  99. 39088157,63245971,102334123,165580123,267914279,
  100. 433494437,701408717,1134903127,1836311879,2971215073,
  101. {! not correlated to fib number - this is prime memory limit} 4294967291);
  102. // Smallest prime >= n-th Fibonacci number.
  103. // http://oeis.org/A138185
  104. PrimaryNumbersJustBiggerThanFibonacciNumbers: array[0..44] of UInt32 = (
  105. {! not correlated to fib number. For empty table} 0,
  106. 5,11,13,23,37,59,89,149,233,379,613,
  107. 991,1597,2591,4201,6779,10949,17713,28657,46381,
  108. 75029,121403,196429,317827,514229,832063,1346273,
  109. 2178313,3524603,5702897,9227479,14930387,24157823,
  110. 39088193,63245989,102334157,165580147,267914303,
  111. 433494437,701408753,1134903179,1836311951,2971215073,
  112. {! not correlated to fib number - this is prime memory limit} 4294967291);
  113. type
  114. { TCuckooHashingCfg }
  115. TCuckooHashingCfg = class
  116. public
  117. const D = 2;
  118. const MAX_LOAD_FACTOR = 0.5;
  119. class function LoadFactor(M: Integer): Integer; virtual;
  120. end;
  121. TStdCuckooHashingCfg = class(TCuckooHashingCfg)
  122. public
  123. const MAX_LOOP = 1000;
  124. end;
  125. TDeamortizedCuckooHashingCfg = class(TCuckooHashingCfg)
  126. public
  127. const L = 5;
  128. end;
  129. TDeamortizedCuckooHashingCfg_D2 = TDeamortizedCuckooHashingCfg;
  130. { TDeamortizedCuckooHashingCfg_D4 }
  131. TDeamortizedCuckooHashingCfg_D4 = class(TDeamortizedCuckooHashingCfg)
  132. public
  133. const D = 4;
  134. const L = 20;
  135. const MAX_LOAD_FACTOR = 0.9;
  136. class function LoadFactor(M: Integer): Integer; override;
  137. end;
  138. { TDeamortizedCuckooHashingCfg_D6 }
  139. TDeamortizedCuckooHashingCfg_D6 = class(TDeamortizedCuckooHashingCfg)
  140. public
  141. const D = 6;
  142. const L = 170;
  143. const MAX_LOAD_FACTOR = 0.99;
  144. class function LoadFactor(M: Integer): Integer; override;
  145. end;
  146. TL5CuckooHashingCfg = class(TCuckooHashingCfg)
  147. public
  148. end;
  149. implementation
  150. { TDeamortizedCuckooHashingCfg_D6 }
  151. class function TDeamortizedCuckooHashingCfg_D6.LoadFactor(M: Integer): Integer;
  152. begin
  153. Result:=Pred(Round(MAX_LOAD_FACTOR*M));
  154. end;
  155. { TDeamortizedCuckooHashingCfg_D4 }
  156. class function TDeamortizedCuckooHashingCfg_D4.LoadFactor(M: Integer): Integer;
  157. begin
  158. Result:=Pred(Round(MAX_LOAD_FACTOR*M));
  159. end;
  160. { TCuckooHashingCfg }
  161. class function TCuckooHashingCfg.LoadFactor(M: Integer): Integer;
  162. begin
  163. Result := Pred(M shr 1);
  164. end;
  165. { TLinearProbing }
  166. class function TLinearProbing.Probe(I, Hash: UInt32): UInt32;
  167. begin
  168. Result := (Hash + I)
  169. end;
  170. { TQuadraticProbing }
  171. class function TQuadraticProbing.Probe(I, Hash: UInt32): UInt32;
  172. begin
  173. Result := (Hash + Sqr(I));
  174. end;
  175. { TDoubleHashingNoMod }
  176. class function TDoubleHashing.Probe(I, Hash1: UInt32; Hash2: UInt32): UInt32;
  177. begin
  178. Result := Hash1 + I * Hash2;
  179. end;
  180. end.