template class mrpt::math::CBinaryRelation
This class models a binary relation through the elements of any given set.
Internal classes not to be directly used by the user.
I.e. for each pair of elements (A,B) it assigns two values, f(A,B) and f(B,A). This class is useful when calling the base function is costly, since it acts like a proxy. It’s also useful if the relationship values do not correspond with the return value of a function. Although it theoretically supports objects with non-trivial constructors or destructors (indicated by specifying “true” as the thrid parameter of the template instantiation), certain operations will cause memory leaks and may even cause undefined behaviour, so it’s reccomended to use only basic types for the parameter U. The parameter T may be any complex object, however, like a smart pointer.
#include <mrpt/math/CBinaryRelation.h> template <typename T, typename U, bool UIsObject = false> class CBinaryRelation { public: // typedefs typedef U(*)(T, T) SimpleFunctionByReturnValue; typedef U(*)(const T&, const T&) FunctionByReturnValue; typedef void(*)(const T&, const T&, U&) FunctionByReferencePass; typedef typename std::set<T>::const_iterator const_iterator; typedef typename std::set<T>::const_reverse_iterator const_reverse_iterator; typedef CMatrixRowAccessor<U> AccessorForFirstElement; typedef CMatrixColumnAccessor<U> AccessorForSecondElement; typedef CConstMatrixRowAccessor<U> ConstAccessorForFirstElement; typedef CConstMatrixColumnAccessor<U> ConstAccessorForSecondElement; // construction CBinaryRelation(const std::set<T>& els); template <typename FunctionType> CBinaryRelation(const std::set<T>& els, FunctionType fun); // methods template <typename FunctionType> void initializeWith(FunctionType fun); template <typename FunctionType> void initializeSymmetricallyWith(FunctionType fun); void setRelationValue(size_t e1, size_t e2, const U& newVal); const U& getRelationValue(size_t e1, size_t e2) const; const U& operator () ( size_t e1, size_t e2 ) const; U& getRelationValue(size_t e1, size_t e2); U& operator () ( size_t e1, size_t e2 ); bool setRelationValue(const T& t1, const T& t2, const U& newVal); U getRelationValue(const T& t1, const T& t2) const; U& getRelationValue(const T& t1, const T& t2); const_iterator begin() const; const_iterator end() const; const_reverse_iterator rbegin() const; const_reverse_iterator rend() const; T operator [] (size_t i) const; AccessorForFirstElement getRelationFrom(size_t i); ConstAccessorForFirstElement getRelationFrom(size_t i) const; AccessorForSecondElement getRelationTo(size_t i); ConstAccessorForSecondElement getRelationTo(size_t i) const; AccessorForFirstElement getRelationFrom(const T& t); ConstAccessorForFirstElement getRelationFrom(const T& t) const; void getRelationFrom( size_t i, vector<U>& vec ); void getRelationFrom( const T& t, vector<U>& vec ); AccessorForSecondElement getRelationTo(const T& t); ConstAccessorForSecondElement getRelationTo(const T& t) const; void getRelationTo( size_t i, vector<U>& vec ); void getRelationTo( const T& t, vector<U>& vec ); void removeElementAt(size_t i); bool removeElement(const T& el); size_t removeElements(const std::set<T>& vals); void removeElementsAt(const std::set<size_t>& poss); std::pair<bool, size_t> insertElement(const T& el); template <typename FunctionType> std::pair<bool, size_t> insertElement( const T& el, FunctionType fun ); size_t insertElements(const std::set<T>& els); template <typename FunctionType> size_t insertElements( const std::set<T>& els, FunctionType fun ); void setElements(const std::set<T>& newEls); size_t size() const; };
Typedefs
typedef U(*)(T, T) SimpleFunctionByReturnValue
Simple function type, used to initialize chunks of the matrix.
typedef U(*)(const T&, const T&) FunctionByReturnValue
Function type which obtains the relation value by a return value.
typedef void(*)(const T&, const T&, U&) FunctionByReferencePass
Function type which obtains the relation value by reference pass.
typedef typename std::set<T>::const_iterator const_iterator
Constant iterator through the set elements.
typedef typename std::set<T>::const_reverse_iterator const_reverse_iterator
Constant reverse iterator through the set elements.
typedef CMatrixRowAccessor<U> AccessorForFirstElement
Accessor type to every value related to any element A, i.e., f(A,x).
typedef CMatrixColumnAccessor<U> AccessorForSecondElement
Accessor type to every value related to any element B, i.e., f(x,B).
typedef CConstMatrixRowAccessor<U> ConstAccessorForFirstElement
Const accessor type to every value related to any element A, i.e., f(A,x).
typedef CConstMatrixColumnAccessor<U> ConstAccessorForSecondElement
Const accessor type to every value related to any element B, i.e., f(x,B).
Construction
CBinaryRelation(const std::set<T>& els)
Default constructor, doesn’t initialize the relation.
template <typename FunctionType> CBinaryRelation( const std::set<T>& els, FunctionType fun )
Constructor which initializes the relation using a given function.
Methods
template <typename FunctionType> void initializeWith(FunctionType fun)
Initialize the whole relation with a given function.
template <typename FunctionType> void initializeSymmetricallyWith(FunctionType fun)
Initialize the whole relation with a given function, assuming that the relation is symmetrical.
void setRelationValue(size_t e1, size_t e2, const U& newVal)
Manually set a relationship value, given the indices.
const U& getRelationValue(size_t e1, size_t e2) const
Get a relation value, given the indices.
U& getRelationValue(size_t e1, size_t e2)
Get a reference to a relation value given its indices, which allows both querying and setting the value.
bool setRelationValue(const T& t1, const T& t2, const U& newVal)
Manually set a relationship value, given the elements.
Returns false if any of the elements is not present.
U getRelationValue(const T& t1, const T& t2) const
Get a relation value, given the elements.
Throws domain_error if any of the elements is not present.
U& getRelationValue(const T& t1, const T& t2)
Get a reference to a relation value given the elements, which allows both querying and setting.
Throws domain_error if any of the elements is not present.
const_iterator begin() const
Gets an iterator to the starting point of the elements set.
const_iterator end() const
Gets an iterator to the ending point of the elements set.
const_reverse_iterator rbegin() const
Gets a reverse iterator to the ending point of the elements set.
const_reverse_iterator rend() const
Gets a reverse iterator to the starting point of the elements set.
T operator [] (size_t i) const
Operator for direct access to a element given its index.
AccessorForFirstElement getRelationFrom(size_t i)
Gets an accessor for every value related to an element A given its index, i.e., every f(A,x).
This accessor is iterable.
ConstAccessorForFirstElement getRelationFrom(size_t i) const
Gets a constant accessor for every value related to an element A given its index, i.e., every f(A,x).
This accessor is iterable.
AccessorForSecondElement getRelationTo(size_t i)
Gets an accessor for every value related to an element B given its index, i.e., every f(x,B).
This accessor is iterable.
ConstAccessorForSecondElement getRelationTo(size_t i) const
Gets a constant accessor for every value related to an element B given its index, i.e., every f(x,B).
This accessor is fully iterable.
AccessorForFirstElement getRelationFrom(const T& t)
Gets an iterable accessor for every value related to an element A, i.e., every f(A,x).
A domain_error will be thrown if the element is not present.
ConstAccessorForFirstElement getRelationFrom(const T& t) const
Gets an iterable constant accessor for every value related to an element A, i.e., every f(A,x).
A domain_error will be thrown if the element is not present.
AccessorForSecondElement getRelationTo(const T& t)
Gets an iterable accessor for every value related to an element B, i.e., every f(x,B).
A domain_error will be thrown if the element is not present.
ConstAccessorForSecondElement getRelationTo(const T& t) const
Gets an iterable constant accessor for every value related to an alement B, i.e., every f(x,B).
A domain_error will be thrown if the element is not present.
void removeElementAt(size_t i)
Removes an element at a concrete position.
bool removeElement(const T& el)
Removes an element.
Returns false if the element was not present and thus could’nt be eliminated.
size_t removeElements(const std::set<T>& vals)
Removes a set of elements.
Returns the number of elements which were actually erased.
std::pair<bool, size_t> insertElement(const T& el)
Inserts an element.
If the element was present, returns false and its current position. If it wasn’t, returns true and the position in which it was inserted.
template <typename FunctionType> std::pair<bool, size_t> insertElement( const T& el, FunctionType fun )
Inserts an element and initializes its relationship values, even if it was already present.
size_t insertElements(const std::set<T>& els)
Inserts a set of elements into the relation.
Does not initialize the actual relation.
template <typename FunctionType> size_t insertElements( const std::set<T>& els, FunctionType fun )
Inserts a set of elements into the relation, initializing the actual relation with a given function.
void setElements(const std::set<T>& newEls)
Completely resets the relation, using a new set of elements.
Does not initialize the relation.
size_t size() const
Returns the amount of elements present in the relation.