4 Website: http://www.vultaire.net/software/jben/
5 License: GNU General Public License (GPL) version 2
6 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>
24 #include "vocablist.h"
26 #include "string_utils.h"
33 VocabList::VocabList() {
34 /* vocabList.reserve(1000); */ /* We might want to do this later...? */
37 /* "BinarySearch" searches for a query string and returns its index.
38 If the string is not found, it returns the index at which the query
39 should be inserted. matchFound indicates whether the return value is
40 a match or merely an insertion point. */
41 int VocabList::BinarySearch(const wstring
& query
, bool* matchFound
) {
42 int listLength
= vocabList
.size();
43 int lowBound
= 0, highBound
= listLength
-1;
44 int compareVal
=0, index
=0;
46 /* Special case: empty list */
48 if(matchFound
) *matchFound
= false;
52 /* Search for a match. Return when found. */
53 while(lowBound
<= highBound
) {
54 index
= (lowBound
+highBound
)/2;
55 compareVal
= query
.compare(vocabList
[index
]);
57 if(matchFound
) *matchFound
= true;
59 } else if(compareVal
<0) {
66 /* Match was not found. "index" will be our insertion point, and
67 matchFound is false. */
68 if(matchFound
) *matchFound
= false;
69 if(compareVal
>0) index
++; /* If query is greater than the final item we
70 looked at, then we want to insert
75 /* "Add" inserts an item into the study list.
76 It uses a custom binary search function to find an insertion point for the
77 string, and to check if the string is already in the list.
78 No duplicate strings are allowed in the list, and such duplicates are quietly
80 bool VocabList::Add(const wstring
& s
) {
84 insertPoint
= BinarySearch(s
, &matchFound
);
86 vocabList
.insert(vocabList
.begin() + insertPoint
, s
);
92 int VocabList::AddList(const wstring
& s
) {
93 list
<wstring
> t
= StrTokenize
<wchar_t>(s
, L
"\n");
99 if(token
.length()>0) {
100 if(Add(token
)) count
++;
108 << " duplicate entries were detected, and were therefore ommitted.";
109 el
.Push(EL_Info
, os
.str(), (void *)this);
114 wstring
VocabList::ToString(wchar_t separator
) {
117 vector
<wstring
>::iterator it
= vocabList
.begin();
118 if(it
!=vocabList
.end()) {
121 for(; it
!=vocabList
.end(); it
++) {
122 result
.append(1, separator
);
130 void VocabList::Clear() {
134 int VocabList::Size() {return vocabList
.size();}
136 const wstring
& VocabList::operator[](unsigned int index
) {
137 return vocabList
[index
];
140 int VocabList::GetIndexByWord(const wstring
& s
) {
141 /* Index returned should be 0-based. -1 indicates not found. */
143 int index
= BinarySearch(s
, &found
);
145 printf("GetIndexByWord: BinarySearch (%ls) returned found=%d and index=%d.\n", s
.c_str(), (int)found
, index
);
147 if(!found
) return -1;
151 vector
<wstring
>& VocabList::GetVocabList() {return vocabList
;}