generics.memoryexpanders.pas 6.2 KB

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