values-intervals.c 16.7 KB
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
/*
  Domains represented by sequences of intervals.
*/

#ifdef COMPACT_DOMAINS
#  error "COMPACT_DOMAINS defined: shouldn't be reading this file"
#endif

struct fd_iterator {
  fd_value domain;
  int last;
  bool started;
};

fd_value fd_new_value(int min, int max)
{
  fd_value v = malloc(sizeof(struct fd_value));

  if (v)
    {
      if (min < max)
	{
	  v->kind = FD_MULTIPLE;
	  v->value.interval.lower = min;
	  v->value.interval.upper = max;
	}
      else if (min == max)
	{
	  v->kind = FD_SINGLETON;
	  v->value.value = min;
	}
      else
	v->kind = FD_EMPTY;

      v->next = 0;
    }
  else
    perror("fd_new_value(?): malloc");

  return v;
}

bool _fd_val_empty(fd_value domain)
{
  return domain->kind == FD_EMPTY;
}

int _fd_val_single(fd_value domain, int *value)
{
  if (domain->kind != FD_SINGLETON || domain->next != 0)
    return 0;

  if (value != NULL)
    *value = domain->value.value;

  return 1;
}

static void _fd_free_value_segment(fd_value value)
{
  free(value);
}


void _fd_free_value(fd_value value)
{
  fd_value next;

  while (value)
    {
      next = value->next;
      free(value);
      value = next;
    }
}


/* return maximum value from DOMAIN */
int _fd_val_max(fd_value domain)
{
  fd_value v = domain;

  while (v->next)
    v = v->next;

  switch (v->kind)
    {
      case FD_SINGLETON:
	return v->value.value;
      case FD_MULTIPLE:
	return v->value.interval.upper;
      case FD_EMPTY:
	fd__fatal("empty domain in _fd_val_max");
	/* NOTREACHED */
      default:
	fd__fatal("corrupt domain in _fd_val_max?");
	/* NOTREACHED */
    }
}

/* return minimum value from DOMAIN */
int _fd_val_min(fd_value domain)
{
  switch (domain->kind)
    {
      case FD_SINGLETON:
	return domain->value.value;
      case FD_MULTIPLE:
	return domain->value.interval.lower;
      case FD_EMPTY:
	fd__fatal("empty domain in _fd_val_min");
	/* NOTREACHED */
      default:
	fd__fatal("corrupt domain in _fd_val_min?");
	/* NOTREACHED */
    }
}

/* return and remove minimum value from DOMAIN */
int _fd_val_del_min(fd_value domain)
{
  int min;

  switch (domain->kind)
    {
      case FD_SINGLETON:
	min = domain->value.value;

	if (domain->next == 0)
	  domain->kind = FD_EMPTY;
	else
	  {
	    fd_value next = domain->next;

	    *domain = *domain->next;

	    _fd_free_value_segment(next);
	  }

	break;
      case FD_MULTIPLE:
	min = domain->value.interval.lower;

	if (min + 1 == domain->value.interval.upper)
	  {
	    domain->kind = FD_SINGLETON;
	    domain->value.value = min + 1;
	  }
	else
	  domain->value.interval.lower++;

	break;
      case FD_EMPTY:
	fd__fatal("empty domain in _fd_val_del_min");
	/* NOTREACHED */
      default:
	fd__fatal("corrupt domain in _fd_val_del_min?");
	/* NOTREACHED */
    }

  return min;
}

/* return and remove maximum value from DOMAIN */
int _fd_val_del_max(fd_value domain)
{
  fd_value v = domain, *p = &domain;
  int max;

  while (v->next)
    {
      p = &v->next;
      v = v->next;
    }

  switch (v->kind)
    {
      case FD_SINGLETON:
	max = v->value.value;

	if (v == domain)
	    v->kind = FD_EMPTY;
	else
	  {
	    *p = v->next;
	    _fd_free_value_segment(v);
	  }

	break;
      case FD_MULTIPLE:
	max = v->value.interval.upper;

	if (v->value.interval.lower == --v->value.interval.upper)
	  {
	    v->kind = FD_SINGLETON;
	    v->value.value = v->value.interval.lower;
	  }

	break;
      case FD_EMPTY:
	fd__fatal("empty domain in _fd_val_del_max");
	/* NOTREACHED */
      default:
	fd__fatal("corrupt domain in _fd_val_del_max?");
	/* NOTREACHED */
    }

  return max;
}

