HP Integrity NonStop H-Series User Manual
Page 107
![background image](/manuals/396950/107/background.png)
void concordance::readText (istream & in)
{
string line;
for (int i = 1; getline(in, line, '\n'); i++) {
allLower(line);
list
split (line, " ,.;:", words);
list
for (wptr = words.begin(); wptr != words.end(); ++wptr)
addWord(*wptr, i);
}
}
Lines are read from the input stream one by one. The text of the line is first converted into lower
case, then the line is split into words, using the function split() described in
. Each word is then entered into the concordance. The method used to enter a value
into the concordance is as follows:
void concordance::addWord (string word, int line)
{
// see if word occurs in list
// first get range of entries with same key
wordDictType::iterator low = wordMap.lower_bound(word);
wordDictType::iterator high = wordMap.upper_bound(word);
// loop over entries, see if any match current line
for ( ; low != high; ++low)
if ((*low).second == line)
return;
// didn't occur, add now
wordMap.insert(wordDictType::value_type(word, line));
}
The major portion of addWord() is concerned with ensuring values are not duplicated in the word
map if the same word occurs twice on the same line. To assure this, the range of values matching the
key is examined, each value is tested, and if any match the line number then no insertion is
performed. It is only if the loop terminates without discovering the line number that the new
word/line number pair is inserted.
The final step is to print the concordance. This is performed in the following fashion:
void concordance::printConcordance (ostream & out)
{
string lastword("");
wordDictType::iterator pairPtr;
wordDictType::iterator stop = wordMap.end();
for (pairPtr = wordMap.begin(); pairPtr != stop; ++pairPtr)
// if word is same as previous, just print line number
if (lastword == (*pairPtr).first)
out << " " << (*pairPtr).second;
else { // first entry of word