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 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
|
// Graph of strongly connected components -*- c++ -*-
#ifdef __GNUC__
# pragma implementation
# ifdef __sgi
# define _XOPEN_SOURCE 1
# define _XOPEN_SOURCE_EXTENDED 1
# endif // __sgi
#endif // __GNUC__
#include "ComponentGraph.h"
#include "Graph.h"
#include "BitVector.h"
#include <list>
#include <stdlib.h>
#include <errno.h>
#include <set>
#if defined __digital__
# define fileno(f) ((f)->_file)
#endif // __digital__
#if defined __CYGWIN__
# define fileno(f) __sfileno(f)
#endif // __CYGWIN__
/** @file ComponentGraph.C
* Graph of strongly connected components
*/
/* Copyright 2000-2002,2005 Marko Mkel (msmakela@tcs.hut.fi).
This file is part of MARIA, a reachability analyzer and model checker
for high-level Petri nets.
MARIA is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
MARIA 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
General Public License for more details.
The GNU General Public License is often shipped with GNU software, and
is generally kept in a file called COPYING or LICENSE. If you do not
have a copy of the license, write to the Free Software Foundation,
59 Temple Place, Suite 330, Boston, MA 02111 USA. */
/** Tarjan state */
struct tarjan
{
/** reachability graph state */
card_t state;
/** number of unprocessed successors */
unsigned numSucc;
};
/** Stack for Tarjan's algorithm */
typedef std::list<struct tarjan> TarjanStack;
/** Component directory entry */
struct compdirent
{
/** Number of states in the component */
card_t numStates;
/** Offset to the state numbers of the component (or FPOS_NONE) */
Graph::fpos_t statePos;
/** Offset to the successor arcs of the component (or FPOS_NONE) */
Graph::fpos_t succPos;
/** Offset to the first predecessor arc record (or FPOS_NONE) */
Graph::fpos_t predPos;
};
/** Predecessor arc entry */
struct predent
{
/** Offset to the next predecessor position (or FPOS_NONE) */
Graph::fpos_t nextPos;
/** Number of the predecessor component */
card_t comp;
};
#ifdef NO_MMAP
/** an empty file structure */
# define NULL_F { 0, 0, 0 }
/** pretend that mmapped files are always open */
# define isOpen(file) true
#else // NO_MMAP
# ifdef USE_MMAP
/** an empty file structure */
# define NULL_F { -1, 0, 0, 0 }
# else // USE_MMAP
/** an empty file structure */
# define NULL_F 0
# endif // USE_MMAP
/** Check if a file is open
* @param file the file
* @return true if the file has been opened
*/
static bool
isOpen (const file_t& file)
{
# ifdef USE_MMAP
return file.fd >= 0;
# else // USE_MMAP
return bool (file);
# endif // USE_MMAP
}
#endif // NO_MMAP
/** an empty file pointer */
static const file_t nofile = NULL_F;
#ifdef USE_MMAP
# ifndef NO_MMAP
/** Open a temporary file
* @param size desired length of the file in bytes
* @return an opened file, or something on which isOpen() does not hold
*/
static file_t
tempFile (size_t size)
{
FILE* file = tmpfile ();
if (!file) {
perror ("tmpfile");
return nofile;
}
file_t f;
f.fd = dup (fileno (file));
fclose (file);
if (f.fd < 0) {
perror ("tempFile: dup");
return nofile;
}
f.len = 0;
f.alloc = size;
if (ftruncate (f.fd, f.alloc)) {
perror ("tempFile: ftruncate");
close (f.fd);
return nofile;
}
f.addr =
# ifdef __sun
(caddr_t)
# endif // __sun
mmap (0, f.alloc, PROT_READ | PROT_WRITE, MAP_SHARED, f.fd, 0);
if (f.addr == reinterpret_cast<void*>(MAP_FAILED)) {
close (f.fd);
perror ("tempFile: mmap");
return nofile;
}
return f;
}
# endif // !NO_MMAP
/** Extend a file
* @param file the file
*/
static void
extend (file_t& file)
{
assert (isOpen (file) && file.addr);
if (file.len < file.alloc)
return;
# ifndef NO_MMAP
if (file.addr)
munmap (file.addr, file.alloc);
# endif // !NO_MMAP
while (file.alloc < file.len) {
if (!(file.alloc *= 2)) {
fputs ("extend: file size overflow\n", stderr);
abort ();
}
}
# ifdef NO_MMAP
if (!(file.addr = realloc (file.addr, file.alloc))) {
perror ("extend: realloc");
abort ();
}
# else // NO_MMAP
if (ftruncate (file.fd, file.alloc)) {
perror ("extend: ftruncate");
abort ();
}
file.addr =
# ifdef __sun
(caddr_t)
# endif // __sun
mmap (0, file.alloc, PROT_READ | PROT_WRITE, MAP_SHARED, file.fd, 0);
if (file.addr == reinterpret_cast<void*>(MAP_FAILED)) {
perror ("extend: mmap");
abort ();
}
# endif // NO_MMAP
}
#endif // USE_MMAP
/** Close a temporary file
* @param f the file to be closed
*/
static void
closeFile (file_t& f)
{
#ifdef USE_MMAP
# ifdef NO_MMAP
if (f.addr)
free (f.addr);
# else // NO_MMAP
munmap (f.addr, f.alloc);
close (f.fd);
# endif // NO_MMAP
#else // USE_MMAP
fclose (f);
#endif // USE_MMAP
}
ComponentGraph::ComponentGraph (const class Graph& graph) :
myGraph (graph),
myNumComponents (0), myNumTerminals (0),
myStateComponents (nofile),
myComponentDirectory (nofile), myComponentStates (0),
mySuccComponents (0), myPredComponents (0)
{
}
ComponentGraph::~ComponentGraph ()
{
if (isOpen (myStateComponents)) {
assert (isOpen (myComponentDirectory));
assert (myComponentStates && mySuccComponents && myPredComponents);
closeFile (myStateComponents);
closeFile (myComponentDirectory);
fclose (myComponentStates);
fclose (mySuccComponents);
fclose (myPredComponents);
}
}
inline bool
ComponentGraph::openFiles ()
{
assert (!myNumComponents);
assert (!isOpen (myStateComponents));
assert (!isOpen (myComponentDirectory));
assert (!myComponentStates);
assert (!mySuccComponents);
assert (!myPredComponents);
#ifdef USE_MMAP
if (!isOpen (myStateComponents =
tempFile (myGraph.getNumStates () * sizeof (card_t))));
else if (!isOpen (myComponentDirectory = tempFile (getpagesize ())))
closeFile (myStateComponents), myStateComponents = nofile;
#else // USE_MMAP
if (!(myStateComponents = tmpfile ()))
perror ("tmpfile");
else if (!(myComponentDirectory = tmpfile ()))
perror ("tmpfile"),
closeFile (myStateComponents), myStateComponents = nofile;
#endif // USE_MMAP
else if (!(myComponentStates = tmpfile ()))
perror ("tmpfile"),
closeFile (myStateComponents), closeFile (myComponentDirectory),
myStateComponents = myComponentDirectory = nofile;
else if (!(mySuccComponents = tmpfile ()))
perror ("tmpfile"),
closeFile (myStateComponents), closeFile (myComponentDirectory),
fclose (myComponentStates),
myStateComponents = myComponentDirectory = nofile,
myComponentStates = 0;
else if (!(myPredComponents = tmpfile ()))
perror ("tmpfile"),
closeFile (myStateComponents), closeFile (myComponentDirectory),
fclose (myComponentStates), fclose (mySuccComponents),
myStateComponents = myComponentDirectory = nofile,
myComponentStates = mySuccComponents = 0;
else
return true;
return false;
}
inline void
ComponentGraph::setComponent (card_t state, card_t comp)
{
#ifdef USE_MMAP
assert (size_t (myStateComponents.alloc) >= (state + 1) * sizeof state);
reinterpret_cast<card_t*>(myStateComponents.addr)[state] = ++comp;
#else // USE_MMAP
fseek (myStateComponents, state * sizeof state, SEEK_SET);
fwrite (&++comp, sizeof comp, 1, myStateComponents);
#endif // USE_MMAP
}
/** Flag: has the analysis been interrupted? */
extern volatile bool interrupted;
inline void
ComponentGraph::computeArcs ()
{
assert (myNumComponents);
assert (!ftell (mySuccComponents) && !ftell (myPredComponents));
/** successor and predecessor components */
std::set<card_t> succComp, predComp;
for (unsigned comp = 0; comp < myNumComponents; comp++) {
card_t* states = getStates (comp);
assert (states && *states);
for (; *states; (*states)--) {
card_t s;
for (Graph::fpos_t pos = myGraph.getPredecessors (states[*states]);
pos != FPOS_NONE;) {
s = getComponent (myGraph.getPredecessor (pos));
if (s != CARD_T_MAX && s != comp)
predComp.insert (s);
}
if (card_t* succ = myGraph.getSuccessors (states[*states])) {
for (assert (*succ > 0); *succ; (*succ)--) {
s = getComponent (succ[*succ]);
if (s != CARD_T_MAX && s != comp)
succComp.insert (s);
}
delete[] succ;
}
}
delete[] states;
if (succComp.empty ())
myNumTerminals++;
#ifdef USE_MMAP
struct compdirent& ent =
reinterpret_cast<struct compdirent*>(myComponentDirectory.addr)[comp];
assert (size_t (myComponentDirectory.len) >= (comp + 1) * sizeof ent);
#else // USE_MMAP
struct compdirent ent;
fseek (myComponentDirectory, comp * sizeof ent, SEEK_SET);
if (1 != fread (&ent, sizeof ent, 1, myComponentDirectory))
return;
#endif // USE_MMAP
assert (ent.numStates &&
ent.statePos != FPOS_NONE &&
ent.succPos == FPOS_NONE &&
ent.predPos == FPOS_NONE);
std::set<card_t>::const_iterator i;
ent.succPos = ftell (mySuccComponents);
for (i = succComp.begin (); i != succComp.end (); i++)
fwrite (&*i, sizeof *i, 1, mySuccComponents);
ent.predPos = ftell (myPredComponents);
for (i = predComp.begin (); i != predComp.end (); i++)
fwrite (&*i, sizeof *i, 1, myPredComponents);
#ifndef USE_MMAP
fseek (myComponentDirectory, -sizeof ent, SEEK_CUR);
fwrite (&ent, sizeof ent, 1, myComponentDirectory);
#endif // !USE_MMAP
succComp.clear ();
predComp.clear ();
}
}
#ifndef USE_MMAP
/** Write a strongly connected component
* @param compDirectory stronly connected component directory
* @param compStates file containing the component state numbers
* @param stateComps map from state numbers to component numbers
* @param visited states that have been visited
* @param processed states that have been processed
* @param st the search stack
* @param stsize size of the search stack
* @param root number of the root state of the component
* @param num number of the component
* @return new size of the search stack
*/
inline unsigned
writeComponent (FILE* compDirectory,
FILE* compStates,
FILE* stateComps,
class BitVector& visited,
class BitVector& processed,
const card_t* st,
unsigned stsize,
card_t root,
unsigned num)
{
struct compdirent c = {
0, // number of states
ftell (compStates), // offset to state numbers
FPOS_NONE, // offset to successor component numbers
FPOS_NONE // offset to predecessor component numbers
};
assert (stsize > 0);
assert (card_t (ftell (compDirectory)) == num * sizeof c);
assert (!processed[root] || !visited[root]);
num++;
unsigned i = stsize;
while (i--) {
card_t state = st[i];
assert (!processed[state] || !visited[state]);
fseek (stateComps, state * sizeof state, SEEK_SET);
fwrite (&num, sizeof num, 1, stateComps);
processed.assign (state, true);
visited.assign (state, true);
c.numStates++;
if (state == root) break;
assert (i > 0);
}
fwrite (st + i, sizeof *st, stsize - i, compStates);
fwrite (&c, sizeof c, 1, compDirectory);
return i;
}
#endif // !USE_MMAP
/** Write a strongly connected component
* @param compDirectory stronly connected component directory
* @param compStates file containing the component state numbers
* @param stateComps memory-based map from state to component numbers
* @param visited states that have been visited
* @param processed states that have been processed
* @param st the search stack
* @param stsize size of the search stack
* @param root number of the root state of the component
* @param num number of the component
* @return new size of the search stack
*/
inline unsigned
writeComponent (file_t& compDirectory,
FILE* compStates,
card_t* stateComps,
class BitVector& visited,
class BitVector& processed,
const card_t* st,
unsigned stsize,
card_t root,
unsigned num)
{
#ifdef USE_MMAP
assert (card_t (compDirectory.len) == num * sizeof (struct compdirent));
compDirectory.len += sizeof (struct compdirent);
extend (compDirectory);
struct compdirent& c =
reinterpret_cast<struct compdirent*>(compDirectory.addr)[num];
#else // USE_MMAP
struct compdirent c;
assert (card_t (ftell (compDirectory)) == num * sizeof c);
#endif // USE_MMAP
c.numStates = 0;
c.statePos = ftell (compStates);
c.succPos = c.predPos = FPOS_NONE;
assert (stsize > 0);
assert (!processed[root] || !visited[root]);
num++;
unsigned i = stsize;
while (i--) {
card_t state = st[i];
assert (!processed[state] || !visited[state]);
stateComps[state] = num;
processed.assign (state, true);
visited.assign (state, true);
c.numStates++;
if (state == root) break;
assert (i > 0);
}
fwrite (st + i, sizeof *st, stsize - i, compStates);
#ifndef USE_MMAP
fwrite (&c, sizeof c, 1, compDirectory);
#endif // !USE_MMAP
return i;
}
unsigned
ComponentGraph::compute (card_t state,
const class Expression* cond)
{
assert (!myNumComponents);
if (!myGraph.eval (state, cond) || !openFiles ())
return 0;
/** Current and allocated length of the component stack */
unsigned cstsize = 0, cstalloc = 128;
/** Component stack */
card_t* cst = new card_t[cstalloc];
TarjanStack st;
/** Visited states */
class BitVector visited (myGraph.getNumStates ());
/** States belonging to a component */
class BitVector processed (myGraph.getNumStates ());
// Combinations for visited[state] and processed[state]:
// initially 0 0
// visited in the search 1 0
// visited; depth adjusted 0 1
// processed component 1 1
#ifdef USE_MMAP
/** Search depth numbers or component numbers */
card_t* const depths = reinterpret_cast<card_t*>(myStateComponents.addr);
#else // USE_MMAP
/** Search depth numbers (if insufficient memory, use get/setComponent) */
card_t* depths =
reinterpret_cast<card_t*>(calloc (myGraph.getNumStates (),
sizeof *depths));
#endif // USE_MMAP
/** Search depth */
card_t depth = 1;
/** Successor state numbers */
card_t* succ;
while (!interrupted) {
// compute a strongly connected component
while (!interrupted) {
assert (!processed[state] || !visited[state]);
if (processed[state] || visited.tset (state))
break;
// push the state on the component stack
if (cstsize == cstalloc) {
if (card_t* ncst = new card_t[cstalloc <<= 1]) {
memcpy (ncst, cst, cstsize * sizeof *cst);
delete[] cst; cst = ncst;
}
else {
delete[] cst;
#ifndef USE_MMAP
free (depths);
#endif // !USE_MMAP
interrupted = true;
return myNumComponents;
}
}
cst[cstsize++] = state;
// determine the successors of the state
succ = myGraph.getSuccessors (state);
#ifndef USE_MMAP
if (depths)
#endif // !USE_MMAP
depths[state] = ++depth;
#ifndef USE_MMAP
else
setComponent (state, depth++);
#endif // !USE_MMAP
struct tarjan t = { state, succ ? *succ : 0 };
for (; t.numSucc; t.numSucc--) {
card_t s = succ[t.numSucc];
if (!(processed[s] && visited[s]) && myGraph.eval (s, cond)) {
state = s;
break;
}
}
delete[] succ;
st.push_front (t);
}
// backtrack
while (!interrupted) {
if (st.empty ()) {
#ifndef USE_MMAP
if (depths) {
#endif // !USE_MMAP
if (cstsize &&
::writeComponent (myComponentDirectory,
myComponentStates,
depths,
visited, processed,
cst, cstsize, *cst,
myNumComponents++))
assert (false);
#ifndef USE_MMAP
fseek (myStateComponents, 0, SEEK_SET);
fwrite (depths, sizeof depths, myGraph.getNumStates (),
myStateComponents);
free (depths);
}
else
if (cstsize &&
::writeComponent (myComponentDirectory,
myComponentStates,
myStateComponents,
visited, processed,
cst, cstsize, *cst,
myNumComponents++))
assert (false);
#endif // !USE_MMAP
delete[] cst;
computeArcs ();
return myNumComponents;
}
/** Minimum search depth in the state */
const card_t minDepth = processed[state] && visited[state]
? CARD_T_MAX
#ifdef USE_MMAP
: depths[state] - 1;
#else // USE_MMAP
: (depths ? (depths[state] - 1) : getComponent (state));
#endif // USE_MMAP
struct tarjan& t = *st.begin ();
state = t.state;
#ifndef USE_MMAP
if (depths) {
#endif // !USE_MMAP
if ((depths[state] - 1) > minDepth) {
depths[state] = minDepth + 1;
if (processed.tset (state))
assert (!visited[state]);
else
assert (visited[state]), visited.assign (state, false);
}
#ifndef USE_MMAP
}
else {
if (getComponent (state) > minDepth) {
setComponent (state, minDepth);
if (processed.tset (state))
assert (!visited[state]);
else
assert (visited[state]), visited.assign (state, false);
}
}
#endif // !USE_MMAP
if (t.numSucc-- > 1) {
succ = myGraph.getSuccessors (state);
assert (succ && *succ > t.numSucc);
for (; t.numSucc; t.numSucc--) {
card_t s = succ[t.numSucc];
if (!(processed[s] && visited[s]) && myGraph.eval (s, cond)) {
state = s;
break;
}
}
delete[] succ;
if (t.numSucc)
break;
}
st.pop_front ();
if (processed[state])
continue;
#ifdef USE_MMAP
cstsize = ::writeComponent (myComponentDirectory,
myComponentStates,
depths,
visited, processed,
cst, cstsize, state,
myNumComponents++);
#else // USE_MMAP
cstsize = depths
? ::writeComponent (myComponentDirectory,
myComponentStates,
depths,
visited, processed,
cst, cstsize, state,
myNumComponents++)
: ::writeComponent (myComponentDirectory,
myComponentStates,
myStateComponents,
visited, processed,
cst, cstsize, state,
myNumComponents++);
#endif // USE_MMAP
}
}
delete[] cst;
#ifndef USE_MMAP
free (depths);
#endif // !USE_MMAP
return myNumComponents;
}
card_t
ComponentGraph::getComponent (card_t state) const
{
#ifdef USE_MMAP
assert (size_t (myStateComponents.alloc) >= (state + 1) * sizeof state);
return reinterpret_cast<card_t*>(myStateComponents.addr)[state] - 1;
#else // USE_MMAP
fseek (myStateComponents, state * sizeof state, SEEK_SET);
return 1 == fread (&state, sizeof state, 1, myStateComponents)
? --state
: CARD_T_MAX;
#endif // USE_MMAP
}
card_t*
ComponentGraph::getStates (card_t comp) const
{
assert (comp < myNumComponents);
#ifdef USE_MMAP
struct compdirent& c =
reinterpret_cast<struct compdirent*>(myComponentDirectory.addr)[comp];
if (size_t (myComponentDirectory.len) < (comp + 1) * sizeof c ||
c.statePos == FPOS_NONE)
return 0;
#else // USE_MMAP
struct compdirent c;
fseek (myComponentDirectory, comp * sizeof c, SEEK_SET);
if (1 != fread (&c, sizeof c, 1, myComponentDirectory) ||
c.statePos == FPOS_NONE)
return 0;
#endif // USE_MMAP
assert (c.numStates && c.numStates <= myGraph.getNumStates ());
card_t* states = new card_t[c.numStates + 1];
fseek (myComponentStates, c.statePos, SEEK_SET);
if (c.numStates !=
fread (states + 1, sizeof *states, c.numStates, myComponentStates)) {
delete[] states;
return 0;
}
*states = c.numStates;
return states;
}
card_t
ComponentGraph::getNumSucc (card_t comp) const
{
assert (comp < myNumComponents);
#ifdef USE_MMAP
const struct compdirent* c =
reinterpret_cast<struct compdirent*>(myComponentDirectory.addr) + comp;
if (size_t (myComponentDirectory.len) < (comp + 1) * sizeof *c)
return 0;
if (c[0].succPos == FPOS_NONE)
return CARD_T_MAX;
if (size_t (myComponentDirectory.len) < (comp + 2) * sizeof *c) {
fseek (mySuccComponents, 0, SEEK_END);
return (ftell (mySuccComponents) - c[0].succPos) / sizeof (card_t);
}
#else // USE_MMAP
struct compdirent c[2];
fseek (myComponentDirectory, comp * sizeof *c, SEEK_SET);
switch (fread (c, sizeof *c, 2, myComponentDirectory)) {
case 0:
return 0;
case 1:
fseek (mySuccComponents, 0, SEEK_END);
c[1].succPos = ftell (mySuccComponents);
// fall through
case 2:
if (c[0].succPos == FPOS_NONE)
return CARD_T_MAX;
}
#endif // USE_MMAP
assert (c[0].succPos <= c[1].succPos);
return (c[1].succPos - c[0].succPos) / sizeof (card_t);
}
card_t
ComponentGraph::getNumPred (card_t comp) const
{
assert (comp < myNumComponents);
#ifdef USE_MMAP
const struct compdirent* c =
reinterpret_cast<struct compdirent*>(myComponentDirectory.addr) + comp;
if (size_t (myComponentDirectory.len) < (comp + 1) * sizeof *c)
return 0;
if (c[0].predPos == FPOS_NONE)
return CARD_T_MAX;
if (size_t (myComponentDirectory.len) < (comp + 2) * sizeof *c) {
fseek (myPredComponents, 0, SEEK_END);
return (ftell (myPredComponents) - c[0].predPos) / sizeof (card_t);
}
#else // USE_MMAP
struct compdirent c[2];
fseek (myComponentDirectory, comp * sizeof *c, SEEK_SET);
switch (fread (c, sizeof *c, 2, myComponentDirectory)) {
case 0:
return 0;
case 1:
fseek (myPredComponents, 0, SEEK_END);
c[1].predPos = ftell (myPredComponents);
// fall through
case 2:
if (c[0].predPos == FPOS_NONE)
return CARD_T_MAX;
}
#endif // USE_MMAP
assert (c[0].predPos <= c[1].predPos);
return (c[1].predPos - c[0].predPos) / sizeof (card_t);
}
card_t*
ComponentGraph::getSucc (card_t comp) const
{
assert (comp < myNumComponents);
Graph::fpos_t pos;
#ifdef USE_MMAP
const struct compdirent* c =
reinterpret_cast<struct compdirent*>(myComponentDirectory.addr) + comp;
if (size_t (myComponentDirectory.len) < (comp + 1) * sizeof *c ||
c[0].succPos == FPOS_NONE)
return 0;
if (size_t (myComponentDirectory.len) < (comp + 2) * sizeof *c) {
fseek (mySuccComponents, 0, SEEK_END);
pos = ftell (mySuccComponents);
}
else
pos = c[1].succPos;
#else // USE_MMAP
struct compdirent c[2];
fseek (myComponentDirectory, comp * sizeof *c, SEEK_SET);
switch (fread (c, sizeof *c, 2, myComponentDirectory)) {
case 0:
return 0;
case 1:
fseek (mySuccComponents, 0, SEEK_END);
c[1].succPos = ftell (mySuccComponents);
// fall through
case 2:
if (c[0].succPos == FPOS_NONE)
return 0;
}
pos = c[1].succPos;
#endif // USE_MMAP
assert (c[0].succPos <= pos);
if (card_t numSucc = (pos - c[0].succPos) / sizeof (card_t)) {
card_t* comps = new card_t[numSucc + 1];
fseek (mySuccComponents, c[0].succPos, SEEK_SET);
if (numSucc != fread (comps + 1, sizeof *comps,
numSucc, mySuccComponents)) {
delete[] comps;
return 0;
}
*comps = numSucc;
return comps;
}
return 0;
}
card_t*
ComponentGraph::getPred (card_t comp) const
{
assert (comp < myNumComponents);
Graph::fpos_t pos;
#ifdef USE_MMAP
const struct compdirent* c =
reinterpret_cast<struct compdirent*>(myComponentDirectory.addr) + comp;
if (size_t (myComponentDirectory.len) < (comp + 1) * sizeof *c ||
c[0].predPos == FPOS_NONE)
return 0;
if (size_t (myComponentDirectory.len) < (comp + 2) * sizeof *c) {
fseek (myPredComponents, 0, SEEK_END);
pos = ftell (myPredComponents);
}
else
pos = c[1].predPos;
#else // USE_MMAP
struct compdirent c[2];
fseek (myComponentDirectory, comp * sizeof *c, SEEK_SET);
switch (fread (c, sizeof *c, 2, myComponentDirectory)) {
case 0:
return 0;
case 1:
fseek (myPredComponents, 0, SEEK_END);
c[1].predPos = ftell (myPredComponents);
// fall through
case 2:
if (c[0].predPos == FPOS_NONE)
return 0;
}
pos = c[1].predPos;
#endif // USE_MMAP
assert (c[0].predPos <= pos);
if (card_t numPred = (pos - c[0].predPos) / sizeof (card_t)) {
card_t* comps = new card_t[numPred + 1];
fseek (myPredComponents, c[0].predPos, SEEK_SET);
if (numPred != fread (comps + 1, sizeof *comps,
numPred, myPredComponents)) {
delete[] comps;
return 0;
}
*comps = numPred;
return comps;
}
return 0;
}
card_t*
ComponentGraph::path (card_t state, card_t comp,
const class Expression* pathc) const
{
if (!myGraph.eval (state, pathc))
return 0;
/** Visited states */
class BitVector visited (myGraph.getNumStates ());
visited.assign (state, true);
/** Search queue */
std::list<card_t> sq;
sq.push_front (CARD_T_MAX); /* mark for the next level */
sq.push_back (state);
/** Arcs in the explored graph (target, source) */
Graph::PathMap p;
for (;;) {
if ((state = *sq.begin ()) == CARD_T_MAX) {
sq.pop_front ();
if (sq.empty ())
return 0;
sq.push_back (CARD_T_MAX);
state = *sq.begin ();
}
sq.pop_front ();
assert (!sq.empty ());
assert (visited[state]);
if (getComponent (state) == comp)
return Graph::toPath (p, state);
if (card_t* succ = myGraph.getSuccessors (state)) {
for (assert (*succ > 0); *succ; (*succ)--) {
card_t s = succ[*succ];
if (!visited.tset (s) && myGraph.eval (s, pathc)) {
sq.push_back (s);
p.insert (Graph::PathMap::value_type (s, state));
}
}
delete[] succ;
}
}
}
card_t*
ComponentGraph::rpath (card_t state, card_t comp,
const class Expression* pathc) const
{
if (!myGraph.eval (state, pathc))
return 0;
/** Visited states */
class BitVector visited (myGraph.getNumStates ());
visited.assign (state, true);
/** Search queue */
std::list<card_t> sq;
sq.push_front (CARD_T_MAX); /* mark for the next level */
sq.push_back (state);
/** Arcs in the explored graph (target, source) */
Graph::PathMap p;
for (;;) {
if ((state = *sq.begin ()) == CARD_T_MAX) {
sq.pop_front ();
if (sq.empty ())
return 0;
sq.push_back (CARD_T_MAX);
state = *sq.begin ();
}
sq.pop_front ();
assert (!sq.empty ());
assert (visited[state]);
if (getComponent (state) == comp)
return Graph::toPath (p, state);
for (Graph::fpos_t pos = myGraph.getPredecessors (state);
pos != FPOS_NONE;) {
card_t s = myGraph.getPredecessor (pos);
if (!visited.tset (s) && myGraph.eval (s, pathc)) {
sq.push_back (s);
p.insert (Graph::PathMap::value_type (s, state));
}
}
}
}
|