int _fd_val_contains_val(fd_value domain, int value)
{
  fd_value d = domain;

  while (d)
    {
      switch (d->kind)
	{
	  case FD_SINGLETON:
	    if (value < d->value.value)
	      return 0;

	    if (value == d->value.value)
	      return 1;

	    break;
	  case FD_MULTIPLE:
	    if (value <= d->value.interval.upper)
	      return  d->value.interval.lower <= value;

	    break;
	  case FD_EMPTY:
	    fd__fatal("empty domain in _fd_val_contains_val");
	    /* NOTREACHED */
	  default:
	    fd__fatal("corrupt domain in _fd_val_contains_val?");
	    /* NOTREACHED */
	}

      d = d->next;
    }

  return 0;
}


void _fd_val_set_value(fd_value domain, int value)
{
  switch (domain->kind)
    {
      case FD_EMPTY:
	fd__fatal("_fd_val_set_val called on a variable with empty domain");
	/* NOTREACHED */
      case FD_MULTIPLE:
	domain->kind = FD_SINGLETON;
      case FD_SINGLETON:
	domain->value.value = value;
	if (domain->next)
	  {
	    _fd_free_value(domain->next);
	    domain->next = 0;
	  }
	break;
      default:
	fd__fatal("corrupt domain in _fd_val_set_value?");
	/* NOTREACHED */
    }
}

/* remove all values greater than or equal to VALUE from DOMAIN */
int _fd_val_del_ge(int value, fd_value domain)
{
  fd_value prev, cur;

#ifdef DEBUG_VALUE
  printf("remove {%d..} from ", value);
  _fd_val_print(domain);
  putchar('\n');
#endif

  if (domain->kind == FD_EMPTY)
    return 0;

  prev = cur = domain;

  while (cur)
    if (cur->kind == FD_SINGLETON && cur->value.value < value)
      {
	prev = cur;
	cur = cur->next;
      }
    else if (cur->kind == FD_MULTIPLE && cur->value.interval.upper < value)
      {
	prev = cur;
	cur = cur->next;
      }
    else
      break;

  if (cur == 0)
    return 0;

  if (cur->kind == FD_SINGLETON || cur->value.interval.lower >= value)
    {
      if (cur == domain)
	{
	  domain->kind = FD_EMPTY;
	  _fd_free_value(domain->next);
	  domain->next = 0;
	}
      else
	{
	  _fd_free_value(cur);
	  prev->next = 0;
	}
    }
  else // cur->value.interval.lower < value <= cur->value.interval.upper
    {
      cur->value.interval.upper = value - 1;
      if (cur->value.interval.lower == cur->value.interval.upper)
	{
	  cur->kind = FD_SINGLETON;
	  cur->value.value = cur->value.interval.lower;
	}
      _fd_free_value(cur->next);
      cur->next = 0;
    }

  return 1;
}

/* remove all values greater than VALUE from DOMAIN */
int _fd_val_del_gt(int value, fd_value domain)
{
  fd_value prev, cur;

#ifdef DEBUG_VALUE
  printf("remove {%d..} from ", value + 1);
  _fd_val_print(domain);
  putchar('\n');
#endif

  if (domain->kind == FD_EMPTY)
    return 0;

  prev = cur = domain;

  while (cur)
    if (cur->kind == FD_SINGLETON && cur->value.value <= value)
      {
	prev = cur;
	cur = cur->next;
      }
    else if (cur->kind == FD_MULTIPLE && cur->value.interval.upper <= value)
      {
	prev = cur;
	cur = cur->next;
      }
    else
      break;

  if (cur == 0)
    return 0;

  if (cur->kind == FD_SINGLETON || cur->value.interval.lower > value)
    {
      if (cur == domain)
	{
	  domain->kind = FD_EMPTY;
	  _fd_free_value(domain->next);
	  domain->next = 0;
	}
      else
	{
	  _fd_free_value(cur);
	  prev->next = 0;
	}
    }
  else // cur->value.interval.lower <= value < cur->value.interval.upper
    {
      cur->value.interval.upper = value;
      if (cur->value.interval.lower == cur->value.interval.upper)
	{
	  cur->kind = FD_SINGLETON;
	  cur->value.value = cur->value.interval.lower;
	}
      _fd_free_value(cur->next);
      cur->next = 0;
    }

  return 1;
}

