1 |
torben |
1 |
/*************************************************************************** |
2 |
|
|
* Copyright (C) 2005 by Torben Nielsen * |
3 |
|
|
* torben@t-hoerup.dk * |
4 |
|
|
* * |
5 |
|
|
* This program is free software; you can redistribute it and/or modify * |
6 |
|
|
* it under the terms of the GNU General Public License as published by * |
7 |
|
|
* the Free Software Foundation; either version 2 of the License, or * |
8 |
|
|
* (at your option) any later version. * |
9 |
|
|
* * |
10 |
|
|
* This program is distributed in the hope that it will be useful, * |
11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
13 |
|
|
* GNU General Public License for more details. * |
14 |
|
|
* * |
15 |
|
|
* You should have received a copy of the GNU General Public License * |
16 |
|
|
* along with this program; if not, write to the * |
17 |
|
|
* Free Software Foundation, Inc., * |
18 |
|
|
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
19 |
|
|
***************************************************************************/ |
20 |
torben |
3 |
#include "containerhash.h" |
21 |
torben |
1 |
|
22 |
torben |
6 |
|
23 |
torben |
3 |
ContainerHash::ContainerHash(GUIUpdate* update) |
24 |
torben |
1 |
: SolutionContainer(update) |
25 |
|
|
{ |
26 |
torben |
15 |
total = 0; |
27 |
torben |
1 |
m_size = 0; |
28 |
|
|
m_hashroof = max_size; |
29 |
|
|
for (int i=0; i< max_size; i++) |
30 |
|
|
solutions[i] = new SolList(); |
31 |
|
|
|
32 |
|
|
} |
33 |
|
|
|
34 |
|
|
|
35 |
torben |
3 |
ContainerHash::~ContainerHash() |
36 |
torben |
1 |
{ |
37 |
torben |
6 |
for (int i=0; i<max_size; i++) |
38 |
|
|
{ |
39 |
|
|
delete solutions[i]; |
40 |
|
|
} |
41 |
torben |
1 |
} |
42 |
|
|
|
43 |
torben |
3 |
int ContainerHash::numSolutions() |
44 |
torben |
1 |
{ |
45 |
|
|
int size = 0; |
46 |
|
|
for (int i=0; i<m_hashroof; i++) { |
47 |
|
|
size += solutions[i]->size(); |
48 |
|
|
} |
49 |
|
|
return size; |
50 |
|
|
} |
51 |
|
|
|
52 |
torben |
3 |
int ContainerHash::totalSolutions() |
53 |
torben |
1 |
{ |
54 |
|
|
return total; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
|
58 |
torben |
3 |
void ContainerHash::uniqueSolutions() |
59 |
torben |
1 |
{ |
60 |
|
|
while (m_hashroof >1 ) { |
61 |
|
|
for (int bucket=0; bucket < m_hashroof; bucket++) { |
62 |
|
|
for (int i=1;i<=4;i++) { |
63 |
|
|
if (m_halt) |
64 |
|
|
return; |
65 |
|
|
uniqueSolutionsWorker(bucket, i,false); |
66 |
|
|
uniqueSolutionsWorker(bucket, i,true); |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
for (int i=0; i< (m_hashroof/2); i++) { |
70 |
|
|
int base2 = (m_hashroof/2)+i; |
71 |
|
|
|
72 |
|
|
|
73 |
|
|
ListIt p = solutions[i]->end(); |
74 |
|
|
solutions[i]->splice(p, *solutions[base2]); |
75 |
|
|
} |
76 |
|
|
m_hashroof /= 2; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
for (int i=1;i<=4;i++) { |
80 |
|
|
uniqueSolutionsWorker(0, i, false); |
81 |
|
|
uniqueSolutionsWorker(0, i, true); |
82 |
|
|
} |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
88 |
torben |
3 |
void ContainerHash::uniqueSolutionsWorker(int bucket, int rot,bool mirror) |
89 |
torben |
1 |
{ |
90 |
|
|
int match_count; |
91 |
|
|
|
92 |
|
|
if (solutions[bucket]->size() == 0) |
93 |
|
|
return; |
94 |
|
|
if (total == -1) |
95 |
|
|
total = solutions[bucket]->size(); |
96 |
|
|
|
97 |
|
|
for (ListIt i=solutions[bucket]->begin(); i != solutions[bucket]->end(); i++) { |
98 |
|
|
if (m_halt) |
99 |
|
|
return; |
100 |
|
|
match_count = 0; |
101 |
torben |
6 |
|
102 |
|
|
std::auto_ptr<Solution> tmp( (*i).get()->copy() ); |
103 |
|
|
|
104 |
torben |
1 |
if (mirror) |
105 |
torben |
2 |
tmp->mirror(); |
106 |
torben |
1 |
for (int k=0;k<rot;k++) |
107 |
torben |
2 |
tmp->rotate90(); |
108 |
torben |
1 |
|
109 |
|
|
//unders�g kun resten af m�ngden, start ved i+1 |
110 |
|
|
ListIt j =i; |
111 |
|
|
j++; |
112 |
torben |
6 |
for ( ; j!=solutions[bucket]->end();j++) |
113 |
|
|
{ |
114 |
|
|
if ( *tmp == *(*j) ) |
115 |
|
|
{ |
116 |
torben |
1 |
solutions[bucket]->erase(j); |
117 |
|
|
break; |
118 |
|
|
} |
119 |
|
|
} |
120 |
|
|
} |
121 |
|
|
} |
122 |
|
|
|
123 |
torben |
3 |
void ContainerHash::addSolution(Solution* sol) |
124 |
torben |
1 |
{ |
125 |
|
|
int hash = m_size % max_size; |
126 |
torben |
6 |
solutions[hash]->push_back( boost::shared_ptr<Solution>(sol) ); |
127 |
torben |
1 |
m_size++; |
128 |
torben |
15 |
total++; |
129 |
torben |
6 |
|
130 |
torben |
1 |
} |
131 |
|
|
|
132 |
torben |
3 |
Solution* ContainerHash::solution(int index) |
133 |
torben |
1 |
{ |
134 |
|
|
int count=0; |
135 |
torben |
2 |
ListIt it; |
136 |
|
|
for (it = solutions[0]->begin(); it != solutions[0]->end(); it++, count++) { |
137 |
torben |
1 |
if (count == index) { |
138 |
torben |
2 |
break; |
139 |
|
|
} |
140 |
torben |
1 |
} |
141 |
torben |
6 |
return (*it).get(); |
142 |
torben |
1 |
} |