Skip to content

Commit

Permalink
Clear up some old-style, confusing "return-via-reference" in the code
Browse files Browse the repository at this point in the history
  • Loading branch information
apontzen committed Dec 20, 2021
1 parent ea74f1b commit 35d947c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 52 deletions.
13 changes: 4 additions & 9 deletions genetIC/src/simulation/particles/mapper/gasmapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,12 @@ namespace particle {
}


/*! \brief Dereferences either the first or second iterator according to the current position, returning the grid and particle index pointed to
\param pIterator - constant pointer to the iterator to de-reference
\param gp - reference to where the resulting grid pointer should be stored
\param i - reference to where the resulting cell index should be stored
*/
virtual void
dereferenceIterator(const iterator *pIterator, ConstGridPtrType &gp, size_t &i) const override {
virtual std::pair<ConstGridPtrType, size_t>
dereferenceIterator(const iterator *pIterator) const override {
if (pIterator->i >= nFirst)
pIterator->subIterators[1]->deReference(gp, i);
return **(pIterator->subIterators[1]);
else
pIterator->subIterators[0]->deReference(gp, i);
return **(pIterator->subIterators[0]);
}


Expand Down
5 changes: 2 additions & 3 deletions genetIC/src/simulation/particles/mapper/graficmapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,8 @@ namespace particle {
}

//! Required by the mapper base class, but not implemented.
virtual void
dereferenceIterator(const iterator * /* *pIterator */, ConstGridPtrType & /*&gp*/,
size_t & /*&i*/) const override {
virtual std::pair<ConstGridPtrType, size_t>
dereferenceIterator(const iterator * /* *pIterator */) const override {
// Grafic files are written out at the grid level and iterators should not be involved.
throw std::runtime_error("Iterators are not supported by GraficMapper");
}
Expand Down
8 changes: 6 additions & 2 deletions genetIC/src/simulation/particles/mapper/mapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ namespace particle {
throw std::runtime_error("Attempting to reverse in a mapper that does not support random access");
}

//! Dereference the specified iterator, storing the grid pointer of the level pointed to and index of the cell pointed to on that level. Only implemented by derived mapper classes
virtual void dereferenceIterator(const iterator *, ConstGridPtrType &, size_t &) const {
/*! \brief Dereference the specified iterator, working out the grid and cell index it refers to
\param pIterator - iterator to dereference
\returns gp - pointer to the grid
\returns i - pointer to the cell within that grid
*/
virtual std::pair<ConstGridPtrType, size_t> dereferenceIterator(const iterator *) const {
throw std::runtime_error("There is no grid associated with this particle mapper");
}

Expand Down
44 changes: 19 additions & 25 deletions genetIC/src/simulation/particles/mapper/mapperiterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ namespace particle {
using GridType = grids::Grid<T>;
using ConstGridPtrType = std::shared_ptr<const grids::Grid<T>>;
using DereferenceType = std::pair<ConstGridPtrType, size_t>;
using EvaluatorPtrType = std::shared_ptr<const ParticleEvaluator<GridDataType>>;

friend class ParticleMapper<GridDataType>;

Expand All @@ -68,9 +69,12 @@ namespace particle {

const ParticleMapper<GridDataType> *pMapper; //!< Pointer to the particle mapper that uses the iterator
const AbstractMultiLevelParticleGenerator<GridDataType> &generator; //!< Generator used by the iterator to create particles for a given cell

private:
mutable ConstGridPtrType pLastGrid; //!< Pointer to the last grid pointed to
mutable std::shared_ptr<const ParticleEvaluator<GridDataType>> pLastGridEvaluator; //!< Evaluator for fields on the last grid pointed to
mutable EvaluatorPtrType pLastGridEvaluator; //!< Evaluator for fields on the last grid pointed to. Don't use directly: always call getEvaluatorAndIndex instead.

protected:
/*! \brief Constructor, that accepts a particle mapper, and a particle generator
\param pMapper - particle mapper that uses this iterator
\param generator - particle generator that will be used to create particles at each cell
Expand Down Expand Up @@ -128,40 +132,30 @@ namespace particle {

//! Dereferences the iterator at its current position and returns a pointer to the level current pointed at, and the index of the cell pointed to on that level
DereferenceType operator*() const {
ConstGridPtrType gp;
size_t i;
deReference(gp, i);
return std::make_pair(gp, i);
return pMapper->dereferenceIterator(this);
}


/*! \brief Performs the dereferencing operation
\param gp - reference to store pointer to level the iterator is pointing to
\param id - reference to store index for cell pointed to on the relevant level
*/
void deReference(ConstGridPtrType &gp, size_t &id) const {
// TODO: weird that this is now partly updating internal state and partly returning results
pMapper->dereferenceIterator(this, gp, id);
//! Get a grid evaluator and the index in that evaluator corresponding to the current location
auto getParticleEvaluatorAndIndex() const {
ConstGridPtrType gp;
size_t id;
std::tie(gp, id) = **this;
if (gp != pLastGrid) {
pLastGrid = gp;
updateGridReference();
pLastGridEvaluator = generator.makeParticleEvaluatorForGrid(*pLastGrid);
}
return std::make_pair(pLastGridEvaluator, id);
}


//! Returns the particle pointed to by the iterator
Particle<T> getParticle() const {
ConstGridPtrType pGrid;
EvaluatorPtrType evaluator;
size_t id;
deReference(pGrid, id);
return pLastGridEvaluator->getParticle(id);
std::tie(evaluator, id) = getParticleEvaluatorAndIndex();
return evaluator->getParticle(id);
}

protected:
//! Updates the stored evaluator to match the last grid pointed to.
void updateGridReference() const {
pLastGridEvaluator = generator.makeParticleEvaluatorForGrid(*pLastGrid);
}

mutable const fields::MultiLevelField<GridDataType> *lastMLField; //!< Pointer to the last multi-level field pointed to
mutable ConstGridPtrType lastGridPtr; //!< Pointer to the last grid pointed to
Expand Down Expand Up @@ -251,10 +245,10 @@ namespace particle {

//! Gets the mass in the cell currently pointed at.
T getMass() const {
ConstGridPtrType pGrid;
EvaluatorPtrType pEval;
size_t id;
deReference(pGrid, id);
return pLastGridEvaluator->getMass();
std::tie(pEval, id) = getParticleEvaluatorAndIndex();
return pEval->getMass();
}

//! Returns a pointer to the pair obtained by dereferencing the iterator at its current position
Expand Down
7 changes: 3 additions & 4 deletions genetIC/src/simulation/particles/mapper/onelevelmapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ namespace particle {
protected:

//! Dereferences the iterator with the grid for this level, and the current position
virtual void
dereferenceIterator(const iterator *pIterator, ConstGridPtrType &gp, size_t &i) const override {
gp = pGrid;
i = pIterator->i;
virtual std::pair<ConstGridPtrType, size_t>
dereferenceIterator(const iterator *pIterator) const override {
return std::make_pair(pGrid, pIterator->i);
}

/*! \brief Decrements the iterator by the specified step.
Expand Down
13 changes: 4 additions & 9 deletions genetIC/src/simulation/particles/mapper/twolevelmapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -778,17 +778,12 @@ namespace particle {
}
}

/*! \brief Dereference the specified iterator, storing the pointed to grid and cell index
\param pIterator - iterator to dereference
\param gp - reference to where the grid pointer should be stored
\param i - reference to where the cell index should be stored
*/
virtual void
dereferenceIterator(const iterator *pIterator, ConstGridPtrType &gp, size_t &i) const override {
virtual std::pair<ConstGridPtrType, size_t>
dereferenceIterator(const iterator *pIterator) const override {
if (pIterator->i >= firstLevel2Particle)
pIterator->subIterators[1]->deReference(gp, i);
return **(pIterator->subIterators[1]);
else
pIterator->subIterators[0]->deReference(gp, i);
return **(pIterator->subIterators[0]);
}


Expand Down

0 comments on commit 35d947c

Please sign in to comment.