Remove Unixware and openserver support
[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 libraries 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             if (Ref_list[i]->relevance() < Ref_list[j]->relevance()) {
68                 UAS_Pointer<UAS_SearchResultsEntry> temp = Ref_list[i];
69                 Ref_list[i] = Ref_list[j];
70                 Ref_list[j] = temp;
71             }
72         }
73     }
74 }
75
76 UAS_Pointer<UAS_List<UAS_SearchResultsEntry> >
77 DtSR_SearchResults::create_results(int index, int nres)
78 {
79     // garbage input
80     if (nres == 0 || index >= (int) f_results.length())
81         return NULL;
82
83     int n;
84     if ((n = index + nres - f_results.length()) > 0)
85         nres -= n;
86
87     UAS_Pointer<UAS_List<UAS_SearchResultsEntry> >
88         uas_res = new UAS_List<UAS_SearchResultsEntry>();
89
90     for (int i = index; i < nres; i++)
91         uas_res->insert_item(f_results[i]);
92
93     return uas_res;
94 }
95
96 void
97 DtSR_SearchResults::merge(UAS_Pointer<DtSR_SearchResults> & res)
98 {
99     if (res == (const int)0 || res->f_ndocs == 0) // nothing to merge
100         return;
101
102     int i;
103
104     if (f_ndocs == 0) { // none is there, just copy all of them
105
106         for (i = 0; i < res->f_ndocs; i++) {
107
108             f_results.insert_item(res->f_results[i]);
109
110             UAS_Pointer<UAS_SearchResultsEntry> crude_ptr = res->f_results[i];
111             UAS_SearchResultsEntry &cptr2 = *crude_ptr;     
112             UAS_Pointer<DtSR_SearchResults> uas_this = this;
113
114             ((DtSR_SearchResultsEntry*)&cptr2)->search_result(uas_this);
115
116         }
117
118         f_ndocs = res->f_ndocs;
119         for (i = 0; i < (int) res->f_stems_list.length(); i++) {
120             f_stems_list.insert_item(res->f_stems_list[i]);
121         }
122         res = NULL; 
123         return;
124     }
125
126     int ndocs = f_ndocs;
127
128     UAS_Pointer<DtSR_SearchResults> uas_this = this;
129
130     // both have some
131     int position = 0;
132     for (i = 0; i < res->f_ndocs; i++, ndocs++) {
133
134         if (! (position < ndocs))
135             break;
136
137         int j;
138         for (j = position; j < ndocs; j++) {
139             if (f_results[j]->relevance() > res->f_results[i]->relevance())
140                 continue;
141
142             f_results.insert_item(res->f_results[i], j);
143 #ifdef DEBUG
144             fprintf(stderr, "(DEBUG) insert at %d\n", j);
145 #endif
146             UAS_SearchResultsEntry* crude_ptr =
147                         (UAS_SearchResultsEntry*)res->f_results[i];
148             ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
149             break;
150         }
151         if (j == ndocs) { // insert at the tail
152             f_results.insert_item(res->f_results[i]);
153 #ifdef DEBUG
154             fprintf(stderr, "(DEBUG) insert at the tail\n");
155 #endif
156             UAS_SearchResultsEntry* crude_ptr =
157                         (UAS_SearchResultsEntry*)res->f_results[i];
158             ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
159         }
160         position = ++j;
161     }
162     for (;i < res->f_ndocs; i++) {
163 #ifdef DEBUG
164         fprintf(stderr, "(DEBUG) append the remaining list\n");
165 #endif
166         f_results.insert_item(res->f_results[i]);
167         UAS_SearchResultsEntry* crude_ptr =
168                 (UAS_SearchResultsEntry*)res->f_results[i];
169         ((DtSR_SearchResultsEntry*)crude_ptr)->search_result(uas_this);
170         ndocs++;
171     }
172
173     // merge stems list
174     for (i = 0; i < (int) res->f_stems_list.length(); i++) {
175         f_stems_list.insert_item(res->f_stems_list[i]);
176     }
177
178     f_ndocs += res->f_ndocs;
179     res = NULL;
180 }
181
182 UAS_Pointer<DtSR_Stems>
183 DtSR_SearchResults::stems(int dbn)
184 {
185     int db_count = DtSR_SearchEngine::search_engine().db_count();
186
187     if (dbn < 0 || dbn >= db_count)
188         return NULL;
189
190     unsigned int i;
191     for (i = 0; i < f_stems_list.length(); i++) {
192         if (f_stems_list[i]->dbn() == dbn)
193             break;
194     }
195     if (i == f_stems_list.length()) // not found
196         return NULL;
197
198     return f_stems_list[i];
199 }
200
201 void
202 DtSR_SearchResults::unreference()
203 {
204     static int nest = 0;
205     if (nest++ == 0)
206         UAS_Base::unreference();
207     nest--;
208 }