root/trunk/mru_class.h

Revision 5, 2.2 kB (checked in by qbert, 6 years ago)

Initial ( and last :( ) commit

Line 
1 #ifndef MRU_CLASS
2 #define MRU_CLASS
3
4 class MRUClass
5 {
6 public:
7     vector<string> keywords;
8
9     // theres got to be a better way to do this
10     void AddItem(const string& str ) {
11         if ( std::find(keywords.begin(),keywords.end(),str )  == keywords.end() )
12             keywords.push_back(str);       
13        
14     }
15     void rootSearch(const string& root, vector<string>& matches )
16     {
17         std::sort(keywords.begin(),keywords.end());
18        
19         if ( keywords.size() )
20         {
21             rootSearchR(0,keywords.size() - 1,root, matches);
22         }
23        
24        
25         std::sort(matches.begin(),matches.end());
26        
27     }
28
29     void Clear() { keywords.clear(); }
30    
31 private:
32    
33     void rootSearchR (int l, int r, const string& root, vector<string>& matches) { // return a vector of matches
34        
35         if ( l > r ) {
36             return;
37         }
38        
39         int m = (l +r ) / 2;
40        
41         if (keywords[m].length() >= root.length() ) {
42            
43             if ( keywords[m].substr(0,root.length() )  == root ) {
44                
45                 matches.push_back(keywords[m]);
46                 int temp = m - 1;
47                
48                 if (temp >= 0 ) {
49                     bool isMatched = true;
50                     while (isMatched) {
51                         isMatched = false;
52                        
53                         if ( keywords[temp].length() >= root.length() ) {
54                            
55                             if ( keywords[temp].substr(0,root.length()  ) == root) {
56                                 matches.push_back(keywords[temp]);
57                                 isMatched = true;
58                             }
59                            
60                         }
61                        
62                         temp--;
63                         if ( temp < 0 ) break;
64                     }
65                    
66                 }
67                 temp = m + 1;
68                 if ( temp < keywords.size() ) {
69                    
70                     bool isMatched = true;
71                     while (isMatched) {
72                        
73                         isMatched = false;
74                        
75                         if ( keywords[temp].length() >= root.length() ) {
76                            
77                             if ( keywords[temp].substr(0,root.length() ) == root) {
78                                 //                              printf("\nMatch for %s \n",keywords[temp].substr(0,root.length() ) .c_str() );
79                                 matches.push_back(keywords[temp]);
80                                 isMatched = true;
81                             }
82                            
83                         }
84                        
85                         temp++;
86                         if ( temp >= keywords.size() ) break;
87                     }
88                    
89                 }
90                
91                 return;
92             }
93            
94         }
95        
96        
97         if (root < keywords[m])
98             rootSearchR(l,m-1,root,matches);       
99         else
100             rootSearchR(m+1,r,root,matches);
101        
102         return;
103        
104     }
105    
106    
107    
108    
109    
110    
111    
112
113    
114 };
115
116 #endif
Note: See TracBrowser for help on using the browser.