Skip to content

Commit b183d72

Browse files
committed
move all examples to using hash set
1 parent 2f5c7cf commit b183d72

4 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎cpp/8puzzle.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
88

99
#include <iostream>
10+
#include <sstream>
1011
#include <stdio.h>
1112
#include <assert.h>
1213
#include <new>
@@ -115,6 +116,7 @@ class PuzzleState
115116
bool GetSuccessors( AStarSearch<PuzzleState> *astarsearch, PuzzleState *parent_node );
116117
float GetCost( PuzzleState &successor );
117118
bool IsSameState( PuzzleState &rhs );
119+
size_t Hash();
118120

119121
void PrintNodeInfo();
120122

@@ -335,6 +337,19 @@ bool PuzzleState::IsSameState( PuzzleState &rhs )
335337

336338
}
337339

340+
// The 9 tiles positions can be encoded as digits
341+
size_t PuzzleState::Hash()
342+
{
343+
stringstream stream;
344+
for( size_t i = 0; i < (BOARD_HEIGHT * BOARD_WIDTH); i++ )
345+
{
346+
stream << tiles[i];
347+
}
348+
size_t state_digits;
349+
stream >> state_digits;
350+
return hash<size_t>{}(state_digits);
351+
}
352+
338353
void PuzzleState::PrintNodeInfo()
339354
{
340355
char str[100];

‎cpp/findpath.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class MapSearchNode
8787
bool GetSuccessors( AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node );
8888
float GetCost( MapSearchNode &successor );
8989
bool IsSameState( MapSearchNode &rhs );
90+
size_t Hash();
9091

9192
void PrintNodeInfo();
9293

@@ -109,6 +110,13 @@ bool MapSearchNode::IsSameState( MapSearchNode &rhs )
109110

110111
}
111112

113+
size_t MapSearchNode::Hash()
114+
{
115+
size_t h1 = hash<float>{}(x);
116+
size_t h2 = hash<float>{}(y);
117+
return h1 ^ (h2 << 1);
118+
}
119+
112120
void MapSearchNode::PrintNodeInfo()
113121
{
114122
char str[100];

‎cpp/min_path_to_Bucharest.cpp‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class PathSearchNode
4949
bool GetSuccessors( AStarSearch<PathSearchNode> *astarsearch, PathSearchNode *parent_node );
5050
float GetCost( PathSearchNode &successor );
5151
bool IsSameState( PathSearchNode &rhs );
52+
size_t Hash();
5253

5354
void PrintNodeInfo();
5455
};
@@ -60,6 +61,11 @@ bool PathSearchNode::IsSameState( PathSearchNode &rhs )
6061
return(false);
6162
}
6263

64+
size_t PathSearchNode::Hash()
65+
{
66+
return hash<int>{}(city);
67+
}
68+
6369
// Euclidean distance between "this" node city and Bucharest
6470
// Note: Numbers are taken from the book
6571
float PathSearchNode::GoalDistanceEstimate( PathSearchNode &nodeGoal )

‎cpp/tests.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class MapSearchNode
7373
bool GetSuccessors( AStarSearch<MapSearchNode> *astarsearch, MapSearchNode *parent_node );
7474
float GetCost( MapSearchNode &successor );
7575
bool IsSameState( MapSearchNode &rhs );
76+
size_t Hash();
7677

7778
void PrintNodeInfo();
7879

@@ -190,6 +191,13 @@ float MapSearchNode::GetCost( MapSearchNode &successor )
190191

191192
}
192193

194+
size_t MapSearchNode::Hash()
195+
{
196+
size_t h1 = hash<float>{}(x);
197+
size_t h2 = hash<float>{}(y);
198+
return h1 ^ (h2 << 1);
199+
}
200+
193201
int main(int argc, char *argv[]) {
194202

195203
AStarSearch<MapSearchNode> astarsearch;

0 commit comments

Comments
 (0)