fpmas 1.6
Public Member Functions | List of all members
fpmas::model::Neighbors< AgentType > Class Template Reference

#include <model.h>

Public Member Functions

 Neighbors (const std::vector< Neighbor< AgentType > > &neighbors)
 
iterator begin ()
 
const_iterator begin () const
 
iterator end ()
 
const_iterator end () const
 
std::size_t count () const
 
bool empty () const
 
Neighbor< AgentType > & at (std::size_t i)
 
const Neighbor< AgentType > & at (std::size_t i) const
 
Neighbor< AgentType > & operator[] (std::size_t i)
 
const Neighbor< AgentType > & operator[] (std::size_t i) const
 
template<typename Gen >
Neighborsshuffle (Gen &gen)
 
Neighborsshuffle ()
 
template<typename Compare = std::less<AgentType>>
Neighborssort (Compare comp=Compare())
 
template<typename Filter >
Neighborsfilter (Filter _filter)
 
template<typename Gen >
Neighbor< AgentType > random (Gen &gen)
 
Neighbor< AgentType > random ()
 
template<typename Gen >
const Neighbor< AgentType > random (Gen &gen) const
 
const Neighbor< AgentType > random () const
 

Detailed Description

template<typename AgentType>
class fpmas::model::Neighbors< AgentType >

Helper class used to represent Agents neighbors.

Neighbors are defined as Agents contained in neighbors of the node associated to a given Agent.

This class allows to easily iterate over Agents neighbors thanks to predefined iterators and the shuffle() method.

Template Parameters
AgentTypeType of agents in the neighbors set

Constructor & Destructor Documentation

◆ Neighbors()

template<typename AgentType >
fpmas::model::Neighbors< AgentType >::Neighbors ( const std::vector< Neighbor< AgentType > > &  neighbors)
inline

Neighbors constructor.

Parameters
neighborslist of agent neighbors

Member Function Documentation

◆ begin() [1/2]

template<typename AgentType >
iterator fpmas::model::Neighbors< AgentType >::begin ( )
inline

Begin iterator.

Returns
begin iterator

◆ begin() [2/2]

template<typename AgentType >
const_iterator fpmas::model::Neighbors< AgentType >::begin ( ) const
inline

Const begin iterator.

Returns
const begin iterator

◆ end() [1/2]

template<typename AgentType >
iterator fpmas::model::Neighbors< AgentType >::end ( )
inline

End iterator.

Returns
end iterator

◆ end() [2/2]

template<typename AgentType >
const_iterator fpmas::model::Neighbors< AgentType >::end ( ) const
inline

Const end iterator.

Returns
const end iterator

◆ count()

template<typename AgentType >
std::size_t fpmas::model::Neighbors< AgentType >::count ( ) const
inline

Returns the count of neighbors in this list.

Returns
neighbors count

◆ empty()

template<typename AgentType >
bool fpmas::model::Neighbors< AgentType >::empty ( ) const
inline

Checks if this neighbors list is empty.

Returns
true iff there is no neighbor in this neighbor list

◆ at() [1/2]

template<typename AgentType >
Neighbor< AgentType > & fpmas::model::Neighbors< AgentType >::at ( std::size_t  i)
inline

Returns a reference to the neighbor at position i, with bounds checking.

Parameters
ineighbor position
Exceptions
std::out_of_rangeif !(i < count())

◆ at() [2/2]

template<typename AgentType >
const Neighbor< AgentType > & fpmas::model::Neighbors< AgentType >::at ( std::size_t  i) const
inline

Returns a reference to the neighbor at position i, with bounds checking.

Parameters
ineighbor position
Exceptions
std::out_of_rangeif !(i < count())

◆ operator[]() [1/2]

template<typename AgentType >
Neighbor< AgentType > & fpmas::model::Neighbors< AgentType >::operator[] ( std::size_t  i)
inline

Returns a reference to the neighbor at position i, without bounds checking.

Parameters
ineighbor position

◆ operator[]() [2/2]

template<typename AgentType >
const Neighbor< AgentType > & fpmas::model::Neighbors< AgentType >::operator[] ( std::size_t  i) const
inline

Returns a reference to the neighbor at position i, without bounds checking.

Parameters
ineighbor position

◆ shuffle() [1/2]

template<typename AgentType >
template<typename Gen >
Neighbors & fpmas::model::Neighbors< AgentType >::shuffle ( Gen &  gen)
inline

Internally shuffles the neighbors list using std::shuffle and the provided random number generator.

Can be used to iterate randomly over the neighbors set.

Returns
reference to this Neighbors instance

◆ shuffle() [2/2]

template<typename AgentType >
Neighbors & fpmas::model::Neighbors< AgentType >::shuffle ( )
inline

Shuffles agent using the distributed RandomNeighbors::rd random number generator.

See also
shuffle(Gen&)
Returns
reference to this Neighbors instance

◆ sort()

template<typename AgentType >
template<typename Compare = std::less<AgentType>>
Neighbors & fpmas::model::Neighbors< AgentType >::sort ( Compare  comp = Compare())
inline

Sorts the neighbor list using the specified comparator.

By default, the comparison is performed on neighbors agents using the operator< on AgentType, that can be user defined.

In consequence, neighbors are sorted is ascending order by default. A custom comparator based on the operator> can be specified to sort neighbros in descending order.

En efficient way of defining other comparison functions is the usage of lambda functions:

void agent_behavior() {
auto neighbors = this->outNeighbors<UserAgent>();
neighbors.sort([] (const UserAgent& a1, const UserAgent& a2) {
return a1.getField() < a2.getField();
});
}

More generally, Compare must be a type that defines a method with the following signature:

bool operator()(const AgentType& a1, const AgentType& a2) const;
Template Parameters
Comparecomparator type
Parameters
compcomparator instance

◆ filter()

template<typename AgentType >
template<typename Filter >
Neighbors & fpmas::model::Neighbors< AgentType >::filter ( Filter  _filter)
inline

Filters the neighbor list.

The specified Filter must be an unary predicate such that for each agent in the current neighbor list, agent is kept only if _filter(agent) returns true.

Parameters
_filterfilter to apply
Returns
filtered list

◆ random() [1/4]

template<typename AgentType >
template<typename Gen >
Neighbor< AgentType > fpmas::model::Neighbors< AgentType >::random ( Gen &  gen)
inline

Selects a random element of this neighbors list, using the specified random number generator.

Fails if count() == 0.

Returns
random neighbor

◆ random() [2/4]

template<typename AgentType >
Neighbor< AgentType > fpmas::model::Neighbors< AgentType >::random ( )
inline

Selects a ramdom element of this neighbors list, using the distributed RandomNeighbors::rd random number generator.

See also
random(Gen&)
Returns
random neighbor

◆ random() [3/4]

template<typename AgentType >
template<typename Gen >
const Neighbor< AgentType > fpmas::model::Neighbors< AgentType >::random ( Gen &  gen) const
inline

Selects a random element of this neighbors list, using the specified random number generator.

Fails if count() == 0.

Returns
random neighbor

◆ random() [4/4]

template<typename AgentType >
const Neighbor< AgentType > fpmas::model::Neighbors< AgentType >::random ( ) const
inline

Selects a ramdom element of this neighbors list, using the distributed RandomNeighbors::rd random number generator.

See also
random(Gen&)
Returns
random neighbor

The documentation for this class was generated from the following file: