source: trunk/gsdl/src/mgpp/text/QueryResultsSort.h@ 861

Last change on this file since 861 was 861, checked in by rjmcnab, 24 years ago

fixed a few more bugs

  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1/**************************************************************************
2 *
3 * QueryResultsSort.h -- Functions to sort query results and parallel data
4 * Copyright (C) 1999 Rodger McNab
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: QueryResultsSort.h 861 2000-01-18 23:24:19Z rjmcnab $
21 *
22 **************************************************************************/
23
24#ifndef QUERYRESULTSSORT_H
25#define QUERYRESULTSSORT_H
26
27#include "Terms.h"
28
29
30struct LTRank {
31 bool operator()(const float &f1, const float &f2) const
32 { return (f1 < f2); }
33};
34
35struct GTRank {
36 bool operator()(const float &f1, const float &f2) const
37 { return (f1 > f2); }
38};
39
40
41
42// assumes the rank vector is the same size as the document
43// number vector. assumes the value to be pushed is in
44// docNumHeap + heapSize - 1 and rankHeap + heapSize - 1
45// topIndex can be set
46template <class RandomAccessIterator, class Compare>
47void PushHeap (DocNumArray::iterator docNumHeap,
48 RandomAccessIterator parallelHeap,
49 unsigned long heapSize,
50 const Compare &comp) {
51 unsigned long holeIndex = heapSize-1;
52 unsigned long parent = (holeIndex - 1) / 2;
53 while (holeIndex > 0 && comp (*(parallelHeap+parent),
54 *(parallelHeap+holeIndex))) {
55 swap (*(parallelHeap+holeIndex), *(parallelHeap+parent));
56 swap (*(docNumHeap+holeIndex), *(docNumHeap+parent));
57 holeIndex = parent;
58 parent = (holeIndex - 1) / 2;
59 }
60}
61
62// AdjustHeap is used to add a element to a heap after one
63// element in the heap has been replaced with a new one
64template <class RandomAccessIterator, class Compare>
65void AdjustHeap (DocNumArray::iterator docNumHeap,
66 RandomAccessIterator parallelHeap,
67 unsigned long holeIndex,
68 unsigned long heapSize,
69 const Compare &comp) {
70 unsigned long secondChild = 2 * holeIndex + 2;
71 while (secondChild < heapSize) {
72 if (comp(*(parallelHeap + secondChild),
73 *(parallelHeap + (secondChild - 1))))
74 secondChild--;
75 swap (*(parallelHeap+holeIndex), *(parallelHeap+secondChild));
76 swap (*(docNumHeap+holeIndex), *(docNumHeap+secondChild));
77 holeIndex = secondChild;
78 secondChild = 2 * secondChild + 2;
79 }
80 if (secondChild == heapSize) {
81 swap (*(parallelHeap+holeIndex), *(parallelHeap+(secondChild-1)));
82 swap (*(docNumHeap+holeIndex), *(docNumHeap+(secondChild-1)));
83 holeIndex = secondChild - 1;
84 }
85 PushHeap (docNumHeap, parallelHeap, holeIndex+1, comp);
86}
87
88template <class RandomAccessIterator, class Compare>
89void PopHeap (DocNumArray::iterator docNumHeap,
90 RandomAccessIterator parallelHeap,
91 unsigned long heapSize,
92 const Compare &comp) {
93 swap (*(parallelHeap), *(parallelHeap+heapSize-1));
94 swap (*(docNumHeap), *(docNumHeap+heapSize-1));
95 AdjustHeap (docNumHeap, parallelHeap, 0, heapSize-1, comp);
96}
97
98template <class RandomAccessIterator, class Compare>
99void MakeHeap (DocNumArray::iterator docNumHeap,
100 RandomAccessIterator parallelHeap,
101 unsigned long size,
102 const Compare &comp) {
103 if (size < 2) return;
104
105 unsigned long i = 0;
106 for (i=1; i<=size; i++)
107 PushHeap (docNumHeap, parallelHeap, i, comp);
108}
109
110
111template <class RandomAccessIterator, class Compare>
112void SortHeap (DocNumArray::iterator docNumHeap,
113 RandomAccessIterator parallelHeap,
114 unsigned long size,
115 const Compare &comp) {
116 if (size < 2) return;
117
118 unsigned long i;
119 for (i=size; i>1; i--)
120 PopHeap (docNumHeap, parallelHeap, i, comp);
121}
122
123// uses the heap to get the top "size" elements
124template <class RandomAccessIterator, class Compare>
125void SelectAddHeap (DocNumArray::iterator docNumHeap,
126 RandomAccessIterator parallelHeap,
127 unsigned long heapSize,
128 DocNumArray::iterator docNumAdd,
129 RandomAccessIterator parallelAdd,
130 unsigned long addSize,
131 const Compare &comp) {
132 unsigned long i;
133 for (i=0; i<addSize; i++) {
134 if (comp(*(parallelAdd+i), *(parallelHeap))) {
135 swap (*(docNumHeap), *(docNumAdd+i));
136 swap (*(parallelHeap), *(parallelAdd+i));
137 AdjustHeap (docNumHeap, parallelHeap, 0, heapSize, comp);
138 }
139 }
140}
141
142#endif
Note: See TracBrowser for help on using the repository browser.