-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
CITY.pas
1917 lines (1592 loc) · 69 KB
/
CITY.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
CITY hash calculation - main unit
This library is only a naive reimplementation of reference code that can be
found in this repository:
https://github.com/google/cityhash
Because individual historical versions can produce different hashes for the
same message, all versions were implemented, each in its own unit (for
example version 1.0.3 of the hash is implemented in unit CITY_1_0_3.pas).
This allows you to choose which version to use, if you happen to need a
specific older implementation.
Currently implemented versions are:
1.0.0 ... in CITY_1_0_0.pas
1.0.1 ... in CITY_1_0_1.pas, CITY_1_0_1_Test.pas
1.0.2 ... in CITY_1_0_2.pas, CITY_1_0_2_Test.pas
1.0.3 ... in CITY_1_0_3.pas, CITY_1_0_3_Test.pas
1.1.0 ... in CITY_1_1_0.pas, CITY_1_1_0_Test.pas
1.1.1 ... in CITY_1_1_1.pas, CITY_1_1_1_Test.pas
Functions provided in this unit are only redirecting to latest version,
which is currently 1.1.1.
WARNING - version of this library does not correlate with version of
implemented and used version of the CITY hash!
Version 2.1.1 (2023-04-15)
Last change 2024-04-28
©2016-2023 František Milt
Contacts:
František Milt: [email protected]
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.CityHash
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
BitOps - github.com/TheLazyTomcat/Lib.BitOps
HashBase - github.com/TheLazyTomcat/Lib.HashBase
* SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
SimpleCPUID is required only when PurePascal symbol is not defined.
Library SimpleCPUID might also be required as an indirect dependency.
Indirect dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit CITY;
{$INCLUDE 'CITY_defs.inc'}
interface
uses
Classes,
AuxTypes, HashBase,
CITY_Common,
CITY_1_0_0,
CITY_1_0_1,
CITY_1_0_2,
CITY_1_0_3,
CITY_1_1_0,
CITY_1_1_1;
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ECITYIncompatibleClass = class(ECITYException);
ECITYUnsupportedVersion = class(ECITYException);
ECITYUnsupportedVariant = class(ECITYException);
ECITYInvalidImplementation = class(ECITYException);
{===============================================================================
Common types and constants
===============================================================================}
{
Bytes in TCITY* types are always ordered from least significant byte to most
significant byte (little endian).
Types TCITY*Sys have no such guarantee and their endianness is
system-dependent.
To convert the checksum in default ordering to a required specific ordering,
use methods CITY*ToLE for little endian and CITY*ToBE for big endian.
Note that these methods are expecting the input value to be in default
ordering, if it is not, the result will be wrong. Be carefull when using them.
}
type
TCITY32 = array[0..3] of UInt8;
PCITY32 = ^TCITY32;
TCITY64 = array[0..7] of UInt8;
PCITY64 = ^TCITY64;
TCITY128 = array[0..15] of UInt8;
PCITY128 = ^TCITY128;
TCITY256 = array[0..31] of UInt8;
PCITY256 = ^TCITY256;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type
TCITY32Sys = UInt32;
PCITY32Sys = ^TCITY32Sys;
TCITY64Sys = UInt64;
PCITY64Sys = ^TCITY64Sys;
TCITY128Sys = UInt128;
PCITY128Sys = ^TCITY128Sys;
TCITY256Sys = UInt256;
PCITY256Sys = ^TCITY256Sys;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
type
{
TCITYVersion can be used to select which version of the city hash to use for
calculation - it is here because different historical versions can and will
produce different results.
verDefault corresponds to verCITY111 - this will, unlike in case of verLatest,
never change in the future.
varLatest will always point to the latest implemented version, and therefore
might change in the future.
WARNING - not all widths (32bit, 64bit, 128bit, ...) of the city hash
were implemented from the first version!
}
TCITYVersion = (verDefault,verLatest,verCITY100,verCITY101,verCITY102,
verCITY103,verCITY110,verCITY111);
TCITYVersions = set of TCITYVersion;
{
TCITYVariant can be used to select which variant (with no seed, one seed or
two seeds) of the hash to use for calculation.
WARNING - not all varians are supported by different widths and versions
of the city hash.
}
TCITYVariant = (varPlain,varSeed,varSeeds);
TCITYVariants = set of TCITYVariant;
{===============================================================================
--------------------------------------------------------------------------------
TCityHash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCityHash - class declaration
===============================================================================}
type
TCityHash = class(TBufferHash)
protected
fCityVersion: TCITYVersion;
fCityVariant: TCITYVariant;
procedure SetCityVersion(Value: TCITYVersion); virtual;
procedure SetCityVariant(Value: TCITYVariant); virtual;
procedure Initialize; override;
procedure CheckVersionAndVariant; virtual;
public
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
class Function CityVersionsSupported: TCITYVersions; virtual; abstract;
class Function CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants; virtual; abstract;
constructor CreateAndInitFrom(Hash: THashBase); override;
property CityVersion: TCITYVersion read fCityVersion write SetCityVersion;
property CityVariant: TCityVariant read fCityVariant write SetCityVariant;
end;
{===============================================================================
--------------------------------------------------------------------------------
TCity32Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCity32Hash - class declaration
===============================================================================}
type
TCity32Hash = class(TCityHash)
protected
fCity32: TCity32Sys;
Function GetCity32: TCity32; virtual;
procedure CalculateHash(Memory: Pointer; Count: TMemSize); override;
procedure Initialize; override;
public
class Function CityVersionsSupported: TCITYVersions; override;
class Function CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants; override;
class Function City32ToSys(City32: TCity32): TCity32Sys; virtual;
class Function City32FromSys(City32: TCity32Sys): TCity32; virtual;
class Function City32ToLE(City32: TCity32): TCity32; virtual;
class Function City32ToBE(City32: TCity32): TCity32; virtual;
class Function City32FromLE(City32: TCity32): TCity32; virtual;
class Function City32FromBE(City32: TCity32): TCity32; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TCity32); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TCity32); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property City32: TCity32 read GetCity32;
property City32Sys: TCity32Sys read fCity32;
end;
{===============================================================================
--------------------------------------------------------------------------------
TCity64Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCity64Hash - class declaration
===============================================================================}
type
TCity64Hash = class(TCityHash)
protected
fCity64: TCity64Sys;
fSeed0: UInt64;
fSeed1: UInt64;
Function GetCity64: TCity64; virtual;
procedure CalculateHash(Memory: Pointer; Count: TMemSize); override;
procedure Initialize; override;
public
class Function CityVersionsSupported: TCITYVersions; override;
class Function CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants; override;
class Function City64ToSys(City64: TCity64): TCity64Sys; virtual;
class Function City64FromSys(City64: TCity64Sys): TCity64; virtual;
class Function City64ToLE(City64: TCity64): TCity64; virtual;
class Function City64ToBE(City64: TCity64): TCity64; virtual;
class Function City64FromLE(City64: TCity64): TCity64; virtual;
class Function City64FromBE(City64: TCity64): TCity64; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TCity64); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TCity64); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property City64: TCity64 read GetCity64;
property City64Sys: TCity64Sys read fCity64;
property Seed: UInt64 read fSeed0 write fSeed0;
property Seed0: UInt64 read fSeed0 write fSeed0;
property Seed1: UInt64 read fSeed1 write fSeed1;
end;
{===============================================================================
--------------------------------------------------------------------------------
TCity128HashBase
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCity128HashBase - class declaration
===============================================================================}
type
TCity128HashBase = class(TCityHash)
protected
fCity128: TCity128Sys;
fSeed: UInt128;
Function GetCity128: TCity128; virtual;
procedure Initialize; override;
public
class Function City128ToSys(City128: TCity128): TCity128Sys; virtual;
class Function City128FromSys(City128: TCity128Sys): TCity128; virtual;
class Function City128ToLE(City128: TCity128): TCity128; virtual;
class Function City128ToBE(City128: TCity128): TCity128; virtual;
class Function City128FromLE(City128: TCity128): TCity128; virtual;
class Function City128FromBE(City128: TCity128): TCity128; virtual;
class Function HashSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TCity128); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TCity128); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property City128: TCity128 read GetCity128;
property City128Sys: TCity128Sys read fCity128;
property Seed: UInt128 read fSeed write fSeed;
end;
{===============================================================================
--------------------------------------------------------------------------------
TCity128Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCity128Hash - class declaration
===============================================================================}
type
TCity128Hash = class(TCity128HashBase)
protected
procedure CalculateHash(Memory: Pointer; Count: TMemSize); override;
public
class Function CityVersionsSupported: TCITYVersions; override;
class Function CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants; override;
class Function HashName: String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TCityCRC128Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCityCRC128Hash - class declaration
===============================================================================}
type
TCityCRC128Hash = class(TCity128HashBase)
protected
Function GetHashImplementation: THashImplementation; override;
procedure SetHashImplementation(Value: THashImplementation); override;
procedure CalculateHash(Memory: Pointer; Count: TMemSize); override;
public
class Function CityVersionsSupported: TCITYVersions; override;
class Function CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants; override;
class Function HashImplementationsAvailable: THashImplementations; override;
class Function HashImplementationsSupported: THashImplementations; override;
class Function HashName: String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
TCity256HashBase
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCity256HashBase - class declaration
===============================================================================}
type
TCity256HashBase = class(TCityHash)
protected
fCity256: TCity256Sys;
Function GetCity256: TCity256; virtual;
procedure Initialize; override;
public
class Function City256ToSys(City256: TCity256): TCity256Sys; virtual;
class Function City256FromSys(City256: TCity256Sys): TCity256; virtual;
class Function City256ToLE(City256: TCity256): TCity256; virtual;
class Function City256ToBE(City256: TCity256): TCity256; virtual;
class Function City256FromLE(City256: TCity256): TCity256; virtual;
class Function City256FromBE(City256: TCity256): TCity256; virtual;
class Function HashSize: TMemSize; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TCity256); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TCity256); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property City256: TCity256 read GetCity256;
property City256Sys: TCity256Sys read fCity256;
end;
{===============================================================================
--------------------------------------------------------------------------------
TCityCRC256Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCityCRC256Hash - class declaration
===============================================================================}
type
TCityCRC256Hash = class(TCity256HashBase)
protected
Function GetHashImplementation: THashImplementation; override;
procedure SetHashImplementation(Value: THashImplementation); override;
procedure CalculateHash(Memory: Pointer; Count: TMemSize); override;
public
class Function CityVersionsSupported: TCITYVersions; override;
class Function CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants; override;
class Function HashImplementationsAvailable: THashImplementations; override;
class Function HashImplementationsSupported: THashImplementations; override;
class Function HashName: String; override;
end;
{===============================================================================
--------------------------------------------------------------------------------
Backward compatibility functions
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
Utility functions - declaration
===============================================================================}
const
CITY_VersionMajor = 1;
CITY_VersionMinor = 1;
CITY_VersionRelease = 1;
CITY_VersionFull = UInt64($0001000100010000);
CITY_VersionStr = '1.1.1';
Function Hash128to64(x: UInt128): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
{===============================================================================
Main hash functions - declaration
===============================================================================}
Function CityHash32(s: Pointer; len: TMemSize): UInt32;{$IFDEF CanInline} inline;{$ENDIF}
Function CityHash64(s: Pointer; len: TMemSize): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
Function CityHash64WithSeed(s: Pointer; len: TMemSize; seed: UInt64): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
Function CityHash64WithSeeds(s: Pointer; len: TMemSize; seed0,seed1: UInt64): UInt64;{$IFDEF CanInline} inline;{$ENDIF}
Function CityHash128(s: Pointer; len: TMemSize): UInt128;{$IFDEF CanInline} inline;{$ENDIF}
Function CityHash128WithSeed(s: Pointer; len: TMemSize; seed: UInt128): UInt128;{$IFDEF CanInline} inline;{$ENDIF}
{===============================================================================
CRC hash functions - declaration
===============================================================================}
procedure CityHashCrc256(s: Pointer; len: TMemSize; out result: UInt256);{$IFDEF CanInline} inline;{$ENDIF}
Function CityHashCrc128WithSeed(s: Pointer; len: TMemSize; seed: UInt128): UInt128;{$IFDEF CanInline} inline;{$ENDIF}
Function CityHashCrc128(s: Pointer; len: TMemSize): UInt128;{$IFDEF CanInline} inline;{$ENDIF}
implementation
uses
SysUtils, Math,
UInt64Utils;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W5057:={$WARN 5057 OFF}} // Local variable "$1" does not seem to be initialized
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TCityHash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCityHash - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TCityHash - protected methods
-------------------------------------------------------------------------------}
procedure TCityHash.SetCityVersion(Value: TCITYVersion);
begin
If Value in CityVersionsSupported then
fCityVersion := Value
else
raise ECITYUnsupportedVersion.CreateFmt('TCityHash.SetCityVersion: Unsupported version (%d).',[Ord(Value)]);
end;
//------------------------------------------------------------------------------
procedure TCityHash.SetCityVariant(Value: TCITYVariant);
begin
If Value in CityVariantsSupported(fCityVersion) then
fCityVariant := Value
else
raise ECITYUnsupportedVariant.CreateFmt('TCityHash.SetCityVariant: Unsupported variant (%d).',[Ord(Value)]);
end;
//------------------------------------------------------------------------------
procedure TCityHash.Initialize;
begin
inherited;
fCityVersion := verDefault;
fCityVariant := varPlain;
end;
//------------------------------------------------------------------------------
procedure TCityHash.CheckVersionAndVariant;
begin
If not(fCityVersion in CityVersionsSupported) then
raise ECITYUnsupportedVersion.CreateFmt('%s.CheckVersionAndVariant: Unsupported version (%d).',
[Self.ClassName,Ord(fCityVersion)]);
If not(fCityVariant in CityVariantsSupported(fCityVersion)) then
raise ECITYUnsupportedVariant.CreateFmt('%s.CheckVersionAndVariant: Unsupported variant (%d) for version %d.',
[Self.ClassName,Ord(fCityVariant),Ord(fCityVersion)]);
end;
{-------------------------------------------------------------------------------
TCityHash - public methods
-------------------------------------------------------------------------------}
class Function TCityHash.HashEndianness: THashEndianness;
begin
Result := heLittle;
end;
//------------------------------------------------------------------------------
class Function TCityHash.HashFinalization: Boolean;
begin
Result := True;
end;
//------------------------------------------------------------------------------
constructor TCityHash.CreateAndInitFrom(Hash: THashBase);
begin
inherited CreateAndInitFrom(Hash);
If Hash is TCityHash then
begin
CityVersion := TCityHash(Hash).CityVersion;
CityVariant := TCityHash(Hash).CityVariant;
end
else raise ECITYIncompatibleClass.CreateFmt('TCityHash.CreateAndInitFrom: Incompatible class (%s).',[Hash.ClassName]);
end;
{===============================================================================
--------------------------------------------------------------------------------
TCity32Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCity32Hash - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TCity32Hash - utility functions
-------------------------------------------------------------------------------}
Function SwapEndian(Value: TCITY32Sys): TCITY32Sys; overload;
begin
Result := TCITY32Sys(EndianSwap(UInt32(Value)));
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function SwapEndian(Value: TCITY32): TCITY32; overload;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := TCITY32(SwapEndian(TCITY32Sys(Value)));
end;
{-------------------------------------------------------------------------------
TCity32Hash - protected methods
-------------------------------------------------------------------------------}
Function TCity32Hash.GetCity32: TCity32;
begin
Result := City32FromSys(fCity32);
end;
//------------------------------------------------------------------------------
procedure TCity32Hash.CalculateHash(Memory: Pointer; Count: TMemSize);
begin
CheckVersionAndVariant;
case fCityVersion of
verCITY110: fCity32 := CITY_1_1_0.CityHash32(Memory,Count);
verDefault,
verLatest,
verCITY111: fCity32 := CITY_1_1_1.CityHash32(Memory,Count);
end;
end;
//------------------------------------------------------------------------------
procedure TCity32Hash.Initialize;
begin
inherited;
fCity32 := 0;
end;
{-------------------------------------------------------------------------------
TCity32Hash - public methods
-------------------------------------------------------------------------------}
class Function TCity32Hash.CityVersionsSupported: TCITYVersions;
begin
Result := [verCITY110,verDefault,verLatest,verCITY111];
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants;
begin
case CityVersion of
verDefault,
verLatest,
verCITY110,
verCITY111: Result := [varPlain];
else
Result := [];
end;
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.City32ToSys(City32: TCity32): TCity32Sys;
begin
Result := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(TCity32Sys(City32));
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.City32FromSys(City32: TCity32Sys): TCity32;
begin
Result := TCity32({$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(City32));
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.City32ToLE(City32: TCity32): TCity32;
begin
Result := City32;
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.City32ToBE(City32: TCity32): TCity32;
begin
Result := SwapEndian(City32);
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.City32FromLE(City32: TCity32): TCity32;
begin
Result := City32;
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.City32FromBE(City32: TCity32): TCity32;
begin
Result := SwapEndian(City32);
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.HashSize: TMemSize;
begin
Result := SizeOf(TCITY32);
end;
//------------------------------------------------------------------------------
class Function TCity32Hash.HashName: String;
begin
Result := 'CITY-32';
end;
//------------------------------------------------------------------------------
constructor TCity32Hash.CreateAndInitFrom(Hash: THashBase);
begin
inherited CreateAndInitFrom(Hash);
If Hash is TCity32Hash then
fCity32 := TCity32Hash(Hash).City32Sys
else
raise ECITYIncompatibleClass.CreateFmt('TCity32Hash.CreateAndInitFrom: Incompatible class (%s).',[Hash.ClassName]);
end;
//------------------------------------------------------------------------------
constructor TCity32Hash.CreateAndInitFrom(Hash: TCity32);
begin
CreateAndInit;
fCity32 := City32ToSys(Hash);
end;
//------------------------------------------------------------------------------
procedure TCity32Hash.Init;
begin
inherited;
fCity32 := 0;
end;
//------------------------------------------------------------------------------
Function TCity32Hash.Compare(Hash: THashBase): Integer;
begin
If Hash is TCity32Hash then
begin
If fCity32 > TCity32Hash(Hash).City32Sys then
Result := +1
else If fCity32 < TCity32Hash(Hash).City32Sys then
Result := -1
else
Result := 0;
end
else raise ECITYIncompatibleClass.CreateFmt('TCity32Hash.Compare: Incompatible class (%s).',[Hash.ClassName]);
end;
//------------------------------------------------------------------------------
Function TCity32Hash.AsString: String;
begin
Result := IntToHex(fCity32,8);
end;
//------------------------------------------------------------------------------
procedure TCity32Hash.FromString(const Str: String);
begin
If Length(Str) > 0 then
begin
If Str[1] = '$' then
fCity32 := TCity32Sys(StrToInt(Str))
else
fCity32 := TCity32Sys(StrToInt('$' + Str));
end
else fCity32 := 0;
end;
//------------------------------------------------------------------------------
procedure TCity32Hash.FromStringDef(const Str: String; const Default: TCity32);
begin
inherited FromStringDef(Str,Default);
If not TryFromString(Str) then
fCity32 := City32ToSys(Default);
end;
//------------------------------------------------------------------------------
procedure TCity32Hash.SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault);
var
Temp: TCity32;
begin
case Endianness of
heSystem: Temp := {$IFDEF ENDIAN_BIG}City32ToBE{$ELSE}City32ToLE{$ENDIF}(City32FromSys(fCity32));
heLittle: Temp := City32ToLE(City32FromSys(fCity32));
heBig: Temp := City32ToBE(City32FromSys(fCity32));
else
{heDefault}
Temp := City32FromSys(fCity32);
end;
Stream.WriteBuffer(Temp,SizeOf(TCity32));
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5057{$ENDIF}
procedure TCity32Hash.LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault);
var
Temp: TCity32;
begin
Stream.ReadBuffer(Temp,SizeOf(TCity32));
case Endianness of
heSystem: fCity32 := City32ToSys({$IFDEF ENDIAN_BIG}City32FromBE{$ELSE}City32FromLE{$ENDIF}(Temp));
heLittle: fCity32 := City32ToSys(City32FromLE(Temp));
heBig: fCity32 := City32ToSys(City32FromBE(Temp));
else
{heDefault}
fCity32 := City32ToSys(Temp);
end;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TCity64Hash
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TCity64Hash - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TCity64Hash - utility functions
-------------------------------------------------------------------------------}
Function SwapEndian(Value: TCITY64Sys): TCITY64Sys; overload;
begin
Result := TCITY64Sys(EndianSwap(UInt64(Value)));
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function SwapEndian(Value: TCITY64): TCITY64; overload;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := TCITY64(SwapEndian(TCITY64Sys(Value)));
end;
{-------------------------------------------------------------------------------
TCity64Hash - protected methods
-------------------------------------------------------------------------------}
Function TCity64Hash.GetCity64: TCity64;
begin
Result := City64FromSys(fCity64);
end;
//------------------------------------------------------------------------------
procedure TCity64Hash.CalculateHash(Memory: Pointer; Count: TMemSize);
begin
CheckVersionAndVariant;
case fCityVersion of
verCITY100: case fCityVariant of
varPlain: fCity64 := CITY_1_0_0.CityHash64(Memory,Count);
varSeed: fCity64 := CITY_1_0_0.CityHash64WithSeed(Memory,Count,fSeed0);
varSeeds: fCity64 := CITY_1_0_0.CityHash64WithSeeds(Memory,Count,fSeed0,fSeed1);
end;
verCITY101: case fCityVariant of
varPlain: fCity64 := CITY_1_0_1.CityHash64(Memory,Count);
varSeed: fCity64 := CITY_1_0_1.CityHash64WithSeed(Memory,Count,fSeed0);
varSeeds: fCity64 := CITY_1_0_1.CityHash64WithSeeds(Memory,Count,fSeed0,fSeed1);
end;
verCITY102: case fCityVariant of
varPlain: fCity64 := CITY_1_0_2.CityHash64(Memory,Count);
varSeed: fCity64 := CITY_1_0_2.CityHash64WithSeed(Memory,Count,fSeed0);
varSeeds: fCity64 := CITY_1_0_2.CityHash64WithSeeds(Memory,Count,fSeed0,fSeed1);
end;
verCITY103: case fCityVariant of
varPlain: fCity64 := CITY_1_0_3.CityHash64(Memory,Count);
varSeed: fCity64 := CITY_1_0_3.CityHash64WithSeed(Memory,Count,fSeed0);
varSeeds: fCity64 := CITY_1_0_3.CityHash64WithSeeds(Memory,Count,fSeed0,fSeed1);
end;
verCITY110: case fCityVariant of
varPlain: fCity64 := CITY_1_1_0.CityHash64(Memory,Count);
varSeed: fCity64 := CITY_1_1_0.CityHash64WithSeed(Memory,Count,fSeed0);
varSeeds: fCity64 := CITY_1_1_0.CityHash64WithSeeds(Memory,Count,fSeed0,fSeed1);
end;
verDefault,
verLatest,
verCITY111: case fCityVariant of
varPlain: fCity64 := CITY_1_1_1.CityHash64(Memory,Count);
varSeed: fCity64 := CITY_1_1_1.CityHash64WithSeed(Memory,Count,fSeed0);
varSeeds: fCity64 := CITY_1_1_1.CityHash64WithSeeds(Memory,Count,fSeed0,fSeed1);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TCity64Hash.Initialize;
begin
inherited;
fCity64 := 0;
end;
{-------------------------------------------------------------------------------
TCity64Hash - public methods
-------------------------------------------------------------------------------}
class Function TCity64Hash.CityVersionsSupported: TCITYVersions;
begin
Result := [verDefault,verLatest,verCITY100,verCITY101,verCITY102,verCITY103,verCITY110,verCITY111];
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.CityVariantsSupported(CityVersion: TCITYVersion): TCITYVariants;
begin
case CityVersion of
verDefault,
verLatest,
verCITY100,
verCITY101,
verCITY102,
verCITY103,
verCITY110,
verCITY111: Result := [varPlain,varSeed,varSeeds];
else
Result := [];
end;
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.City64ToSys(City64: TCity64): TCity64Sys;
begin
Result := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(TCity64Sys(City64));
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.City64FromSys(City64: TCity64Sys): TCity64;
begin
Result := TCity64({$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(City64));
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.City64ToLE(City64: TCity64): TCity64;
begin
Result := City64;
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.City64ToBE(City64: TCity64): TCity64;
begin
Result := SwapEndian(City64);
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.City64FromLE(City64: TCity64): TCity64;
begin
Result := City64;
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.City64FromBE(City64: TCity64): TCity64;
begin
Result := SwapEndian(City64);
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.HashSize: TMemSize;
begin
Result := SizeOf(TCITY64);
end;
//------------------------------------------------------------------------------
class Function TCity64Hash.HashName: String;
begin
Result := 'CITY-64';
end;
//------------------------------------------------------------------------------
constructor TCity64Hash.CreateAndInitFrom(Hash: THashBase);
begin
inherited CreateAndInitFrom(Hash);
If Hash is TCity64Hash then
fCity64 := TCity64Hash(Hash).City64Sys
else
raise ECITYIncompatibleClass.CreateFmt('TCity64Hash.CreateAndInitFrom: Incompatible class (%s).',[Hash.ClassName]);
end;
//------------------------------------------------------------------------------
constructor TCity64Hash.CreateAndInitFrom(Hash: TCity64);
begin
CreateAndInit;
fCity64 := City64ToSys(Hash);
end;
//------------------------------------------------------------------------------
procedure TCity64Hash.Init;
begin
inherited;
fCity64 := 0;
end;
//------------------------------------------------------------------------------
Function TCity64Hash.Compare(Hash: THashBase): Integer;
begin
If Hash is TCity64Hash then
Result := CompareUInt64(fCity64,TCity64Hash(Hash).City64Sys)
else
raise ECITYIncompatibleClass.CreateFmt('TCity64Hash.Compare: Incompatible class (%s).',[Hash.ClassName]);
end;