/usr/include/BALL/CONCEPT/enumerator.h is in libball1.4-dev 1.4.3~beta1-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 | // -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_CONCEPT_ENUMERATOR_H
#define BALL_CONCEPT_ENUMERATOR_H
#ifndef BALL_COMMON_H
# include <BALL/common.h>
#endif
#ifndef BALL_COMMON_EXCEPTION_H
# include <BALL/COMMON/exception.h>
#endif
#ifndef BALL_COMMON_RTTI_H
# include <BALL/COMMON/rtti.h>
#endif
#ifndef BALL_CONCEPT_FORWARDITERATOR_H
# include <BALL/CONCEPT/forwardIterator.h>
#endif
#include <vector>
#include <list>
#include <algorithm>
namespace BALL
{
/** EnumeratorIndex class.
The Enumerator class provides a means for enumerating all possible
combinations of objects. The counting is hereby done by the
EnumeratorIndex class. This class is derived from vector and
additionally contains two vectors of the same size which hold the
modulus and the base for each digit. The values of the EnumeratorIndex
vector itself are interpreted as an inhomogenous number consisting of
digits that have different bases. The bases are the numbers of
possibilities for each variant in the list. Most significant component
is operator [] (0), so incrementing starts at operator [] (size() - 1)
\par
\ingroup ConceptsMiscellaneous
*/
class BALL_EXPORT EnumeratorIndex
: private std::vector<Position>
{
public:
/** @name Nested Classes
*/
//@{
/** Exception for reporting incompatible EnumeratorIndex instances,
i.e. instances with different moduli.
*/
class BALL_EXPORT IncompatibleIndex
: public Exception::GeneralException
{
public:
IncompatibleIndex(const char* file, int line);
};
//@}
/** @name Constructors and Destructors
*/
//@{
/** Default Constructor
*/
EnumeratorIndex();
/** Detailed Constructor
@param variant_list the list of variants to be applied
*/
template <typename Variant, typename VariantIterator>
EnumeratorIndex(const std::list<std::pair<VariantIterator, std::vector<Variant> > >& variant_list);
/** Destructor
*/
~EnumeratorIndex();
//@}
/** @name Assignment
*/
//@{
/** Assignment operator.
*/
const EnumeratorIndex& operator = (const EnumeratorIndex& rhs);
/** Assignment operator for Position.
* \throws Exception::IndexOverflow
*/
const EnumeratorIndex& operator = (Position index);
/** Assignment operator for a variant list.
*/
template <typename Variant, typename VariantIterator>
const EnumeratorIndex& operator = (const std::list<std::pair<VariantIterator, std::vector<Variant> > >& variant_list);
//@}
/** @name Accessors
*/
//@{
/** Access the modulus part of the enumerator
*/
const std::vector<Size>& getModulus() const;
/** Access the modulus of a specific position
*/
Size getModulus(Position pos) const;
/** Increment an instance of EnumeratorIndex.
Increment the least significant component and apply any overflow to
more signficant components.
\throws Exception::IndexOverflow
*/
EnumeratorIndex& operator ++ ();
/** Decrement an instance of EnumeratorIndex.
Decrement the least significant component and
apply any underflow to more signficant components.
\throws Exception::IndexUnderflow
*/
EnumeratorIndex& operator -- ();
/** Random access operator
*/
Position operator [] (Position pos) const;
/** Random access operator
*/
Position& operator [] (Position pos);
/** Return the size of the array.
*/
Size getSize() const;
/** Add a new digit.
Push the modulus to the ack of the modulus array and
initialize the corresponding digit with zero.
\par
<b>Example:</b> To construct a hex counter with three
digits, you can use something like:
<tt> EnumerationIndex counter;</tt>\par
<tt> counter << 16 << 16 << 16;</tt>\par
The counter will be initialized with all zeros.
@exception OutOfRange if the modulus is less than 2
*/
EnumeratorIndex& operator << (Size modulus);
//@}
/** @name Predicates for EnumeratorIndex class
*/
//@{
/** Equality operator.
* @param rhs
* @return true, if <tt>rhs</tt> is equal to <tt>this</tt> instance
*/
bool operator == (const EnumeratorIndex& rhs) const;
/** Inequality operator.
* @param rhs
* @return true, if <tt>rhs</tt> is not equal to <tt>this</tt> instance
*/
bool operator != (const EnumeratorIndex& rhs) const;
/** Greater than operator.
* @param rhs
* @return true, if <tt>rhs</tt> is greater than <tt>this</tt> instance
@exception EnumeratorIndex::Incompatible index if the modulus vector differs, i.e. the two indices aren't comparable
*/
bool operator > (const EnumeratorIndex& rhs) const;
/** Lesser than operator.
* @param rhs
* @return true, if <tt>rhs</tt> is lesser than <tt>this</tt> instance
* @exception EnumeratorIndex::Incompatible index if the modulus vector differs, i.e. the two indices aren't comparable
*/
bool operator < (const EnumeratorIndex& rhs) const;
/** Greater or equal operator.
* @param rhs
* @return true, if <tt>rhs</tt> is greater than or equal to <tt>this</tt> instance
* @exception EnumeratorIndex::Incompatible index if the modulus vector differs, i.e. the two indices aren't comparable
*/
bool operator >= (const EnumeratorIndex& rhs) const;
/** Lesser or equal operator.
* @param rhs
* @return true, if <tt>rhs</tt> is lesser than or equal to <tt>this</tt> instance
* @exception EnumeratorIndex::Incompatible index if the modulus vector differs, i.e. the two indices aren't comparable
*/
bool operator <= (const EnumeratorIndex& rhs) const;
//@}
private:
///
std::vector<Size> modulus_;
///
std::vector<Size> base_multipliers_;
};
template <typename Variant, typename VariantIterator>
EnumeratorIndex::EnumeratorIndex(const std::list<std::pair<VariantIterator, std::vector<Variant> > >& variant_list)
: std::vector<Position>(variant_list.size()),
modulus_(variant_list.size()),
base_multipliers_(variant_list.size())
{
this->operator = (variant_list);
}
template <typename Variant, typename VariantIterator>
const EnumeratorIndex& EnumeratorIndex::operator =
(const std::list<std::pair<VariantIterator, std::vector<Variant> > >& variant_list)
{
resize(variant_list.size());
modulus_.resize(variant_list.size());
base_multipliers_.resize(variant_list.size());
// compute the base multipliers for later usage
Index i;
Size multiplier = 1;
typename std::list<std::pair<VariantIterator, std::vector<Variant> > >::const_iterator list_it = variant_list.begin();
for (i = (Size)(size() - 1); i >= 0; i--, list_it++)
{
operator[](i) = 0;
modulus_[i] = (Size)list_it->second.size();
base_multipliers_[i] = multiplier;
multiplier *= modulus_[i];
}
return *this;
}
/** Enumerator class.
The EnumeratorIndex class is designed to enumerate all possible
combinations of things. Applications are e.g. the enumeration of
all possible sequences defined through a multisequence or
enumerating all possible rotamers of a peptide or a bindings site.
\par
Enumerator uses \link EnumeratorIndex EnumeratorIndex \endlink as an inhomogeneous counter
class. It is also highly templatized in order to be adaptable to
most problem instances. In general, the enumeration problem can
be seen as counting with a mixed-base number. For an example of
the Enumerator's usage, please refer to the tutorial.
\par
The Enumerator's template arguments are
- the <tt>Container</tt>
it operates on (e.g. the \link String String \endlink representing the sequence
or the protein containing the amino acids)
- the <tt>SiteIterator</tt> (i.e. an iterator pointing to a
defined position within the container)
- the <tt>Variant</tt> type (i.e. the type of the object to be enumerated).
It has to be the same type as the dereferenced <tt>SiteIterator</tt>.
In the case of a string sequence that has to be mutated, the <tt>Container</tt>
is of class \link String String \endlink , the <tt>SiteIterator</tt> is of type
<tt>String::Iterator</tt>, and <tt>Variant</tt> is obviously of type <tt>char</tt>.
\par
*/
template <class Container, class SiteIterator, class Variant>
class Enumerator
{
protected:
class IteratorTraits_;
public:
/** @name Type definitions
*/
//@{
/** Site mutator function.
A function used to assing one variant of a site in the
container to another. In the trivial case, this is just
the assignment operator (as implemented in the default case),
but more involved enumeration problems (e.g. side chain
rotamer enumeration) might require additional code for the
assignment of the true variant.
*/
typedef void (*MutatorFunction) (Variant&, const Variant&);
/** A vector containing all possible variants for a specific site.
*/
typedef std::vector<Variant> VariantVector;
/** A combination of a site position and all its possible variants.
*/
typedef std::pair<SiteIterator, VariantVector> Site;
/** A list of sites.
*/
typedef std::list<Site> SiteList;
/** Mutable forward iterator
*/
typedef ForwardIterator<Enumerator<Container, SiteIterator, Variant>, Container, EnumeratorIndex*, IteratorTraits_>
Iterator;
/** Constant forward iterator
*/
typedef ConstForwardIterator<Enumerator<Container, SiteIterator, Variant>, Container, EnumeratorIndex*, IteratorTraits_>
ConstIterator;
//@}
/** @name Constructors and Destructors
*/
//@{
/** Default Constructor
*/
Enumerator();
/** Detailed Constructor.
The mutator function is set to a default mutator,
using the assignment operator for <tt>Variant</tt>.
@param container a Container class to be mutated
*/
Enumerator(Container& container);
/** Detailed Constructor
@param container a Container class to be mutated
@param mutator the function defining the mutations to be applied
*/
Enumerator(Container& container, MutatorFunction mutator);
/** Default Destructor
*/
~Enumerator()
{
}
//@}
/** @name Accessors
*/
//@{
/** Add variants to the list of variants
*/
void addVariants(const SiteIterator& it, const VariantVector& variants)
{
variant_sites_.push_back(Site(it, variants));
position_ = variant_sites_;
}
/** Delete variants from the list of variants
*/
void deleteVariants(const SiteIterator& it, const VariantVector& variants)
{
typename SiteList::iterator var_it;
var_it = std::find(variant_sites_.begin(), variant_sites_.end(), Site(it, variants));
if (var_it != variant_sites_.end())
{
variant_sites_.erase(var_it);
}
position_ = variant_sites_;
}
/** Count all variants.
@return the number of all possible variants
*/
Size countVariants()
{
Size total = 1;
typename SiteList::iterator it;
for (it = variant_sites_.begin(); it != variant_sites_.end(); ++it)
{
total *= it->second.size();
}
return total;
}
/** Access the current content
@return a reference to the container class of this enumerator
*/
Container& getCurrent();
/** Create a combination denoted by its number.
@param index the number of the combination to be created
\throws Exception::IndexOverflow
*/
void createCombination(const Position index);
/** Create a combination denoted by an instance of EnumeratorIndex.
@param index the instance of EnumeratorIndex that describes the
combination to be created
\throws EnumeratorIndex::IncompatibleIndex
*/
void createCombination(const EnumeratorIndex& index);
//@}
/** @name Iterators
*/
//@{
///
Iterator begin();
///
Iterator end();
///
ConstIterator begin() const;
///
ConstIterator end() const;
//@}
protected:
friend class IteratorTraits_;
/**
*/
class IteratorTraits_
{
friend class Enumerator<Container, SiteIterator, Variant>;
public:
typedef Enumerator<Container, SiteIterator, Variant>
ContainerType;
typedef Enumerator<Container, SiteIterator, Variant>*
ContainerPointer;
typedef const Enumerator<Container, SiteIterator, Variant>*
ContainerConstPointer;
typedef EnumeratorIndex
IteratorPosition;
typedef Container
ValueType;
IteratorTraits_()
: bound_(0),
position_(),
past_the_end_(false)
{
}
IteratorTraits_(const ContainerType& enumerator)
: bound_(const_cast<ContainerPointer>(&enumerator)),
position_(enumerator.variant_sites_),
past_the_end_(false)
{
}
ContainerConstPointer getContainer() const
{
return bound_;
}
ContainerPointer getContainer()
{
return bound_;
}
bool isSingular() const
{
return (bound_ == 0);
}
IteratorPosition& getPosition()
{
return position_;
}
const IteratorPosition& getPosition() const
{
return position_;
}
bool operator == (const IteratorTraits_& traits) const
{
return ((bound_ == traits.bound_) && (position_ == traits.position_) && (past_the_end_ == traits.past_the_end_));
}
bool operator != (const IteratorTraits_& traits) const
{
return ((bound_ != traits.bound_) || (position_ != traits.position_) || (past_the_end_ != traits.past_the_end_));
}
bool isValid() const
{
return (bound_ != 0);
}
void invalidate()
{
bound_ = 0;
position_ = 0;
past_the_end_ = false;
}
void toBegin()
{
position_ = 0;
past_the_end_ = false;
}
bool isBegin() const
{
return (position_ == EnumeratorIndex()) && (past_the_end_ == false);
}
void toEnd()
{
position_ = 0;
past_the_end_ = true;
}
bool isEnd() const
{
return past_the_end_;
}
ValueType& getData()
{
validate();
return bound_->getCurrent();
}
const ValueType& getData() const
{
// This is logically const only!
const_cast<typename Enumerator<Container, SiteIterator, Variant>::IteratorTraits_*>(this)->validate();
return bound_->getCurrent();
}
void forward()
{
try
{
++position_;
}
catch (Exception::IndexOverflow&)
{
past_the_end_ = true;
position_ = 0;
}
}
void validate()
{
if (!bound_->is_valid_position_
|| (position_ != bound_->position_))
{
bound_->createCombination(position_);
}
}
protected:
ContainerPointer bound_;
EnumeratorIndex position_;
bool past_the_end_;
};
// the default mutation method (calling asignment operator)
static inline void defaultAssign_(Variant& a, const Variant& b)
{
a = b;
}
void mutate_(SiteIterator& it, const Variant& v)
{
mutator_(*it, v);
}
Container& container_;
MutatorFunction mutator_;
SiteList variant_sites_;
EnumeratorIndex position_;
bool is_valid_position_;
};
template <typename Container, typename SiteIterator, typename Variant>
Enumerator<Container, SiteIterator, Variant>::Enumerator()
: container_(const_cast<Container&>(RTTI::getDefault<Container>())),
mutator_(0)
{
}
template <typename Container, typename SiteIterator, typename Variant>
Enumerator<Container, SiteIterator, Variant>::Enumerator(Container& container)
: container_(container),
mutator_(defaultAssign_)
{
}
template <typename Container, typename SiteIterator, typename Variant>
BALL_INLINE
Enumerator<Container, SiteIterator, Variant>::Enumerator
(Container& container, typename Enumerator<Container, SiteIterator, Variant>::MutatorFunction mutator)
: container_(container),
mutator_(mutator)
{
}
template <typename Container, typename SiteIterator, typename Variant>
BALL_INLINE
Container& Enumerator<Container, SiteIterator, Variant>::getCurrent()
{
return container_;
}
template <typename Container, typename SiteIterator, typename Variant>
void Enumerator<Container, SiteIterator, Variant>::createCombination(const Position index)
{
try
{
position_ = index;
createCombination(position_);
}
catch (EnumeratorIndex::IncompatibleIndex&)
{
throw Exception::IndexOverflow(__FILE__, __LINE__, index);
}
}
template <typename Container, typename SiteIterator, typename Variant>
void Enumerator<Container, SiteIterator, Variant>::createCombination(const EnumeratorIndex& index)
{
if (&index != &position_)
{
position_ = index;
}
typename SiteList::iterator it = variant_sites_.begin();
Position i((Position)(index.getSize() - 1));
for (; it != variant_sites_.end(); ++it, --i)
{
mutate_(it->first, it->second[index[i]]);
}
is_valid_position_ = true;
}
template <typename Container, typename SiteIterator, typename Variant>
BALL_INLINE
typename Enumerator<Container, SiteIterator, Variant>::Iterator Enumerator<Container, SiteIterator, Variant>::begin()
{
return Iterator::begin(*this);
}
template <typename Container, typename SiteIterator, typename Variant>
BALL_INLINE
typename Enumerator<Container, SiteIterator, Variant>::Iterator Enumerator<Container, SiteIterator, Variant>::end()
{
return Iterator::end(*this);
}
template <typename Container, typename VariantConstIterator, typename Variant>
BALL_INLINE
typename Enumerator<Container, VariantConstIterator, Variant>::ConstIterator Enumerator<Container, VariantConstIterator, Variant>::begin() const
{
return ConstIterator::begin(*this);
}
template <typename Container, typename VariantConstIterator, typename Variant>
BALL_INLINE
typename Enumerator<Container, VariantConstIterator, Variant>::ConstIterator Enumerator<Container, VariantConstIterator, Variant>::end() const
{
return ConstIterator::end(*this);
}
# ifndef BALL_NO_INLINE_FUNCTIONS
# include <BALL/CONCEPT/enumerator.iC>
# endif
}
#endif // BALL_CONCEPT_ENUMERATOR_H
|