Skip to content

Class NeighborsView

template <typename TSeq>

ClassList > NeighborsView

Non-allocating range over an agent's neighbors. More...

  • #include <agent-bones.hpp>

Classes

Type Name
class iterator

Public Functions

Type Name
NeighborsView () = default
NeighborsView (const size_t * first, size_t n, std::vector< Agent< TSeq > > * pop)
Agent< TSeq > * agent (size_t k) const
iterator begin () const
bool empty () const
iterator end () const
size_t id (size_t k) const
const size_t * ids () const
size_t size () const

Detailed Description

Agent::get_neighbors() builds and returns a std::vector of pointers, which costs a heap allocation on every call and it is called once per susceptible agent per day, in the innermost loop of every state-update function. This view iterates the agent's neighbor ids in place and resolves each one against the model's population as it goes, so the same loop runs without allocating:

for (auto * neighbor : p->neighbors_view(*m))
    ...

The order is the agent's neighbor order, identical to get_neighbors(). The view borrows from the agent and the model, so it must not outlive either, and it is invalidated by anything that changes the agent's ties.

Template parameters:

  • TSeq Sequence type (should match TSeq across the model)

Public Functions Documentation

function NeighborsView [1/2]

NeighborsView::NeighborsView () = default

function NeighborsView [2/2]

inline NeighborsView::NeighborsView (
    const size_t * first,
    size_t n,
    std::vector< Agent < TSeq > > * pop
) 

function agent

inline Agent < TSeq > * NeighborsView::agent (
    size_t k
) const

The k-th neighbor.

Exception:

  • std::out_of_range if k >= size().

function begin

inline iterator NeighborsView::begin () const

function empty

inline bool NeighborsView::empty () const

function end

inline iterator NeighborsView::end () const

function id

inline size_t NeighborsView::id (
    size_t k
) const

Id of the k-th neighbor, without touching the neighbor's Agent.

Exception:

  • std::out_of_range if k >= size().

function ids

inline const size_t * NeighborsView::ids () const

The ids of the size() neighbors, in order (like std::vector::data(): only valid for indices below size(); nullptr when there are none). For scans that must not touch the neighbors' Agents.


function size

inline size_t NeighborsView::size () const


The documentation for this class was generated from the following file include/epiworld/agent-bones.hpp