Diffstat (limited to 'inputmethods/dasher/AlphabetMap.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | inputmethods/dasher/AlphabetMap.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/inputmethods/dasher/AlphabetMap.cpp b/inputmethods/dasher/AlphabetMap.cpp index 09e2c72..c687e45 100644 --- a/inputmethods/dasher/AlphabetMap.cpp +++ b/inputmethods/dasher/AlphabetMap.cpp | |||
@@ -1,86 +1,86 @@ | |||
1 | // AlphabetMap.cpp | 1 | // AlphabetMap.cpp |
2 | // | 2 | // |
3 | ///////////////////////////////////////////////////////////////////////////// | 3 | ///////////////////////////////////////////////////////////////////////////// |
4 | // | 4 | // |
5 | // Copyright (c) 2002 Iain Murray | 5 | // Copyright (c) 2002 Iain Murray |
6 | // | 6 | // |
7 | ///////////////////////////////////////////////////////////////////////////// | 7 | ///////////////////////////////////////////////////////////////////////////// |
8 | 8 | ||
9 | #include "AlphabetMap.h" | 9 | #include "AlphabetMap.h" |
10 | 10 | ||
11 | using namespace Dasher; | 11 | using namespace Dasher; |
12 | using namespace std; | 12 | using namespace std; |
13 | 13 | ||
14 | alphabet_map::alphabet_map(unsigned int InitialTableSize) | 14 | alphabet_map::alphabet_map(unsigned int InitialTableSize) |
15 | : Undefined(0), HashTable(InitialTableSize<<1) | 15 | : HashTable(InitialTableSize<<1), Undefined(0) |
16 | { | 16 | { |
17 | Entries.reserve(InitialTableSize); | 17 | Entries.reserve(InitialTableSize); |
18 | } | 18 | } |
19 | 19 | ||
20 | 20 | ||
21 | void alphabet_map::Add(const string& Key, symbol Value) | 21 | void alphabet_map::Add(const string& Key, symbol Value) |
22 | { | 22 | { |
23 | RecursiveAdd(Key, Value, false); | 23 | RecursiveAdd(Key, Value, false); |
24 | } | 24 | } |
25 | 25 | ||
26 | 26 | ||
27 | void alphabet_map::RecursiveAdd(const string& Key, symbol Value, bool PrefixFlag) | 27 | void alphabet_map::RecursiveAdd(const string& Key, symbol Value, bool PrefixFlag) |
28 | { | 28 | { |
29 | Entry*& HashEntry = HashTable[Hash(Key)]; | 29 | Entry*& HashEntry = HashTable[Hash(Key)]; |
30 | 30 | ||
31 | // Loop through Entries with the correct Hash value. | 31 | // Loop through Entries with the correct Hash value. |
32 | for (Entry* i = HashEntry; i; i=i->Next) { | 32 | for (Entry* i = HashEntry; i; i=i->Next) { |
33 | if (i->Key==Key) { | 33 | if (i->Key==Key) { |
34 | if (PrefixFlag) { | 34 | if (PrefixFlag) { |
35 | // Just tagging - don't change symbol. Recurse if necessary | 35 | // Just tagging - don't change symbol. Recurse if necessary |
36 | i->KeyIsPrefix = true; | 36 | i->KeyIsPrefix = true; |
37 | if (Key.size()>1) | 37 | if (Key.size()>1) |
38 | RecursiveAdd(Key.substr(Key.size()-1), Undefined, true); | 38 | RecursiveAdd(Key.substr(Key.size()-1), Undefined, true); |
39 | } else { | 39 | } else { |
40 | // Add symbol and leave | 40 | // Add symbol and leave |
41 | i->Symbol = Value; | 41 | i->Symbol = Value; |
42 | } | 42 | } |
43 | return; | 43 | return; |
44 | } | 44 | } |
45 | } | 45 | } |
46 | 46 | ||
47 | // When hash table gets 1/2 full... | 47 | // When hash table gets 1/2 full... |
48 | // (no I haven't optimised when to resize) | 48 | // (no I haven't optimised when to resize) |
49 | if (Entries.size()<<1 >= HashTable.size()) { | 49 | if (Entries.size()<<1 >= HashTable.size()) { |
50 | // Double up all the storage | 50 | // Double up all the storage |
51 | HashTable.clear(); | 51 | HashTable.clear(); |
52 | HashTable.resize(Entries.size()<<2); | 52 | HashTable.resize(Entries.size()<<2); |
53 | Entries.reserve(Entries.size()<<1); | 53 | Entries.reserve(Entries.size()<<1); |
54 | 54 | ||
55 | // Rehash as the pointers will all be mangled. | 55 | // Rehash as the pointers will all be mangled. |
56 | for (uint j=0; j<Entries.size(); j++) { | 56 | for (uint j=0; j<Entries.size(); j++) { |
57 | Entry*& HashEntry2 = HashTable[Hash(Entries[j].Key)]; | 57 | Entry*& HashEntry2 = HashTable[Hash(Entries[j].Key)]; |
58 | Entries[j].Next = HashEntry2; | 58 | Entries[j].Next = HashEntry2; |
59 | HashEntry2 = &Entries[j]; | 59 | HashEntry2 = &Entries[j]; |
60 | } | 60 | } |
61 | 61 | ||
62 | // Have to recall this function as the key's hash needs recalculating | 62 | // Have to recall this function as the key's hash needs recalculating |
63 | RecursiveAdd(Key, Value, PrefixFlag); | 63 | RecursiveAdd(Key, Value, PrefixFlag); |
64 | return; | 64 | return; |
65 | } | 65 | } |
66 | 66 | ||
67 | Entries.push_back(Entry(Key, Value, HashEntry)); | 67 | Entries.push_back(Entry(Key, Value, HashEntry)); |
68 | HashEntry = &Entries.back(); | 68 | HashEntry = &Entries.back(); |
69 | } | 69 | } |
70 | 70 | ||
71 | 71 | ||
72 | symbol alphabet_map::Get(const string& Key, bool* KeyIsPrefix) const | 72 | symbol alphabet_map::Get(const string& Key, bool* KeyIsPrefix) const |
73 | { | 73 | { |
74 | // Loop through Entries with the correct Hash value. | 74 | // Loop through Entries with the correct Hash value. |
75 | for (Entry* i = HashTable[Hash(Key)]; i; i=i->Next) { | 75 | for (Entry* i = HashTable[Hash(Key)]; i; i=i->Next) { |
76 | if (i->Key==Key) { | 76 | if (i->Key==Key) { |
77 | if (KeyIsPrefix!=0) | 77 | if (KeyIsPrefix!=0) |
78 | *KeyIsPrefix = i->KeyIsPrefix; | 78 | *KeyIsPrefix = i->KeyIsPrefix; |
79 | return i->Symbol; | 79 | return i->Symbol; |
80 | } | 80 | } |
81 | } | 81 | } |
82 | 82 | ||
83 | if (KeyIsPrefix!=0) | 83 | if (KeyIsPrefix!=0) |
84 | *KeyIsPrefix = false; | 84 | *KeyIsPrefix = false; |
85 | return Undefined; | 85 | return Undefined; |
86 | } | 86 | } |