6f7bca3259b89d6be649d513dab7ca21a2f74944
[oweals/cde.git] / cde / programs / dtinfo / dtinfo / src / UAS / DtSR / DtSR_SearchResults.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 // $XConsortium: DtSR_SearchResults.C /main/9 1996/10/16 14:23:04 cde-hal $
24 /*      Copyright (c) 1995,1996 FUJITSU LIMITED         */
25 /*      All Rights Reserved                             */
26
27 #include "UAS_Exceptions.hh"
28 #include "DtSR_SearchEngine.hh"
29 #include "DtSR_SearchResultsEntry.hh"
30 #include "DtSR_SearchResults.hh"
31
32 DtSR_SearchResults::DtSR_SearchResults
33         (UAS_Pointer<UAS_String> query,
34          UAS_Pointer<UAS_String> scope_name,
35          UAS_Pointer<UAS_List<UAS_SearchResultsEntry> > res,
36          int ndocs,
37          UAS_Pointer<DtSR_Stems> stems,
38          UAS_SearchZones zones, int stype)
39         : UAS_SearchResults(query, scope_name, res, ndocs), f_zones(zones),
40           f_search_type(stype)
41 {
42     f_stems_list.insert_item(stems);
43
44     UAS_List<UAS_SearchResultsEntry>& list =
45                         *(UAS_List<UAS_SearchResultsEntry>*)res;
46
47     for (unsigned int i = 0; i < list.length(); i++) {
48
49         UAS_Pointer<UAS_SearchResultsEntry> crude_ptr = list[i];
50         UAS_SearchResultsEntry &cptr2 = *crude_ptr;
51         UAS_Pointer<DtSR_SearchResults> uas_this = this;
52         ((DtSR_SearchResultsEntry *)&cptr2)->search_result(uas_this);
53
54     }
55 }
56
57 void
58 DtSR_SearchResults::sort(UAS_Pointer<UAS_List<UAS_SearchResultsEntry> > res)
59 {
60     UAS_List<UAS_SearchResultsEntry>& Ref_list =
61                         *(UAS_List<UAS_SearchResultsEntry>*)res;
62   
63     unsigned int list_length = Ref_list.length();
64     for (unsigned int i = 0; i < list_length; i++) {
65         for (unsigned int j = i+1; j < list_length; j++) {
66
67 #ifdef USL
68             // this line does nothing, but if you remove it
69             // this code will not compile on novell.  go figure.  rCs
70
71             UAS_Pointer<UAS_SearchResultsEntry> reflisti = Ref_list[i];
72 #endif
73
74             if (Ref_list[i]->relevance() < Ref_list[j]->relevance()) {
75                 UAS_Pointer<UAS_SearchResultsEntry> temp = Ref_list[i];
76                 Ref_list[i] = Ref_list[j];
77                 Ref_list[j] = temp;
78             }
79         }
80     }
81 }
82
83 UAS_Pointer<UAS_List<UAS_SearchResultsEntry> >
84 DtSR_SearchResults::create_results(int index, int nres)
85 {
86     // garbage input
87     if (nres == 0 || index >= (int) f_results.length())
88         return NULL;
89
90     int n;
91     if ((n = index + nres - f_results.length()) > 0)
92         nres -= n;
93
94     UAS_Pointer<UAS_List<UAS_SearchResultsEntry> >
95         uas_res = new UAS_List<UAS_SearchResultsEntry>();
96
97     for (int i = index; i < nres; i++)
98         uas_res->insert_item(f_results[i]);
99
100     return uas_res;
101 }
102
103 void
104 DtSR_SearchResults::merge(UAS_Pointer<DtSR_SearchResults> & res)
105 {
106     if (res == (const int)0 || res->f_ndocs == 0) // nothing to merge
107         return;
108
109     int i;
110
111     if (f_ndocs == 0) { // none is there, just copy all of them
112
113         for (i = 0; i < res->f_ndocs; i++) {
114
115             f_results.insert_item(res->f_results[i]);
116
117             UAS_Pointer<UAS_SearchResultsEntry> crude_ptr = res->f_results[i];
118             UAS_SearchResultsEntry &cptr2 = *crude_ptr;     
119             UAS_Pointer<DtSR_SearchResults> uas_this = this;
120
121             ((DtSR_SearchResultsEntry*)&cptr2)->search_result(uas_this);
122
123         }
124
125         f_ndocs = res->f_ndocs;
126         for (i = 0; i < (int) res->f_stems_list.length(); i++) {
127             f_stems_list.insert_item(res->f_stems_list[i]);
128         }
129         res = NULL; 
130         return;
131     }
132
133     int ndocs = f_ndocs;
134
135     UAS_Pointer<DtSR_SearchResults> uas_this = this;
136
137     // both have some
138     int position = 0;
139     for (i = 0; i < res->f_ndocs; i++, ndocs++) {
140
141         if (! (position < ndocs))
142             break;
143
144         int j;
145         for (j = position; j < ndocs; j++) {
146             if (f_results[j]->relevance() > res->f_results[i]->relevance())
147                 continue;
148
149             f_results.insert_item(res->f_results[i], j);
150 #ifdef DEBUG
151             fprintf(stderr, "(DEBUG) insert at %d\n", j);
152 #endif
153             UAS_SearchResultsEntry* crude_ptr =
154                         (UAS_SearchResultsEntry*)res->f_results[i];
155             ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
156             break;
157         }
158         if (j == ndocs) { // insert at the tail
159             f_results.insert_item(res->f_results[i]);
160 #ifdef DEBUG
161             fprintf(stderr, "(DEBUG) insert at the tail\n");
162 #endif
163             UAS_SearchResultsEntry* crude_ptr =
164                         (UAS_SearchResultsEntry*)res->f_results[i];
165             ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
166         }
167         position = ++j;
168     }
169     for (;i < res->f_ndocs; i++) {
170 #ifdef DEBUG
171         fprintf(stderr, "(DEBUG) append the remaining list\n");
172 #endif
173         f_results.insert_item(res->f_results[i]);
174         UAS_SearchResultsEntry* crude_ptr =
175                 (UAS_SearchResultsEntry*)res->f_results[i];
176         ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
177         ndocs++;
178     }
179
180     // merge stems list
181     for (i = 0; i < (int) res->f_stems_list.length(); i++) {
182         f_stems_list.insert_item(res->f_stems_list[i]);
183     }
184
185     f_ndocs += res->f_ndocs;
186     res = NULL;
187 }
188
189 UAS_Pointer<DtSR_Stems>
190 DtSR_SearchResults::stems(int dbn)
191 {
192     int db_count = DtSR_SearchEngine::search_engine().db_count();
193
194     if (dbn < 0 || dbn >= db_count)
195         return NULL;
196
197     unsigned int i;
198     for (i = 0; i < f_stems_list.length(); i++) {
199         if (f_stems_list[i]->dbn() == dbn)
200             break;
201     }
202     if (i == f_stems_list.length()) // not found
203         return NULL;
204
205     return f_stems_list[i];
206 }
207
208 void
209 DtSR_SearchResults::unreference()
210 {
211     static int nest = 0;
212     if (nest++ == 0)
213         UAS_Base::unreference();
214     nest--;
215 }