[855] | 1 | /**************************************************************************
|
---|
| 2 | *
|
---|
| 3 | * FragLevelConvert.cpp -- converting between fragment and document numbers
|
---|
| 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: FragLevelConvert.cpp 855 2000-01-14 02:17:52Z sjboddie $
|
---|
| 21 | *
|
---|
| 22 | **************************************************************************/
|
---|
| 23 |
|
---|
| 24 | #include "FragLevelConvert.h"
|
---|
| 25 | #include "bitio_m_stdio.h"
|
---|
| 26 | #include "bitio_gen.h"
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | FragLevelConvert::FragLevelConvert () {
|
---|
| 30 | numFrags = 0;
|
---|
| 31 | numLevelDocs = 0;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | bool FragLevelConvert::Read (FILE *invfFile,
|
---|
| 35 | unsigned long levelPtr,
|
---|
| 36 | unsigned long _numFrags,
|
---|
| 37 | unsigned long _numLevelDocs) {
|
---|
| 38 | levelStarts.erase (levelStarts.begin(), levelStarts.end());
|
---|
| 39 | numFrags = _numFrags;
|
---|
| 40 | numLevelDocs = _numLevelDocs;
|
---|
| 41 |
|
---|
| 42 | // seek to the appropriate place in the inverted file
|
---|
| 43 | fseek (invfFile, levelPtr, SEEK_SET);
|
---|
| 44 |
|
---|
| 45 | stdio_bitio_buffer buffer(invfFile);
|
---|
| 46 |
|
---|
| 47 | unsigned long pTag = numLevelDocs*2;
|
---|
| 48 | unsigned long B = BIO_Bblock_Init (numFrags+pTag, pTag);
|
---|
| 49 | unsigned long fragNum = 0;
|
---|
| 50 | unsigned long i;
|
---|
| 51 | for (i=0; i<numLevelDocs; i++) {
|
---|
| 52 | unsigned long delta = buffer.bblock_decode (B, NULL)-1;
|
---|
| 53 | fragNum += delta;
|
---|
| 54 |
|
---|
| 55 | levelStarts.push_back (fragNum);
|
---|
| 56 |
|
---|
| 57 | // ignore end, assume every fragment is in a level
|
---|
| 58 | delta = buffer.bblock_decode (B, NULL)-1;
|
---|
| 59 | fragNum += delta;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // fragNum would be the starting fragment of the next document
|
---|
| 63 | // if there was one
|
---|
| 64 | levelStarts.push_back (fragNum);
|
---|
| 65 |
|
---|
| 66 | buffer.done();
|
---|
| 67 |
|
---|
| 68 | return true;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | bool FragLevelConvert::FragToLevel (unsigned long fragNum,
|
---|
| 72 | unsigned long &levelDocNum) const {
|
---|
| 73 | // do binary search for something containing this fragment number
|
---|
| 74 | unsigned long low = 1, high = numLevelDocs;
|
---|
| 75 | unsigned long mid;
|
---|
| 76 |
|
---|
| 77 | while ((mid = (low+high)/2) >= 1 && low <= high) {
|
---|
| 78 | if (fragNum > levelStarts[mid]) low = mid+1;
|
---|
| 79 | else if (fragNum <= levelStarts[mid-1]) high = mid-1;
|
---|
| 80 | else {
|
---|
| 81 | levelDocNum = mid;
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
| 85 | return false;
|
---|
| 86 | }
|
---|
| 87 |
|
---|