-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathLongHashFunction.java
More file actions
850 lines (790 loc) · 38.6 KB
/
LongHashFunction.java
File metadata and controls
850 lines (790 loc) · 38.6 KB
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
/*
* Copyright 2014 Higher Frequency Trading http://www.higherfrequencytrading.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.openhft.hashing;
import org.jetbrains.annotations.NotNull;
import sun.nio.ch.DirectBuffer;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import static net.openhft.hashing.CharSequenceAccess.nativeCharSequenceAccess;
import static net.openhft.hashing.UnsafeAccess.*;
import static net.openhft.hashing.Util.VALID_STRING_HASH;
import static net.openhft.hashing.Util.checkArrayOffs;
/**
* Hash function producing {@code long}-valued result from byte sequences of any length and
* a plenty of different sources which "feels like byte sequences". Except {@link
* #hashBytes(byte[])}, {@link #hashBytes(ByteBuffer)} (with their "sliced" versions) and
* {@link #hashMemory(long, long)} methods, which actually accept byte sequences, notion of byte
* sequence is defined as follows:
* <ul>
* <li>For methods accepting arrays of Java primitives, {@code String}s and
* {@code StringBuilder}s, byte sequence is how the input's bytes are actually lay in memory.
* </li>
* <li>For methods accepting single primitive values, byte sequence is how this primitive
* would be put into memory with {@link ByteOrder#nativeOrder() native} byte order, or
* equivalently, {@code hashXxx(primitive)} has always the same result as {@code
* hashXxxs(new xxx[] {primitive})}, where "xxx" is any Java primitive type name.</li>
* <li>For {@link #hash(Object, Access, long, long)} method byte sequence abstraction
* is defined by the given {@link Access} strategy to the given object.</li>
* </ul>
*
* <p>Hash function implementation could either produce equal results for equal input on platforms
* with different {@link ByteOrder}, favoring one byte order in terms of performance, or different
* results, but performing equally well. This choice should be explicitly documented for all
* {@code LongHashFunction} implementations.
*
* <h2>Subclassing</h2>
* To implement a specific hash function algorithm, this class should be subclassed. Only methods
* that accept single primitives, {@link #hashVoid()}, and {@link #hash(Object, Access, long, long)}
* should be implemented; others have default implementations which ultimately delegate to the
* {@link #hash(Object, Access, long, long)} abstract method.
*
* <p>Notes about how exactly methods with default implementations are implemented in doc comments
* are given for information and could be changed at any moment. However, it could hardly cause
* any issues with subclassing, except probably little performance degradation. Methods documented
* as "shortcuts" could either delegate to the referenced method or delegate directly to the method
* to which the referenced method delegates.
*
* <p>{@code LongHashFunction} implementations shouldn't assume that {@code Access} strategies
* do defensive checks and access only bytes within the requested range.
*/
public abstract class LongHashFunction implements Serializable {
private static final long serialVersionUID = 0L;
/**
* Returns a {@code LongHashFunction} that implements the
* <a href="https://github.com/google/cityhash/blob/8af9b8c2b889d80c22d6bc26ba0df1afb79a30db/src/city.cc">
* CityHash64 algorithm, version 1.1</a> without seed values. This implementation produce
* equal results for equal input on platforms with different {@link ByteOrder}, but is slower
* on big-endian platforms than on little-endian.
*
* @return a {@code LongHashFunction} implementing the CityHash64 algorithm, version 1.1, without seed values
* @see #city_1_1(long)
* @see #city_1_1(long, long)
*/
public static LongHashFunction city_1_1() {
return CityAndFarmHash_1_1.asLongHashFunctionWithoutSeed();
}
/**
* Returns a hash function implementing the
* <a href="https://github.com/google/cityhash/blob/8af9b8c2b889d80c22d6bc26ba0df1afb79a30db/src/city.cc">
* CityHash64 algorithm, version 1.1</a> using the given seed value. This implementation produce
* equal results for equal input on platforms with different {@link ByteOrder}, but is slower
* on big-endian platforms than on little-endian.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the CityHash64 algorithm, version 1.1, with the provided seed
* @see #city_1_1()
* @see #city_1_1(long, long)
*/
public static LongHashFunction city_1_1(long seed) {
return CityAndFarmHash_1_1.asLongHashFunctionWithSeed(seed);
}
/**
* Returns a hash function implementing the
* <a href="https://github.com/google/cityhash/blob/8af9b8c2b889d80c22d6bc26ba0df1afb79a30db/src/city.cc">
* CityHash64 algorithm, version 1.1</a> using the two given seed values. This implementation
* produce equal results for equal input on platforms with different {@link ByteOrder}, but
* is slower on big-endian platforms than on little-endian.
*
* @param seed0 the first seed value to be used for hashing
* @param seed1 the second seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the CityHash64 algorithm, version 1.1, with the provided seed values
* @see #city_1_1()
* @see #city_1_1(long)
*/
public static LongHashFunction city_1_1(long seed0, long seed1) {
return CityAndFarmHash_1_1.asLongHashFunctionWithTwoSeeds(seed0, seed1);
}
/**
* Returns a hash function implementing so-called
* <a href="https://github.com/google/farmhash/blob/a371645d2caa1685541d9963b94751c23b235c72/dev/farmhashna.cc">
* farmhashna algorithm</a>, without seed values. This implementation produces equal results for
* equal input on platforms with different {@link ByteOrder}, but is slower on big-endian
* platforms than on little-endian.
*
* <p>{@code farmhashna} was introduced in FarmHash 1.0. For inputs shorter than 32 bytes it's
* output is equivalent to {@link #city_1_1()} output.
*
* @return a {@code LongHashFunction} implementing the farmhashna algorithm without seed values
* @see #farmNa(long)
* @see #farmNa(long, long)
*/
public static LongHashFunction farmNa() {
return CityAndFarmHash_1_1.naWithoutSeeds();
}
/**
* Returns a hash function implementing so-called
* <a href="https://github.com/google/farmhash/blob/a371645d2caa1685541d9963b94751c23b235c72/dev/farmhashna.cc">
* farmhashna algorithm</a>, using the given seed value. This implementation produces equal
* results for equal input on platforms with different {@link ByteOrder}, but is slower on
* big-endian platforms than on little-endian.
*
* <p>{@code farmhashna} was introduced in FarmHash 1.0. For inputs shorter than 32 bytes it's
* output is equivalent to {@link #city_1_1(long)} output.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the farmhashna algorithm with the given seed value
* @see #farmNa()
* @see #farmNa(long, long)
*/
public static LongHashFunction farmNa(long seed) {
return CityAndFarmHash_1_1.naWithSeed(seed);
}
/**
* Returns a hash function implementing the so-called
* <a href="https://github.com/google/farmhash/blob/a371645d2caa1685541d9963b94751c23b235c72/dev/farmhashna.cc">
* farmhashna algorithm</a> using the two given seed values. This implementation produces equal
* results for equal input on platforms with different {@link ByteOrder}, but is slower on
* big-endian platforms than on little-endian.
*
* <p>{@code farmhashna} was introduced in FarmHash 1.0. For inputs shorter than 32 bytes, the
* output is equivalent to {@link #city_1_1(long, long)} output.
*
* @param seed0 the first seed value to be used for hashing
* @param seed1 the second seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the farmhashna algorithm with the two given seed values
* @see #farmNa()
* @see #farmNa(long)
*/
public static LongHashFunction farmNa(long seed0, long seed1) {
return CityAndFarmHash_1_1.naWithSeeds(seed0, seed1);
}
/**
* Returns a hash function implementing the so-called
* <a href="https://github.com/google/farmhash/blob/34c13ddfab0e35422f4c3979f360635a8c050260/dev/farmhashuo.cc">
* farmhashuo algorithm</a> without seed values. This implementation produces equal results for
* equal input on platforms with different {@link ByteOrder}, but is slower on big-endian
* platforms than on little-endian.
*
* <p>{@code farmhashuo} was introduced in FarmHash 1.1.
*
* @return a {@code LongHashFunction} implementing the farmhashuo algorithm without seed values
* @see #farmUo(long)
* @see #farmUo(long, long)
*/
public static LongHashFunction farmUo() {
return CityAndFarmHash_1_1.uoWithoutSeeds();
}
/**
* Returns a hash function implementing the so-called
* <a href="https://github.com/google/farmhash/blob/34c13ddfab0e35422f4c3979f360635a8c050260/dev/farmhashuo.cc">
* farmhashuo algorithm</a> with the given seed value. This implementation produces equal results
* for equal input on platforms with different {@link ByteOrder}, but is slower on big-endian
* platforms than on little-endian.
*
* <p>{@code farmhashuo} was introduced in FarmHash 1.1.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the farmhashuo algorithm with the given seed value
* @see #farmUo()
* @see #farmUo(long, long)
*/
public static LongHashFunction farmUo(long seed) {
return CityAndFarmHash_1_1.uoWithSeed(seed);
}
/**
* Returns a hash function implementing the so-called
* <a href="https://github.com/google/farmhash/blob/34c13ddfab0e35422f4c3979f360635a8c050260/dev/farmhashuo.cc">
* farmhashuo algorithm</a> with the two given seed values. This implementation produces equal
* results for equal input on platforms with different {@link ByteOrder}, but is slower on
* big-endian platforms than on little-endian.
*
* <p>{@code farmhashuo} was introduced in FarmHash 1.1.
*
* @param seed0 the first seed value to be used for hashing
* @param seed1 the second seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the farmhashuo algorithm with the two given seed values
* @see #farmUo()
* @see #farmUo(long)
*/
public static LongHashFunction farmUo(long seed0, long seed1) {
return CityAndFarmHash_1_1.uoWithSeeds(seed0, seed1);
}
/**
* Returns a 64-bit hash function implementing the
* <a href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">MurmurHash3
* algorithm</a> without seed values. This implementation produces equal results for equal input
* on platforms with different {@link ByteOrder}, but is slower on big-endian platforms than on
* little-endian.
*
* @return a {@code LongHashFunction} implementing the MurmurHash3 algorithm without seed values
* @see #murmur_3(long)
*/
public static LongHashFunction murmur_3() {
return MurmurHash_3.asLongHashFunctionWithoutSeed();
}
/**
* Returns a 64-bit hash function implementing the
* <a href="https://github.com/aappleby/smhasher/blob/master/src/MurmurHash3.cpp">MurmurHash3
* algorithm</a> with the given seed value. This implementation produces equal results for equal
* input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms
* than on little-endian.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the MurmurHash3 algorithm with the given seed value
* @see #murmur_3()
*/
public static LongHashFunction murmur_3(long seed) {
return MurmurHash_3.asLongHashFunctionWithSeed(seed);
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">xxHash
* algorithm</a> without a seed value (0 is used as default seed value). This implementation
* produces equal results for equal input on platforms with different {@link
* ByteOrder}, but is slower on big-endian platforms than on little-endian.
*
* @return a {@code LongHashFunction} implementing the xxHash algorithm without a seed value
* @see #xx(long)
*/
public static LongHashFunction xx() {
return XxHash.asLongHashFunctionWithoutSeed();
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">xxHash
* algorithm</a> with the given seed value. This implementation produces equal results for equal
* input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms
* than on little-endian.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the xxHash algorithm with the given seed value
* @see #xx()
*/
public static LongHashFunction xx(long seed) {
return XxHash.asLongHashFunctionWithSeed(seed);
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">xxHash
* algorithm</a> without a seed value (0 is used as default seed value). This implementation
* produces equal results for equal input on platforms with different {@link
* ByteOrder}, but is slower on big-endian platforms than on little-endian.
*
* @deprecated use {link #xx()} instead; this method exists for backwards compatibility.
*/
@Deprecated
public static LongHashFunction xx_r39() {
return xx();
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">xxHash
* algorithm</a> with the given seed value. This implementation produces equal results for equal
* input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms
* than on little-endian.
*
* @deprecated use {link #xx(long)} instead; this method exists for backwards compatibility.
*/
@Deprecated
public static LongHashFunction xx_r39(long seed) {
return xx(seed);
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">XXH3 64bit
* algorithm</a> without a seed value (0 is used as default seed value). This implementation
* produces equal results for equal input on platforms with different {@link
* ByteOrder}, but is slower on big-endian platforms than on little-endian.
*
* @return a {@code LongHashFunction} implementing the XXH3 64-bit algorithm without a seed value
* @see #xx3(long)
*/
public static LongHashFunction xx3() {
return XXH3.asLongHashFunctionWithoutSeed();
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">XXH3 64bit
* algorithm</a> with the given seed value. This implementation produces equal results for equal
* input on platforms with different {@link ByteOrder}, but is slower on big-endian platforms
* than on little-endian.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the XXH3 64-bit algorithm with the given seed value
* @see #xx3()
*/
public static LongHashFunction xx3(final long seed) {
return XXH3.asLongHashFunctionWithSeed(seed);
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">XXH128 low
* 64bit algorithm</a> without a seed value (0 is used as default seed value). This
* implementation produces equal results for equal input on platforms with different {@link
* ByteOrder}, but is slower on big-endian platforms than on little-endian.
*
* @return a {@code LongHashFunction} implementing the XXH128 low 64bit algorithm without a seed value
* @see #xx128low(long)
*/
public static LongHashFunction xx128low() {
return XXH3.asLongTupleLowHashFunctionWithoutSeed();
}
/**
* Returns a hash function implementing the <a href="https://github.com/Cyan4973/xxHash">XXH128 low
* 64bit algorithm</a> with the given seed value. This implementation produces equal results for
* equal input on platforms with different {@link ByteOrder}, but is slower on big-endian
* platforms than on little-endian.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the XXH128 low 64bit algorithm with the given seed value
* @see #xx128low()
*/
public static LongHashFunction xx128low(final long seed) {
return XXH3.asLongTupleLowHashFunctionWithSeed(seed);
}
/**
* Returns a hash function implementing the
* <a href="https://github.com/wangyi-fudan/wyhash/blob/9f68c1b10166a54c17f55b284c21bd455fd0f7e2/wyhash.h">
* wyhash algorithm, version 3</a> without a seed value (0 is used as default seed value). This
* implementation produces equal results for equal input on platforms with different {@link
* ByteOrder}, but is slower on big-endian platforms than on little-endian.
*
* @return a {@code LongHashFunction} implementing the wyhash algorithm, version 3, without a seed value
* @see #wy_3(long)
*/
public static LongHashFunction wy_3() {
return WyHash.asLongHashFunctionWithoutSeed();
}
/**
* Returns a hash function implementing the
* <a href="https://github.com/wangyi-fudan/wyhash/blob/9f68c1b10166a54c17f55b284c21bd455fd0f7e2/wyhash.h">
* wyhash algorithm, version 3</a> with the given seed value. This implementation produces equal
* results for equal input on platforms with different {@link ByteOrder}, but is slower on
* big-endian platforms than on little-endian.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the wyhash algorithm, version 3, with the given seed value
* @see #wy_3()
*/
public static LongHashFunction wy_3(long seed) {
return WyHash.asLongHashFunctionWithSeed(seed);
}
/**
* Returns a hash function implementing the 64 bit version of
* <a href="https://github.com/jandrewrogers/MetroHash">metrohash algorithm</a> without
* a seed value (0 is used as default seed value), with the initialization vector for
* metrohash64_2. This implementation produces equal results for equal input on platforms with
* different {@link ByteOrder}, but is slower on big-endian platforms than on little-endian.
*
* @return a {@code LongHashFunction} implementing the 64 bit version of metrohash algorithm without a seed value
* @see #metro(long)
*/
public static LongHashFunction metro() {
return MetroHash.asLongHashFunctionWithoutSeed();
}
/**
* Returns a hash function implementing the 64 bit version of
* <a href="https://github.com/jandrewrogers/MetroHash">metrohash algorithm</a> with the given
* seed value, with the initialization vector for metrohash64_2. This implementation produces
* equal results for equal input on platforms with different {@link ByteOrder}, but is slower on
* big-endian platforms than on little-endian.
*
* @param seed the seed value to be used for hashing
* @return a {@code LongHashFunction} implementing the 64 bit version of metrohash algorithm with the given seed value
* @see #metro()
*/
public static LongHashFunction metro(long seed) {
return MetroHash.asLongHashFunctionWithSeed(seed);
}
/**
* Constructor for use in subclasses.
*/
protected LongHashFunction() {
}
/**
* Returns the hash code for the given {@code long} value; this method is consistent with
* {@code LongHashFunction} methods that accept sequences of bytes, assuming the {@code input}
* value is interpreted in {@linkplain ByteOrder#nativeOrder() native} byte order. For example,
* the result of {@code hashLong(v)} call is identical to the result of
* {@code hashLongs(new long[] {v})} call for any {@code long} value.
*
* @param input the long value to be hashed
* @return the hash code for the given long value
*/
public abstract long hashLong(long input);
/**
* Returns the hash code for the given {@code int} value; this method is consistent with
* {@code LongHashFunction} methods that accept sequences of bytes, assuming the {@code input}
* value is interpreted in {@linkplain ByteOrder#nativeOrder() native} byte order. For example,
* the result of {@code hashInt(v)} call is identical to the result of
* {@code hashInts(new int[] {v})} call for any {@code int} value.
*
* @param input the int value to be hashed
* @return the hash code for the given int value
*/
public abstract long hashInt(int input);
/**
* Returns the hash code for the given {@code short} value; this method is consistent with
* {@code LongHashFunction} methods that accept sequences of bytes, assuming the {@code input}
* value is interpreted in {@linkplain ByteOrder#nativeOrder() native} byte order. For example,
* the result of {@code hashShort(v)} call is identical to the result of
* {@code hashShorts(new short[] {v})} call for any {@code short} value.
* As a consequence, {@code hashShort(v)} call produce always the same result as {@code
* hashChar((char) v)}.
*
* @param input the short value to be hashed
* @return the hash code for the given short value
*/
public abstract long hashShort(short input);
/**
* Returns the hash code for the given {@code char} value; this method is consistent with
* {@code LongHashFunction} methods that accept sequences of bytes, assuming the {@code input}
* value is interpreted in {@linkplain ByteOrder#nativeOrder() native} byte order. For example,
* the result of {@code hashChar(v)} call is identical to the result of
* {@code hashChars(new char[] {v})} call for any {@code char} value.
* As a consequence, {@code hashChar(v)} call produce always the same result as {@code
* hashShort((short) v)}.
*
* @param input the char value to be hashed
* @return the hash code for the given char value
*/
public abstract long hashChar(char input);
/**
* Returns the hash code for the given {@code byte} value. This method is consistent with
* {@code LongHashFunction} methods that accept sequences of bytes. For example, the result of
* {@code hashByte(v)} call is identical to the result of
* {@code hashBytes(new byte[] {v})} call for any {@code byte} value.
*
* @param input the byte value to be hashed
* @return the hash code for the given byte value
*/
public abstract long hashByte(byte input);
/**
* Returns the hash code for the empty (zero-length) bytes sequence,
* for example {@code hashBytes(new byte[0])}.
*
* @return the hash code for the empty bytes sequence
*/
public abstract long hashVoid();
/**
* Returns the hash code for {@code len} continuous bytes of the given {@code input} object,
* starting from the given offset. The abstraction of input as ordered byte sequence and
* "offset within the input" is defined by the given {@code access} strategy.
*
* <p>This method doesn't promise to throw a {@code RuntimeException} if {@code
* [off, off + len - 1]} subsequence exceeds the bounds of the bytes sequence, defined by {@code
* access} strategy for the given {@code input}, so use this method with caution.
*
* @param input the object to read bytes from
* @param access access which defines the abstraction of the given input
* as ordered byte sequence
* @param off offset to the first byte of the subsequence to hash
* @param len length of the subsequence to hash
* @param <T> the type of the input
* @return hash code for the specified bytes subsequence
*/
public abstract <T> long hash(T input, Access<T> access, long off, long len);
private long unsafeHash(Object input, long off, long len) {
return hash(input, UnsafeAccess.INSTANCE, off, len);
}
/**
* Shortcut for {@link #hashBooleans(boolean[]) hashBooleans(new boolean[] {input})}.
* Note that this is not necessarily equal to {@code hashByte(input ? (byte) 1 : (byte) 0)},
* because booleans could be stored differently in this JVM.
*
* @param input the boolean value to be hashed
* @return the hash code for the given boolean value
*/
public long hashBoolean(boolean input) {
return hashByte(input ? TRUE_BYTE_VALUE : FALSE_BYTE_VALUE);
}
/**
* Shortcut for {@link #hashBooleans(boolean[], int, int) hashBooleans(input, 0, input.length)}.
*
* @param input the boolean array to be hashed
* @return the hash code for the given boolean array
*/
public long hashBooleans(@NotNull boolean[] input) {
return unsafeHash(input, BOOLEAN_BASE, input.length);
}
/**
* Returns the hash code for the specified subsequence of the given {@code boolean} array.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@linkplain Access#unsafe() unsafe} {@code Access}.
*
* @param input the array to read data from
* @param off index of the first {@code boolean} in the subsequence to hash
* @param len length of the subsequence to hash
* @return hash code for the specified subsequence
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length}
* or {@code len < 0}
*/
public long hashBooleans(@NotNull boolean[] input, int off, int len) {
checkArrayOffs(input.length, off, len);
return unsafeHash(input, BOOLEAN_BASE + off, len);
}
/**
* Shortcut for {@link #hashBytes(byte[], int, int) hashBytes(input, 0, input.length)}.
*
* @param input the byte array to be hashed
* @return the hash code for the given byte array
*/
public long hashBytes(@NotNull byte[] input) {
return unsafeHash(input, BYTE_BASE, input.length);
}
/**
* Returns the hash code for the specified subsequence of the given {@code byte} array.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@linkplain Access#unsafe() unsafe} {@code Access}.
*
* @param input the array to read bytes from
* @param off index of the first {@code byte} in the subsequence to hash
* @param len length of the subsequence to hash
* @return hash code for the specified subsequence
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length}
* or {@code len < 0}
*/
public long hashBytes(@NotNull byte[] input, int off, int len) {
checkArrayOffs(input.length, off, len);
return unsafeHash(input, BYTE_BASE + off, len);
}
/**
* Shortcut for {@link #hashBytes(ByteBuffer, int, int)
* hashBytes(input, input.position(), input.remaining())}.
*
* @param input the ByteBuffer to be hashed
* @return the hash code for the given ByteBuffer
*/
public long hashBytes(ByteBuffer input) {
return hashByteBuffer(input, input.position(), input.remaining());
}
/**
* Returns the hash code for the specified subsequence of the given {@code ByteBuffer}.
*
* <p>This method doesn't alter the state (mark, position, limit or order) of the given
* {@code ByteBuffer}.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@link Access#toByteBuffer()}.
*
* @param input the buffer to read bytes from
* @param off index of the first {@code byte} in the subsequence to hash
* @param len length of the subsequence to hash
* @return hash code for the specified subsequence
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.capacity()}
* or {@code len < 0}
*/
public long hashBytes(@NotNull ByteBuffer input, int off, int len) {
checkArrayOffs(input.capacity(), off, len);
return hashByteBuffer(input, off, len);
}
private long hashByteBuffer(@NotNull ByteBuffer input, int off, int len) {
if (input.hasArray()) {
return unsafeHash(input.array(), BYTE_BASE + input.arrayOffset() + off, len);
} else if (input instanceof DirectBuffer) {
return unsafeHash(null, ((DirectBuffer) input).address() + off, len);
} else {
return hash(input, ByteBufferAccess.INSTANCE, off, len);
}
}
/**
* Returns the hash code of bytes of the wild memory from the given address. Use with caution.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@linkplain Access#unsafe() unsafe} {@code Access}.
*
* @param address the address of the first byte to hash
* @param len length of the byte sequence to hash
* @return hash code for the specified byte sequence
*/
public long hashMemory(long address, long len) {
return unsafeHash(null, address, len);
}
/**
* Shortcut for {@link #hashChars(char[], int, int) hashChars(input, 0, input.length)}.
*
* @param input the char array to be hashed
* @return the hash code for the given char array
*/
public long hashChars(@NotNull char[] input) {
return unsafeHash(input, CHAR_BASE, input.length * 2L);
}
/**
* Returns the hash code for bytes, as they lay in memory, of the specified subsequence
* of the given {@code char} array.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@linkplain Access#unsafe() unsafe} {@code Access}.
*
* @param input the array to read data from
* @param off index of the first {@code char} in the subsequence to hash
* @param len length of the subsequence to hash, in chars (i. e. the length of the bytes
* sequence to hash is {@code len * 2L})
* @return hash code for the specified subsequence
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length}
* or {@code len < 0}
*/
public long hashChars(@NotNull char[] input, int off, int len) {
checkArrayOffs(input.length, off, len);
return unsafeHash(input, CHAR_BASE + (off * 2L), len * 2L);
}
/**
* Shortcut for {@link #hashChars(String, int, int) hashChars(input, 0, input.length())}.
*
* @param input the String to be hashed
* @return the hash code for the given String
*/
public long hashChars(@NotNull String input) {
return VALID_STRING_HASH.longHash(input, this, 0, input.length());
}
/**
* Returns the hash code for bytes of the specified subsequence of the given {@code String}'s
* underlying {@code char} array.
*
* <p>Default implementation could either delegate to {@link #hash(Object, Access, long, long)}
* using {@link Access#toNativeCharSequence()}, or to {@link #hashChars(char[], int, int)}.
*
* @param input the string which bytes to hash
* @param off index of the first {@code char} in the subsequence to hash
* @param len length of the subsequence to hash, in chars (i. e. the length of the bytes
* sequence to hash is {@code len * 2L})
* @return the hash code of the given {@code String}'s bytes
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length()}
* or {@code len < 0}
*/
public long hashChars(@NotNull String input, int off, int len) {
checkArrayOffs(input.length(), off, len);
return VALID_STRING_HASH.longHash(input, this, off, len);
}
/**
* Shortcut for {@link #hashChars(StringBuilder, int, int) hashChars(input, 0, input.length())}.
*
* @param input the StringBuilder to be hashed
* @return the hash code for the given StringBuilder
*/
public long hashChars(@NotNull StringBuilder input) {
return hashNativeChars(input);
}
/**
* Returns the hash code for bytes of the specified subsequence of the given
* {@code StringBuilder}'s underlying {@code char} array.
*
* <p>Default implementation could either delegate to {@link #hash(Object, Access, long, long)}
* using {@link Access#toNativeCharSequence()}, or to {@link #hashChars(char[], int, int)}.
*
* @param input the string builder which bytes to hash
* @param off index of the first {@code char} in the subsequence to hash
* @param len length of the subsequence to hash, in chars (i. e. the length of the bytes
* sequence to hash is {@code len * 2L})
* @return the hash code of the given {@code String}'s bytes
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length()}
* or {@code len < 0}
*/
public long hashChars(@NotNull StringBuilder input, int off, int len) {
checkArrayOffs(input.length(), off, len);
return hashNativeChars(input, off, len);
}
/**
* Returns the hash code for the entire CharSequence.
*
* @param input the CharSequence to be hashed
* @return the hash code for the given CharSequence
*/
long hashNativeChars(CharSequence input) {
return hashNativeChars(input, 0, input.length());
}
/**
* Returns the hash code for a subsequence of the given CharSequence.
*
* @param input the CharSequence to be hashed
* @param off the index of the first char in the subsequence
* @param len the length of the subsequence
* @return the hash code for the specified subsequence of the given CharSequence
*/
long hashNativeChars(CharSequence input, int off, int len) {
return hash(input, nativeCharSequenceAccess(), off * 2L, len * 2L);
}
/**
* Shortcut for {@link #hashShorts(short[], int, int) hashShorts(input, 0, input.length)}.
*
* @param input the short array to be hashed
* @return the hash code for the given short array
*/
public long hashShorts(@NotNull short[] input) {
return unsafeHash(input, SHORT_BASE, input.length * 2L);
}
/**
* Returns the hash code for bytes, as they lay in memory, of the specified subsequence
* of the given {@code short} array.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@linkplain Access#unsafe() unsafe} {@code Access}.
*
* @param input the array to read data from
* @param off index of the first {@code short} in the subsequence to hash
* @param len length of the subsequence to hash, in shorts (i. e. the length of the bytes
* sequence to hash is {@code len * 2L})
* @return hash code for the specified subsequence
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length}
* or {@code len < 0}
*/
public long hashShorts(@NotNull short[] input, int off, int len) {
checkArrayOffs(input.length, off, len);
return unsafeHash(input, SHORT_BASE + (off * 2L), len * 2L);
}
/**
* Shortcut for {@link #hashInts(int[], int, int) hashInts(input, 0, input.length)}.
*
* @param input the integer array to be hashed
* @return the hash code for the given integer array
*/
public long hashInts(@NotNull int[] input) {
return unsafeHash(input, INT_BASE, input.length * 4L);
}
/**
* Returns the hash code for bytes, as they lay in memory, of the specified subsequence
* of the given {@code int} array.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@linkplain Access#unsafe() unsafe} {@code Access}.
*
* @param input the array to read data from
* @param off index of the first {@code int} in the subsequence to hash
* @param len length of the subsequence to hash, in ints (i. e. the length of the bytes
* sequence to hash is {@code len * 4L})
* @return hash code for the specified subsequence
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length}
* or {@code len < 0}
*/
public long hashInts(@NotNull int[] input, int off, int len) {
checkArrayOffs(input.length, off, len);
return unsafeHash(input, INT_BASE + (off * 4L), len * 4L);
}
/**
* Shortcut for {@link #hashLongs(long[], int, int) hashLongs(input, 0, input.length)}.
*
* @param input the long array to be hashed
* @return the hash code for the given long array
*/
public long hashLongs(@NotNull long[] input) {
return unsafeHash(input, LONG_BASE, input.length * 8L);
}
/**
* Returns the hash code for bytes, as they lay in memory, of the specified subsequence
* of the given {@code long} array.
*
* <p>Default implementation delegates to {@link #hash(Object, Access, long, long)} method
* using {@linkplain Access#unsafe() unsafe} {@code Access}.
*
* @param input the array to read data from
* @param off index of the first {@code long} in the subsequence to hash
* @param len length of the subsequence to hash, in longs (i. e. the length of the bytes
* sequence to hash is {@code len * 8L})
* @return hash code for the specified subsequence
* @throws IndexOutOfBoundsException if {@code off < 0} or {@code off + len > input.length}
* or {@code len < 0}
*/
public long hashLongs(@NotNull long[] input, int off, int len) {
checkArrayOffs(input.length, off, len);
return unsafeHash(input, LONG_BASE + (off * 8L), len * 8L);
}
}