-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagrammer.h
115 lines (98 loc) · 3.05 KB
/
anagrammer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*************************************************************
*
* Copyright (C) 2010 John O'Laughlin
*
* All rights reserved.
*
*************************************************************
*/
#ifndef ANAGRAMMER_H
#define ANAGRAMMER_H
#include <cstdint>
#include <map>
#include <tr1/unordered_map>
#include <vector>
#include "board.h"
#include "constants.h"
#include "move.h"
#include "types.h"
#include "rack.h"
#include "util.h"
using namespace std;
class Anagrammer
{
public:
Anagrammer(const char* lex);
void anagram(const char* input);
void findMoves(const Board &board, const Rack &rack);
vector<Move>* moves() { return &_moves; }
inline bool isValid() const { return _valid; }
static void convertLeaves(const char *input, const char *output);
void testLeaves();
float prodValue(uint64_t product);
uint64_t stringProd(const char *s);
private:
struct leave_t {
uint64_t product;
float value;
};
struct BlankState {
uint idx;
uint lim;
uint pos;
uchar ltr;
};
uint _mask[32];
const uint *_dawg;
const uchar *_leaves;
const uint64_t *_primes;
uint _numLeaves;
uchar _perm[MAXIMUM_RACK_SIZE + 1];
uint _nodes[MAXIMUM_RACK_SIZE + 1];
bool _valid;
uint _rackLen;
uint _counts[BLANK + 1];
uint64_t _rackProd;
uint _rackScores[MAXIMUM_RACK_SIZE];
vector<Move> _moves;
struct Square {
uint row;
uint col;
uint horiz;
uint minLen;
uint maxLen;
uint node;
};
vector<Square> _squares;
inline char toChar(unsigned int index);
inline void printTruncated(int length);
inline void printTruncated(int length, bool octo);
inline bool hasChild(uint nodeIndex, uchar c);
inline uint getChild(uint nodeIndex, char c);
inline uchar getChildLetter(uint nodeIndex, uint idx);
inline void printChildren(uint nodeIndex);
inline uint numChildren(uint nodeIndex);
inline bool terminates(uint node);
inline bool inSmallerDict(uint node);
inline unsigned int getPointer(uint node);
inline leave_t* indexLeave(uint index);
inline bool skipAhead(uint start);
inline void increase(uint pos);
inline void reverseAfter(uint pos);
inline uint countTiles(const char *input);
inline void countRackTiles(const Rack &rack);
inline uint setFirstPerm(const char *input);
inline void setRackFirstPerm(const Rack &rack);
inline void lookupRackScores(const Config *config, const Rack &r);
inline void findSquares(const Board &board, const Rack &rack);
inline void findOpenerSquares(const Board &board, const Rack &rack);
inline void findNonOpenerSquares(const Board &board, const Rack &rack);
inline void findScoringPlays(const Board &Board, const Rack &rack);
inline void findExchanges(const Rack &rack);
inline void findPass();
const uint* loadDawg(const char *filename);
const uchar* loadLeaves(const char *filename);
const uint64_t* loadPrimes(const char *filename);
void computeMasks();
};
#endif