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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// TypeAhead.hh for FbTk - Fluxbox Toolkit
// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#ifndef FBTK_TYPEAHEAD_HH
#define FBTK_TYPEAHEAD_HH
#include "ITypeAheadable.hh"
#include <vector>
#include "SearchResult.hh"
namespace FbTk {
template <typename Items, typename Item_Type>
class TypeAhead {
/*
a class template can't be split into separate interface + implementation files, an interface summary is given here:
public:
void init(Items const &items);
// accessors:
inline int stringSize() const { return m_searchstr.size(); }
Items matched() const;
// modifiers:
Items putCharacter(char ch);
void putBackSpace();
void reset()
private:
SearchResults m_search_results;
std::string m_searchstr;
Items const *m_ref;
// helper
void fillValues(BaseItems const &search, ValueVec &fillin) const;
// reverts to searchstate before current
void revert();
// search performs iteration and sets state
void search(char char_to_test);
void doSearch(char to_test,
Items const &items,
SearchResult &mySearchResult) const;
void doSearch(char to_test,
BaseItems const &search,
SearchResult &mySearchResult) const;
*/
public:
typedef std::vector < ITypeAheadable* > BaseItems;
typedef BaseItems::const_iterator BaseItemscIt;
typedef std::vector < SearchResult > SearchResults;
typedef typename Items::const_iterator ItemscIt;
void init(Items const &items) { m_ref = &items; }
inline size_t stringSize() const { return m_searchstr.size(); }
void seek() {
if (!m_search_results.empty())
m_searchstr = m_search_results.back().seekedString();
}
Items putCharacter(char ch) {
if (isprint(ch))
search(ch);
return matched();
}
void putBackSpace() {
if (!m_search_results.empty())
revert();
}
void reset() {
m_searchstr.clear();
m_search_results.clear();
}
Items matched() const {
Items last_matched;
if (!m_search_results.empty())
fillValues(m_search_results.back().result(), last_matched);
else
return *m_ref;
return last_matched;
}
private:
SearchResults m_search_results;
std::string m_searchstr;
Items const *m_ref; // reference to vector we are operating on
void fillValues(BaseItems const &search, Items &fillin) const {
for (BaseItemscIt it = search.begin(); it != search.end(); it++) {
Item_Type tmp = dynamic_cast<Item_Type>(*it);
if (tmp)
fillin.push_back(tmp);
}
}
void revert() {
m_search_results.pop_back();
if (m_search_results.empty())
m_searchstr.clear();
else
m_searchstr = m_search_results.back().seekedString();
}
void search(char char_to_test) {
SearchResult mySearchResult(m_searchstr + char_to_test);
size_t num_items = m_ref->size();
// check if we have already a searched set
if (m_search_results.empty())
doSearch(char_to_test, *m_ref, mySearchResult);
else {
num_items = m_search_results.back().size();
doSearch(char_to_test, m_search_results.back().result(),
mySearchResult);
}
if (mySearchResult.size() > 0 ) {
if (mySearchResult.size() < num_items) {
mySearchResult.seek();
m_search_results.push_back(mySearchResult);
}
m_searchstr += char_to_test;
}
}
// iteration based on original list of items
void doSearch(char to_test, Items const &items,
SearchResult &mySearchResult) const {
for (ItemscIt it = items.begin(); it != items.end(); it++) {
if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled())
mySearchResult.add(*it);
}
}
// iteration based on last SearchResult
void doSearch(char to_test, BaseItems const &search,
SearchResult &mySearchResult) const {
for (BaseItemscIt it = search.begin(); it != search.end(); it++) {
if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled())
mySearchResult.add(*it);
}
}
}; // end Class TypeAhead
} // end namespace FbTk
#endif // FBTK_TYPEAHEAD_HH
|