File: archer_query_array.c

package info (click to toggle)
bow 19991122-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,544 kB
  • ctags: 2,987
  • sloc: ansic: 38,660; lisp: 1,072; makefile: 594; perl: 492; yacc: 149; sh: 91
file content (357 lines) | stat: -rw-r--r-- 9,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#include <bow/libbow.h>
#include <bow/archer_query.h>
#include <bow/archer_query_array.h>

/* free function for passing to bow_array_new() for word_occurences */
void
archer_query_array_free_wo (archer_query_word_occurence * wo)
{
  /* printf(". freeing word_occurence %p\n", wo); */
  bow_array_free (wo->pi);
}

/* free function for passing to bow_array_new() for results */
void
archer_query_array_free_result (archer_query_result * r)
{
  /* printf(". freeing result %p with di %d and wo %p\n", r, r->di, r->wo); */
  bow_array_free (r->wo);
}

/* merge two bow_arrays of ints --- union in other words */
static bow_array *
merge_int (bow_array * a, bow_array * b)
{
  bow_array *ret;
  int i, j;

  ret = bow_array_new (0, sizeof (int), NULL);

  j = 0;
  for (i = 0; i < a->length; i++)
    {
      while ((j < b->length) && (*(int *) bow_array_entry_at_index (a, i) >
				 *(int *) bow_array_entry_at_index (b, j)))
	{
	  bow_array_append (ret, (int *) bow_array_entry_at_index (b, j));
	  j++;
	}

      if ((j < b->length) && (*(int *) bow_array_entry_at_index (a, i) ==
			      *(int *) bow_array_entry_at_index (b, j)))
	j++;

      bow_array_append (ret, (int *) bow_array_entry_at_index (a, i));
    }

  for (; j < b->length; j++)
    bow_array_append (ret, (int *) bow_array_entry_at_index (b, j));

  return ret;
}

/* add a duplicate of a word_occurence to a bow_array */
static void
dup_add_wo (bow_array * a, archer_query_word_occurence * wo)
{
  archer_query_word_occurence new_word_occurence;

  new_word_occurence.is_li = wo->is_li;
  new_word_occurence.wi = wo->wi;
  new_word_occurence.pi = bow_array_duplicate (wo->pi);
  bow_array_append (a, &new_word_occurence);
}

/* add a new word_occurence to a bow_array that contains a pi list that is
   the union of the two word_occurences passed */
static void
dup_add_merge_wo (bow_array * a, archer_query_word_occurence * wo1,
		  archer_query_word_occurence * wo2)
{
  archer_query_word_occurence new_word_occurence;

  new_word_occurence.is_li = wo1->is_li;
  new_word_occurence.wi = wo1->wi;
  new_word_occurence.pi = merge_int (wo1->pi, wo2->pi);
  bow_array_append (a, &new_word_occurence);
}


static inline int
wo_cmp(bow_array *a, int ai, bow_array *b, int bi)
{
  archer_query_word_occurence 
    *awo = (archer_query_word_occurence *)bow_array_entry_at_index(a, ai),
    *bwo = (archer_query_word_occurence *)bow_array_entry_at_index(b, bi);

  if (awo->wi < bwo->wi)
    return -1;
  else if (awo->wi > bwo->wi)
    return +1;
  else if (awo->is_li && !bwo->is_li)
    return -1;
  else if (!awo->is_li && bwo->is_li)
    return +1;
  else
    return 0;
}

/* merge two arrays of word_occurences (union) */
static bow_array *
merge_wo (bow_array * a, bow_array * b)
{
  bow_array *ret;
  int i, j, cmp;

  ret = bow_array_new (0, sizeof (archer_query_word_occurence),
		       archer_query_array_free_wo);
  j = 0;

  for (i = 0; i < a->length; i++)
  {
    while (j < b->length)
    {
      cmp = wo_cmp(a, i, b, j);
      if (cmp <= 0) break;
      dup_add_wo (ret, (archer_query_word_occurence *)
		       bow_array_entry_at_index (b, j));
      j++;
    }

    if (j < b->length && cmp == 0)
    {
      dup_add_merge_wo (ret,
			(archer_query_word_occurence *)
			bow_array_entry_at_index (a, i),
			(archer_query_word_occurence *)
			bow_array_entry_at_index (b, j));
      j++;
    }
    else
      dup_add_wo (ret, (archer_query_word_occurence *)
		       bow_array_entry_at_index (a, i));
  }

  for (; j < b->length; j++)
    dup_add_wo (ret, (archer_query_word_occurence *)
		bow_array_entry_at_index (b, j));

  return ret;
}

/* add a new result to a bow_array that has a word_occurence list that is the
   union of the two passed */
static void
dup_add_merge_result (bow_array * a, archer_query_result * r1,
		      archer_query_result * r2)
{
  archer_query_result new_result;

  new_result.di = r1->di;
  new_result.wo = merge_wo (r1->wo, r2->wo);
  bow_array_append (a, &new_result);
}

/* add a duplicate of a result to a bow_array */
static void
dup_add_result (bow_array * a, archer_query_result * r)
{
  archer_query_result new_result;

  new_result.di = r->di;
  new_result.wo = bow_array_duplicate_wo (r->wo);
  bow_array_append (a, &new_result);
}

/* the actual code for doing unions and intersections of bow_arrays of results.
 */
