1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * lists.c -- List processing functions for boolean queries
|
---|
4 | * Copyright (C) 1994 Neil Sharman, Alistair Moffat and Lachlan Andrew
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: lists.c 13 1998-11-17 09:36:00Z rjmcnab $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 |
|
---|
26 | #include "memlib.h"
|
---|
27 |
|
---|
28 | #include "lists.h"
|
---|
29 | #include "local_strings.h"
|
---|
30 | #include "locallib.h"
|
---|
31 |
|
---|
32 | /*
|
---|
33 | $Log$
|
---|
34 | Revision 1.1 1998/11/17 09:34:47 rjmcnab
|
---|
35 | *** empty log message ***
|
---|
36 |
|
---|
37 | * Revision 1.3 1994/10/20 03:56:50 tes
|
---|
38 | * I have rewritten the boolean query optimiser and abstracted out the
|
---|
39 | * components of the boolean query.
|
---|
40 | *
|
---|
41 | * Revision 1.2 1994/09/20 04:41:37 tes
|
---|
42 | * For version 1.1
|
---|
43 | *
|
---|
44 | */
|
---|
45 |
|
---|
46 | static char *RCSID = "$Id: lists.c 13 1998-11-17 09:36:00Z rjmcnab $";
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Make a DocList large enough to hold num entries
|
---|
54 | */
|
---|
55 | DocList *
|
---|
56 | MakeDocList (int num)
|
---|
57 | {
|
---|
58 | DocList *d;
|
---|
59 | if (!(d = Xmalloc (sizeof (DocList) + sizeof (DocEntry) * (num - 1))))
|
---|
60 | return (NULL);
|
---|
61 | d->num = 0;
|
---|
62 | return (d);
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /*
|
---|
67 | * Change the size of a DocList so that it can hold num entries
|
---|
68 | */
|
---|
69 | DocList *
|
---|
70 | ResizeDocList (DocList * d, int num)
|
---|
71 | {
|
---|
72 | if (!(d = Xrealloc (d, sizeof (DocList) + sizeof (DocEntry) * (num - 1))))
|
---|
73 | return (NULL);
|
---|
74 | return (d);
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | /* IntersectLists () */
|
---|
79 | /* Returns the intersection of List1 and List2 by deleting from List1 all */
|
---|
80 | /* entries not in List2. All of List2 is freed, and the nodes of List1 not */
|
---|
81 | /* included in the resultant list are freed. */
|
---|
82 | /* On exit the intersection of the lists is returned, or NULL on empty */
|
---|
83 | /* intersection. */
|
---|
84 | /* Assumes that both lists are ordered on increasing Value. */
|
---|
85 | DocList *
|
---|
86 | IntersectLists (DocList * List1, DocList * List2)
|
---|
87 | {
|
---|
88 | DocEntry *s1, *d1, *s2;
|
---|
89 | if (!List2 || !List1)
|
---|
90 | {
|
---|
91 | if (List1)
|
---|
92 | Xfree (List1);
|
---|
93 | if (List2)
|
---|
94 | Xfree (List2);
|
---|
95 | return (MakeDocList (0));
|
---|
96 | }
|
---|
97 | d1 = s1 = List1->DE;
|
---|
98 | s2 = List2->DE;
|
---|
99 | while (s1 - List1->DE < List1->num && s2 - List2->DE < List2->num)
|
---|
100 | {
|
---|
101 | if (s1->DocNum == s2->DocNum)
|
---|
102 | *d1++ = *s1++;
|
---|
103 | if (s1 - List1->DE < List1->num)
|
---|
104 | while (s2 - List2->DE < List2->num && s2->DocNum < s1->DocNum)
|
---|
105 | s2++;
|
---|
106 | if (s2 - List2->DE < List2->num)
|
---|
107 | while (s1 - List1->DE < List1->num && s1->DocNum < s2->DocNum)
|
---|
108 | s1++;
|
---|
109 | }
|
---|
110 | List1->num = d1 - List1->DE;
|
---|
111 | Xfree (List2);
|
---|
112 | return (List1);
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | /* DiffLists () */
|
---|
117 | /* Returns the difference of List1 and List2 by deleting from List1 all */
|
---|
118 | /* entries in List2. All of List2 is freed, and the nodes of List1 not */
|
---|
119 | /* included in the resultant list are freed. */
|
---|
120 | /* On exit the differrence of the lists is returned, or NULL on empty */
|
---|
121 | /* intersection. */
|
---|
122 | /* Assumes that both lists are ordered on increasing Value. */
|
---|
123 | DocList *
|
---|
124 | DiffLists (DocList * List1, DocList * List2)
|
---|
125 | {
|
---|
126 | DocEntry *s1, *d1, *s2;
|
---|
127 | if (!List1)
|
---|
128 | {
|
---|
129 | if (List2)
|
---|
130 | Xfree (List2);
|
---|
131 | return (MakeDocList (0));
|
---|
132 | }
|
---|
133 | if (!List2)
|
---|
134 | return (List1);
|
---|
135 | d1 = s1 = List1->DE;
|
---|
136 | s2 = List2->DE;
|
---|
137 | while (s1 - List1->DE < List1->num)
|
---|
138 | {
|
---|
139 | if (s2 - List2->DE == List2->num)
|
---|
140 | *d1++ = *s1++;
|
---|
141 | else
|
---|
142 | {
|
---|
143 | if (s1->DocNum == s2->DocNum)
|
---|
144 | {
|
---|
145 | s1++;
|
---|
146 | s2++;
|
---|
147 | }
|
---|
148 | else if (s1->DocNum < s2->DocNum)
|
---|
149 | *d1++ = *s1++;
|
---|
150 | else
|
---|
151 | s2++;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | List1->num = d1 - List1->DE;
|
---|
155 | Xfree (List2);
|
---|
156 | return (List1);
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* MergeLists () */
|
---|
160 | /* Finds the union of two lists and returns the head of the new list formed. */
|
---|
161 | /* Assumes that both lists are ordered on increasing Value. */
|
---|
162 | DocList *
|
---|
163 | MergeLists (DocList * List1, DocList * List2)
|
---|
164 | {
|
---|
165 | DocList *NewList;
|
---|
166 | DocEntry *d, *s1, *s2;
|
---|
167 | if (!List1)
|
---|
168 | {
|
---|
169 | if (List2)
|
---|
170 | return (List2);
|
---|
171 | else
|
---|
172 | return (MakeDocList (0));
|
---|
173 | }
|
---|
174 | if (!List2)
|
---|
175 | return (List1);
|
---|
176 | if (!(NewList = MakeDocList (List1->num + List2->num)))
|
---|
177 | {
|
---|
178 | Xfree (List1);
|
---|
179 | Xfree (List2);
|
---|
180 | return (NULL);
|
---|
181 | }
|
---|
182 |
|
---|
183 | s1 = List1->DE;
|
---|
184 | s2 = List2->DE;
|
---|
185 | d = NewList->DE;
|
---|
186 | while (s1 - List1->DE < List1->num || s2 - List2->DE < List2->num)
|
---|
187 | {
|
---|
188 | if (s1 - List1->DE >= List1->num)
|
---|
189 | *d++ = *s2++;
|
---|
190 | else if (s2 - List2->DE >= List2->num)
|
---|
191 | *d++ = *s1++;
|
---|
192 | else if (s1->DocNum == s2->DocNum)
|
---|
193 | {
|
---|
194 | *d = *s1++;
|
---|
195 | (d++)->Weight += (s2++)->Weight;
|
---|
196 | }
|
---|
197 | else if (s1->DocNum < s2->DocNum)
|
---|
198 | *d++ = *s1++;
|
---|
199 | else
|
---|
200 | *d++ = *s2++;
|
---|
201 | }
|
---|
202 | NewList->num = d - NewList->DE;
|
---|
203 | Xfree (List1);
|
---|
204 | Xfree (List2);
|
---|
205 | return (NewList);
|
---|
206 | }
|
---|