source: main/tags/2.80/indexers/mgpp/text/QueryResultsSort.h@ 24540

Last change on this file since 24540 was 9613, checked in by kjdon, 19 years ago

added in x++ -> ++x changes submitted by Emanuel Dejanu

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