File: OrQuery.cc

package info (click to toggle)
htdig 1%3A3.2.0b6-3.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 14,936 kB
  • ctags: 9,369
  • sloc: ansic: 49,626; cpp: 46,470; sh: 23,053; xml: 4,180; perl: 2,543; makefile: 868; php: 79; asm: 14
file content (126 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download | duplicates (9)
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
// 
// OrQuery.cc
//
// OrQuery: an operator query that merges all the results of its operands
//          i.e. does 'or' combination
// 
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1995-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
// 
// $Id: OrQuery.cc,v 1.4 2004/05/28 13:15:24 lha Exp $
//


#include "OrQuery.h"
//
// return a ResultList containing an Or of the results of the operands
// evaluate all operands to do so
//
//	l	r	or
//	---------------------
//	0	0	0
//	0	b	b
//	0	x	x
//	a	0	a
//	a	b	union(a,b)
//	a	x	a
//	x	0	x
//	x	b	b
//	x	x	x
//
// i.e. nulls and ignored are left out union
//
// Note that all operands are evaluated
// Ignored operands are not included in the operation
// the longer input result list is passed separately to Union
//

ResultList *
OrQuery::Evaluate()
{
	ResultList *result = 0;
	ResultList *longer = 0;
	List  shorter;
	int ignores = 0;
	operands.Start_Get();
	Query *operand = (Query *) operands.Get_Next();
	while(operand)
	{
		ResultList *next = operand->GetResults();
		if(next)
		{
			if(!next->IsIgnore())
			{
				if(!longer || longer->Count() < next->Count())
				{
					if(longer)
					{
						shorter.Add(longer);
					}
					longer = next;
				}
				else
				{
					shorter.Add(next);
				}
			}
			else
			{
				ignores++;
			}
		}
		operand = (Query *) operands.Get_Next();
	}
	if(longer)
	{
		result = Union(*longer, shorter);
		shorter.Release();
	}
	else if(ignores == operands.Count())
	{
		result = new ResultList;
		result->Ignore();
	}
	return result;
}

//
// copy unique DocMatches to the resulting list
// matches with the same docId are merged
// the longer list is assumed to be the first parameter
// this is a modest optimisation
//
ResultList *
OrQuery::Union(const ResultList &longer, const List &lists)
{
	ResultList *result = new ResultList(longer);

	ListCursor lc;	
	lists.Start_Get(lc);
	ResultList *current = (ResultList *) lists.Get_Next(lc);
	while(current)
	{
		DictionaryCursor c;
		current->Start_Get(c);
		DocMatch *match = (DocMatch *) current->Get_NextElement(c);
		while(match)
		{
			DocMatch *previous = result->find(match->GetId());
			if(previous)
			{
				previous->Merge(*match);
			}
			else
			{
				DocMatch *copy = new DocMatch(*match);
				result->add(copy);
			}
			match = (DocMatch *) current->Get_NextElement(c);
		}
		current = (ResultList *) lists.Get_Next(lc);
	}
	return result;
}