Assignment Search Framework
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
permutation_hash_function.h
Go to the documentation of this file.
1 /*
2  * permutation_hash_function.h
3  *
4  * LICENSE HERE
5  *
6  * Created on: 2016-09-23
7  * Author: Rick Valenzano
8  */
9 
10 #ifndef PERMUTATION_HASH_FUNCTION_H_
11 #define PERMUTATION_HASH_FUNCTION_H_
12 
13 #include "state_hash_function.h"
14 #include "../utils/combinatorics.h"
15 #include <vector>
16 
17 template<class state_t>
19 {
20 public:
22  virtual ~PermutationHashFunction();
23 
24  virtual StateHash getStateHash(const state_t &state) const;
25 };
26 
27 template<class state_t>
29 {
30 }
31 
32 template<class state_t>
34 {
35 }
36 
37 template<class state_t>
39 {
40  std::vector<unsigned> permutation = state.permutation;
41 
42  StateHash hash_value = 0;
43  unsigned num_left = permutation.size();
44  for(unsigned i = 0; i < permutation.size(); i++) {
45  hash_value += permutation[i] * get_64_bit_factorial(num_left - 1);
46  num_left--;
47  for(unsigned j = i + 1; j < permutation.size(); j++) {
48  if(permutation[j] > permutation[i])
49  permutation[j]--;
50  }
51  }
52  return hash_value;
53 }
54 
55 #endif /* PERMUTATION_HASH_FUNCTION_H_ */
uint64_t StateHash
The hash value of a state.
Definition: state_hash_function.h:14
uint64_t get_64_bit_factorial(unsigned n)
Definition: combinatorics.cpp:106
virtual StateHash getStateHash(const state_t &state) const
Definition: permutation_hash_function.h:38
Definition: permutation_hash_function.h:18
Definition: state_hash_function.h:22
PermutationHashFunction()
Definition: permutation_hash_function.h:28
virtual ~PermutationHashFunction()
Definition: permutation_hash_function.h:33