/* remove all values less than or equal to VALUE from DOMAIN */
int _fd_val_del_le(int value, fd_value domain)
{
  fd_value cur;

#ifdef DEBUG_VALUE
  printf("remove {..%d} from ", value);
  _fd_val_print(domain);
  putchar('\n');
#endif

  if (domain->kind == FD_EMPTY)
    return 0;

  // see if there is anything to remove
  if (_fd_val_min(domain) > value)
    return 0;

  // if the first segment of the domain is an interval whose upper
  // bound is greater than value, adjust it
  if (domain->kind == FD_MULTIPLE && domain->value.interval.upper > value)
    {
      domain->value.interval.lower = value + 1;
      if (domain->value.interval.lower == domain->value.interval.upper)
	{
	  domain->kind = FD_SINGLETON;
	  domain->value.value = domain->value.interval.lower;
	}

      return 1;
    }

  // otherwise, the values in the first segment are all less than or
  // equal to value, so don't bother with it
  cur = domain->next;

  // delete all segments with values less than or equal to value
  while (cur &&
	 (cur->kind == FD_SINGLETON && cur->value.value <= value ||
	  cur->kind == FD_MULTIPLE && cur->value.interval.upper <= value))
    {
      fd_value v = cur;

      cur = cur->next;
      _fd_free_value_segment(v);
    }

  if (cur && cur->kind == FD_MULTIPLE && cur->value.interval.lower <= value)
    {
      cur->value.interval.lower = value + 1;
      if (cur->value.interval.lower == cur->value.interval.upper)
	{
	  cur->kind = FD_SINGLETON;
	  cur->value.value = cur->value.interval.lower;
	}
    }

  if (cur == 0)
    {
      domain->kind = FD_EMPTY;
      domain->next = 0;
    }
  else
    {
      memcpy(domain, cur, sizeof(struct fd_value));
      _fd_free_value_segment(cur);
    }

  return 1;
}

/* remove all values less than VALUE from DOMAIN */
int _fd_val_del_lt(int value, fd_value domain)
{
  fd_value cur;

#ifdef DEBUG_VALUE
  printf("remove {..%d} from ", value - 1);
  _fd_val_print(domain);
  putchar('\n');
#endif

  if (domain->kind == FD_EMPTY)
    return 0;

  // see if there is anything to remove
  if (_fd_val_min(domain) >= value)
    return 0;

  // if the first segment of the domain is an interval whose upper
  // bound is greater than or equal to value, adjust it
  if (domain->kind == FD_MULTIPLE && domain->value.interval.upper >= value)
    {
      domain->value.interval.lower = value;
      if (domain->value.interval.lower == domain->value.interval.upper)
	{
	  domain->kind = FD_SINGLETON;
	  domain->value.value = domain->value.interval.lower;
	}

      return 1;
    }

  // otherwise, the values in the first segment are all less than or
  // equal to value, so don't bother with it
  cur = domain->next;

  // delete all segments with values less than value
  while (cur &&
	 (cur->kind == FD_SINGLETON && cur->value.value < value ||
	  cur->kind == FD_MULTIPLE && cur->value.interval.upper < value))
    {
      fd_value v = cur;

      cur = cur->next;
      _fd_free_value_segment(v);
    }

  if (cur && cur->kind == FD_MULTIPLE && cur->value.interval.lower < value)
    {
      cur->value.interval.lower = value;
      if (cur->value.interval.lower == cur->value.interval.upper)
	{
	  cur->kind = FD_SINGLETON;
	  cur->value.value = cur->value.interval.lower;
	}
    }

  if (cur == 0)
    {
      domain->kind = FD_EMPTY;
      domain->next = 0;
    }
  else
    {
      memcpy(domain, cur, sizeof(struct fd_value));
      _fd_free_value_segment(cur);
    }

  return 1;
}

