source: main/tags/2.80/indexers/mgpp/text/Weights.cpp@ 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: 3.3 KB
Line 
1/**************************************************************************
2 *
3 * Weights.cpp -- Dealing with document weights
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#include "UCArray.h"
23#include "Weights.h"
24#include "sysfuncs.h"
25
26#define MAXBITS (sizeof(unsigned long) * 8)
27
28
29ApproxWeightsData::ApproxWeightsData () {
30 weightBuf = NULL;
31 table = NULL;
32 Free();
33}
34
35ApproxWeightsData::~ApproxWeightsData () {
36 Free ();
37}
38
39void ApproxWeightsData::Free () {
40 bits = 0;
41 mask = 0;
42 L = 0.0;
43 B = 0.0;
44 numLevelDocs = 0;
45 if (weightBuf != NULL) {
46 delete [] weightBuf;
47 weightBuf = NULL;
48 }
49 if (table != NULL) {
50 delete [] table;
51 table = NULL;
52 }
53}
54
55bool ApproxWeightsData::Read (FILE *approxWeightsFile, unsigned long diskPtr,
56 unsigned long _numLevelDocs) {
57 Free ();
58
59 // go to the appropriate place in the weights file
60 fseek (approxWeightsFile, diskPtr, SEEK_SET);
61
62 // read in approx weights parameters
63 ReadUC (approxWeightsFile, bits);
64 ReadD (approxWeightsFile, L);
65 ReadD (approxWeightsFile, B);
66
67 mask = (bits == 32) ? 0xffffffff : (1 << bits) - 1;
68 numLevelDocs = _numLevelDocs;
69
70 // allocate buffer for the weights
71 unsigned long arrSize = (bits*numLevelDocs+sizeof(unsigned long)-1) / 32;
72 weightBuf = new unsigned long[arrSize];
73
74 // read in the weights
75 unsigned long i;
76 for (i=0; i<arrSize; ++i) {
77 ReadUL (approxWeightsFile, weightBuf[i]);
78 }
79
80 // create precomputed table of values if it will be small enough
81 // (if not small enough table == NULL
82 if (bits <= 12) {
83 unsigned long i, tableSize = (1 << bits);
84 table = new float[tableSize];
85 table[0] = L;
86 for (i = 1; i < tableSize; ++i)
87 table[i] = table[i - 1] * B;
88 }
89
90 return true;
91}
92
93float ApproxWeightsData::GetLowerApproxDocWeight (unsigned long levelDocNum) {
94 // sanity check
95 if (bits == 0 || weightBuf == NULL) return 1.0;
96
97 register unsigned long c, pos;
98 register unsigned long *dw;
99
100 // get the compressed version of the weight
101 pos = levelDocNum * bits;
102 dw = &weightBuf[pos / MAXBITS];
103 pos &= (MAXBITS - 1);
104 c = *dw >> pos;
105 if (pos + bits > MAXBITS)
106 c |= *(dw + 1) << (MAXBITS - pos);
107 c &= mask;
108
109 // decompress the weight
110 if (table != NULL) return table[c];
111 return (L * pow (B, (double) c));
112}
113
114
115
116float GetExactDocWeight (FILE *exactWeightsFile, unsigned long diskPtr,
117 unsigned long levelDocNum) {
118 // go to the appropriate place in the weights file
119 fseek (exactWeightsFile, diskPtr+sizeof(float)*levelDocNum, SEEK_SET);
120
121 // read in the exact weight
122 float weight;
123 ReadF (exactWeightsFile, weight);
124
125 return weight;
126}
Note: See TracBrowser for help on using the repository browser.