/usr/include/polymake/GenericIncidenceMatrix.h is in libpolymake-dev-common 3.2r2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 | /* Copyright (c) 1997-2018
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.polymake.org
This program 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: http://www.gnu.org/licenses/gpl.txt.
This program 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.
--------------------------------------------------------------------------------
*/
#ifndef POLYMAKE_GENERIC_INCIDENCE_MATRIX_H
#define POLYMAKE_GENERIC_INCIDENCE_MATRIX_H
#include "polymake/internal/matrix_methods.h"
#include "polymake/GenericIO.h"
namespace pm {
/* ------------------------
* GenericIncidenceMatrix
* ------------------------ */
template <typename symmetric=NonSymmetric> class IncidenceMatrix;
template <typename TMatrix>
class GenericIncidenceMatrix
: public Generic<TMatrix>
, public matrix_methods<TMatrix, bool>
, public operators::base {
template <typename> friend class GenericIncidenceMatrix;
protected:
GenericIncidenceMatrix() {}
GenericIncidenceMatrix(const GenericIncidenceMatrix&) {}
public:
typedef bool element_type;
static const bool is_symmetric=check_container_feature<TMatrix, Symmetric>::value,
is_flat=check_container_feature<TMatrix, FlatStorage>::value;
typedef typename std::conditional<is_symmetric, Symmetric, NonSymmetric>::type symmetric;
typedef typename Generic<TMatrix>::top_type top_type;
typedef IncidenceMatrix<symmetric> persistent_type;
typedef GenericIncidenceMatrix generic_type;
template <typename Other>
static constexpr bool is_expandable_by()
{
return !is_symmetric && object_traits<TMatrix>::is_resizeable!=0;
}
template <typename TMatrix2>
static constexpr bool compatible_symmetry_types()
{
return !is_symmetric || TMatrix2::is_symmetric;
}
protected:
template <typename TMatrix2>
void assign(const GenericIncidenceMatrix<TMatrix2>& m)
{
copy_range(pm::rows(m).begin(), entire(pm::rows(*this)));
}
template <typename TMatrix2, typename Operation>
void assign_op(const GenericIncidenceMatrix<TMatrix2>& m, const Operation& op, std::true_type)
{
perform_assign(pm::entire(pm::rows(*this)), pm::rows(m).begin(), op);
}
template <typename TMatrix2, typename Operation>
void assign_op(const GenericIncidenceMatrix<TMatrix2>& m, const Operation& op, std::false_type)
{
assign_op(m+T(m), op, std::true_type());
}
template <typename TMatrix2>
constexpr bool trivial_assignment(const GenericIncidenceMatrix<TMatrix2>&) const { return false; }
constexpr bool trivial_assignment(const GenericIncidenceMatrix& m) const { return this==&m; }
template <typename TMatrix2>
void assign_impl(const GenericIncidenceMatrix<TMatrix2>& m, std::false_type)
{
this->top().assign(m+T(m));
}
template <typename TMatrix2>
void assign_impl(const GenericIncidenceMatrix<TMatrix2>& m, std::true_type)
{
this->top().assign(m.top());
}
public:
top_type& operator= (const GenericIncidenceMatrix& m)
{
if (!trivial_assignment(m)) {
if (!object_traits<TMatrix>::is_resizeable && (POLYMAKE_DEBUG || !Unwary<TMatrix>::value)) {
if (this->rows() != m.rows() || this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::operator= - dimension mismatch");
}
this->top().assign(m.top());
}
return this->top();
}
template <typename TMatrix2>
top_type& operator= (const GenericIncidenceMatrix<TMatrix2>& m)
{
if (!object_traits<TMatrix>::is_resizeable && (POLYMAKE_DEBUG || !Unwary<TMatrix>::value)) {
if (this->rows() != m.rows() || this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::operator= - dimension mismatch");
}
assign_impl(m, bool_constant<!is_symmetric || TMatrix2::is_symmetric>());
return this->top();
}
template <typename TMatrix2>
top_type& operator+= (const GenericIncidenceMatrix<TMatrix2>& m)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TMatrix2>::value) {
if (this->rows() != m.rows() || this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::operator+= - dimension mismatch");
}
this->top().assign_op(m.top(), BuildBinary<operations::add>(), bool_constant<!is_symmetric || TMatrix2::is_symmetric>());
return this->top();
}
template <typename TMatrix2>
top_type& operator-= (const GenericIncidenceMatrix<TMatrix2>& m)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TMatrix2>::value) {
if (this->rows() != m.rows() || this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::operator-= - dimension mismatch");
}
this->top().assign_op(m.top(), BuildBinary<operations::sub>(), bool_constant<!is_symmetric || TMatrix2::is_symmetric>());
return this->top();
}
template <typename TMatrix2>
top_type& operator*= (const GenericIncidenceMatrix<TMatrix2>& m)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TMatrix2>::value) {
if (this->rows() != m.rows() || this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::operator*= - dimension mismatch");
}
this->top().assign_op(m.top(), BuildBinary<operations::mul>(), bool_constant<!is_symmetric || TMatrix2::is_symmetric>());
return this->top();
}
template <typename TMatrix2>
top_type& operator^= (const GenericIncidenceMatrix<TMatrix2>& m)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TMatrix2>::value) {
if (this->rows() != m.rows() || this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::operator^= - dimension mismatch");
}
this->top().assign_op(m.top(), BuildBinary<operations::bitwise_xor>(), bool_constant<!is_symmetric || TMatrix2::is_symmetric>());
return this->top();
}
template <typename TMatrix2, typename=typename std::enable_if<is_expandable_by<TMatrix2>()>::type>
top_type& operator/= (const GenericIncidenceMatrix<TMatrix2>& m)
{
if (m.rows()) {
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TMatrix2>::value) {
if (this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::operator/= - dimension mismatch");
}
this->top().append_rows(m.top());
}
return this->top();
}
template <typename TSet, typename=typename std::enable_if<is_expandable_by<TSet>()>::type>
top_type& operator/= (const GenericSet<TSet, int, operations::cmp>& s)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TSet>::value) {
if (!s.top().empty() && (s.top().front() < 0 || s.top().back() >= this->cols()))
throw std::runtime_error("GenericIncidenceMatrix::operator/= - set elements out of range");
}
this->top().append_row(s.top());
return this->top();
}
template <typename TSet, typename=typename std::enable_if<is_expandable_by<TSet>()>::type>
top_type& operator/= (const Complement<TSet, int, operations::cmp>& s)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value) {
if (!s.base().empty() && (s.base().front() < 0 || s.base().back() >= this->cols()))
throw std::runtime_error("GenericIncidenceMatrix::operator/= - set elements out of range");
}
this->top().append_row(sequence(0,this->cols()) * s);
}
template <typename TMatrix2, typename=typename std::enable_if<is_expandable_by<TMatrix2>()>::type>
top_type& operator|= (const GenericIncidenceMatrix<TMatrix2>& m)
{
if (m.cols()) {
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TMatrix2>::value) {
if (this->rows() != m.rows())
throw std::runtime_error("GenericIncidenceMatrix::operator|= - dimension mismatch");
}
this->top().append_cols(m.top());
}
return this->top();
}
template <typename TSet, typename=typename std::enable_if<is_expandable_by<TSet>()>::type>
top_type& operator|= (const GenericSet<TSet, int, operations::cmp>& s)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TSet>::value) {
if (!s.top().empty() && (s.top().front() < 0 || s.top().back() >= this->rows()))
throw std::runtime_error("GenericIncidenceMatrix::operator|= - set elements out of range");
}
this->top().append_col(s.top());
return this->top();
}
template <typename TSet, typename=typename std::enable_if<is_expandable_by<TSet>()>::type>
top_type& operator|= (const Complement<TSet, int, operations::cmp>& s)
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value) {
if (!s.base().empty() && (s.base().front() < 0 || s.base().back() >= this->rows()))
throw std::runtime_error("GenericIncidenceMatrix::operator|= - set elements out of range");
}
this->top().append_col(sequence(0,this->rows()) * s);
}
template <typename TMatrix2, typename=typename std::enable_if<is_symmetric==TMatrix2::is_symmetric>::type>
void swap(GenericIncidenceMatrix<TMatrix2>& m)
{
if (trivial_assignment(m)) return;
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value || !Unwary<TMatrix2>::value) {
if (this->rows() != m.rows() || this->cols() != m.cols())
throw std::runtime_error("GenericIncidenceMatrix::swap - dimension mismatch");
}
swap_ranges(entire(pm::rows(*this)), pm::rows(m).begin());
}
top_type& transitive_closure();
template <typename Result>
struct rebind_generic {
typedef GenericIncidenceMatrix<Result> type;
};
template <typename TMatrix2> friend
bool operator== (const GenericIncidenceMatrix& l, const GenericIncidenceMatrix<TMatrix2>& r)
{
if (l.rows() != r.rows() || l.cols() != r.cols()) return false;
return operations::cmp_unordered()(rows(l), rows(r)) == cmp_eq;
}
template <typename TMatrix2> friend
bool operator!= (const GenericIncidenceMatrix& l, const GenericIncidenceMatrix<TMatrix2>& r)
{
return !(l==r);
}
#if POLYMAKE_DEBUG
void dump() const __attribute__((used)) { cerr << this->top() << std::flush; }
#endif
};
struct is_incidence_matrix;
template <typename TMatrix>
struct spec_object_traits< GenericIncidenceMatrix<TMatrix> >
: spec_or_model_traits<TMatrix, is_container> {
private:
typedef spec_or_model_traits<TMatrix, is_container> base_t;
public:
static const int dimension=2,
is_symmetric=check_container_feature<TMatrix, Symmetric>::value,
is_resizeable= base_t::is_specialized ? base_t::is_resizeable : 2-is_symmetric;
typedef is_incidence_matrix generic_tag;
};
template <typename TMatrix>
typename GenericIncidenceMatrix<TMatrix>::top_type& GenericIncidenceMatrix<TMatrix>::transitive_closure()
{
if (POLYMAKE_DEBUG || !Unwary<TMatrix>::value) {
if (this->rows() != this->cols())
throw std::runtime_error("GenericIncidenceMatrix::transitive_closure - matrix not square");
}
bool changed;
do {
changed=false;
for (auto r_i=entire(pm::rows(this->top())); !r_i.at_end(); ++r_i) {
const int ones=r_i->size();
for (auto e=r_i->begin(); !e.at_end(); ++e)
if (e.index() != r_i.index())
(*r_i) += this->row(e.index());
if (r_i->size() > ones) changed=true;
}
} while (changed);
return this->top();
}
/* ----------------------------------------
* SingleIncidenceRow, SingleIncidenceCol
* ---------------------------------------- */
template <typename SetRef>
class SingleIncidenceRow
: protected single_line_matrix<SetRef>
, public GenericIncidenceMatrix< SingleIncidenceRow<SetRef> > {
typedef single_line_matrix<SetRef> base_t;
public:
typedef bool value_type;
typedef bool reference;
typedef const bool const_reference;
SingleIncidenceRow(typename base_t::arg_type arg) : base_t(arg) {}
friend class Rows<SingleIncidenceRow>;
};
template <typename SetRef>
class SingleIncidenceCol
: protected single_line_matrix<SetRef>
, public GenericIncidenceMatrix< SingleIncidenceCol<SetRef> > {
typedef single_line_matrix<SetRef> base_t;
public:
typedef bool value_type;
typedef bool reference;
typedef const bool const_reference;
SingleIncidenceCol(typename base_t::arg_type arg) : base_t(arg) {}
};
template <typename SetRef>
struct spec_object_traits< SingleIncidenceRow<SetRef> >
: spec_object_traits< single_value_container<SetRef> > {};
template <typename SetRef>
struct spec_object_traits< SingleIncidenceCol<SetRef> >
: spec_object_traits< single_value_container<SetRef> > {};
class SingleElementIncidenceLine
: public modified_container_impl< SingleElementIncidenceLine,
mlist< ContainerTag< single_value_container<int, true> >,
OperationTag< BuildUnaryIt<operations::index2element> > > >
, public GenericSet<SingleElementIncidenceLine, int, operations::cmp> {
protected:
single_value_container<int, true> _index;
public:
SingleElementIncidenceLine() {}
SingleElementIncidenceLine(int i) : _index(i) {}
const single_value_container<int, true>& get_container() const { return _index; }
int dim() const { return 1; }
};
template <>
struct check_container_feature<SingleElementIncidenceLine, sparse_compatible> : std::true_type {};
template <typename Iterator1Ref, typename Iterator2Ref>
struct SingleElementIncidenceLine_factory {
typedef Iterator1Ref first_argument_type;
typedef Iterator2Ref second_argument_type;
typedef SingleElementIncidenceLine result_type;
result_type operator() (Iterator1Ref it, Iterator2Ref) const
{
return *it;
}
template <typename Iterator1, typename Iterator2>
result_type operator() (operations::partial, const Iterator1&, const Iterator2&) const
{
return result_type();
}
};
template <typename SetRef>
class IncidenceLines_across
: public modified_container_pair_impl< IncidenceLines_across<SetRef>,
mlist< Container1Tag< sequence >,
Container2Tag< SetRef >,
IteratorCouplerTag< zipping_coupler<operations::cmp, set_union_zipper> >,
OperationTag< BuildBinaryIt<SingleElementIncidenceLine_factory> >,
HiddenTag< single_line_matrix<SetRef> > > > {
public:
sequence get_container1() const { return sequence(0, size()); }
typename single_value_container<SetRef>::const_reference get_container2() const { return this->hidden().get_line(); }
int size() const { return this->hidden().get_line().dim(); }
};
template <typename SetRef>
class Rows< SingleIncidenceRow<SetRef> >
: public redirected_container< Rows< SingleIncidenceRow<SetRef> >,
mlist< ContainerTag< single_value_container<SetRef> >,
MasqueradedTop > > {
protected:
~Rows();
public:
const single_value_container<SetRef>& get_container() const { return this->hidden()._line; }
};
template <typename SetRef>
class Cols< SingleIncidenceRow<SetRef> > : public IncidenceLines_across<SetRef> {
protected:
~Cols();
};
template <typename SetRef>
class Rows< SingleIncidenceCol<SetRef> > : public IncidenceLines_across<SetRef> {
protected:
~Rows();
};
template <typename SetRef>
class Cols< SingleIncidenceCol<SetRef> > : public Rows< SingleIncidenceRow<SetRef> > {
protected:
~Cols();
};
template <typename ContainerRef1, typename ContainerRef2>
class IncidenceLineChain
: public modified_container_impl< IncidenceLineChain<ContainerRef1,ContainerRef2>,
mlist< ContainerTag< ContainerChain<ContainerRef1,ContainerRef2> >,
OperationTag< BuildUnaryIt<operations::index2element> >,
ExpectedFeaturesTag< indexed > > >,
public GenericSet<IncidenceLineChain<ContainerRef1,ContainerRef2>, int, operations::cmp> {
typedef modified_container_impl<IncidenceLineChain> base_t;
protected:
typename base_t::container chain;
public:
const typename base_t::container& get_container() const { return chain; }
IncidenceLineChain(typename base_t::container::first_arg_type arg1,
typename base_t::container::second_arg_type arg2)
: chain(arg1,arg2) {}
int dim() const
{
return chain.get_container1().dim() + chain.get_container2().dim();
}
};
template <typename ContainerRef1, typename ContainerRef2>
struct check_container_feature<IncidenceLineChain<ContainerRef1, ContainerRef2>, sparse_compatible> : std::true_type {};
template <typename ContainerRef1, typename ContainerRef2>
struct spec_object_traits< IncidenceLineChain<ContainerRef1, ContainerRef2> >
: spec_object_traits<is_container> {
static const bool is_temporary=true, is_always_const=true;
};
/* ----------------------------
* SameElementIncidenceMatrix
* ---------------------------- */
template <bool elem>
class SameElementIncidenceMatrix
: public GenericIncidenceMatrix<SameElementIncidenceMatrix<elem> > {
public:
typedef bool value_type;
typedef bool reference;
typedef const bool const_reference;
SameElementIncidenceMatrix(int r_arg, int c_arg) : r(r_arg), c(c_arg) {}
protected:
void stretch_rows(int new_r) const
{
r=new_r;
}
void stretch_cols(int new_c) const
{
c=new_c;
}
mutable int r, c;
friend class Rows<SameElementIncidenceMatrix>;
friend class Cols<SameElementIncidenceMatrix>;
template <typename, typename> friend class RowChain;
template <typename, typename> friend class ColChain;
};
template <bool elem>
class SameElementIncidenceLine
: public modified_container_impl< SameElementIncidenceLine<elem>,
mlist< ContainerTag< sequence >,
OperationTag< pair<nothing, operations::identity<int> > >,
ExpectedFeaturesTag< end_sensitive > > >,
public GenericSet<SameElementIncidenceLine<elem>, int, operations::cmp> {
public:
int dim() const { return reinterpret_cast<const int&>(*this); }
int size() const { return elem ? dim() : 0; }
bool empty() const { return !elem; }
sequence get_container() const { return sequence(0, size()); }
};
template <bool elem>
struct check_container_feature< SameElementIncidenceLine<elem>, sparse_compatible> : std::true_type {};
template <bool elem>
class Rows< SameElementIncidenceMatrix<elem> >
: public redirected_container< Rows< SameElementIncidenceMatrix<elem> >,
mlist< ContainerTag< constant_masquerade_container< SameElementIncidenceLine<elem> > >,
MasqueradedTop > > {
typedef redirected_container<Rows> _super;
protected:
~Rows();
public:
const typename _super::container& get_container() const
{
return reinterpret_cast<const typename _super::container&>(this->hidden().c);
}
int size() const { return this->hidden().r; }
bool empty() const { return !size(); }
};
template <bool elem>
class Cols< SameElementIncidenceMatrix<elem> >
: public redirected_container< Cols< SameElementIncidenceMatrix<elem> >,
mlist< ContainerTag< constant_masquerade_container< SameElementIncidenceLine<elem> > >,
MasqueradedTop > > {
typedef redirected_container<Cols> base_t;
protected:
~Cols();
public:
const typename base_t::container& get_container() const
{
return reinterpret_cast<const typename base_t::container&>(this->hidden().r);
}
int size() const { return this->hidden().c; }
bool empty() const { return !size(); }
};
// TODO: decltype(auto) would be a nice abberviation of the following singatures,
// but GCC has still a bug thwarting this: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66690
template <typename Matrix1, typename Matrix2> inline
RowChain< ColChain<const unwary_t<Matrix1>&, SameElementIncidenceMatrix<false> >,
ColChain<SameElementIncidenceMatrix<false>, const unwary_t<Matrix2>&> >
diag(const GenericIncidenceMatrix<Matrix1>& m1, const GenericIncidenceMatrix<Matrix2>& m2)
{
return RowChain< ColChain<const unwary_t<Matrix1>&, SameElementIncidenceMatrix<false> >,
ColChain<SameElementIncidenceMatrix<false>, const unwary_t<Matrix2>&> >
( ColChain<const unwary_t<Matrix1>&, SameElementIncidenceMatrix<false> >
( m1.top(), SameElementIncidenceMatrix<false>(m1.rows(), m2.cols()) ),
ColChain<SameElementIncidenceMatrix<false>, const unwary_t<Matrix2>&>
( SameElementIncidenceMatrix<false>(m2.rows(), m1.cols()), m2.top() )
);
}
/// the same, but with corner blocks filled with 1
template <typename Matrix1, typename Matrix2> inline
RowChain< ColChain<const unwary_t<Matrix1>&, SameElementIncidenceMatrix<true> >,
ColChain<SameElementIncidenceMatrix<true>, const unwary_t<Matrix2>&> >
diag_1(const GenericIncidenceMatrix<Matrix1>& m1, const GenericIncidenceMatrix<Matrix2>& m2)
{
return RowChain< ColChain<const unwary_t<Matrix1>&, SameElementIncidenceMatrix<true> >,
ColChain<SameElementIncidenceMatrix<true>, const unwary_t<Matrix2>&> >
( ColChain<const unwary_t<Matrix1>&, SameElementIncidenceMatrix<true> >
( m1.top(), SameElementIncidenceMatrix<true>(m1.rows(), m2.cols()) ),
ColChain<SameElementIncidenceMatrix<true>, const unwary_t<Matrix2>&>
( SameElementIncidenceMatrix<true>(m2.rows(), m1.cols()), m2.top() )
);
}
/* ------------
* complement
* ------------ */
template <typename Matrix>
class ComplementIncidenceMatrix
: public GenericIncidenceMatrix< ComplementIncidenceMatrix<Matrix> > {
protected:
ComplementIncidenceMatrix();
~ComplementIncidenceMatrix();
public:
typedef bool value_type;
typedef bool reference;
typedef const bool const_reference;
typedef Matrix hidden_type;
const Matrix& hidden() const { return reinterpret_cast<const Matrix&>(*this); }
};
template <typename Matrix>
struct spec_object_traits< ComplementIncidenceMatrix<Matrix> >
: spec_object_traits<is_container> {
static const bool is_always_const=true;
typedef Matrix masquerade_for;
};
template <typename Matrix>
struct check_container_feature< ComplementIncidenceMatrix<Matrix>, Symmetric >
: check_container_feature<Matrix, Symmetric> {};
template <typename Matrix>
class matrix_random_access_methods< ComplementIncidenceMatrix<Matrix> > {
public:
bool operator() (int i, int j) const
{
return ! reinterpret_cast<const Matrix&>(*this)(i,j);
}
};
template <typename LineRef>
struct ComplementIncidenceLine_factory {
typedef LineRef argument_type;
typedef LazySet2<sequence, LineRef, set_difference_zipper> result_type;
result_type operator() (typename function_argument<LineRef>::const_type l) const
{
return result_type(sequence(0,l.dim()), l);
}
};
template <typename Matrix>
class Rows< ComplementIncidenceMatrix<Matrix> >
: public modified_container_impl< Rows< ComplementIncidenceMatrix<Matrix> >,
mlist< HiddenTag< Rows<Matrix> >,
OperationTag< BuildUnary<ComplementIncidenceLine_factory> > > > {
protected:
~Rows();
};
template <typename Matrix>
class Cols< ComplementIncidenceMatrix<Matrix> >
: public modified_container_impl< Cols< ComplementIncidenceMatrix<Matrix> >,
mlist< HiddenTag< Cols<Matrix> >,
OperationTag< BuildUnary<ComplementIncidenceLine_factory> > > > {
protected:
~Cols();
};
/* --------------------------------------------
* boolean operations with incidence matrices
* -------------------------------------------- */
template <typename MatrixRef1, typename MatrixRef2, typename Controller>
class LazyIncidenceMatrix2
: public modified_container_pair_base<MatrixRef1, MatrixRef2, Controller>,
public GenericIncidenceMatrix< LazyIncidenceMatrix2<MatrixRef1,MatrixRef2,Controller> > {
typedef modified_container_pair_base<MatrixRef1, MatrixRef2, Controller> _base;
public:
typedef bool value_type;
typedef bool reference;
typedef const bool const_reference;
LazyIncidenceMatrix2(typename _base::first_arg_type src1_arg, typename _base::second_arg_type src2_arg, const Controller& c_arg=Controller())
: _base(src1_arg, src2_arg, c_arg) {}
};
template <typename MatrixRef1, typename MatrixRef2, typename Controller>
struct spec_object_traits< LazyIncidenceMatrix2<MatrixRef1,MatrixRef2,Controller> >
: spec_object_traits<is_container> {
static const bool is_lazy=true, is_temporary=true, is_always_const=true;
};
template <typename MatrixRef1, typename MatrixRef2, typename Controller>
struct check_container_feature< LazyIncidenceMatrix2<MatrixRef1,MatrixRef2,Controller>, Symmetric > {
static const bool value= check_container_ref_feature<MatrixRef1,Symmetric>::value &&
check_container_ref_feature<MatrixRef2,Symmetric>::value;
};
template <typename MatrixRef1, typename MatrixRef2, typename Controller>
class matrix_random_access_methods< LazyIncidenceMatrix2<MatrixRef1,MatrixRef2,Controller> > {
typedef LazyIncidenceMatrix2<MatrixRef1,MatrixRef2,Controller> master;
public:
bool operator() (int i, int j) const
{
const master& me=static_cast<const master&>(*this);
return me.get_operation().contains(me.get_container1()(i,j), me.get_container2()(i,j));
}
};
template <typename MatrixRef1, typename MatrixRef2, typename Controller>
class Rows< LazyIncidenceMatrix2<MatrixRef1, MatrixRef2, Controller> >
: public modified_container_pair_impl< Rows< LazyIncidenceMatrix2<MatrixRef1,MatrixRef2,Controller> >,
mlist< Container1Tag< masquerade<pm::Rows, MatrixRef1> >,
Container2Tag< masquerade<pm::Rows, MatrixRef2> >,
OperationTag< operations::construct_binary2_with_arg<LazySet2,Controller> >,
MasqueradedTop > > {
typedef modified_container_pair_impl<Rows> base_t;
protected:
~Rows();
public:
const typename base_t::container1& get_container1() const
{
return rows(this->hidden().get_container1());
}
const typename base_t::container2& get_container2() const
{
return rows(this->hidden().get_container2());
}
typename base_t::operation get_operation() const
{
return this->hidden().get_operation();
}
};
template <typename MatrixRef1, typename MatrixRef2, typename Controller>
class Cols< LazyIncidenceMatrix2<MatrixRef1, MatrixRef2, Controller> >
: public modified_container_pair_impl< Cols< LazyIncidenceMatrix2<MatrixRef1,MatrixRef2,Controller> >,
mlist< Container1Tag< masquerade<pm::Cols, MatrixRef1> >,
Container2Tag< masquerade<pm::Cols, MatrixRef2> >,
OperationTag< operations::construct_binary2_with_arg<LazySet2,Controller> >,
MasqueradedTop > > {
typedef modified_container_pair_impl<Cols> base_t;
protected:
~Cols();
public:
const typename base_t::container1& get_container1() const
{
return cols(this->hidden().get_container1());
}
const typename base_t::container2& get_container2() const
{
return cols(this->hidden().get_container2());
}
typename base_t::operation get_operation() const
{
return this->hidden().get_operation();
}
};
/* ------------------------------------
* operations building block matrices
* ------------------------------------ */
namespace operations {
template <typename OpRef>
struct bitwise_inv_impl<OpRef, is_incidence_matrix>
: reinterpret_impl<OpRef, const ComplementIncidenceMatrix<typename deref<unwary_t<OpRef>>::type>&> {};
template <typename Matrix>
struct bitwise_inv_impl<const ComplementIncidenceMatrix<Matrix>&, is_incidence_matrix> {
typedef const ComplementIncidenceMatrix<Matrix>& argument_type;
typedef const Matrix& result_type;
result_type operator() (argument_type x) const
{
return x.hidden();
}
};
template <typename LeftRef, typename RightRef>
struct add_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef LazyIncidenceMatrix2<typename attrib<unwary_t<LeftRef>>::plus_const,
typename attrib<unwary_t<RightRef>>::plus_const, set_union_zipper>
result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r) const
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (l.rows() != r.rows() || l.cols() != r.cols())
throw std::runtime_error("operator+(GenericIncidenceMatrix,GenericIncidenceMatrix) - dimension mismatch");
}
return result_type(unwary(l), unwary(r));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r) const
{
l+=r;
}
};
template <typename LeftRef, typename RightRef>
struct sub_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef LazyIncidenceMatrix2<typename attrib<unwary_t<LeftRef>>::plus_const,
typename attrib<unwary_t<RightRef>>::plus_const, set_difference_zipper>
result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r) const
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (l.rows() != r.rows() || l.cols() != r.cols())
throw std::runtime_error("operator-(GenericIncidenceMatrix,GenericIncidenceMatrix) - dimension mismatch");
}
return result_type(unwary(l), unwary(r));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r) const
{
l-=r;
}
};
template <typename LeftRef, typename RightRef>
struct mul_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef LazyIncidenceMatrix2<typename attrib<unwary_t<LeftRef>>::plus_const,
typename attrib<unwary_t<RightRef>>::plus_const, set_intersection_zipper>
result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r) const
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (l.rows() != r.rows() || l.cols() != r.cols())
throw std::runtime_error("operator*(GenericIncidenceMatrix,GenericIncidenceMatrix) - dimension mismatch");
}
return result_type(unwary(l), unwary(r));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r) const
{
l*=r;
}
};
template <typename LeftRef, typename RightRef>
struct bitwise_xor_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef LazyIncidenceMatrix2<typename attrib<unwary_t<LeftRef>>::plus_const,
typename attrib<unwary_t<RightRef>>::plus_const, set_symdifference_zipper>
result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r) const
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (l.rows() != r.rows() || l.cols() != r.cols())
throw std::runtime_error("operator^(GenericIncidenceMatrix,GenericIncidenceMatrix) - dimension mismatch");
}
return result_type(unwary(l), unwary(r));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r) const
{
l^=r;
}
};
template <typename LeftRef, typename RightRef>
struct concat_impl<LeftRef, RightRef, cons<is_set, is_set> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef IncidenceLineChain<typename attrib<unwary_t<LeftRef>>::plus_const,
typename attrib<unwary_t<RightRef>>::plus_const>
result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r) const
{
return result_type(unwary(l), unwary(r));
}
};
template <typename LeftRef, typename RightRef>
struct div_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef RowChain<typename attrib<unwary_t<LeftRef>>::plus_const,
typename attrib<unwary_t<RightRef>>::plus_const> result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r) const
{
return result_type(unwary(l), unwary(r));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r)
{
l/=r;
}
};
template <typename LeftRef, typename RightRef>
struct div_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_set> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef Set_with_dim<typename attrib<unwary_t<RightRef>>::plus_const> Right;
typedef RowChain<typename attrib<unwary_t<LeftRef>>::plus_const, SingleIncidenceRow<Right> > result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r)
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (!set_within_range(r, l.cols()))
throw std::runtime_error("operator/(GenericIncidenceMatrix,GenericSet) - dimension mismatch");
}
return result_type(unwary(l), Right(unwary(r), l.cols()));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r)
{
l/=r;
}
};
template <typename LeftRef, typename RightRef>
struct div_impl<LeftRef, RightRef, cons<is_set, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef Set_with_dim<typename attrib<unwary_t<LeftRef>>::plus_const> Left;
typedef RowChain<SingleIncidenceRow<Left>, typename attrib<unwary_t<RightRef>>::plus_const> result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r)
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (!set_within_range(l, r.cols()))
throw std::runtime_error("operator/(GenericSet,GenericIncidenceMatrix) - dimension mismatch");
}
return result_type(Left(unwary(l), r.cols()), unwary(r));
}
};
template <typename LeftRef, typename RightRef>
struct bitwise_or_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef ColChain<typename attrib<unwary_t<LeftRef>>::plus_const,
typename attrib<unwary_t<RightRef>>::plus_const> result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r) const
{
return result_type(unwary(l), unwary(r));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r)
{
l|=r;
}
};
template <typename LeftRef, typename RightRef>
struct bitwise_or_impl<LeftRef, RightRef, cons<is_incidence_matrix, is_set> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef Set_with_dim<typename attrib<unwary_t<RightRef>>::plus_const> Right;
typedef ColChain<typename attrib<unwary_t<LeftRef>>::plus_const, SingleIncidenceCol<Right> > result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r)
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (!set_within_range(r, l.rows()))
throw std::runtime_error("operator|(GenericIncidenceMatrix,GenericSet) - dimension mismatch");
}
return result_type(unwary(l), Right(unwary(r), l.rows()));
}
void assign(typename lvalue_arg<LeftRef>::type l, typename function_argument<RightRef>::const_type r)
{
l|=r;
}
};
template <typename LeftRef, typename RightRef>
struct bitwise_or_impl<LeftRef, RightRef, cons<is_set, is_incidence_matrix> > {
typedef LeftRef first_argument_type;
typedef RightRef second_argument_type;
typedef Set_with_dim<typename attrib<unwary_t<LeftRef>>::plus_const> Left;
typedef ColChain<SingleIncidenceCol<Left>, typename attrib<unwary_t<RightRef>>::plus_const> result_type;
result_type operator() (typename function_argument<LeftRef>::const_type l,
typename function_argument<RightRef>::const_type r)
{
if (POLYMAKE_DEBUG || !Unwary<LeftRef>::value || !Unwary<RightRef>::value) {
if (!set_within_range(l, r.rows()))
throw std::runtime_error("operator|(GenericSet,GenericIncidenceMatrix) - dimension mismatch");
}
return result_type(Left(unwary(l), r.rows()), unwary(r));
}
};
} // end namespace operations
namespace operators {
template <typename Matrix1, typename Matrix2> inline
typename operations::add_impl<const Matrix1&, const Matrix2&>::result_type
operator+ (const GenericIncidenceMatrix<Matrix1>& l, const GenericIncidenceMatrix<Matrix2>& r)
{
operations::add_impl<const Matrix1&, const Matrix2&> op;
return op(concrete(l), concrete(r));
}
template <typename Matrix1, typename Matrix2> inline
typename operations::sub_impl<const Matrix1&, const Matrix2&>::result_type
operator- (const GenericIncidenceMatrix<Matrix1>& l, const GenericIncidenceMatrix<Matrix2>& r)
{
operations::sub_impl<const Matrix1&, const Matrix2&> op;
return op(concrete(l), concrete(r));
}
} // end namespace operators
} // end namespace pm
namespace polymake {
using pm::GenericIncidenceMatrix;
}
namespace std {
template <typename Matrix1, typename Matrix2> inline
void swap(pm::GenericIncidenceMatrix<Matrix1>& m1, pm::GenericIncidenceMatrix<Matrix2>& m2)
{
m1.top().swap(m2.top());
}
}
#ifdef POLYMAKE_GENERIC_MATRIX_H
# include "polymake/internal/Incidence_and_SparseMatrix.h"
#endif
#endif // POLYMAKE_GENERIC_INCIDENCE_MATRIX_H
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|