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