1 
2 /*********************************************************
3  *  Soothsayer, an extensible predictive text entry system
4  *  ------------------------------------------------------
5  *
6  *  Copyright (C) 2008  Matteo Vescovi <matteo.vescovi@yahoo.co.uk>
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21                                                                              *
22                                                                 **********(*)*/
23 
24 #include "prediction.h"
25 #include <assert.h>
26 
Prediction()27 Prediction::Prediction()
28 {}
29 
~Prediction()30 Prediction::~Prediction()
31 {}
32 
operator =(const Prediction & right)33 const Prediction &Prediction::operator=( const Prediction &right )
34 {
35     if( &right != this ) {
36 	suggestions = right.suggestions;
37 
38 	//assert( ( suggestions == right.suggestions ) );
39     }
40 
41     return *this;
42 }
43 
operator ==(const Prediction & right) const44 bool Prediction::operator== (const Prediction& right) const
45 {
46     // same instance is obviously equal to itself
47     if (&right == this) {
48 	return true;
49     } else {
50 	if (size() != right.size()) {
51 	    return false;
52 	} else {
53 	    // need to compare each suggestion
54 	    bool result = true;
55 	    int i = 0;
56 	    while (i < size() && result) {
57 		if (getSuggestion(i) != right.getSuggestion(i)) {
58 		    result = false;
59 		}
60 		i++;
61 	    }
62 	    return result;
63 	}
64     }
65 }
66 
size() const67 int Prediction::size() const
68 {
69     return suggestions.size();
70 }
71 
getSuggestion(int i) const72 Suggestion Prediction::getSuggestion(int i) const
73 {
74     assert( i >= 0 && static_cast<unsigned int>(i) < suggestions.size() );
75 
76     return suggestions[i];
77 }
78 
addSuggestion(Suggestion s)79 void Prediction::addSuggestion(Suggestion s)
80 {
81     // insert s so that suggestions vector is sorted
82 
83     // handle empty vector first
84     if( suggestions.empty() ) {
85 	suggestions.push_back( s );
86     } else {
87 	std::vector< Suggestion >::iterator i = suggestions.begin();
88 	while( i != suggestions.end() && s < *i ) {
89 	    i++;
90 	}
91 	suggestions.insert( i, s );
92     }
93 }
94 
toString() const95 std::string Prediction::toString() const
96 {
97     std::string str;
98     std::vector<Suggestion>::const_iterator i;
99     for( i=suggestions.begin(); i!=suggestions.end(); i++ ) {
100 	str += i->toString();
101     }
102     return str;
103 }
104 
operator <<(std::ostream & output,const Prediction & p)105 std::ostream &operator<<( std::ostream &output, const Prediction &p )
106 {
107     std::vector<Suggestion>::const_iterator i;
108     for( i=p.suggestions.begin(); i!=p.suggestions.end(); i++ ) {
109 	output << *i << std::endl;
110     }
111 
112     return output;
113 }
114