/* remove VALUE from DOMAIN */
int _fd_val_del_val(int value, fd_value domain)
{
  fd_value cur, prev;
  int found = 0;

#ifdef DEBUG_VALUE
  printf("remove {%d} from ", value);
  _fd_val_print(domain);
  putchar('\n');
#endif

  if (domain->kind == FD_EMPTY)
    return 0;

  cur = domain;

  while (!found && cur)
    {
      if (cur->kind == FD_SINGLETON)
	{
	  if (cur->value.value < value)
	    {
	      prev = cur;
	      cur = cur->next;
	    }
	  else if (cur->value.value == value)
	    found = 1;
	  else // value doesn't belong to domain
	    break;
	}
      else // FD_MULTIPLE
	{
	  if (cur->value.interval.upper < value)
	    {
	      prev = cur;
	      cur = cur->next;
	    }
	  else if (cur->value.interval.lower <= value)
	    found = 1;
	  else // value doesn't belong to domain
	    break;
	}
    }

  if (!found)
    return 0;

  if (cur->kind == FD_SINGLETON)
    {
      if (cur == domain)
	{
	  if (domain->next == 0)
	    domain->kind = FD_EMPTY;
	  else
	    {
	      fd_value next = domain->next;
	      memcpy(domain, next, sizeof(struct fd_value));
	      _fd_free_value_segment(next);
	    }
	}
      else
	{
	  prev->next = cur->next;
	  _fd_free_value_segment(cur);
	}
    }
  else // FD_MULTIPLE
    {
      if (value == cur->value.interval.lower)
	cur->value.interval.lower = value + 1;
      else if (value == cur->value.interval.upper)
	cur->value.interval.upper = value - 1;
      else // cur->value.interval.lower < value < cur->value.interval.upper)
	{
	  // split interval
	  fd_value next = cur->next;

	  cur->next = fd_new_value(value + 1, cur->value.interval.upper);

	  cur->value.interval.upper = value - 1;

	  cur->next->next = next;
	}

      // check if the resulting interval is a singleton
      if (cur->value.interval.lower == cur->value.interval.upper)
	{
	  cur->kind = FD_SINGLETON;
	  cur->value.value = cur->value.interval.lower;
	}
    }

  return 1;
}

/* remove every value other than VALUE from DOMAIN */
int _fd_val_del_other(fd_value domain, int value)
{
  // XXX: keeping it simple
  return _fd_val_del_lt(value, domain) | _fd_val_del_gt(value, domain);
}

/* remove from DOMAIN1 all values not in DOMAIN2 */
// XXX
int _fd_val_intersect(fd_value domain1, fd_value domain2)
{
  fd_value copy2;
  int v, min1, max1;
  int changed = 0;

  if (domain1->kind == FD_EMPTY)
    return 0;

  if (domain2->kind == FD_EMPTY)
    {
      _fd_free_value(domain1->next);
      domain1->next = 0;
      domain1->kind = FD_EMPTY;

      return 1;
    }

  copy2 = _fd_val_clone(domain2);

  min1 = _fd_val_min(domain1);
  max1 = _fd_val_max(domain1);

  for (v = min1; v <= max1; ++v)
    {
      if (! _fd_val_del_val(v, copy2))
	changed |= _fd_val_del_val(v, domain1);

      if (copy2->kind == FD_EMPTY)
	{
	  _fd_free_value(copy2);

	  return _fd_val_del_gt(v, domain1) || changed;
	}
    }

  _fd_free_value(copy2);

  return changed;
}

