Add GNU LGPL headers to all .c .C and .h files
[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 "DtSR_SearchEngine.hh"
28 #include "DtSR_SearchResultsEntry.hh"
29 #include "DtSR_SearchResults.hh"
30
31 DtSR_SearchResults::DtSR_SearchResults
32         (UAS_Pointer<UAS_String> query,
33          UAS_Pointer<UAS_String> scope_name,
34          UAS_Pointer<UAS_List<UAS_SearchResultsEntry> > res,
35          int ndocs,
36          UAS_Pointer<DtSR_Stems> stems,
37          UAS_SearchZones zones, int stype)
38         : UAS_SearchResults(query, scope_name, res, ndocs), f_zones(zones),
39           f_search_type(stype)
40 {
41     f_stems_list.insert_item(stems);
42
43     UAS_List<UAS_SearchResultsEntry>& list =
44                         *(UAS_List<UAS_SearchResultsEntry>*)res;
45
46     for (int i = 0; i < list.length(); i++) {
47
48         UAS_Pointer<UAS_SearchResultsEntry> crude_ptr = list[i];
49         UAS_SearchResultsEntry &cptr2 = *crude_ptr;
50         UAS_Pointer<DtSR_SearchResults> uas_this = this;
51         ((DtSR_SearchResultsEntry *)&cptr2)->search_result(uas_this);
52
53     }
54 }
55
56 void
57 DtSR_SearchResults::sort(UAS_Pointer<UAS_List<UAS_SearchResultsEntry> > res)
58 {
59     UAS_List<UAS_SearchResultsEntry>& Ref_list =
60                         *(UAS_List<UAS_SearchResultsEntry>*)res;
61   
62     unsigned int list_length = Ref_list.length();
63     for (int i = 0; i < list_length; i++) {
64         for (int j = i+1; j < list_length; j++) {
65
66 #ifdef USL
67             // this line does nothing, but if you remove it
68             // this code will not compile on novell.  go figure.  rCs
69
70             UAS_Pointer<UAS_SearchResultsEntry> reflisti = Ref_list[i];
71 #endif
72
73             if (Ref_list[i]->relevance() < Ref_list[j]->relevance()) {
74                 UAS_Pointer<UAS_SearchResultsEntry> temp = Ref_list[i];
75                 Ref_list[i] = Ref_list[j];
76                 Ref_list[j] = temp;
77             }
78         }
79     }
80 }
81
82 UAS_Pointer<UAS_List<UAS_SearchResultsEntry> >
83 DtSR_SearchResults::create_results(int index, int nres)
84 {
85     // garbage input
86     if (nres == 0 || index >= f_results.length())
87         return NULL;
88
89     int n;
90     if ((n = index + nres - f_results.length()) > 0)
91         nres -= n;
92
93     UAS_Pointer<UAS_List<UAS_SearchResultsEntry> >
94         uas_res = new UAS_List<UAS_SearchResultsEntry>();
95
96     for (int i = index; i < nres; i++)
97         uas_res->insert_item(f_results[i]);
98
99     return uas_res;
100 }
101
102 void
103 DtSR_SearchResults::merge(UAS_Pointer<DtSR_SearchResults> & res)
104 {
105     if (res == (const int)NULL || res->f_ndocs == 0) // nothing to merge
106         return;
107
108     int i;
109
110     if (f_ndocs == 0) { // none is there, just copy all of them
111
112         for (i = 0; i < res->f_ndocs; i++) {
113
114             f_results.insert_item(res->f_results[i]);
115
116             UAS_Pointer<UAS_SearchResultsEntry> crude_ptr = res->f_results[i];
117             UAS_SearchResultsEntry &cptr2 = *crude_ptr;     
118             UAS_Pointer<DtSR_SearchResults> uas_this = this;
119
120             ((DtSR_SearchResultsEntry*)&cptr2)->search_result(uas_this);
121
122         }
123
124         f_ndocs = res->f_ndocs;
125         for (i = 0; i < res->f_stems_list.length(); i++) {
126             f_stems_list.insert_item(res->f_stems_list[i]);
127         }
128         res = NULL; 
129         return;
130     }
131
132     int ndocs = f_ndocs;
133
134     UAS_Pointer<DtSR_SearchResults> uas_this = this;
135
136     // both have some
137     int position = 0;
138     for (i = 0; i < res->f_ndocs; i++, ndocs++) {
139
140         if (! (position < ndocs))
141             break;
142
143         for (int j = position; j < ndocs; j++) {
144             if (f_results[j]->relevance() > res->f_results[i]->relevance())
145                 continue;
146
147             f_results.insert_item(res->f_results[i], j);
148 #ifdef DEBUG
149             fprintf(stderr, "(DEBUG) insert at %d\n", j);
150 #endif
151             UAS_SearchResultsEntry* crude_ptr =
152                         (UAS_SearchResultsEntry*)res->f_results[i];
153             ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
154             break;
155         }
156         if (j == ndocs) { // insert at the tail
157             f_results.insert_item(res->f_results[i]);
158 #ifdef DEBUG
159             fprintf(stderr, "(DEBUG) insert at the tail\n");
160 #endif
161             UAS_SearchResultsEntry* crude_ptr =
162                         (UAS_SearchResultsEntry*)res->f_results[i];
163             ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
164         }
165         position = ++j;
166     }
167     for (;i < res->f_ndocs; i++) {
168 #ifdef DEBUG
169         fprintf(stderr, "(DEBUG) append the remaining list\n");
170 #endif
171         f_results.insert_item(res->f_results[i]);
172         UAS_SearchResultsEntry* crude_ptr =
173                 (UAS_SearchResultsEntry*)res->f_results[i];
174         ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
175         ndocs++;
176     }
177
178     // merge stems list
179     for (i = 0; i < res->f_stems_list.length(); i++) {
180         f_stems_list.insert_item(res->f_stems_list[i]);
181     }
182
183     f_ndocs += res->f_ndocs;
184     res = NULL;
185 }
186
187 UAS_Pointer<DtSR_Stems>
188 DtSR_SearchResults::stems(int dbn)
189 {
190     int db_count = DtSR_SearchEngine::search_engine().db_count();
191
192     if (dbn < 0 || dbn >= db_count)
193         return NULL;
194
195     int i;
196     for (i = 0; i < f_stems_list.length(); i++) {
197         if (f_stems_list[i]->dbn() == dbn)
198             break;
199     }
200     if (i == f_stems_list.length()) // not found
201         return NULL;
202
203     return f_stems_list[i];
204 }
205
206 void
207 DtSR_SearchResults::unreference()
208 {
209     static int nest = 0;
210     if (nest++ == 0)
211         UAS_Base::unreference();
212     nest--;
213 }