/usr/include/mathic/DivList.h is in libmathic-dev 1.0~git20160320-4.
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 | #ifndef MATHIC_DIV_ARRAY_GUARD
#define MATHIC_DIV_ARRAY_GUARD
#include "stdinc.h"
#include "DivMask.h"
#include "Comparer.h"
#include <memtailor.h>
#include <vector>
#include <string>
#include <list>
#include <algorithm>
#include <sstream>
namespace mathic {
/** An object that supports queries for divisors of a monomial using
an array of monomials. See DivFinder for more documentation.
Extra fields for Configuration:
* static const bool UseLinkedList
Use a linked list if true, otherwise use an array.
* static const bool UseDivMask
Use div masks if true.
* bool getSortOnInsert() const
Keep the monomials sorted to speed up queries.
*/
template<class Configuration>
class DivList;
namespace DivListHelper {
// Implementation details for DivList.
template<bool B, class E>
struct ListImpl;
template<class Entry>
struct ListImpl<false, Entry> {
typedef std::vector<Entry> Impl;
};
template<class Entry>
struct ListImpl<true, Entry> {
typedef std::list<Entry> Impl;
};
}
template<class C>
class DivList {
public:
typedef C Configuration;
typedef typename C::Entry Entry;
typedef typename C::Monomial Monomial;
typedef typename C::Exponent Exponent;
static const bool UseLinkedList = C::UseLinkedList;
static const bool UseDivMask = C::UseDivMask;
private:
typedef typename DivMask::Extender<Entry, C::UseDivMask> ExtEntry;
typedef typename DivMask::Extender<const Monomial&,C::UseDivMask> ExtMonoRef;
typedef typename DivMask::Calculator<C> DivMaskCalculator;
typedef typename DivListHelper::ListImpl<C::UseLinkedList, ExtEntry>::Impl
List;
typedef typename List::iterator ListIter;
typedef typename List::const_iterator CListIter;
public:
class iterator;
class const_iterator;
DivList(const C& configuration);
template<class Iter>
void insert(Iter begin, Iter end);
void insert(const Entry& entry);
bool removeMultiples(const Monomial& monomial);
template<class MultipleOutput>
bool removeMultiples
(const Monomial& monomial, MultipleOutput& out);
bool removeElement(const Monomial& monomial);
iterator findDivisorIterator(const Monomial& monomial);
Entry* findDivisor(const Monomial& monomial);
const Entry* findDivisor(const Monomial& monomial) const;
template<class DO>
void findAllDivisors(const Monomial& monomial, DO& out);
template<class DO>
void findAllDivisors(const Monomial& monomial, DO& out) const;
template<class DO>
void findAllMultiples(const Monomial& monomial, DO& out);
template<class Output>
void findAllMultiples(const Monomial& monomial, Output& output) const {
ConstEntryOutput<Output> constOutput(output);
const_cast<DivList<C>&>(*this).findAllMultiples(monomial, constOutput);
}
template<class EntryOutput>
void forAll(EntryOutput& output);
template<class EntryOutput>
void forAll(EntryOutput& output) const;
iterator begin() {return iterator(_list.begin());}
const_iterator begin() const {return const_iterator(_list.begin());}
iterator end() {return iterator(_list.end());}
const_iterator end() const {return const_iterator(_list.end());}
bool empty() const {return _list.empty();}
size_t size() const {return _list.size();}
std::string getName() const;
C& getConfiguration() {return _conf;}
const C& getConfiguration() const {return _conf;}
void moveToFront(iterator pos);
void rebuild();
/** Returns the number of bytes allocated by this object. Does not
include sizeof(*this), does not include any additional memory
that the configuration may have allocated and does not include
any memory that an Entry may point to. Does include
sizeof(Entry) as well as unused memory that is being kept to
avoid frequent allocations. */
size_t getMemoryUse() const;
private:
DivList(const DivList<C>&); // unavailable
void operator=(const DivList<C>&); // unavailable
void resetNumberOfChangesTillRebuild();
void reportChanges(size_t changesMadeCount);
template<class DO>
class ConstEntryOutput {
public:
ConstEntryOutput(DO& out): _out(out) {}
bool proceed(const Entry& entry) {return _out.proceed(entry);}
private:
DO& _out;
};
class DummyMultipleOutput {
public:
void push_back(Entry& e) {}
};
List _list;
C _conf;
DivMaskCalculator _divMaskCalculator;
size_t _changesTillRebuild; /// Update using reportChanges().
};
template<class C>
class DivList<C>::iterator :
public std::iterator<std::bidirectional_iterator_tag, Entry> {
public:
friend class DivList<C>;
explicit iterator(ListIter it): _it(it) {}
operator const_iterator() const {return const_iterator(_it);}
bool operator==(iterator it) {return _it == it._it;}
bool operator!=(iterator it) {return _it != it._it;}
bool operator==(const_iterator it); // {return it == const_iterator(*this);}
bool operator!=(const_iterator it); // {return it != const_iterator(*this);}
iterator& operator++() {++_it; return *this;}
iterator operator++(int) {iterator tmp = *this; operator++(); return tmp;}
iterator& operator--() {--_it; return *this;}
iterator operator--(int) {iterator tmp = *this; operator--(); return tmp;}
Entry& operator*() const {return _it->get();}
Entry* operator->() const {return &_it->get();}
protected:
ListIter getInternal() {return _it;}
private:
ListIter _it;
};
template<class C>
class DivList<C>::const_iterator :
public std::iterator<std::bidirectional_iterator_tag, const Entry> {
public:
friend class DivList<C>;
explicit const_iterator(CListIter it): _it(it) {}
bool operator==(const_iterator it) {return _it == it._it;}
bool operator!=(const_iterator it) {return _it != it._it;}
bool operator==(iterator it) {return *this == const_iterator(it);}
bool operator!=(iterator it) {return *this != const_iterator(it);}
const Entry& operator*() const {return _it->get();}
const Entry* operator->() const {return &_it->get();}
const_iterator& operator++() {++_it; return *this;}
const_iterator operator++(int) {
const_iterator tmp = *this;
operator++();
return tmp;
}
const_iterator& operator--() {--_it; return *this;}
const_iterator operator--(int) {
const_iterator tmp = *this;
operator--();
return tmp;
}
protected:
ListIter getInternal() {return _it;}
private:
CListIter _it;
};
template<class C>
bool DivList<C>::iterator::operator==(const_iterator it) {return it == const_iterator(*this);}
template<class C>
bool DivList<C>::iterator::operator!=(const_iterator it) {return it != const_iterator(*this);}
namespace DivListHelper {
template<class C, class E, class M, class MO>
size_t removeMultiples
(C& conf, std::vector<E>& list, const M& monomial, MO& out) {
typedef typename std::vector<E>::iterator iterator;
iterator it = list.begin();
iterator oldEnd = list.end();
for (; it != oldEnd; ++it) {
if (monomial.divides(*it, conf)) {
out.push_back(it->get());
break;
}
}
if (it == oldEnd)
return 0;
iterator newEnd = it;
for (++it; it != oldEnd; ++it) {
if (!monomial.divides(*it, conf)) {
*newEnd = *it;
++newEnd;
} else
out.push_back(it->get());
}
const size_t origSize = list.size();
const size_t newSize = std::distance(list.begin(), newEnd);
MATHIC_ASSERT(newSize < list.size());
list.resize(newSize);
return origSize - newSize;
}
template<class C, class E, class M, class MO>
size_t removeMultiples(C& conf,
std::list<E>& list,
const M& monomial,
MO& out) {
#ifdef MATHIC_DEBUG
const size_t origSize = list.size();
#endif
typedef typename std::list<E>::iterator iterator;
iterator it = list.begin();
iterator oldEnd = list.end();
size_t removedCount = 0;
while (it != oldEnd) {
if (monomial.divides(*it, conf)) {
out.push_back(it->get());
++removedCount;
it = list.erase(it);
} else
++it;
}
MATHIC_ASSERT(list.size() + removedCount == origSize);
return removedCount;
}
template<class E, class It>
void moveToFront(std::vector<E>& list, It pos) {
E valueToMove = *pos;
It begin = list.begin();
while (pos != begin) {
It prev = pos;
--pos;
*prev = *pos;
}
list.front() = valueToMove;
}
template<class E, class It>
void moveToFront(std::list<E>& list, It pos) {
list.splice(list.begin(), list, pos);
}
template<class C, class E>
typename std::list<E>::iterator
insertSort(C& conf, std::list<E>& list, const E& entry) {
typedef typename std::list<E>::iterator iterator;
iterator end = list.end();
iterator it = list.begin();
for (; it != end; ++it)
if (conf.isLessThan(entry.get(), it->get()))
break;
return list.insert(it, entry);
}
template<class C, class E>
typename std::vector<E>::iterator
insertSort(C& conf, std::vector<E>& list, const E& entry) {
typedef typename std::vector<E>::iterator iterator;
iterator it = std::upper_bound(list.begin(), list.end(), entry,
Comparer<C>(conf));
return list.insert(it, entry);
}
template<class C, class E>
void sortAll(C& conf, std::list<E>& list) {
list.sort(Comparer<C>(conf));
}
template<class C, class E>
void sortAll(C& conf, std::vector<E>& list) {
std::sort(list.begin(), list.end(), Comparer<C>(conf));
}
template<class C, class E, class M>
typename std::vector<E>::iterator
findDivisorSorted(C& conf, std::vector<E>& list, const M& monomial) {
typedef typename std::vector<E>::iterator iterator;
iterator rangeEnd =
std::upper_bound(list.begin(), list.end(), monomial, Comparer<C>(conf));
iterator it = list.begin();
for (; it != rangeEnd; ++it)
if (it->divides(monomial, conf))
return it;
return list.end();
}
template<class C, class E, class M>
typename std::list<E>::iterator
findDivisorSorted(C& conf, std::list<E>& list, const M& monomial) {
typedef typename std::list<E>::iterator iterator;
iterator end = list.end();
iterator it = list.begin();
size_t count = 0;
for (; it != end; ++it) {
++count;
if (count == 35) {
count = 0;
if (conf.isLessThan(monomial.get(), it->get()))
break;
}
if (it->divides(monomial, conf))
return it;
}
return end;
}
template<class C, class E, class M, class DO>
void findAllDivisorsSorted
(C& conf, std::vector<E>& list, const M& monomial, DO& out) {
typedef typename std::vector<E>::iterator iterator;
iterator rangeEnd =
std::upper_bound(list.begin(), list.end(), monomial, Comparer<C>(conf));
iterator it = list.begin();
for (; it != rangeEnd; ++it)
if (it->divides(monomial, conf))
if (!out.proceed(it->get()))
break;
}
template<class C, class E, class M, class O>
void findAllDivisorsSorted
(C& conf, std::list<E>& list, const M& monomial, O& out) {
typedef typename std::list<E>::iterator iterator;
iterator end = list.end();
iterator it = list.begin();
size_t count = 0;
for (; it != end; ++it) {
++count;
if (count == 35) {
count = 0;
if (conf.isLessThan(monomial.get(), it->get()))
break;
}
if (it->divides(monomial, conf))
if (!out.proceed(it->get()))
break;
}
}
}
template<class C>
DivList<C>::DivList(const C& configuration):
_conf(configuration),
_divMaskCalculator(configuration) {
resetNumberOfChangesTillRebuild();
}
template<class C>
void DivList<C>::insert(const Entry& entry) {
ExtEntry extEntry(entry, _divMaskCalculator, _conf);
if (!_conf.getSortOnInsert())
_list.push_back(extEntry);
else
DivListHelper::insertSort(_conf, _list, extEntry);
reportChanges(1);
}
template<class C>
template<class Iter>
void DivList<C>::insert(Iter rangeBegin, Iter rangeEnd) {
if (!empty()) {
for (; rangeBegin != rangeEnd; ++rangeBegin)
insert(*rangeBegin);
return;
}
if (rangeBegin == rangeEnd)
return;
_divMaskCalculator.rebuild(rangeBegin, rangeEnd, _conf);
for (; rangeBegin != rangeEnd; ++rangeBegin)
_list.push_back(ExtEntry(*rangeBegin, _divMaskCalculator, _conf));
if (_conf.getSortOnInsert())
DivListHelper::sortAll(_conf, _list);
resetNumberOfChangesTillRebuild();
}
template<class C>
template<class MO>
bool DivList<C>::removeMultiples(const Monomial& monomial, MO& out) {
ExtMonoRef extMonomial(monomial, _divMaskCalculator, _conf);
#ifdef MATHIC_DEBUG
const size_t origSize = size();
#endif
const size_t removedCount =
DivListHelper::removeMultiples(_conf, _list, extMonomial, out);
MATHIC_ASSERT_NO_ASSUME(size() + removedCount == origSize);
reportChanges(removedCount);
return removedCount > 0;
}
template<class C>
bool DivList<C>::removeMultiples(const Monomial& monomial) {
DummyMultipleOutput out;
return removeMultiples(monomial, out);
}
template<class C>
bool DivList<C>::removeElement(const Monomial& monomial) {
const size_t varCount = _conf.getVarCount();
for (ListIter it = _list.begin(); it != _list.end(); ++it) {
for (size_t var = 0; var < varCount; ++var) {
if (_conf.getExponent(monomial, var) !=
_conf.getExponent(it->get(), var)) {
goto skip;
}
}
_list.erase(it);
return true;
skip:;
}
return false;
}
template<class C>
typename DivList<C>::iterator
DivList<C>::findDivisorIterator(const Monomial& monomial) {
ExtMonoRef extMonomial(monomial, _divMaskCalculator, _conf);
if (!_conf.getSortOnInsert()) {
ListIter listEnd = _list.end();
for (ListIter it = _list.begin(); it != listEnd; ++it) {
if (it->divides(extMonomial, _conf))
return iterator(it);
}
return iterator(listEnd);
} else
return iterator(DivListHelper::findDivisorSorted(_conf, _list, extMonomial));
}
template<class C>
typename DivList<C>::Entry*
DivList<C>::findDivisor(const Monomial& monomial) {
ExtMonoRef extMonomial(monomial, _divMaskCalculator, _conf);
if (!_conf.getSortOnInsert()) {
const ListIter listEnd = _list.end();
for (ListIter it = _list.begin(); it != listEnd; ++it) {
if (it->divides(extMonomial, _conf))
return &it->get();
}
return 0;
} else {
ListIter it = DivListHelper::findDivisorSorted(_conf, _list, extMonomial);
return it == _list.end() ? 0 : &it->get();
}
}
template<class C>
const typename DivList<C>::Entry*
DivList<C>::findDivisor(const Monomial& monomial) const {
return const_cast<DivList<C>&>(*this).findDivisor(monomial);
}
template<class C>
template<class DO>
void DivList<C>::findAllDivisors(const Monomial& monomial, DO& out) {
ExtMonoRef extMonomial(monomial, _divMaskCalculator, _conf);
if (!_conf.getSortOnInsert()) {
const ListIter listEnd = _list.end();
for (ListIter it = _list.begin(); it != listEnd; ++it)
if (it->divides(extMonomial, _conf))
if (!out.proceed(it->get()))
break;
} else
DivListHelper::findAllDivisorsSorted(_conf, _list, extMonomial, out);
}
template<class C>
template<class DO>
void DivList<C>::findAllMultiples(const Monomial& monomial, DO& out) {
// todo: consider doing sorted version
ExtMonoRef extMonomial(monomial, _divMaskCalculator, _conf);
const ListIter listEnd = _list.end();
for (ListIter it = _list.begin(); it != listEnd; ++it)
if (extMonomial.divides(*it, _conf))
if (!out.proceed(it->get()))
break;
}
template<class C>
template<class DO>
void DivList<C>::findAllDivisors(const Monomial& monomial,
DO& output) const {
ConstEntryOutput<DO> constOutput(output);
const_cast<DivList<C>&>(*this).findAllDivisors(monomial, constOutput);
}
template<class C>
template<class EntryOutput>
void DivList<C>::forAll(EntryOutput& output) {
iterator stop = end();
for (iterator it = begin(); it != stop; ++it)
if (!output.proceed(*it))
return;
}
template<class C>
template<class EntryOutput>
void DivList<C>::forAll(EntryOutput& output) const {
ConstEntryOutput<EntryOutput> constOutput(output);
const_cast<DivList<C>&>(*this).forAll(constOutput);
}
template<class C>
std::string DivList<C>::getName() const {
std::ostringstream out;
out <<"DivList"
<< (UseLinkedList ? " linked" : " array");
if (UseDivMask && _conf.getDoAutomaticRebuilds()) {
out << " autob:" << _conf.getRebuildRatio()
<< '/' << _conf.getRebuildMin();
}
out << (_conf.getSortOnInsert() ? " sort" : "")
<< (UseDivMask ? " dmask" : "");
return out.str();
}
template<class C>
void DivList<C>::moveToFront(iterator pos) {
DivListHelper::moveToFront(_list, pos.getInternal());
}
template<class C>
void DivList<C>::rebuild() {
const size_t totalSize = size();
typedef memt::ArenaVector<Entry, true> TmpContainer;
TmpContainer tmpCopy(memt::Arena::getArena(), totalSize);
std::copy(begin(), end(), std::back_inserter<TmpContainer>(tmpCopy));
_divMaskCalculator.rebuild(tmpCopy.begin(), tmpCopy.end(), _conf);
ListIter listEnd = _list.end();
for (ListIter it = _list.begin(); it != listEnd; ++it)
it->recalculateDivMask(_divMaskCalculator, _conf);
resetNumberOfChangesTillRebuild();
}
template<class C>
void DivList<C>::resetNumberOfChangesTillRebuild() {
if (!_conf.getDoAutomaticRebuilds())
return;
MATHIC_ASSERT(_conf.getRebuildRatio() > 0);
_changesTillRebuild = std::max
(static_cast<size_t>(size() * _conf.getRebuildRatio()),
_conf.getRebuildMin());
}
template<class C>
void DivList<C>::reportChanges(size_t changesMadeCount) {
// note how negative value/overflow of _changesTillRebuild cannot
// happen this way.
if (!_conf.getDoAutomaticRebuilds())
return;
if (_changesTillRebuild > changesMadeCount)
_changesTillRebuild -= changesMadeCount;
else
rebuild();
}
template<class C>
size_t DivList<C>::getMemoryUse() const {
return _list.capacity() * sizeof(_list.front());
}
}
#endif
|