Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from jrittenh/master
Browse files Browse the repository at this point in the history
Updated to compile on Gentoo/linux
  • Loading branch information
ShootMe authored Mar 31, 2017
2 parents 18ed73b + c5c6714 commit 0c8feeb
Show file tree
Hide file tree
Showing 17 changed files with 2,263 additions and 2,234 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
*.suo
*.user
*.sln.docstates
*.o
KlondikeSolver
.depend
*.swp

# Build results

Expand Down
26 changes: 13 additions & 13 deletions Card.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include"Card.h"
void Card::Clear() {
Rank = EMPTY;
Suit = NONE;
}
void Card::Set(unsigned char value) {
Value = value;
Rank = (value % 13) + 1;
Suit = value / 13;
IsRed = Suit & 1;
IsOdd = Rank & 1;
Foundation = Suit + 9;
#include"Card.h"

void Card::Clear() {
Rank = EMPTY;
Suit = NONE;
}
void Card::Set(unsigned char value) {
Value = value;
Rank = (value % 13) + 1;
Suit = value / 13;
IsRed = Suit & 1;
IsOdd = Rank & 1;
Foundation = Suit + 9;
}
64 changes: 32 additions & 32 deletions Card.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#ifndef Card_h
#define Card_h
enum Cards {
EMPTY = 0,
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
};
enum Suits {
CLUBS = 0,
DIAMONDS,
SPADES,
HEARTS,
NONE = 255
};
struct Card {
unsigned char Suit, Rank, IsOdd, IsRed, Foundation, Value;
void Clear();
void Set(unsigned char value);
};
#ifndef Card_h
#define Card_h
enum Cards {
EMPTY = 0,
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING
};

enum Suits {
CLUBS = 0,
DIAMONDS,
SPADES,
HEARTS,
NONE = 255
};

struct Card {
unsigned char Suit, Rank, IsOdd, IsRed, Foundation, Value;
void Clear();
void Set(unsigned char value);
};
#endif
270 changes: 135 additions & 135 deletions HashMap.h
Original file line number Diff line number Diff line change
@@ -1,136 +1,136 @@
#ifndef HashMap_h
#define HashMap_h
using namespace std;
#define KEYSIZE 21
#define KEYSIZEM1 KEYSIZE - 1
struct HashKey {
char Key[KEYSIZE];
HashKey() {
for (int i = 0; i < KEYSIZE; i++) {
Key[i] = 0;
}
}
int ComputeHash() const {
unsigned long hash = 0;
int i = 0;
while (i < KEYSIZE) {
hash = (int)Key[i++] + (hash << 7) + (hash << 16) - hash;
}
return hash;
}
bool operator==(HashKey const& key) const {
int i = 0;
while (i < KEYSIZEM1 && Key[i] == key.Key[i]) { i++; }
return Key[i] == key.Key[i];
}
char & operator[](int index) {
return Key[index];
}
};
template <typename T> struct KeyValue {
KeyValue<T> * Next;
HashKey Key;
T Value;
int Hash;
KeyValue();
KeyValue(int hash, HashKey const& key, T const& value, KeyValue<T> * next);
~KeyValue();
};
template <typename T> KeyValue<T>::KeyValue() {
Next = NULL;
}
template <typename T> KeyValue<T>::KeyValue(int hash, HashKey const& key, T const& value, KeyValue<T> * next) {
Next = next;
Key = key;
Hash = hash;
Value = value;
}
template <typename T> KeyValue<T>::~KeyValue() {
delete Next;
}
template <typename T> class HashMap {
private:
KeyValue<T> * table;
int size, capacity, powerOf2, maxLength, slotsUsed;
public:
HashMap(int powerOf2);
~HashMap();
int Size();
int Capacity();
int SlotsUsed();
int MaxLength();
void Clear();
KeyValue<T> * Add(HashKey const& key, T const& value);
};
template <typename T> HashMap<T>::HashMap(int powerOf2) {
this->powerOf2 = powerOf2;
size = 0;
slotsUsed = 0;
maxLength = 0;
capacity = (1 << powerOf2) - 1;
table = new KeyValue<T>[capacity + 1];
}
template <typename T> HashMap<T>::~HashMap() {
delete[] table;
}
template <typename T> int HashMap<T>::Size() {
return size;
}
template <typename T> int HashMap<T>::MaxLength() {
return maxLength;
}
template <typename T> int HashMap<T>::SlotsUsed() {
return slotsUsed;
}
template <typename T> int HashMap<T>::Capacity() {
return capacity + 1;
}
template <typename T> KeyValue<T> * HashMap<T>::Add(HashKey const& key, T const& value) {
int hash = key.ComputeHash();
int i = hash;
i ^= (hash >> 16);
i &= capacity;
KeyValue<T> * node = &table[i];
int length = 1;
while (node != NULL && node->Key[0] != 0) {
if (node->Hash == hash && key == node->Key) { return node; }
node = node->Next;
length++;
}
++size;
node = &table[i];
if (node->Key[0] != 0) {
node->Next = new KeyValue<T>(node->Hash, node->Key, node->Value, node->Next);
}
node->Hash = hash;
node->Key = key;
node->Value = value;
if (length > maxLength) { maxLength = length; }
if (length == 1) { slotsUsed++; }
return NULL;
}
template <typename T> void HashMap<T>::Clear() {
KeyValue<T> * old = table;
delete[] old;
table = new KeyValue<T>[capacity + 1];
size = 0;
slotsUsed = 0;
maxLength = 0;
}
#ifndef HashMap_h
#define HashMap_h
using namespace std;

#define KEYSIZE 21
#define KEYSIZEM1 KEYSIZE - 1

struct HashKey {
char Key[KEYSIZE];

HashKey() {
for (int i = 0; i < KEYSIZE; i++) {
Key[i] = 0;
}
}

int ComputeHash() const {
unsigned long hash = 0;
int i = 0;

while (i < KEYSIZE) {
hash = (int)Key[i++] + (hash << 7) + (hash << 16) - hash;
}

return hash;
}
bool operator==(HashKey const& key) const {
int i = 0;
while (i < KEYSIZEM1 && Key[i] == key.Key[i]) { i++; }
return Key[i] == key.Key[i];
}
char & operator[](int index) {
return Key[index];
}
};

template <typename T> struct KeyValue {
KeyValue<T> * Next;
HashKey Key;
T Value;
int Hash;

KeyValue();
KeyValue(int hash, HashKey const& key, T const& value, KeyValue<T> * next);
~KeyValue();
};

template <typename T> KeyValue<T>::KeyValue() {
Next = NULL;
}
template <typename T> KeyValue<T>::KeyValue(int hash, HashKey const& key, T const& value, KeyValue<T> * next) {
Next = next;
Key = key;
Hash = hash;
Value = value;
}
template <typename T> KeyValue<T>::~KeyValue() {
delete Next;
}

template <typename T> class HashMap {
private:
KeyValue<T> * table;
int size, capacity, powerOf2, maxLength, slotsUsed;

public:
HashMap(int powerOf2);
~HashMap();

int Size();
int Capacity();
int SlotsUsed();
int MaxLength();
void Clear();
KeyValue<T> * Add(HashKey const& key, T const& value);
};

template <typename T> HashMap<T>::HashMap(int powerOf2) {
this->powerOf2 = powerOf2;
size = 0;
slotsUsed = 0;
maxLength = 0;
capacity = (1 << powerOf2) - 1;
table = new KeyValue<T>[capacity + 1];
}
template <typename T> HashMap<T>::~HashMap() {
delete[] table;
}
template <typename T> int HashMap<T>::Size() {
return size;
}
template <typename T> int HashMap<T>::MaxLength() {
return maxLength;
}
template <typename T> int HashMap<T>::SlotsUsed() {
return slotsUsed;
}
template <typename T> int HashMap<T>::Capacity() {
return capacity + 1;
}
template <typename T> KeyValue<T> * HashMap<T>::Add(HashKey const& key, T const& value) {
int hash = key.ComputeHash();
int i = hash;
i ^= (hash >> 16);
i &= capacity;
KeyValue<T> * node = &table[i];

int length = 1;
while (node != NULL && node->Key[0] != 0) {
if (node->Hash == hash && key == node->Key) { return node; }
node = node->Next;
length++;
}

++size;
node = &table[i];
if (node->Key[0] != 0) {
node->Next = new KeyValue<T>(node->Hash, node->Key, node->Value, node->Next);
}
node->Hash = hash;
node->Key = key;
node->Value = value;

if (length > maxLength) { maxLength = length; }
if (length == 1) { slotsUsed++; }
return NULL;
}
template <typename T> void HashMap<T>::Clear() {
KeyValue<T> * old = table;
delete[] old;
table = new KeyValue<T>[capacity + 1];
size = 0;
slotsUsed = 0;
maxLength = 0;
}
#endif
Loading

0 comments on commit 0c8feeb

Please sign in to comment.