HOG2
MR1Permutation.cpp
Go to the documentation of this file.
1 //
2 // MR1Permutation.cpp
3 // hog2 glut
4 //
5 // Created by Nathan Sturtevant on 6/19/16.
6 // Copyright © 2016 University of Denver. All rights reserved.
7 //
8 
9 #include "MR1Permutation.h"
10 #include <assert.h>
11 #include <string.h>
12 
13 template <typename T>
14 inline void swap(T &a, T &b)
15 {
16  T tmp = a;
17  a = b;
18  b = tmp;
19 }
20 
21 uint64_t MR1KPermutation::Rank(int *locs, int *dual, int distinctSize, int puzzleSize) const
22 {
23  uint64_t result2 = 0;
24  uint64_t multiplier = 1;
25  for (int i = 0; i < distinctSize; i++)
26  {
27  int tmp = dual[i];
28  unsigned int tmp2 = locs[i];
29 
30  result2 += (tmp-i)*multiplier;
31  multiplier *= (puzzleSize-i);
32 
33  if (tmp2 < puzzleSize)
34  {
35  swap(locs[i], locs[dual[i]]);
36  swap(dual[tmp2], dual[i]);
37  }
38  }
39 
40  return result2;
41 }
42 
43 uint64_t MR1KPermutation::Rank(uint8_t *locs, uint8_t *dual, int distinctSize, int puzzleSize) const
44 {
45  uint64_t result2 = 0;
46  uint64_t multiplier = 1;
47  for (int i = 0; i < distinctSize; i++)
48  {
49  int tmp = dual[i];
50  unsigned int tmp2 = locs[i];
51 
52  result2 += (tmp-i)*multiplier;
53  multiplier *= (puzzleSize-i);
54 
55  if (tmp2 < puzzleSize)
56  {
57  swap(locs[i], locs[dual[i]]);
58  swap(dual[tmp2], dual[i]);
59  }
60  }
61 
62  return result2;
63 }
64 
68 void MR1KPermutation::Unrank(uint64_t hash, int *puzzle, int *dual, int distinctSize, int puzzleSize) const
69 {
70 // std::vector<int> &dual = dualCache[threadID];
71 // dual.resize(puzzleSize); // vector for distinct item locations
72  for (int x = 0; x < puzzleSize; x++)
73  dual[x] = x;
74 
75  int last = (puzzleSize-distinctSize);
76  memset(puzzle, 0xFF, puzzleSize*sizeof(puzzle[0]));
77 
78  for (int i = 0; i < distinctSize; i++)
79  {
80  swap(dual[i+hash%(puzzleSize-i)], dual[i]);
81  hash = hash/(puzzleSize-i);
82  puzzle[dual[i]] = i;//distinct[i-last-1];
83  //printf("Setting %d to be %d\n", dual[i-1], i-last-1);
84  }
85 }
86 
87 //MR1Permutation::MR1Permutation(std::vector<int> distincts, int permSize, int maxNumThreads)
88 //:distinct(distincts), puzzleSize(permSize), distinctSize(distincts.size()),
89 // dualCache(maxNumThreads), locsCache(maxNumThreads), valueStack(maxNumThreads)
90 //{
91 // maxRank = 1;
92 // for (int x = (int)puzzleSize; x > puzzleSize-distincts.size(); x--)
93 // {
94 // maxRank *= x;
95 // }
96 //}
97 //
98 //uint64_t MR1Permutation::GetMaxRank() const
99 //{
100 // return maxRank;
101 //}
102 //
103 //inline void swap(int &a, int &b)
104 //{
105 // int tmp = a;
106 // a = b;
107 // b = tmp;
108 //}
109 //
110 //uint64_t MR1Permutation::GetRank(const std::vector<int> &puzzle, int threadID) const
111 //{
112 // std::vector<int> &locs = locsCache[threadID];
113 // std::vector<int> &dual = dualCache[threadID];
114 // std::vector<int> &values = valueStack[threadID];
115 // locs.resize(puzzleSize); // vector for distinct item locations
116 // dual.resize(puzzleSize); // vector for distinct item locations
117 // values.resize(0);
118 // memset(&locs[0], 0xFF, locs.size()*sizeof(locs[0]));
119 // memset(&dual[0], 0xFF, dual.size()*sizeof(dual[0]));
120 //
121 // // find current duals
122 // for (unsigned int x = 0; x < puzzleSize; x++)
123 // {
124 // if (puzzle[x] != -1)
125 // dual[puzzle[x]] = x;
126 // }
127 // // get locs by converting from the distinct array
128 // for (int x = 0; x < distinctSize; x++)
129 // {
130 // locs[puzzleSize-x-1] = dual[distinct[distinctSize-x-1]];
131 // //dual[distinct[distinctSize-x-1]] = -1;
132 // }
133 // memset(&dual[0], 0xFF, dual.size()*sizeof(dual[0]));
134 // // get new duals for the actual locs (after conversion)
135 // for (int x = puzzleSize-distinctSize; x < puzzleSize; x++)
136 // {
137 // dual[locs[x]] = x;
138 // }
139 //
140 // size_t last = puzzleSize-distinctSize;
141 // for (size_t i = puzzleSize; i > last; i--)
142 // {
143 // values.push_back(locs[i-1]); //val = locs[i-1];//get(perm, i-1);
144 //
145 // if (dual[i-1] != -1)
146 // {
147 // swap(locs[i-1], locs[dual[i-1]]);
148 // swap(dual[values.back()], dual[i-1]);
149 // }
150 // }
151 // uint64_t result = 0;
152 // int cnt = last+1;
153 // while (values.size() > 0)
154 // {
155 // result *= cnt;
156 // result += values.back();
157 // values.pop_back();
158 // cnt++;
159 // }
160 // return result;
161 //}
162 //
163 //void MR1Permutation::Unrank(uint64_t hash, std::vector<int> &puzzle, int threadID) const
164 //{
165 // puzzle.resize(puzzleSize);
166 // std::vector<int> &dual = dualCache[threadID];
167 // dual.resize(puzzleSize); // vector for distinct item locations
168 // for (int x = 0; x < dual.size(); x++)
169 // dual[x] = x;
170 //
171 // size_t last = (puzzleSize-distinctSize);
172 // memset(&puzzle[0], 0xFF, puzzleSize*sizeof(puzzle[0]));
173 //
174 // for (size_t i = puzzleSize; i > last; i--)
175 // {
176 // swap(dual[hash%i], dual[i-1]);
177 // hash = hash/i;
178 // puzzle[dual[i-1]] = distinct[i-last-1];
179 // }
180 //}
swap
void swap(T &a, T &b)
Definition: MR1Permutation.cpp:14
MR1KPermutation::Unrank
void Unrank(uint64_t hash, int *items, int *dual, int k, int N) const
Given the hash returns the state and its dual.
Definition: MR1Permutation.cpp:68
MR1Permutation.h
MR1KPermutation::Rank
uint64_t Rank(int *items, int *dual, int k, int N) const
Definition: MR1Permutation.cpp:21