Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Hashing/HashUtil.cs @ 16477

Last change on this file since 16477 was 16272, checked in by bburlacu, 6 years ago

#2950: Refactor hash extensions and utility methods: hashes are computed from byte[] arrays, simplification accepts an argument specifying which hash function to use. Update SymbolicDataAnalysisBuildingBlockAnalyzer and SymbolicDataAnalysisExpressionDiversityPreservingCrossover.

File size: 3.6 KB
RevLine 
[16218]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
[16272]23using System;
24using System.Security.Cryptography;
25
[16218]26namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
27  public static class HashUtil {
28    // This class contains some hash functions adapted from http://partow.net/programming/hashfunctions/index.html#AvailableHashFunctions
29
30    // 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.
[16272]31    public static ulong RSHash(byte[] input) {
[16218]32      const int b = 378551;
[16263]33      ulong a = 63689;
34      ulong hash = 0;
[16218]35
36      foreach (var v in input) {
37        hash = (hash * a) + v;
38        a *= b;
39      }
40      return hash;
41    }
42
43    // A bitwise hash function written by Justin Sobel
[16272]44    public static ulong JSHash(byte[] input) {
[16263]45      ulong hash = 1315423911;
[16218]46      for (int i = 0; i < input.Length; ++i)
47        hash ^= (hash << 5) + input[i] + (hash >> 2);
48      return hash;
49    }
50
51    // 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.
[16272]52    public static ulong BKDRHash(byte[] input) {
[16263]53      ulong seed = 131;
54      ulong hash = 0;
[16218]55      foreach (var v in input) {
56        hash = (hash * seed) + v;
57      }
58      return hash;
59    }
60
61    // 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.
[16272]62    public static ulong SDBMHash(byte[] input) {
[16263]63      ulong hash = 0;
[16218]64      foreach (var v in input) {
65        hash = v + (hash << 6) + (hash << 16) - hash;
66      }
67      return hash;
68    }
69
70    // 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.
[16272]71    public static ulong DJBHash(byte[] input) {
[16263]72      ulong hash = 5381;
[16218]73      foreach (var v in input) {
74        hash = (hash << 5) + hash + v;
75      }
76      return hash;
77    }
78
79    // 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.
[16272]80    public static ulong DEKHash(byte[] input) {
[16263]81      ulong hash = (ulong)input.Length;
[16218]82      foreach (var v in input) {
83        hash = (hash << 5) ^ (hash >> 27) ^ v;
84      }
85      return hash;
86    }
87
[16272]88    public static ulong CryptoHash(HashAlgorithm ha, byte[] input) {
89      return BitConverter.ToUInt64(ha.ComputeHash(input), 0);
90    }
[16218]91  }
92}
Note: See TracBrowser for help on using the repository browser.