1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 |
|
---|
23 | namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
|
---|
24 | public static class HashUtil {
|
---|
25 | // This class contains some hash functions adapted from http://partow.net/programming/hashfunctions/index.html#AvailableHashFunctions
|
---|
26 |
|
---|
27 | // A simple hash function from Robert Sedgwicks Algorithms in C book.I've added some simple optimizations to the algorithm in order to speed up its hashing process.
|
---|
28 | public static ulong RSHash(ulong[] input) {
|
---|
29 | const int b = 378551;
|
---|
30 | ulong a = 63689;
|
---|
31 | ulong hash = 0;
|
---|
32 |
|
---|
33 | foreach (var v in input) {
|
---|
34 | hash = (hash * a) + v;
|
---|
35 | a *= b;
|
---|
36 | }
|
---|
37 | return hash;
|
---|
38 | }
|
---|
39 |
|
---|
40 | // A bitwise hash function written by Justin Sobel
|
---|
41 | public static ulong JSHash(ulong[] input) {
|
---|
42 | ulong hash = 1315423911;
|
---|
43 | for (int i = 0; i < input.Length; ++i)
|
---|
44 | hash ^= (hash << 5) + input[i] + (hash >> 2);
|
---|
45 | return hash;
|
---|
46 | }
|
---|
47 |
|
---|
48 | // This hash function comes from Brian Kernighan and Dennis Ritchie's book "The C Programming Language". It is a simple hash function using a strange set of possible seeds which all constitute a pattern of 31....31...31 etc, it seems to be very similar to the DJB hash function.
|
---|
49 | public static ulong BKDRHash(ulong[] input) {
|
---|
50 | ulong seed = 131;
|
---|
51 | ulong hash = 0;
|
---|
52 | foreach (var v in input) {
|
---|
53 | hash = (hash * seed) + v;
|
---|
54 | }
|
---|
55 | return hash;
|
---|
56 | }
|
---|
57 |
|
---|
58 | // This is the algorithm of choice which is used in the open source SDBM project. The hash function seems to have a good over-all distribution for many different data sets. It seems to work well in situations where there is a high variance in the MSBs of the elements in a data set.
|
---|
59 | public static ulong SDBMHash(ulong[] input) {
|
---|
60 | ulong hash = 0;
|
---|
61 | foreach (var v in input) {
|
---|
62 | hash = v + (hash << 6) + (hash << 16) - hash;
|
---|
63 | }
|
---|
64 | return hash;
|
---|
65 | }
|
---|
66 |
|
---|
67 | // An algorithm produced by Professor Daniel J. Bernstein and shown first to the world on the usenet newsgroup comp.lang.c. It is one of the most efficient hash functions ever published.
|
---|
68 | public static ulong DJBHash(ulong[] input) {
|
---|
69 | ulong hash = 5381;
|
---|
70 | foreach (var v in input) {
|
---|
71 | hash = (hash << 5) + hash + v;
|
---|
72 | }
|
---|
73 | return hash;
|
---|
74 | }
|
---|
75 |
|
---|
76 | // An algorithm proposed by Donald E.Knuth in The Art Of Computer Programming Volume 3, under the topic of sorting and search chapter 6.4.
|
---|
77 | public static ulong DEKHash(ulong[] input) {
|
---|
78 | ulong hash = (ulong)input.Length;
|
---|
79 | foreach (var v in input) {
|
---|
80 | hash = (hash << 5) ^ (hash >> 27) ^ v;
|
---|
81 | }
|
---|
82 | return hash;
|
---|
83 | }
|
---|
84 |
|
---|
85 | //public static ulong CryptoHash(HashAlgorithm ha, ulong[] input) {
|
---|
86 | // return BitConverter.ToInt32(ha.ComputeHash(input.ToByteArray()), 0);
|
---|
87 | //}
|
---|
88 |
|
---|
89 | //public static byte[] ToByteArray(this ulong[] input) {
|
---|
90 | // var bytes = new byte[input.Length * sizeof(int)];
|
---|
91 | // int pos = 0;
|
---|
92 | // foreach (var v in input) {
|
---|
93 | // var b0 = (byte)((v >> 24) & 0xFF);
|
---|
94 | // var b1 = (byte)((v >> 16) & 0xFF);
|
---|
95 | // var b2 = (byte)((v >> 8) & 0xFF);
|
---|
96 | // var b3 = (byte)(v & 0xFF);
|
---|
97 | // bytes[pos++] = b0;
|
---|
98 | // bytes[pos++] = b1;
|
---|
99 | // bytes[pos++] = b2;
|
---|
100 | // bytes[pos++] = b3;
|
---|
101 | // }
|
---|
102 | // return bytes;
|
---|
103 | //}
|
---|
104 | }
|
---|
105 | }
|
---|