1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
|
/* "Position vector", a (compressed) list of word positions in documents */
/* Copyright (C) 1998 Andrew McCallum
Written by: Andrew Kachites McCallum <mccallum@cs.cmu.edu>
This file is part of the Bag-Of-Words Library, `libbow'.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License
as published by the Free Software Foundation, version 2.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */
#include <bow/libbow.h>
#include <bow/archer.h>
#define PV_DEBUG 1
/* The first four bytes of a segment are an int that indicate how many
bytes are allocated in this segment. The last four bytes of a
segment are an int that indicates the seek location of the next
segment. The read/write_segment_bytes_remaining does not include
the size of the two int's. */
/* Always enough for one "document index"/"word index" pair:
5 bytes == 6+4*7 == 34 bits for di, likewise for pi. */
#define bow_pv_max_sizeof_di_pi (2 * 5)
static int bow_pv_sizeof_first_segment = 2 * bow_pv_max_sizeof_di_pi;
/* Fill in PV with the correct initial values, and write the first
segment header to disk. What this function does must match what
bow_pv_add_di_pi() does when it adds a new segment. */
void
bow_pv_init (bow_pv *pv, FILE *fp)
{
pv->count = 0;
/* Set the size of this first segment to its default */
pv->write_segment_bytes = bow_pv_sizeof_first_segment;
pv->write_segment_bytes_remaining = bow_pv_sizeof_first_segment;
pv->read_segment_bytes_remaining = bow_pv_sizeof_first_segment;
/* Seek to the end of the file, which is the position at which this
PV will begin. */
fseek (fp, 0, SEEK_END);
/* Remember this value so that we can rewind the read position later. */
pv->seek_start = ftell (fp);
/* Write the "header", which is the number of bytes in this segment. */
bow_fwrite_int (pv->write_segment_bytes_remaining, fp);
/* Remember this position. It is the seek position at which we will
start writing and reading. */
pv->write_seek_end = ftell (fp);
pv->read_seek_end = pv->write_seek_end;
/* Seek forward to the end of this segment and write a (temporary
value for) the "tailer". This will reserve all the intervening
bytes for this segment, and future calls to seek to the end of
the file (for other PV's) will go after the end of this
segment. */
fseek (fp, pv->write_segment_bytes_remaining, SEEK_CUR);
bow_fwrite_int (-1, fp);
/* Initialize our DI and PI context. */
pv->read_last_di = -1;
pv->read_last_pi = -1;
pv->write_last_di = -1;
pv->write_last_pi = -1;
}
/* Write to FP the unsigned integer I, marked with the special flag
saying if it is a DI or a PI, (as indicated by IS_DI). Assumes
that FP is already seek'ed to the correct position, and there is
enough space there in this segment to write the info. Returns the
number of bytes written. */
static inline int
old_bow_pv_write_unsigned_int (unsigned int i, int is_di, FILE *fp)
{
bow_pe pe;
int byte_count = 1;
if (is_di)
pe.bits.is_di = 1;
else
pe.bits.is_di = 0;
while (i > 0x3f) /* binary = 00111111 */
{
pe.bits.is_more = 1;
pe.bits.index = i & 0x3f; /* binary = 00111111 */
fputc (pe.byte, fp);
byte_count++;
i = i >> 6;
}
pe.bits.is_more = 0;
pe.bits.index = i;
fputc (pe.byte, fp);
return byte_count;
}
/* Write to FP the unsigned integer I, marked with the special flag
saying if it is a DI or a PI, (as indicated by IS_DI). Assumes
that FP is already seek'ed to the correct position, and there is
enough space there in this segment to write the info. Returns the
number of bytes written. */
static inline int
bow_pv_write_unsigned_int (unsigned int i, int is_di, FILE *fp)
{
bow_pe pe;
int byte_count = 1; /* Count already the last byte */
/* assert (i < (1 < 6+7+7+7+1)); */
if (is_di)
pe.bits.is_di = 1;
else
pe.bits.is_di = 0;
if (i > 0x3f) /* binary = 00111111 */
{
pe.bits.is_more = 1;
pe.bits.index = i & 0x3f; /* binary = 00111111 */
fputc (pe.byte, fp); /* Write the first byte */
byte_count++;
i = i >> 6;
while (i > 0x7f) /* binary = 01111111 */
{
pe.bits_more.is_more = 1;
pe.bits_more.index = i & 0x7f;
fputc (pe.byte, fp);
byte_count++;
i = i >> 7;
}
pe.bits_more.is_more = 0;
pe.bits_more.index = i;
fputc (pe.byte, fp);
}
else
{
pe.bits.is_more = 0;
pe.bits.index = i;
/* Write the first byte and only */
fputc (pe.byte, fp);
}
return byte_count;
}
/* Read an unsigned integer into I, and indicate whether it is a
"document index" or a "position index" by the value of IS_DI.
Assumes that FP is already seek'ed to the correct position. Returns
the number of bytes read. */
static inline int
old_bow_pv_read_unsigned_int (unsigned int *i, int *is_di, FILE *fp)
{
bow_pe pe;
int index;
int shift = 0;
int byte_count = 1;
pe.byte = fgetc (fp);
if (pe.bits.is_di)
*is_di = 1;
else
*is_di = 0;
index = pe.bits.index;
while (pe.bits.is_more)
{
shift += 6;
pe.byte = fgetc (fp);
byte_count++;
index |= pe.bits.index << shift;
}
*i = index;
return byte_count;
}
/* Read an unsigned integer into I, and indicate whether it is a
"document index" or a "position index" by the value of IS_DI.
Assumes that FP is already seek'ed to the correct position. Returns
the number of bytes read. */
static inline int
bow_pv_read_unsigned_int (unsigned int *i, int *is_di, FILE *fp)
{
bow_pe pe;
int index;
int shift = 6;
int byte_count = 1;
pe.byte = fgetc (fp);
if (pe.bits.is_di)
*is_di = 1;
else
*is_di = 0;
index = pe.bits.index;
while (pe.bits.is_more)
/* The above test relies on pe.bits.is_more == pe.bits_more.is_more */
{
pe.byte = fgetc (fp);
byte_count++;
index |= pe.bits_more.index << shift;
shift += 7;
}
*i = index;
return byte_count;
}
#define PV_WRITE_SIZE_INT(N) \
(((N) < (1 << (6))) \
? 1 \
: (((N) < (1 << (6+7))) \
? 2 \
: (((N) < (1 << (6+7+7))) \
? 3 \
: (((N) < (1 << (6+7+7+7))) \
? 4 \
: 5))))
static inline int
bow_pv_write_size_di_pi (bow_pv *pv, int di, int pi)
{
int size = 0;
if (pv->write_last_di != di)
size += PV_WRITE_SIZE_INT (di - pv->write_last_di);
size += PV_WRITE_SIZE_INT (pi - pv->write_last_pi);
return size;
}
static inline int
bow_pv_write_size_di_li_pi (bow_pv *pv, int di, int li[], int ln, int pi)
{
int i;
int size = 0;
assert(ln <= BOW_MAX_WORD_LABELS);
if ((pv->write_last_di != di) || (ln != 0))
size += PV_WRITE_SIZE_INT (di - pv->write_last_di);
for (i = 0; i < ln; i++) size += PV_WRITE_SIZE_INT (li[i]);
if(pv->write_last_di == di)
size += PV_WRITE_SIZE_INT (pi - pv->write_last_pi);
else size += PV_WRITE_SIZE_INT(pi + 1);
return size;
}
static inline int
bow_pv_read_size_di_pi (bow_pv *pv, int di, int pi)
{
int size = 0;
if (pv->read_last_di != di)
size += PV_WRITE_SIZE_INT (di - pv->read_last_di);
size += PV_WRITE_SIZE_INT (pi - pv->read_last_pi);
return size;
}
/* Write "document index" DI and "position index" PI to FP. Assumes
that FP is already seek'ed to the correct position, and there is
space there in this segment to write the info. Returns the number
of bytes written. */
static inline int
bow_pv_write_next_di_pi (bow_pv *pv, int di, int pi, FILE *fp)
{
int bytes_written = 0;
assert (di >= pv->write_last_di);
if (di != pv->write_last_di)
{
bytes_written +=
bow_pv_write_unsigned_int (di - pv->write_last_di, 1, fp);
pv->write_last_di = di;
pv->write_last_pi = -1;
}
bytes_written +=
bow_pv_write_unsigned_int (pi - pv->write_last_pi, 0, fp);
pv->write_last_pi = pi;
return bytes_written;
}
/* Write "document index" DI, "list indices" LI (LN of them) and
"position index" PI to FP. Assumes that FP is already seek'ed to
the correct position, and there is space there in this segment to
write the info. Returns the number of bytes written. */
static inline int
bow_pv_write_next_di_li_pi (bow_pv *pv, int di, int li[], int ln, int pi,
FILE *fp)
{
int i, bytes_written = 0;
assert (di >= pv->write_last_di);
assert(ln <= BOW_MAX_WORD_LABELS);
if ((di != pv->write_last_di) || (ln > 0))
{
bytes_written +=
bow_pv_write_unsigned_int (di - pv->write_last_di, 1, fp);
if(di != pv->write_last_di) pv->write_last_pi = -1;
pv->write_last_di = di;
}
for(i = 0; i < ln; i++)
bytes_written += bow_pv_write_unsigned_int(li[i], 1, fp);
bytes_written += bow_pv_write_unsigned_int (pi - pv->write_last_pi, 0, fp);
pv->write_last_pi = pi;
return bytes_written;
}
/* Read "document index" DI and "position index" PI from FP. Assumes
that FP is already seek'ed to the correct position. Returns the
number of bytes read. */
static inline int
bow_pv_read_next_di_pi (bow_pv *pv, int *di, int *pi, FILE *fp)
{
int incr;
int bytes_read = 0;
int is_di;
bytes_read += bow_pv_read_unsigned_int (&incr, &is_di, fp);
if (is_di)
{
pv->read_last_di += incr;
pv->read_last_pi = -1;
bytes_read += bow_pv_read_unsigned_int (&incr, &is_di, fp);
assert (!is_di);
}
pv->read_last_pi += incr;
*di = pv->read_last_di;
*pi = pv->read_last_pi;
return bytes_read;
}
/* Read "document index" DI, "list indices" LI (LN of them) and
"position index" PI from FP. The size of LI is read from LN which
is then set to the actual number of "list indices" returned (thus
if LN is too small you may not get all of them). Assumes that FP is
already seek'ed to the correct position. Returns the number of
bytes read. */
static inline int
bow_pv_read_next_di_li_pi (bow_pv *pv, int *di, int li[], int *ln, int *pi,
FILE *fp)
{
int incr;
int bytes_read = 0;
int is_di;
int li_count = 0;
bytes_read += bow_pv_read_unsigned_int (&incr, &is_di, fp);
if (is_di)
{
pv->read_last_di += incr;
if(incr != 0) pv->read_last_pi = -1;
bytes_read += bow_pv_read_unsigned_int (&incr, &is_di, fp);
while (is_di) {
if(li_count < *ln) li[li_count++] = incr;
bytes_read += bow_pv_read_unsigned_int (&incr, &is_di, fp);
}
}
pv->read_last_pi += incr;
*di = pv->read_last_di;
*pi = pv->read_last_pi;
*ln = li_count;
return bytes_read;
}
/* Add "document index" DI and "position index" PI to PV by writing
the correct information to FP. Does not assume that FP is already
seek'ed to the correct position. Will add a new PV segment on disk
if necessary. Assumes that both DI and PI are greater than or
equal to the last DI and PI written, respectively. */
void
bow_pv_add_di_pi (bow_pv *pv, int di, int pi, FILE *fp)
{
int byte_count;
/* Make sure there is definitely enough room in this segment to
write another DI and PI. If not, start a new segment, write the
"tailer" to point to its seek position, write the "header" of the
new segment, and leave seek FP to a position in the new segment
ready to write the DI and PI. */
if (pv->write_segment_bytes_remaining < bow_pv_max_sizeof_di_pi)
{
int seek_tailer;
int seek_new_segment;
/* Calculate the seek position at which we will write the
"tailer" of the current segment. The "tailer" will contain
the seek position of the new segment. */
seek_tailer = pv->write_seek_end + pv->write_segment_bytes_remaining;
/* Go to the very end of the file, and remember this seek position.
This is the beginning of the new segment. */
fseek (fp, 0, SEEK_END);
seek_new_segment = ftell (fp);
/* Calculate the size of the new segment. */
pv->write_segment_bytes *= 2;
pv->write_segment_bytes_remaining = pv->write_segment_bytes;
/* Write the "header" of the new segment. The "header" contains the
number of bytes in the new segment. */
bow_fwrite_int (pv->write_segment_bytes_remaining, fp);
/* Remember this seek position after the "header" of the new
segment. It is here that we will start writing new DI's and
PI's. */
pv->write_seek_end = ftell (fp);
/* Seek forward to the end of this segment and write a (temporary
value for) the "tailer". This will reserve all the intervening
bytes for this segment, and future calls to seek to the end of
the file (for other PV's) will go after the end of this
segment. */
fseek (fp, pv->write_segment_bytes_remaining, SEEK_CUR);
bow_fwrite_int (-1, fp);
/* Go back to the "tailer" of the last segment, and write the
position of the new segment there. */
fseek (fp, seek_tailer, SEEK_SET);
#if PV_DEBUG
/* Check to make sure it is the originally-written temporary value */
bow_fread_int (&byte_count, fp);
assert (byte_count == -1);
fseek (fp, seek_tailer, SEEK_SET);
#endif
bow_fwrite_int (seek_new_segment, fp);
}
/* Seek to the correct position, write the DI and PI, and decrement
our count of the number of bytes remaining in this segment, and
update the seek position for writing the next DI and PI. */
fseek (fp, pv->write_seek_end, SEEK_SET);
byte_count = bow_pv_write_next_di_pi (pv, di, pi, fp);
pv->write_segment_bytes_remaining -= byte_count;
pv->write_seek_end += byte_count;
/* Increment the PV's count of the total number of word occurrences */
pv->count++;
assert (pv->write_segment_bytes_remaining >= 0);
}
/* Add "document index" DI, "label indices" LI (LN of them) and
"position index" PI to PV by writing the correct information to FP.
Does not assume that FP is already seek'ed to the correct position.
Will add a new PV segment on disk if necessary. Assumes that both
DI and PI are greater than or equal to the last DI and PI written,
respectively. */
void
bow_pv_add_di_li_pi (bow_pv *pv, int di, int li[], int ln, int pi,
FILE *fp)
{
int byte_count;
int size = bow_pv_write_size_di_li_pi(pv, di, li, ln, pi);
assert(ln <= BOW_MAX_WORD_LABELS);
assert(di >= pv->write_last_di);
assert(pi < pv->write_last_pi ? di != pv->write_last_di : 1 );
/* Make sure there is definitely enough room in this segment to
write this DI, PI and LIs. If not, write an end-of-segment
marker (a zero), start a new segment, write the "tailer" to point
to its seek position, write the "header" of the new segment, and
leave seek FP to a position in the new segment ready to write the
DI, PI and LIs. "Enough room" is the size of the current record
plus one byte for the end-of-segment marker */
if (pv->write_segment_bytes_remaining < size + 1)
{
int seek_tailer;
int seek_new_segment;
assert(pv->write_segment_bytes_remaining > 0);
/* first write an end-of-segment marker */
fseek (fp, pv->write_seek_end, SEEK_SET);
fputc (0, fp);
/* Calculate the seek position at which we will write the
"tailer" of the current segment. The "tailer" will contain
the seek position of the new segment. */
seek_tailer = pv->write_seek_end + pv->write_segment_bytes_remaining;
/* Go to the very end of the file, and remember this seek position.
This is the beginning of the new segment. */
fseek (fp, 0, SEEK_END);
seek_new_segment = ftell (fp);
/* Calculate the size of the new segment. */
do {
pv->write_segment_bytes *= 2;
}
while (pv->write_segment_bytes < size);
pv->write_segment_bytes_remaining = pv->write_segment_bytes;
/* Write the "header" of the new segment. The "header" contains the
number of bytes in the new segment. */
bow_fwrite_int (pv->write_segment_bytes_remaining, fp);
/* Remember this seek position after the "header" of the new
segment. It is here that we will start writing new DI's and
PI's. */
pv->write_seek_end = ftell (fp);
/* Seek forward to the end of this segment and write a (temporary
value for) the "tailer". This will reserve all the intervening
bytes for this segment, and future calls to seek to the end of
the file (for other PV's) will go after the end of this
segment. */
fseek (fp, pv->write_segment_bytes_remaining, SEEK_CUR);
bow_fwrite_int (-1, fp);
/* Go back to the "tailer" of the last segment, and write the
position of the new segment there. */
fseek (fp, seek_tailer, SEEK_SET);
#if PV_DEBUG
/* Check to make sure it is the originally-written temporary value */
bow_fread_int (&byte_count, fp);
assert (byte_count == -1);
fseek (fp, seek_tailer, SEEK_SET);
#endif
bow_fwrite_int (seek_new_segment, fp);
}
/* Seek to the correct position, write the DI and PI, and decrement
our count of the number of bytes remaining in this segment, and
update the seek position for writing the next DI and PI. */
fseek (fp, pv->write_seek_end, SEEK_SET);
byte_count = bow_pv_write_next_di_li_pi (pv, di, li, ln, pi, fp);
pv->write_segment_bytes_remaining -= byte_count;
pv->write_seek_end += byte_count;
/* Increment the PV's count of the total number of word occurrences */
pv->count++;
assert (pv->write_segment_bytes_remaining >= 0);
}
/* Read the next "document index" DI and "position index" PI. Does
not assume that FP is already seek'ed to the correct position.
Will jump to a new PV segment on disk if necessary. */
void
bow_pv_next_di_pi (bow_pv *pv, int *di, int *pi, FILE *fp)
{
int byte_count;
/* If we are about to read from the same location as we would write,
then we are at the end of the PV. Return special DI and PI
values indicate that we are at the end. */
if (pv->read_seek_end == pv->write_seek_end)
{
return_end_of_pv:
*di = *pi = -1;
return;
}
/* If the special flag was set by bow_pv_unnext(), then return the
same values returned last time without reading the next entry,
and unset the flag. */
if (pv->read_seek_end < 0)
{
*di = pv->read_last_di;
*pi = pv->read_last_pi;
pv->read_seek_end = -pv->read_seek_end;
assert (pv->read_seek_end > 0);
return;
}
/* Make sure that there was definitely enough room in this segment
to write another DI and PI. If not, then it was written in the
next segment, so go there and get set up for reading from it. */
if (pv->read_segment_bytes_remaining < bow_pv_max_sizeof_di_pi)
{
int seek_new_segment;
/* Go to the "tailer" of this segment, and read the seek
position of the next segment. */
fseek (fp, pv->read_seek_end + pv->read_segment_bytes_remaining,
SEEK_SET);
bow_fread_int (&seek_new_segment, fp);
fseek (fp, seek_new_segment, SEEK_SET);
/* Read the number of bytes in this segment, and remember it. */
bow_fread_int (&(pv->read_segment_bytes_remaining), fp);
/* Remember the new position from which to read the next DI and PI */
pv->read_seek_end = ftell (fp);
/* If this segment has not yet been written to, we are at end of PV */
if (pv->read_seek_end == pv->write_seek_end)
goto return_end_of_pv;
}
/* Seek to the correct position, read the DI and PI, decrement our
count of the number of bytes remaining in this segment, and
update the seek position for reading the next DI and PI. */
fseek (fp, pv->read_seek_end, SEEK_SET);
byte_count =
bow_pv_read_next_di_pi (pv, di, pi, fp);
pv->read_segment_bytes_remaining -= byte_count;
pv->read_seek_end += byte_count;
assert (pv->read_segment_bytes_remaining >= 0);
}
/* Read the next "document index" DI, "list indices" LI (LN of them)
and "position index" PI. Does not assume that FP is already
seek'ed to the correct position. Will jump to a new PV segment on
disk if necessary. LN should contain the size of LI and will be set
to the number of labels put in to the array (thus if LN is too
small you may not get all of them).*/
void
bow_pv_next_di_li_pi (bow_pv *pv, int *di, int li[], int* ln, int *pi,
FILE *fp)
{
int byte_count;
assert(*ln <= BOW_MAX_WORD_LABELS);
/* If we are about to read from the same location as we would write,
then we are at the end of the PV. Return special DI and PI
values indicate that we are at the end. */
if (pv->read_seek_end == pv->write_seek_end)
{
return_end_of_pv:
*di = *pi = -1;
return;
}
/* If the special flag was set by bow_pv_unnext(), then return the
same values returned last time without reading the next entry,
and unset the flag. We ignore the labels (no good way to keep
track of them in this case).*/
if (pv->read_seek_end < 0)
{
*di = pv->read_last_di;
*pi = pv->read_last_pi;
pv->read_seek_end = -pv->read_seek_end;
assert (pv->read_seek_end > 0);
return;
}
/* Make sure that there was definitely enough room in this segment
to write another DI and PI. If not, then a zero was written and
the record was written in the next segment, so go there and get
set up for reading from it. */
fseek (fp, pv->read_seek_end, SEEK_SET);
if (fgetc (fp) == 0)
{
int seek_new_segment;
/* Go to the "tailer" of this segment, and read the seek
position of the next segment. */
fseek (fp, pv->read_seek_end + pv->read_segment_bytes_remaining,
SEEK_SET);
bow_fread_int (&seek_new_segment, fp);
fseek (fp, seek_new_segment, SEEK_SET);
/* Read the number of bytes in this segment, and remember it. */
bow_fread_int (&(pv->read_segment_bytes_remaining), fp);
/* Remember the new position from which to read the next DI and PI */
pv->read_seek_end = ftell (fp);
/* If this segment has not yet been written to, we are at end of PV */
if (pv->read_seek_end == pv->write_seek_end)
goto return_end_of_pv;
}
/* Seek to the correct position, read the DI and PI, decrement our
count of the number of bytes remaining in this segment, and
update the seek position for reading the next DI and PI. */
fseek (fp, pv->read_seek_end, SEEK_SET);
byte_count = bow_pv_read_next_di_li_pi (pv, di, li, ln, pi, fp);
pv->read_segment_bytes_remaining -= byte_count;
pv->read_seek_end += byte_count;
assert (pv->read_segment_bytes_remaining >= 0);
}
/* Undo the effect of the last call to bow_pv_next_di_li_pi(). That
is, make the next call to bow_pv_next_di_li_pi() return the same DI
and PI as the last call did. This function may not be called
multiple times in a row without calling bow_pv_next_di_li_pi() in
between. Furthermore note that the subsequent call to
bow_pv_next_di_li_pi() will not have labels set correctly. */
void
bow_pv_unnext (bow_pv *pv)
{
/* Make sure that this function wasn't call two times in a row. */
assert (pv->read_seek_end > 0);
pv->read_seek_end = -pv->read_seek_end;
}
/* Rewind the read position to the beginning of the PV */
void
bow_pv_rewind (bow_pv *pv, FILE *fp)
{
/* If PV is already rewound, just return immediately */
if (pv->read_last_di == -1 && pv->read_last_pi == -1)
return;
fseek (fp, pv->seek_start, SEEK_SET);
bow_fread_int (&(pv->read_segment_bytes_remaining), fp);
pv->read_seek_end = ftell (fp);
pv->read_last_di = -1;
pv->read_last_pi = -1;
}
/* Write the in-memory portion of PV to FP */
void
bow_pv_write (bow_pv *pv, FILE *fp)
{
bow_fwrite_int (pv->count, fp);
bow_fwrite_int (pv->seek_start, fp);
bow_fwrite_int (pv->read_seek_end, fp);
bow_fwrite_int (pv->read_last_di, fp);
bow_fwrite_int (pv->read_last_pi, fp);
bow_fwrite_int (pv->read_segment_bytes_remaining, fp);
bow_fwrite_int (pv->write_seek_end, fp);
bow_fwrite_int (pv->write_last_di, fp);
bow_fwrite_int (pv->write_last_pi, fp);
bow_fwrite_int (pv->write_segment_bytes, fp);
bow_fwrite_int (pv->write_segment_bytes_remaining, fp);
}
/* Read the in-memory portion of PV from FP */
void
bow_pv_read (bow_pv *pv, FILE *fp)
{
bow_fread_int (&pv->count, fp);
bow_fread_int (&pv->seek_start, fp);
bow_fread_int (&pv->read_seek_end, fp);
bow_fread_int (&pv->read_last_di, fp);
bow_fread_int (&pv->read_last_pi, fp);
bow_fread_int (&pv->read_segment_bytes_remaining, fp);
bow_fread_int (&pv->write_seek_end, fp);
bow_fread_int (&pv->write_last_di, fp);
bow_fread_int (&pv->write_last_pi, fp);
bow_fread_int (&pv->write_segment_bytes, fp);
bow_fread_int (&pv->write_segment_bytes_remaining, fp);
}
|