static bow_array *
union_or_intersection (bow_array * a, bow_array * b, int unionn)
{
  bow_array *ret;
  int i, j;

  ret = bow_array_new (0, sizeof (archer_query_result),
		       archer_query_array_free_result);
  j = 0;

  for (i = 0; i < a->length; i++)
    {
      while ((j < b->length) &&
	     (((archer_query_result *) bow_array_entry_at_index (a, i))->di >
	      ((archer_query_result *) bow_array_entry_at_index (b, j))->di))
	{
	  if (unionn)
	    dup_add_result (ret, (archer_query_result *)
			    bow_array_entry_at_index (b, j));
	  j++;
	}

      if ((j < b->length) &&
	  (((archer_query_result *) bow_array_entry_at_index (a, i))->di ==
	   ((archer_query_result *) bow_array_entry_at_index (b, j))->di))
	{
	  dup_add_merge_result (ret,
		      (archer_query_result *) bow_array_entry_at_index (a, i),
		     (archer_query_result *) bow_array_entry_at_index (b, j));
	  j++;
	}

      if (unionn)
	dup_add_result (ret, (archer_query_result *)
			bow_array_entry_at_index (a, i));
    }

  if (unionn)
    for (; j < b->length; j++)
      dup_add_result (ret, (archer_query_result *)
		      bow_array_entry_at_index (b, j));

  return ret;
}

/* Add the contents of "from" to the end of "onto".  Trash "from" */
void archer_query_array_append(bow_array *onto, bow_array *from)
{
    int i, len, last_di = -1;
    bow_array *tmp;
    archer_query_result *aqrp, *last = NULL;

    len = onto->length;
    if (len)
    {
      last = (archer_query_result *)bow_array_entry_at_index(onto, len - 1);
      last_di = last->di;
    }

    for (i = 0; i < from->length; ++i)
    {
	aqrp = (archer_query_result *)bow_array_entry_at_index(from, i);
	if (aqrp->di == last_di)
	{
	  tmp = merge_wo(last->wo, aqrp->wo);
	  bow_array_free(last->wo);
	  last->wo = tmp;
	}
	else
	{
	  last = (archer_query_result *)
	    bow_array_entry_at_index(onto, bow_array_append(onto, aqrp));
	  last->wo = bow_array_duplicate_wo(last->wo);
	  last_di = aqrp->di;
	}
    }

    bow_array_free(from);
}

/* assumes they're bow_arrays of results and sorted by di. returns a
   new array containing duplicates of all elements of a that have a di
   equal to that of an element of b, merging the pi lists of each
   result. */
bow_array *
archer_query_array_intersection (bow_array * a, bow_array * b)
{
  return union_or_intersection (a, b, 0);
}

/* assumes they're bow_arrays of results and sorted by di. returns a
   new array containing duplicates of all elements of a and all
   elements of b such that any element of a with the same di as an
   element of b has a word_occurrence array that is the union of the 
   two corresponding arrays. phew. */
bow_array *
archer_query_array_union (bow_array * a, bow_array * b)
{
  return union_or_intersection (a, b, 1);
}

/* assumes they're bow_arrays of results, sorted, and b is a subset of
   a. returns a new array containing duplicates of all elements of a
   that did not have a di equal to that of an element of b. */
bow_array *
archer_query_array_subtract (bow_array * a, bow_array * b)
{
  int i, j;
  bow_array *ret;

  assert (a->length > b->length);

  ret = bow_array_new (a->length - b->length, sizeof (archer_query_result),
		       archer_query_array_free_result);

  j = 0;
  for (i = 0; i < b->length; i++)
    {
      archer_query_result *aj;
      archer_query_result *bi;

      bi = (archer_query_result *) bow_array_entry_at_index (b, i);
      aj = (archer_query_result *) bow_array_entry_at_index (a, j);

      while (aj->di < bi->di)
      {
	dup_add_result (ret, aj);
	aj = (archer_query_result *) bow_array_entry_at_index(a, ++j);
      }

      assert (aj->di == bi->di);

      ++j;
    }

  return ret;
}

/* duplicates the array entirely, regardless of its content */
bow_array *
bow_array_duplicate (bow_array * array)
{
  bow_array *ret;

  ret = (bow_array *) malloc (sizeof (bow_array));
  memcpy (ret, array, sizeof (bow_array));
  ret->entries = calloc (ret->size, ret->entry_size);
  memcpy (ret->entries, array->entries, ret->size * ret->entry_size);

  return ret;
}

/* duplicates the array entirely, regardless of its content
   has to do a little extra because of some nested dynamically alloc'd mem
 */
bow_array *
bow_array_duplicate_wo (bow_array * wo_array)
{
  int i;
  bow_array *ret;
  archer_query_word_occurence *wop;

  ret = (bow_array *) malloc (sizeof (bow_array));
  memcpy (ret, wo_array, sizeof (bow_array));
  ret->entries = calloc (ret->size, ret->entry_size);
  memcpy (ret->entries, wo_array->entries, ret->size * ret->entry_size);

  for (i = 0; i < ret->length; ++i)
  {
    wop = (archer_query_word_occurence *)bow_array_entry_at_index(ret, i);
    wop->pi = bow_array_duplicate(wop->pi);
  }

  return ret;
}

bow_boolean
archer_query_array_contains (bow_array * a, int di)
{
  int i;
  bow_boolean found;

  found = bow_no;

  for (i = 0; (i < a->length) && !found; i++)
    if (((archer_query_result *) bow_array_entry_at_index (a, i))->di == di)
      found = bow_yes;

  return found;
}