1#ifndef COMMA_PROJECT_FIRST_ORDER_NEIGHBOURHOOD_H
2#define COMMA_PROJECT_FIRST_ORDER_NEIGHBOURHOOD_H
23#include <unordered_set>
39 typename CoMMAIndexType,
40 typename CoMMAWeightType,
41 typename CoMMAIntType>
44Some remarks about the implementation. All the work is done in the function
"update",
45hence that is where we have to focus on. There are only two constraints
for the
46returned object: it should be an iterable, and the content (the indices of the neighbours)
47must be ordered
using their weights. An ordered set of pair of (index, weight) with
49and that would have been quite hard to achieve even
if defining a custom
operator==,
50have a look here, https:
51We still use sets, but we have an extra check before inserting whether there already is
52a pair with the same index.
60 std::set<CoMMAPairType, CustomPairGreaterFunctor<CoMMAPairType>>;
78 const std::unordered_set<CoMMAIndexType> &s_neighbours_of_seed,
79 const std::vector<CoMMAWeightType> &weights
104 update_it(new_fc, new_neighbours.cbegin(), new_neighbours.cend());
117 const CoMMAIndexType new_fc,
141 std::unordered_set<CoMMAIndexType>
_s_fc;
154 for (
const auto &[idx, w] : candidates_w_weights)
155 this->_candidates.push_back(idx);
168 typename CoMMAIndexType,
169 typename CoMMAWeightType,
170 typename CoMMAIntType>
172 public Neighbourhood<CoMMAIndexType, CoMMAWeightType, CoMMAIntType> {
196 const std::unordered_set<CoMMAIndexType> &s_neighbours_of_seed,
197 const std::vector<CoMMAWeightType> &weights
199 Neighbourhood<CoMMAIndexType, CoMMAWeightType, CoMMAIntType>(
200 s_neighbours_of_seed, weights
222 const CoMMAIndexType new_fc,
227 this->
_s_fc.insert(new_fc);
229 std::find(this->
_candidates.begin(), this->_candidates.end(), new_fc);
236 for (
auto it_fc = neighs_begin; it_fc != neighs_end; ++it_fc) {
237 if ((std::find(this->
_candidates.begin(), this->_candidates.end(), *it_fc)
238 == this->_candidates.end())
239 && (this->_s_fc.count(*it_fc) == 0)
240 && (this->_s_neighbours_of_seed.count(*it_fc) > 0)) {
243 neighs.emplace(*it_fc, this->
_weights[*it_fc]);
263 typename CoMMAIndexType,
264 typename CoMMAWeightType,
265 typename CoMMAIntType>
267 public Neighbourhood<CoMMAIndexType, CoMMAWeightType, CoMMAIntType> {
292 const std::unordered_set<CoMMAIndexType> &s_neighbours_of_seed,
293 const std::vector<CoMMAWeightType> &weights,
294 CoMMAIntType dimension
296 Neighbourhood<CoMMAIndexType, CoMMAWeightType, CoMMAIntType>(
297 s_neighbours_of_seed, weights
306 CoMMAIntType> &other) =
default;
321 const CoMMAIndexType new_fc,
327 this->
_s_fc.insert(new_fc);
332 auto it = std::find_if(
336 if (it != queue.end()) queue.erase(it);
342 for (
auto it_fc = neighs_begin; it_fc != neighs_end; ++it_fc) {
343 if ((this->
_s_fc.count(*it_fc) == 0)
344 && (this->_s_neighbours_of_seed.count(*it_fc) > 0)) {
346 curr_set.emplace(*it_fc, this->
_weights[*it_fc]);
350 this->_q_neighs_w_weights.push_front(curr_set);
357 if (this->_q_neighs_w_weights.size()
366 for (
auto prev_q = this->_q_neighs_w_weights.begin() + 1;
367 prev_q != this->_q_neighs_w_weights.end();
369 curr_set.insert(prev_q->begin(), prev_q->end());
372 auto cur_front =
decltype(this->_q_neighs_w_weights.size()){0};
373 auto cur_back =
decltype(this->_q_neighs_w_weights.size()
374 ){this->_q_neighs_w_weights.size() - 1};
375 while (cur_front <= cur_back) {
377 auto it = this->_q_neighs_w_weights.begin() + (cur_front++);
382 it = this->_q_neighs_w_weights.begin() + (cur_back--);
397 const CoMMAIntType lvl
424 typename CoMMAIndexType,
425 typename CoMMAWeightType,
426 typename CoMMAIntType>
448 virtual std::shared_ptr<NeighbourhoodBaseType>
create(
449 const std::unordered_set<CoMMAIndexType> &s_neighbours_of_seed,
450 const std::vector<CoMMAWeightType> &priority_weights,
451 const CoMMAIntType dimension
458 virtual std::shared_ptr<NeighbourhoodBaseType>
clone(
459 std::shared_ptr<NeighbourhoodBaseType> other
471 typename CoMMAIndexType,
472 typename CoMMAWeightType,
473 typename CoMMAIntType>
505 inline std::shared_ptr<NeighbourhoodBaseType>
create(
506 const std::unordered_set<CoMMAIndexType> &s_neighbours_of_seed,
507 const std::vector<CoMMAWeightType> &priority_weights,
508 const CoMMAIntType dimension
511 return std::make_shared<NeighbourhoodDerivedType>(
512 s_neighbours_of_seed, priority_weights
521 inline std::shared_ptr<NeighbourhoodBaseType>
clone(
522 std::shared_ptr<NeighbourhoodBaseType> other
525 return std::make_shared<NeighbourhoodDerivedType>(
526 *std::dynamic_pointer_cast<NeighbourhoodDerivedType>(other)
539 typename CoMMAIndexType,
540 typename CoMMAWeightType,
541 typename CoMMAIntType>
573 inline std::shared_ptr<NeighbourhoodBaseType>
create(
574 const std::unordered_set<CoMMAIndexType> &s_neighbours_of_seed,
575 const std::vector<CoMMAWeightType> &priority_weights,
576 const CoMMAIntType dimension
578 return std::make_shared<NeighbourhoodDerivedType>(
579 s_neighbours_of_seed, priority_weights, dimension
588 inline std::shared_ptr<NeighbourhoodBaseType>
clone(
589 std::shared_ptr<NeighbourhoodBaseType> other
592 return std::make_shared<NeighbourhoodDerivedType>(
593 *std::dynamic_pointer_cast<NeighbourhoodDerivedType>(other)
#define CoMMAUnused(var)
Convenient function to avoid unused warnings.
Definition: Util.h:38
Class representing the neighbourhood of a given cell in the graph. In this derived class the neighbou...
Definition: Neighbourhood.h:172
std::set< CoMMAPairType, CustomPairGreaterFunctor< CoMMAPairType > > CoMMASetOfPairType
Type of set of pairs.
Definition: Neighbourhood.h:60
void update_it(const CoMMAIndexType new_fc, ContainerIndexConstIt neighs_begin, ContainerIndexConstIt neighs_end) override
Method that updates the neighbourhood. Given the new_fc, if is in the neighbours, it is deleted....
Definition: Neighbourhood.h:221
Neighbourhood_Extended(const std::unordered_set< CoMMAIndexType > &s_neighbours_of_seed, const std::vector< CoMMAWeightType > &weights)
Constructor.
Definition: Neighbourhood.h:195
~Neighbourhood_Extended() override=default
Destructor.
Neighbourhood_Extended(const Neighbourhood_Extended< CoMMAIndexType, CoMMAWeightType, CoMMAIntType > &other)=default
Copy constructor.
typename ContainerIndexType::const_iterator ContainerIndexConstIt
Type for constant iterators of containers of indices.
Definition: Neighbourhood.h:69
Class representing the neighbourhood of a given cell in the graph. In this derived class,...
Definition: Neighbourhood.h:267
~Neighbourhood_Pure_Front() override=default
Destructor.
void update_it(const CoMMAIndexType new_fc, ContainerIndexConstIt neighs_begin, ContainerIndexConstIt neighs_end) override
Method that updates the neighbourhood. Given the new_fc, if is in the neighbours, it is deleted....
Definition: Neighbourhood.h:320
Neighbourhood_Pure_Front(const Neighbourhood_Pure_Front< CoMMAIndexType, CoMMAWeightType, CoMMAIntType > &other)=default
Copy constructor.
std::set< CoMMAPairType, CustomPairGreaterFunctor< CoMMAPairType > > CoMMASetOfPairType
Type of set of pairs.
Definition: Neighbourhood.h:60
const CoMMASetOfPairType & get_neighbours_by_level(const CoMMAIntType lvl) const
Get the neighbours from a previous stage.
Definition: Neighbourhood.h:396
std::deque< CoMMASetOfPairType > _q_neighs_w_weights
History of the first-order-neighbourhoods of the fine cells recently agglomerated.
Definition: Neighbourhood.h:409
CoMMAIntType _dimension
Dimensionality of the problem (_dimension = 2 -> 2D, _dimension = 3 -> 3D)
Definition: Neighbourhood.h:413
Neighbourhood_Pure_Front(const std::unordered_set< CoMMAIndexType > &s_neighbours_of_seed, const std::vector< CoMMAWeightType > &weights, CoMMAIntType dimension)
Constructor.
Definition: Neighbourhood.h:291
typename ContainerIndexType::const_iterator ContainerIndexConstIt
Type for constant iterators of containers of indices.
Definition: Neighbourhood.h:69
Pure abstract class for a creator of Neighbourhood objects. It can create from scratch or by copy.
Definition: Neighbourhood.h:427
virtual std::shared_ptr< NeighbourhoodBaseType > clone(std::shared_ptr< NeighbourhoodBaseType > other) const =0
Create a new Neighbourhood object by copy.
Neighbourhood< CoMMAIndexType, CoMMAWeightType, CoMMAIntType > NeighbourhoodBaseType
Shortcut for the Neighborhood object type.
Definition: Neighbourhood.h:431
virtual ~NeighbourhoodCreator()=default
Destructor.
NeighbourhoodCreator()=default
Constructor.
virtual std::shared_ptr< NeighbourhoodBaseType > create(const std::unordered_set< CoMMAIndexType > &s_neighbours_of_seed, const std::vector< CoMMAWeightType > &priority_weights, const CoMMAIntType dimension) const =0
Create a new Neighbourhood object from scratch using the given arguments.
Creator of Neighbourhood_Extended objects. It can create from scratch or by copy.
Definition: Neighbourhood.h:475
~NeighbourhoodExtendedCreator() override=default
Destructor.
std::shared_ptr< NeighbourhoodBaseType > create(const std::unordered_set< CoMMAIndexType > &s_neighbours_of_seed, const std::vector< CoMMAWeightType > &priority_weights, const CoMMAIntType dimension) const override
Create a new Neighbourhood object from scratch using the given arguments.
Definition: Neighbourhood.h:505
NeighbourhoodExtendedCreator()
Constructor.
Definition: Neighbourhood.h:489
std::shared_ptr< NeighbourhoodBaseType > clone(std::shared_ptr< NeighbourhoodBaseType > other) const override
Create a new Neighbourhood object by copy.
Definition: Neighbourhood.h:521
Class representing the neighbourhood of a given cell in the graph. Mind that no information about the...
Definition: Neighbourhood.h:42
const std::vector< CoMMAWeightType > & _weights
Priority weights.
Definition: Neighbourhood.h:138
PairFindFirstBasedFunctor< CoMMAPairType > CoMMAPairFindFirstBasedType
Functor used if find-like function relying only on first element of the pair.
Definition: Neighbourhood.h:63
Neighbourhood(const std::unordered_set< CoMMAIndexType > &s_neighbours_of_seed, const std::vector< CoMMAWeightType > &weights)
Constructor.
Definition: Neighbourhood.h:77
const std::unordered_set< CoMMAIndexType > _s_neighbours_of_seed
Set of the neighbours of seed given as an input in the constructor. Here, we can find all the neighbo...
Definition: Neighbourhood.h:135
void update(const CoMMAIndexType new_fc, const ContainerIndexType &new_neighbours)
Method that updates the neighbourhood. Given the new_fc, if is in the neighbours, it is deleted....
Definition: Neighbourhood.h:101
const CandidatesContainerType & get_candidates() const
Get candidates that should be consider in the next step of the agglomeration.
Definition: Neighbourhood.h:126
CandidatesContainerType _candidates
Candidates that should be considered when choosing the next fine cell to add to the coarse one.
Definition: Neighbourhood.h:146
std::deque< CoMMAIndexType > CandidatesContainerType
Type for container of candidates.
Definition: Neighbourhood.h:65
std::vector< CoMMAIndexType > ContainerIndexType
Type for containers of indices.
Definition: Neighbourhood.h:67
std::set< CoMMAPairType, CustomPairGreaterFunctor< CoMMAPairType > > CoMMASetOfPairType
Type of set of pairs.
Definition: Neighbourhood.h:60
Neighbourhood(const Neighbourhood< CoMMAIndexType, CoMMAWeightType, CoMMAIntType > &other)=default
Copy constructor.
std::unordered_set< CoMMAIndexType > _s_fc
Set of the fine cells composing the coarse cell.
Definition: Neighbourhood.h:141
void extract_and_update_candidates(const CoMMASetOfPairType &candidates_w_weights)
Extract the indices from a list of index-weight pairs and add them at the back of the candidates list...
Definition: Neighbourhood.h:151
std::pair< CoMMAIndexType, CoMMAWeightType > CoMMAPairType
Type of pair.
Definition: Neighbourhood.h:57
virtual void update_it(const CoMMAIndexType new_fc, ContainerIndexConstIt neighs_begin, ContainerIndexConstIt neighs_end)=0
Method that updates the neighbourhood. Given the new_fc, if is in the neighbours, it is deleted....
virtual ~Neighbourhood()=default
Destructor.
typename ContainerIndexType::const_iterator ContainerIndexConstIt
Type for constant iterators of containers of indices.
Definition: Neighbourhood.h:69
Creator of Neighbourhood_Extended objects. It can create from scratch or by copy.
Definition: Neighbourhood.h:543
NeighbourhoodPureFrontCreator()
Constructor.
Definition: Neighbourhood.h:557
std::shared_ptr< NeighbourhoodBaseType > clone(std::shared_ptr< NeighbourhoodBaseType > other) const override
Create a new Neighbourhood object by copy.
Definition: Neighbourhood.h:588
~NeighbourhoodPureFrontCreator() override=default
Destructor.
std::shared_ptr< NeighbourhoodBaseType > create(const std::unordered_set< CoMMAIndexType > &s_neighbours_of_seed, const std::vector< CoMMAWeightType > &priority_weights, const CoMMAIntType dimension) const override
Create a new Neighbourhood object from scratch using the given arguments.
Definition: Neighbourhood.h:573
Functor implementing an operator telling if a given value if the first one of pair.
Definition: Util.h:258
Definition: Agglomerator.h:37
Functor for pairs implementing a custom 'greater than'. It relies on the 'greater than' operator for ...
Definition: Util.h:201