1 /*
2  * This file is part of ELKI:
3  * Environment for Developing KDD-Applications Supported by Index-Structures
4  *
5  * Copyright (C) 2018
6  * ELKI Development Team
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Affero General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Affero General Public License for more details.
17  *
18  * You should have received a copy of the GNU Affero General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 package de.lmu.ifi.dbs.elki.utilities;
22 
23 /**
24  * This class collects various static helper methods.
25  *
26  * For helper methods related to special application fields see other utilities
27  * classes.
28  *
29  * @author Erich Schubert
30  * @since 0.1
31  */
32 public final class Util {
33   /**
34    * Prime number used in hash code computation.
35    */
36   private static final long HASHPRIME = 2654435761L;
37 
38   /**
39    * Fake constructor: do not instantiate.
40    */
Util()41   private Util() {
42     // Do not instantiate.
43   }
44 
45   /**
46    * Mix multiple hashcodes into one.
47    *
48    * @param hash1 First hashcode to mix
49    * @param hash2 Second hashcode to mix
50    * @return Mixed hash code
51    */
mixHashCodes(int hash1, int hash2)52   public static int mixHashCodes(int hash1, int hash2) {
53     return (int) (hash1 * HASHPRIME + hash2);
54   }
55 
56   /**
57    * Mix multiple hashcodes into one.
58    *
59    * @param hash1 First hashcode to mix
60    * @param hash2 Second hashcode to mix
61    * @param hash3 Third hashcode to mix
62    * @return Mixed hash code
63    */
mixHashCodes(int hash1, int hash2, int hash3)64   public static int mixHashCodes(int hash1, int hash2, int hash3) {
65     long result = hash1 * HASHPRIME + hash2;
66     return (int) (result * HASHPRIME + hash3);
67   }
68 
69   /**
70    * Mix multiple hashcodes into one.
71    *
72    * @param hash Hashcodes to mix
73    * @return Mixed hash code
74    */
mixHashCodes(int... hash)75   public static int mixHashCodes(int... hash) {
76     if(hash.length == 0) {
77       return 0;
78     }
79     long result = hash[0];
80     for(int i = 1; i < hash.length; i++) {
81       result = result * HASHPRIME + hash[i];
82     }
83     return (int) result;
84   }
85 }
86