-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCache.h
More file actions
80 lines (62 loc) · 3.27 KB
/
WordCache.h
File metadata and controls
80 lines (62 loc) · 3.27 KB
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
// Copyright 2006-13 HumaNature Studios Inc.
#ifndef __WORDCACHE_H__
#define __WORDCACHE_H__
namespace core {
// Collection of phrases, with lists to associate with each phrase.
// Data is organized for fast lookup.
class WordCache : public Phyre::PBase
{
PHYRE_BIND_DECLARE_CLASS(WordCache, PBase);
protected:
U32 mPhraseCount; // All arrays (besides the buffers) are the same length. This variable is unnecessary, but nice to have.
// Buffer containing all phrases.
// Each phrase ends with '\0'.
// Text is stored as UTF-8.
// Phrases are stored longest to shorted, phrases of same length are then sorted alphabetically.
// Phrases should be lowercase.
Phyre::PArray<Phyre::PChar> mPhraseBuffer;
Phyre::PArray<Phyre::PUInt32> mPhrasesStart; // index, in buffer, of each phrase's first character
Phyre::PArray<Phyre::PUInt8> mPhrasesLength; // length of each phrase in buffer (not counting '\0') (mgraebtodo: used anywhere?)
// Buffer containing each phrase's lists of starred and unstarred dekos
// All of a phrase's starred dekos are stored in a row, followed by that phrase's unstarred dekos.
// The next deko in the buffer is the next phrase's first starred deko.
Phyre::PArray<Phyre::PUInt16> mDekoBuffer;
Phyre::PArray<Phyre::PUInt32> mDekosStart; // index, in buffer, of each phrase's first starred deko
Phyre::PArray<Phyre::PUInt8> mDekosStarred; // number of starred dekos for each phrase
Phyre::PArray<Phyre::PUInt8> mDekosUnstarred; // number of unstarred dekos for each phrase
public:
static const U32 kPhraseNotFound;
struct Match
{
U32 phraseIndex; // phrase's index in wordcache
U32 phraseInTextUnicodeIndex; // Number of UTF-8 characters preceding phrase in text (not the byte index)
};
// Search text for phrases. Fill outMatches with results.
// text will be altered by this function.
void findMatches(const char* text, std::vector<Match>& outMatches) const;
// If phrase exists, returns its index. Else returns kPhraseNotFound
inline U32 findPhrase(const char* str) const;
inline U32 getPhraseCount() const;
inline const char* getPhrase(U32 phraseI) const;
inline U32 getStarredDekoCount(U32 phraseI) const;
inline U16 getStarredDeko(U32 phraseI, U32 dekoI) const;
inline U32 getUnstarredDekoCount(U32 phraseI) const;
inline U16 getUnstarredDeko(U32 phraseI, U32 dekoI) const;
// How to sort strA relative to strB:
// <0: strA belongs before strB
// 0: strA equals strB
// >0: strA belongs after strB
static inline int CompareStrings(const char* strA, size_t lenStrA, const char* strB, size_t lenStrB);
#ifdef PHYRE_TOOL_BUILD
// Fill wordcache, based on jsonStr.
// jsonStr is modified during read and cannot be used again.
// returns true if no errors occurred.
bool ReadJson(char* jsonStr);
#endif // PHYRE_TOOL_BUILD
protected:
inline U32 getStarredDekoIndex(U32 phraseI, U32 dekoI) const;
inline U32 getUnstarredDekoIndex(U32 phraseI, U32 dekoI) const;
};
}
#include "WordCache.inl"
#endif // __WORDCACHE_H__