source: main/branches/64_bit_Greenstone/greenstone2/common-src/indexers/mgpp/text/QueryResultsSort.h@ 23508

Last change on this file since 23508 was 23508, checked in by sjm84, 13 years ago

Committing 64 bit changes into the branch

  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
RevLine 
[3365]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,
[23508]47 mg_u_long heapSize,
[3365]48 const Compare &comp) {
[23508]49 mg_u_long holeIndex = heapSize-1;
50 mg_u_long parent = (holeIndex - 1) / 2;
[3365]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,
[23508]65 mg_u_long holeIndex,
66 mg_u_long heapSize,
[3365]67 const Compare &comp) {
[23508]68 mg_u_long secondChild = 2 * holeIndex + 2;
[3365]69 while (secondChild < heapSize) {
70 if (comp(*(parallelHeap + secondChild),
71 *(parallelHeap + (secondChild - 1))))
[9613]72 --secondChild;
[3365]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,
[23508]89 mg_u_long heapSize,
[3365]90 const Compare &comp) {
91 swap (*(parallelHeap), *(parallelHeap+heapSize-1));
92 swap (*(docNumHeap), *(docNumHeap+heapSize-1));
[23508]93 AdjustHeap (docNumHeap, parallelHeap, (mg_u_long)0, heapSize-1, comp);
[3365]94}
95
96template <class RandomAccessIterator, class Compare>
97void MakeHeap (DocNumArray::iterator docNumHeap,
98 RandomAccessIterator parallelHeap,
[23508]99 mg_u_long size,
[3365]100 const Compare &comp) {
101 if (size < 2) return;
102
[23508]103 mg_u_long i = 0;
[9613]104 for (i=1; i<=size; ++i)
[3365]105 PushHeap (docNumHeap, parallelHeap, i, comp);
106}
107
108
109template <class RandomAccessIterator, class Compare>
110void SortHeap (DocNumArray::iterator docNumHeap,
111 RandomAccessIterator parallelHeap,
[23508]112 mg_u_long size,
[3365]113 const Compare &comp) {
114 if (size < 2) return;
115
[23508]116 mg_u_long i;
[9613]117 for (i=size; i>1; --i)
[3365]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,
[23508]125 mg_u_long heapSize,
[3365]126 DocNumArray::iterator docNumAdd,
127 RandomAccessIterator parallelAdd,
[23508]128 mg_u_long addSize,
[3365]129 const Compare &comp) {
[23508]130 mg_u_long i;
[9613]131 for (i=0; i<addSize; ++i) {
[3365]132 if (comp(*(parallelAdd+i), *(parallelHeap))) {
133 swap (*(docNumHeap), *(docNumAdd+i));
134 swap (*(parallelHeap), *(parallelAdd+i));
[23508]135 AdjustHeap (docNumHeap, parallelHeap, (mg_u_long)0, heapSize, comp);
[3365]136 }
137 }
138}
139
140#endif
Note: See TracBrowser for help on using the repository browser.