9#ifndef CBINARYRELATION_H_
10#define CBINARYRELATION_H_
20namespace mrpt {
namespace math {
54 template<
typename FunctionType>
inline void applyFunction(FunctionType fun,
size_t e1,
size_t e2,
const T &T1,
const T &T2) {
55 detail::applyFunction<T,U,UIsObject,FunctionType>(*
this,fun,e1,e2,T1,T2);
73 typename std::set<T>::const_iterator it=
elements.begin();
74 for (
size_t i=0;i<
elements.size();++i,++it) {
75 typename std::set<T>::const_iterator jt=
elements.begin();
83 typename std::set<T>::const_iterator it=
elements.begin();
84 for (
size_t i=0;i<
elements.size();++i,++it) {
86 typename std::set<T>::const_iterator jt=it;
88 for (
size_t j=i+1;j<
elements.size();++j,++jt) {
123 typename std::set<T>::const_iterator it1=std::find(b,e,t1),it2=std::find(b,e,t2);
124 if (it1==e||it2==e)
return false;
125 setRelationValue(
static_cast<size_t>(std::distance(b,it1)),
static_cast<size_t>(std::distance(b,it2)),newVal);
133 typename std::set<T>::const_iterator it1=std::find(b,e,t1),it2=std::find(b,e,t2);
134 if (it1==e||it2==e)
throw std::domain_error(
"Element not found");
135 return getRelationValue(
static_cast<size_t>(std::distance(b,it1)),
static_cast<size_t>(std::distance(b,it2)));
143 typename std::set<T>::const_iterator it1=std::find(b,e,t1),it2=std::find(b,e,t2);
144 if (it1==e||it2==e)
throw std::domain_error(
"Element not found");
176 typename std::set<T>::const_iterator it=
elements.begin();
209 typename std::set<T>::const_iterator it=std::find(b,e,
t);
210 if (it==e)
throw std::domain_error(
"Element not found");
219 typename std::set<T>::const_iterator it=std::find(b,e,
t);
220 if (it==e)
throw std::domain_error(
"Element not found");
228 std::copy(access.
begin(),access.
end(),vec.begin());
232 typename std::set<T>::const_iterator it=std::find(b,e,
t);
233 if (it==e)
throw std::domain_error(
"Element not found");
241 typename std::set<T>::const_iterator it=std::find(b,e,
t);
242 if (it==e)
throw std::domain_error(
"Element not found");
243 return getRelationTo(
static_cast<size_t>(std::distance(b,it)));
251 typename std::set<T>::const_iterator it=std::find(b,e,
t);
252 if (it==e)
throw std::domain_error(
"Element not found");
253 return getRelationTo(
static_cast<size_t>(std::distance(b,it)));
260 std::copy(access.
begin(),access.
end(),vec.begin());
264 typename std::set<T>::const_iterator it=std::find(b,e,
t);
265 if (it==e)
throw std::domain_error(
"Element not found");
273 typename std::set<T>::const_iterator it=
elements.begin();
285 typename std::set<T>::const_iterator it=std::find(e,b,el);
286 if (it==e)
return false;
294 std::set<size_t> positions;
295 for (
typename std::set<T>::const_iterator it=vals.begin();it!=vals.end();++it) {
296 typename std::set<T>::iterator elsIt=std::find(
elements.begin(),
elements.end(),*it);
297 if (elsIt!=
elements.end()) positions.insert(std::distance(
elements.begin(),elsIt));
300 return positions.size();
303 relation.removeRowsAndCols(poss,poss);
304 for (std::set<size_t>::const_reverse_iterator it=poss.rbegin();it!=poss.rend();++it) {
305 typename std::set<T>::const_iterator it2=
elements.begin();
306 std::advance(it2,*it);
315 std::pair<typename std::set<T>::iterator,
bool> ins=
elements.insert(el);
316 size_t dist=std::distance(
elements.begin(),ins.first);
318 std::multiset<size_t> newEls;
320 relation.insertRowsAndCols(newEls,newEls);
321 return std::make_pair(
true,dist);
322 }
else return std::make_pair(
false,dist);
327 template<
typename FunctionType> std::pair<bool,size_t>
insertElement(
const T &el,FunctionType fun) {
329 size_t pos=ins.second;
330 for (
size_t i=0;i<
elements.size();++i) {
341 if (els.empty())
return 0;
345 std::vector<size_t> added;
347 added.reserve(els.size());
348 for (
typename std::set<T>::const_iterator it=els.begin();it!=els.end();++it) {
349 std::pair<typename std::set<T>::iterator,
bool> ins=
elements.insert(*it);
350 size_t dist=std::distance(
elements.begin(),ins.first);
352 added.push_back(dist);
353 for (std::vector<size_t>::iterator it2=added.begin();it2!=added.end();++it2)
if (*it2>=dist) ++(*it2);
357 std::sort(added.begin(),added.end());
358 for (
size_t j=1;j<added.size();++j) added[j]-=j;
359 std::multiset<size_t> poss(added.begin(),added.end());
360 relation.insertRowsAndCols(poss,poss);
366 template<
typename FunctionType>
size_t insertElements(
const std::set<T> &els,FunctionType fun) {
367 if (els.empty())
return 0;
369 std::set<size_t> poss;
373 for (
typename std::set<T>::const_iterator it=els.begin();it!=els.end();++it) poss.insert(std::distance(
begin,find(
begin,
end,*it)));
375 std::set<size_t> nPoss;
376 std::set<size_t>::const_iterator
begin=poss.begin(),
end=poss.end();
378 vector<const T *> proxy;
379 proxy.reserve(poss.size());
380 for (std::set<size_t>::const_iterator it=
begin;it!=
end;++it) {
382 proxy.push_back(&e1);
386 for (std::set<size_t>::const_iterator it=nPoss.begin();it!=nPoss.end();++it) {
388 typename std::vector<const T *>::const_iterator itV=proxy.begin();
389 for (std::set<size_t>::const_iterator it2=poss.begin();it2!=poss.end();++it2,++itV)
applyFunction(fun,*it,*it2,e1,**itV);
399 relation.setSize(newEls.size(),newEls.size());
411 template<
typename T,
typename U,
bool UIsObject,
typename FunctionType>
inline void applyFunction(
CBinaryRelation<T,U,UIsObject> &o, FunctionType fun,
size_t e1,
size_t e2,
const T &T1,
const T &T2) {
417 template<
typename T,
typename U,
bool UIsObject>
inline void applyFunction(
CBinaryRelation<T,U,UIsObject> &o,
typename CBinaryRelation<T,U,UIsObject>::FunctionByReferencePass fun,
size_t e1,
size_t e2,
const T &T1,
const T &T2) {
This class models a binary relation through the elements of any given set.
void setRelationValue(size_t e1, size_t e2, const U &newVal)
Manually set a relationship value, given the indices.
AccessorForSecondElement getRelationTo(const T &t)
Gets an iterable accessor for every value related to an element B, i.e., every f(x,...
void initializeWith(FunctionType fun)
Initialize the whole relation with a given function.
CConstMatrixRowAccessor< U > ConstAccessorForFirstElement
Const accessor type to every value related to any element A, i.e., f(A,x).
ConstAccessorForSecondElement getRelationTo(size_t i) const
Gets a constant accessor for every value related to an element B given its index, i....
void initializeSymmetricallyWith(FunctionType fun)
Initialize the whole relation with a given function, assuming that the relation is symmetrical.
AccessorForFirstElement getRelationFrom(size_t i)
Gets an accessor for every value related to an element A given its index, i.e., every f(A,...
U getRelationValue(const T &t1, const T &t2) const
Get a relation value, given the elements.
CConstMatrixColumnAccessor< U > ConstAccessorForSecondElement
Const accessor type to every value related to any element B, i.e., f(x,B).
U & getRelationValue(const T &t1, const T &t2)
Get a reference to a relation value given the elements, which allows both querying and setting.
const_reverse_iterator rend() const
Gets a reverse iterator to the starting point of the elements set.
AccessorForSecondElement getRelationTo(size_t i)
Gets an accessor for every value related to an element B given its index, i.e., every f(x,...
bool removeElement(const T &el)
Removes an element.
ConstAccessorForSecondElement getRelationTo(const T &t) const
Gets an iterable constant accessor for every value related to an alement B, i.e., every f(x,...
MatrixType relation
Matrix storing the relation.
CMatrixRowAccessor< U > AccessorForFirstElement
Accessor type to every value related to any element A, i.e., f(A,x).
CBinaryRelation(const std::set< T > &els)
Default constructor, doesn't initialize the relation.
void getRelationFrom(size_t i, vector< U > &vec)
void applyFunction(FunctionType fun, size_t e1, size_t e2, const T &T1, const T &T2)
Template used to make the function interface independent from the function type.
void getRelationTo(const T &t, vector< U > &vec)
CBinaryRelation(const std::set< T > &els, FunctionType fun)
Constructor which initializes the relation using a given function.
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 val...
U(* SimpleFunctionByReturnValue)(T, T)
Simple function type, used to initialize chunks of the matrix.
std::set< T >::const_reverse_iterator const_reverse_iterator
Constant reverse iterator through the set elements.
detail::MatrixWrapper< U, UIsObject >::MatrixType MatrixType
Matrix type used to store the actual relation.
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.
const_iterator begin() const
Gets an iterator to the starting point of the elements set.
void getRelationTo(size_t i, vector< U > &vec)
std::set< T > elements
Actual set of elements.
CMatrixColumnAccessor< U > AccessorForSecondElement
Accessor type to every value related to any element B, i.e., f(x,B).
size_t size() const
Returns the amount of elements present in the relation.
ConstAccessorForFirstElement getRelationFrom(const T &t) const
Gets an iterable constant accessor for every value related to an element A, i.e., every f(A,...
const_reverse_iterator rbegin() const
Gets a reverse iterator to the ending point of the elements set.
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.
size_t removeElements(const std::set< T > &vals)
Removes a set of elements.
bool setRelationValue(const T &t1, const T &t2, const U &newVal)
Manually set a relationship value, given the elements.
std::pair< bool, size_t > insertElement(const T &el)
Inserts an element.
const U & operator()(size_t e1, size_t e2) const
const U & getRelationValue(size_t e1, size_t e2) const
Get a relation value, given the indices.
std::set< T >::const_iterator const_iterator
Constant iterator through the set elements.
T operator[](size_t i) const
Operator for direct access to a element given its index.
ConstAccessorForFirstElement getRelationFrom(size_t i) const
Gets a constant accessor for every value related to an element A given its index, i....
void getRelationFrom(const T &t, vector< U > &vec)
U(* FunctionByReturnValue)(const T &, const T &)
Function type which obtains the relation value by a return value.
void(* FunctionByReferencePass)(const T &, const T &, U &)
Function type which obtains the relation value by reference pass.
size_t insertElements(const std::set< T > &els)
Inserts a set of elements into the relation.
const_iterator end() const
Gets an iterator to the ending point of the elements set.
void removeElementsAt(const std::set< size_t > &poss)
void removeElementAt(size_t i)
Removes an element at a concrete position.
AccessorForFirstElement getRelationFrom(const T &t)
Gets an iterable accessor for every value related to an element A, i.e., every f(A,...
void setElements(const std::set< T > &newEls)
Completely resets the relation, using a new set of elements.
U & operator()(size_t e1, size_t e2)
A vector-like wrapper for a const Matrix for accessing the elements of a given column with a [] opera...
const_iterator end() const
const_iterator begin() const
A vector-like wrapper for a const Matrix for accessing the elements of a given row with a [] operator...
const_iterator end() const
const_iterator begin() const
A vector-like wrapper for a Matrix for accessing the elements of a given column with a [] operator.
A vector-like wrapper for a Matrix for accessing the elements of a given row with a [] operator.
This template is a trick to switch the type of a variable using a boolean variable in the template.
EIGEN_STRONG_INLINE const AdjointReturnType t() const
Transpose.
double BASE_IMPEXP distance(const TPoint2D &p1, const TPoint2D &p2)
Gets the distance between two points in a 2D space.
#define ASSERT_BELOW_(__A, __B)
void applyFunction(CBinaryRelation< T, U, UIsObject > &o, FunctionType fun, size_t e1, size_t e2, const T &T1, const T &T2)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.