/* create a clone of VALUE */
fd_value _fd_val_clone(fd_value value)
{
  fd_value new, *next;

  new = 0;
  next = &new;

  while (value != 0)
    {
      switch (value->kind)
	{
	  case FD_SINGLETON:
	    *next = fd_new_value(value->value.value, value->value.value);
	    break;
	  case FD_MULTIPLE:
	    *next = fd_new_value(value->value.interval.lower,
				 value->value.interval.upper);
	    break;
	  case FD_EMPTY:
	    *next = fd_new_value(1, 0);
	    break;
	  default:
	    fd__fatal("corrupt domain in _fd_val_clone?");
	    /* NOTREACHED */
	}

      next = &(*next)->next;

      value = value->next;
    }

  return new;
}

#if 01 // XXX: only used with compact domains?
/* copy FROM's value to TO */
void _fd_val_copy(fd_value to, fd_value from)
{
  // free to's segments other than the first
  if (to->next)
    _fd_free_value(to->next);

  // copy from's first segment
  memcpy(to, from, sizeof(*from));

  while (from->next != 0)
    {
      from = from->next;

      switch (from->kind)
	{
	  case FD_SINGLETON:
	    to->next = fd_new_value(from->value.value, from->value.value);
	    break;
	  case FD_MULTIPLE:
	    to->next = fd_new_value(from->value.interval.lower,
				    from->value.interval.upper);
	    break;
#if 0 // XXX: shouldn't happen
	  case FD_EMPTY:
	    to->next = fd_new_value(1, 0);
	    break;
#endif
	  default:
	    fd__fatal("corrupt domain in _fd_val_copy?");
	    /* NOTREACHED */
	}

      to = to->next;
    }
}
#endif

int _fd_val_size(fd_value domain)
{
  fd_value v = domain;
  int s = 0;

  while (v && v->kind != FD_EMPTY)
    {
      switch (v->kind)
	{
	  case FD_SINGLETON:
	    s++;
	    break;
	  case FD_MULTIPLE:
	    s += v->value.interval.upper - v->value.interval.lower + 1;
	    break;
	  default:
	    fd__fatal("corrupt domain in _fd_val_size?");
	    /* NOTREACHED */
	}

      v = v->next;
    }

  return s;
}

void _fd_val_print(fd_value value)
{
  putchar('{');

  while (value)
    {
      switch (value->kind)
	{
	  case FD_SINGLETON:
	    printf("%d", value->value.value);
	    break;
	  case FD_MULTIPLE:
	    printf("%d..%d", value->value.interval.lower,
		             value->value.interval.upper);
	    break;
	}

      value = value->next;

      if (value)
	printf(",");
    }

  putchar('}');
}

/* Iterate over a domain */

fd_iterator _fd_val_iterator(fd_value domain)
{
  struct fd_iterator *iterator = malloc(sizeof(*iterator)); // XXX: NULL

  iterator->domain = domain;
  iterator->started = false;

  return iterator;
}

int _fd_val_has_next(fd_iterator iterator)
{
  return !iterator->started || iterator->domain != NULL;
}

int _fd_val_next_element(fd_iterator iterator)
{
  int v;

  if (!iterator->started)
    {
      iterator->started = true;

      if (iterator->domain->kind == FD_SINGLETON)
	{
	  v = iterator->last = iterator->domain->value.value;

	  iterator->domain = iterator->domain->next;
	}
      else // FD_MULTIPLE
	v = iterator->last = iterator->domain->value.interval.lower;
    }
  else
    {
      if (iterator->domain->kind == FD_SINGLETON)
	{
	  v = iterator->last = iterator->domain->value.value;

	  iterator->domain = iterator->domain->next;
	}
      else // FD_MULTIPLE
	{
	  if (iterator->last < iterator->domain->value.interval.lower)
	    v = iterator->last = iterator->domain->value.interval.lower;
	  else
	    {
	      v = ++iterator->last;

	      if (v == iterator->domain->value.interval.upper)
		iterator->domain = iterator->domain->next;
	    }
	}
    }

  return v;
}

void _fd_val_iterator_dispose(fd_iterator iterator)
{
  free(iterator);
}