Template Class CBinaryRelation

Class Documentation

template<typename T, typename U, bool UIsObject = false>
class CBinaryRelation

This class models a binary relation through the elements of any given set. 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.

Internal classes not to be directly used by the user.

Public Types

using SimpleFunctionByReturnValue = U (*)(T, T)

Simple function type, used to initialize chunks of the matrix.

using FunctionByReturnValue = U (*)(const T&, const T&)

Function type which obtains the relation value by a return value.

using FunctionByReferencePass = void (*)(const T&, const T&, U&)

Function type which obtains the relation value by reference pass.

using const_iterator = typename std::set<T>::const_iterator

Constant iterator through the set elements.

using const_reverse_iterator = typename std::set<T>::const_reverse_iterator

Constant reverse iterator through the set elements.

using AccessorForFirstElement = CMatrixRowAccessor<U>

Accessor type to every value related to any element A, i.e., f(A,x).

using AccessorForSecondElement = CMatrixColumnAccessor<U>

Accessor type to every value related to any element B, i.e., f(x,B).

using ConstAccessorForFirstElement = CConstMatrixRowAccessor<U>

Const accessor type to every value related to any element A, i.e., f(A,x).

using ConstAccessorForSecondElement = CConstMatrixColumnAccessor<U>

Const accessor type to every value related to any element B, i.e., f(x,B).

Public Functions

inline explicit CBinaryRelation(const std::set<T> &els)

Default constructor, doesn’t initialize the relation.

template<typename FunctionType>
inline CBinaryRelation(const std::set<T> &els, FunctionType fun)

Constructor which initializes the relation using a given function.

template<typename FunctionType>
inline void initializeWith(FunctionType fun)

Initialize the whole relation with a given function.

template<typename FunctionType>
inline void initializeSymmetricallyWith(FunctionType fun)

Initialize the whole relation with a given function, assuming that the relation is symmetrical.

inline void setRelationValue(size_t e1, size_t e2, const U &newVal)

Manually set a relationship value, given the indices.

inline const U &getRelationValue(size_t e1, size_t e2) const

Get a relation value, given the indices.

inline const U &operator()(size_t e1, size_t e2) const
inline 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.

inline U &operator()(size_t e1, size_t e2)
inline 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.

inline 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.

inline 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.

inline const_iterator begin() const

Gets an iterator to the starting point of the elements set.

inline const_iterator end() const

Gets an iterator to the ending point of the elements set.

inline const_reverse_iterator rbegin() const

Gets a reverse iterator to the ending point of the elements set.

inline const_reverse_iterator rend() const

Gets a reverse iterator to the starting point of the elements set.

inline T operator[](size_t i) const

Operator for direct access to a element given its index.

inline 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.

inline 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.

inline 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.

inline 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.

inline 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.

inline 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.

inline void getRelationFrom(size_t i, vector<U> &vec)
inline void getRelationFrom(const T &t, vector<U> &vec)
inline 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.

inline 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.

inline void getRelationTo(size_t i, vector<U> &vec)
inline void getRelationTo(const T &t, vector<U> &vec)
inline void removeElementAt(size_t i)

Removes an element at a concrete position.

inline bool removeElement(const T &el)

Removes an element. Returns false if the element was not present and thus could’nt be eliminated.

inline size_t removeElements(const std::set<T> &vals)

Removes a set of elements. Returns the number of elements which were actually erased.

inline void removeElementsAt(const std::set<size_t> &poss)
inline 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>
inline 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.

inline 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>
inline 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.

inline void setElements(const std::set<T> &newEls)

Completely resets the relation, using a new set of elements. Does not initialize the relation.

inline size_t size() const

Returns the amount of elements present in the relation.