IAP GITLAB

Skip to content
Snippets Groups Projects
Commit 35e30593 authored by Antonio Augusto Alves Junior's avatar Antonio Augusto Alves Junior Committed by ralfulrich
Browse files

[refactory-2020] stack implementations: more cleaning up. Moving...

[refactory-2020] stack implementations: more cleaning up. Moving implemententations to inl. SecondaryView.hpp
parent 7061f830
No related branches found
No related tags found
No related merge requests found
...@@ -16,114 +16,56 @@ ...@@ -16,114 +16,56 @@
namespace corsika { namespace corsika {
/** template <template <typename> class TParticleInterfaceA,
* CombinedParticleInterface can be used to combine the data of several StackData template <typename> class TParticleInterfaceB, typename TStackIterator>
* objects.
*
* You may combine two StackData object, see class CombinedStackImpl
* below, into one Stack, using a combined StackIterator (aka
* CombinedParticleInterface) interface class.
*
* This allows to add specific information to a given Stack, could
* be special information on a subset of entries
* (e.g. NuclearStackExtension) or also (multi) thinning weights for
* all particles.
*
* Many Stacks can be combined into more complex object.
*
* The two sub-stacks must both provide their independent
* ParticleInterface classes.
*
*/
template <template <typename> typename TParticleInterfaceA,
template <typename> class TParticleInterfaceB, typename TStackIterator>
struct CombinedParticleInterface
: public TParticleInterfaceB<TParticleInterfaceA<TStackIterator>> {
typedef CombinedParticleInterface<TParticleInterfaceA, TParticleInterfaceB,
TStackIterator>
pi_c_type;
typedef TParticleInterfaceA<TStackIterator> pi_a_type;
typedef TParticleInterfaceB<TParticleInterfaceA<TStackIterator>> pi_b_type;
protected:
using pi_b_type::getIndex; // choose B, A would also work
using pi_b_type::getStackData; // choose B, A would also work
public:
/**
* @name wrapper for user functions
* @{
*
* In this set of functions we call the user-provide
* TParticleInterface setParticleData(...) methods, either with
* parent particle reference, or w/o.
*
* There is one implicit assumption here: if only one data tuple
* is provided for setParticleData, the data is passed on to
* TParticleInterfaceA and the TParticleInterfaceB is
* default-initialized. There are many occasions where this is the
* desired behaviour, e.g. for thinning etc.
*
*/
template <typename... TArgs1> template <typename... TArgs1>
void setParticleData(std::tuple<TArgs1...> const vA) { void CombinedParticleInterface<TParticleInterfaceA, TParticleInterfaceB, TStackIterator>::setParticleData(std::tuple<TArgs1...> const vA) {
pi_a_type::setParticleData(vA); pi_a_type::setParticleData(vA);
pi_b_type::setParticleData(); pi_b_type::setParticleData();
} }
template <template <typename> class TParticleInterfaceA,
template <typename> class TParticleInterfaceB, typename TStackIterator>
template <typename... TArgs1, typename... TArgs2> template <typename... TArgs1, typename... TArgs2>
void setParticleData(std::tuple<TArgs1...> const vA, std::tuple<TArgs2...> const vB) { void CombinedParticleInterface<TParticleInterfaceA, TParticleInterfaceB, TStackIterator>::setParticleData(std::tuple<TArgs1...> const vA, std::tuple<TArgs2...> const vB) {
pi_a_type::setParticleData(vA); pi_a_type::setParticleData(vA);
pi_b_type::setParticleData(vB); pi_b_type::setParticleData(vB);
} }
template <template <typename> class TParticleInterfaceA,
template <typename> class TParticleInterfaceB, typename TStackIterator>
template <typename... TArgs1> template <typename... TArgs1>
void setParticleData(pi_a_type& p, std::tuple<TArgs1...> const vA) { void CombinedParticleInterface<TParticleInterfaceA, TParticleInterfaceB, TStackIterator>::setParticleData(pi_a_type& p, std::tuple<TArgs1...> const vA) {
// static_assert(MT<I>::has_not, "error"); // static_assert(MT<I>::has_not, "error");
pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA); // original stack pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA); // original stack
pi_b_type::setParticleData(static_cast<pi_b_type&>(p)); // addon stack pi_b_type::setParticleData(static_cast<pi_b_type&>(p)); // addon stack
} }
template <template <typename> class TParticleInterfaceA,
template <typename> class TParticleInterfaceB,typename TStackIterator>
template <typename... TArgs1, typename... TArgs2> template <typename... TArgs1, typename... TArgs2>
void setParticleData(pi_c_type& p, std::tuple<TArgs1...> const vA, void CombinedParticleInterface<TParticleInterfaceA, TParticleInterfaceB, TStackIterator>::setParticleData(pi_c_type& p, std::tuple<TArgs1...> const vA,
std::tuple<TArgs2...> const vB) { std::tuple<TArgs2...> const vB) {
pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA); pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA);
pi_b_type::setParticleData(static_cast<pi_b_type&>(p), vB); pi_b_type::setParticleData(static_cast<pi_b_type&>(p), vB);
} }
///@} ///@}
std::string as_string() const { template <template <typename> class TParticleInterfaceA,
template <typename> class TParticleInterfaceB, typename TStackIterator>
std::string CombinedParticleInterface<TParticleInterfaceA, TParticleInterfaceB, TStackIterator>::as_string() const {
return fmt::format("[[{}][{}]]", pi_a_type::as_string(), pi_b_type::as_string()); return fmt::format("[[{}][{}]]", pi_a_type::as_string(), pi_b_type::as_string());
} }
protected:
}; template <typename Stack1Impl, typename Stack2Impl>
void CombinedStackImpl< Stack1Impl, Stack2Impl>::clear() {
/**
* @class CombinedStackImpl
*
* Memory implementation of a combined data stack.
*
* The two stack data user objects Stack1Impl and Stack2Impl are
* merged into one consistent Stack container object providing
* access to the combined number of data entries.
*/
template <typename Stack1Impl, typename Stack2Impl>
class CombinedStackImpl : public Stack1Impl, public Stack2Impl {
public:
void clear() {
Stack1Impl::clear(); Stack1Impl::clear();
Stack2Impl::clear(); Stack2Impl::clear();
} }
template <typename Stack1Impl, typename Stack2Impl>
unsigned int getSize() const { return Stack1Impl::getSize(); } void CombinedStackImpl< Stack1Impl, Stack2Impl>::copy(const unsigned int i1, const unsigned int i2) {
unsigned int getCapacity() const { return Stack1Impl::getCapacity(); }
/**
* Function to copy particle at location i1 in stack to i2
*/
void copy(const unsigned int i1, const unsigned int i2) {
if (i1 >= getSize() || i2 >= getSize()) { if (i1 >= getSize() || i2 >= getSize()) {
std::ostringstream err; std::ostringstream err;
err << "CombinedStack: trying to access data beyond size of stack!"; err << "CombinedStack: trying to access data beyond size of stack!";
...@@ -133,10 +75,8 @@ namespace corsika { ...@@ -133,10 +75,8 @@ namespace corsika {
Stack2Impl::copy(i1, i2); Stack2Impl::copy(i1, i2);
} }
/** template <typename Stack1Impl, typename Stack2Impl>
* Function to copy particle at location i2 in stack to i1 void CombinedStackImpl< Stack1Impl, Stack2Impl>::swap(const unsigned int i1, const unsigned int i2) {
*/
void swap(const unsigned int i1, const unsigned int i2) {
if (i1 >= getSize() || i2 >= getSize()) { if (i1 >= getSize() || i2 >= getSize()) {
std::ostringstream err; std::ostringstream err;
err << "CombinedStack: trying to access data beyond size of stack!"; err << "CombinedStack: trying to access data beyond size of stack!";
...@@ -146,30 +86,17 @@ namespace corsika { ...@@ -146,30 +86,17 @@ namespace corsika {
Stack2Impl::swap(i1, i2); Stack2Impl::swap(i1, i2);
} }
void incrementSize() { template <typename Stack1Impl, typename Stack2Impl>
void CombinedStackImpl< Stack1Impl, Stack2Impl>::incrementSize() {
Stack1Impl::incrementSize(); Stack1Impl::incrementSize();
Stack2Impl::incrementSize(); Stack2Impl::incrementSize();
} }
void decrementSize() { template <typename Stack1Impl, typename Stack2Impl>
void CombinedStackImpl< Stack1Impl, Stack2Impl>::decrementSize() {
Stack1Impl::decrementSize(); Stack1Impl::decrementSize();
Stack2Impl::decrementSize(); Stack2Impl::decrementSize();
} }
}; // end class CombinedStackImpl
/**
* Helper template alias `CombinedStack` to construct new combined
* stack from two stack data objects and a particle readout interface.
*
* Note that the Stack2Impl provides only /additional/ data to
* Stack1Impl. This is important (see above) since tuple data for
* initialization are forwarded to Stack1Impl (first).
*/
template <typename Stack1Impl, typename Stack2Impl, template <typename> typename _PI>
using CombinedStack = Stack<CombinedStackImpl<Stack1Impl, Stack2Impl>, _PI>;
} // namespace corsika } // namespace corsika
//#include <corsika/detail/framework/stack/CombinedStack.inl>
This diff is collapsed.
...@@ -67,34 +67,37 @@ namespace corsika { ...@@ -67,34 +67,37 @@ namespace corsika {
*/ */
template <typename... TArgs1> template <typename... TArgs1>
void setParticleData(std::tuple<TArgs1...> const vA) { void setParticleData(std::tuple<TArgs1...> const vA);
/* {
pi_a_type::setParticleData(vA); pi_a_type::setParticleData(vA);
pi_b_type::setParticleData(); pi_b_type::setParticleData();
} }*/
template <typename... TArgs1, typename... TArgs2> template <typename... TArgs1, typename... TArgs2>
void setParticleData(std::tuple<TArgs1...> const vA, std::tuple<TArgs2...> const vB) { void setParticleData(std::tuple<TArgs1...> const vA, std::tuple<TArgs2...> const vB); /*{
pi_a_type::setParticleData(vA); pi_a_type::setParticleData(vA);
pi_b_type::setParticleData(vB); pi_b_type::setParticleData(vB);
} }*/
template <typename... TArgs1> template <typename... TArgs1>
void setParticleData(pi_a_type& p, std::tuple<TArgs1...> const vA) { void setParticleData(pi_a_type& p, std::tuple<TArgs1...> const vA);/* {
// static_assert(MT<I>::has_not, "error"); // static_assert(MT<I>::has_not, "error");
pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA); // original stack pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA); // original stack
pi_b_type::setParticleData(static_cast<pi_b_type&>(p)); // addon stack pi_b_type::setParticleData(static_cast<pi_b_type&>(p)); // addon stack
} }*/
template <typename... TArgs1, typename... TArgs2> template <typename... TArgs1, typename... TArgs2>
void setParticleData(pi_c_type& p, std::tuple<TArgs1...> const vA, void setParticleData(pi_c_type& p, std::tuple<TArgs1...> const vA,
std::tuple<TArgs2...> const vB) { std::tuple<TArgs2...> const vB);/* {
pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA); pi_a_type::setParticleData(static_cast<pi_a_type&>(p), vA);
pi_b_type::setParticleData(static_cast<pi_b_type&>(p), vB); pi_b_type::setParticleData(static_cast<pi_b_type&>(p), vB);
} }*/
///@} ///@}
std::string as_string() const { std::string as_string() const;/* {
return fmt::format("[[{}][{}]]", pi_a_type::as_string(), pi_b_type::as_string()); return fmt::format("[[{}][{}]]", pi_a_type::as_string(), pi_b_type::as_string());
} }*/
private: private:
protected: protected:
...@@ -110,13 +113,14 @@ namespace corsika { ...@@ -110,13 +113,14 @@ namespace corsika {
* access to the combined number of data entries. * access to the combined number of data entries.
*/ */
template <typename Stack1Impl, typename Stack2Impl> template <typename Stack1Impl, typename Stack2Impl>
class CombinedStackImpl : public Stack1Impl, public Stack2Impl { struct CombinedStackImpl : public Stack1Impl, public Stack2Impl {
public: public:
void clear() {
void clear();/* {
Stack1Impl::clear(); Stack1Impl::clear();
Stack2Impl::clear(); Stack2Impl::clear();
} }*/
unsigned int getSize() const { return Stack1Impl::getSize(); } unsigned int getSize() const { return Stack1Impl::getSize(); }
unsigned int getCapacity() const { return Stack1Impl::getCapacity(); } unsigned int getCapacity() const { return Stack1Impl::getCapacity(); }
...@@ -124,7 +128,7 @@ namespace corsika { ...@@ -124,7 +128,7 @@ namespace corsika {
/** /**
* Function to copy particle at location i1 in stack to i2 * Function to copy particle at location i1 in stack to i2
*/ */
void copy(const unsigned int i1, const unsigned int i2) { void copy(const unsigned int i1, const unsigned int i2);/* {
if (i1 >= getSize() || i2 >= getSize()) { if (i1 >= getSize() || i2 >= getSize()) {
std::ostringstream err; std::ostringstream err;
err << "CombinedStack: trying to access data beyond size of stack!"; err << "CombinedStack: trying to access data beyond size of stack!";
...@@ -132,12 +136,12 @@ namespace corsika { ...@@ -132,12 +136,12 @@ namespace corsika {
} }
Stack1Impl::copy(i1, i2); Stack1Impl::copy(i1, i2);
Stack2Impl::copy(i1, i2); Stack2Impl::copy(i1, i2);
} }*/
/** /**
* Function to copy particle at location i2 in stack to i1 * Function to copy particle at location i2 in stack to i1
*/ */
void swap(const unsigned int i1, const unsigned int i2) { void swap(const unsigned int i1, const unsigned int i2);/* {
if (i1 >= getSize() || i2 >= getSize()) { if (i1 >= getSize() || i2 >= getSize()) {
std::ostringstream err; std::ostringstream err;
err << "CombinedStack: trying to access data beyond size of stack!"; err << "CombinedStack: trying to access data beyond size of stack!";
...@@ -145,17 +149,17 @@ namespace corsika { ...@@ -145,17 +149,17 @@ namespace corsika {
} }
Stack1Impl::swap(i1, i2); Stack1Impl::swap(i1, i2);
Stack2Impl::swap(i1, i2); Stack2Impl::swap(i1, i2);
} }*/
void incrementSize() { void incrementSize();/* {
Stack1Impl::incrementSize(); Stack1Impl::incrementSize();
Stack2Impl::incrementSize(); Stack2Impl::incrementSize();
} }*/
void decrementSize() { void decrementSize();/* {
Stack1Impl::decrementSize(); Stack1Impl::decrementSize();
Stack2Impl::decrementSize(); Stack2Impl::decrementSize();
} }*/
}; // end class CombinedStackImpl }; // end class CombinedStackImpl
...@@ -173,4 +177,4 @@ namespace corsika { ...@@ -173,4 +177,4 @@ namespace corsika {
} // namespace corsika } // namespace corsika
//#include <corsika/detail/framework/stack/CombinedStack.inl> #include <corsika/detail/framework/stack/CombinedStack.inl>
...@@ -200,11 +200,8 @@ namespace corsika { ...@@ -200,11 +200,8 @@ namespace corsika {
* Method to add a new secondary particle on this SecondaryView * Method to add a new secondary particle on this SecondaryView
*/ */
template <typename... Args> template <typename... Args>
stack_view_iterator addSecondary(const Args... v) { stack_view_iterator addSecondary(const Args... v);
CORSIKA_LOG_TRACE("SecondaryView::addSecondary(Args&&)");
stack_view_iterator proj = getProjectile(); // make this const
return addSecondary(proj, v...);
}
/** /**
* overwrite Stack::getSize to return actual number of secondaries * overwrite Stack::getSize to return actual number of secondaries
*/ */
...@@ -224,60 +221,23 @@ namespace corsika { ...@@ -224,60 +221,23 @@ namespace corsika {
*/ */
// NOTE: the "+1" is since "0" is special marker here for PROJECTILE, see // NOTE: the "+1" is since "0" is special marker here for PROJECTILE, see
// getIndexFromIterator // getIndexFromIterator
stack_view_iterator begin() { stack_view_iterator begin();
unsigned int i = 0;
for (; i < getSize(); ++i) {
if (!isErased(i)) break;
}
return stack_view_iterator(*this, i + 1);
}
auto end() { return stack_view_iterator(*this, getSize() + 1); } stack_view_iterator end() { return stack_view_iterator(*this, getSize() + 1); }
auto last() {
unsigned int i = 0;
for (; i < getSize(); ++i) {
if (!isErased(getSize() - 1 - i)) break;
}
return stack_view_iterator(*this, getSize() - 1 - i + 1);
}
auto begin() const { stack_view_iterator last();
unsigned int i = 0;
for (; i < getSize(); ++i) {
if (!isErased(i)) break;
}
return const_stack_view_iterator(*this, i + 1); const_stack_view_iterator begin() const ;
}
auto end() const { return const_stack_view_iterator(*this, getSize() + 1); } const_stack_view_iterator end() const { return const_stack_view_iterator(*this, getSize() + 1); }
auto last() const { const_stack_view_iterator last() const;
unsigned int i = 0;
for (; i < getSize(); ++i) {
if (!isErased(getSize() - 1 - i)) break;
}
return const_stack_view_iterator(*this, getSize() - 1 - i + 1);
}
auto cbegin() const { const_stack_view_iterator cbegin() const;
unsigned int i = 0;
for (; i < getSize(); ++i) {
if (!isErased(i)) break;
}
return const_stack_view_iterator(*this, i + 1);
}
auto cend() const { return const_stack_view_iterator(*this, getSize()); } auto cend() const { return const_stack_view_iterator(*this, getSize()); }
auto clast() const { const_stack_view_iterator clast() const;
unsigned int i = 0;
for (; i < getSize(); ++i) {
if (!isErased(getSize() - 1 - i)) break;
}
return const_stack_view_iterator(*this, getSize() - 1 - i + 1);
}
stack_view_iterator at(unsigned int i) { return stack_view_iterator(*this, i); } stack_view_iterator at(unsigned int i) { return stack_view_iterator(*this, i); }
...@@ -292,21 +252,11 @@ namespace corsika { ...@@ -292,21 +252,11 @@ namespace corsika {
} }
/// @} /// @}
void swap(stack_view_iterator a, stack_view_iterator b) { void swap(stack_view_iterator a, stack_view_iterator b) ;
CORSIKA_LOG_TRACE("View::swap");
inner_stack_.swap(getIndexFromIterator(a.getIndex()), void copy(stack_view_iterator a, stack_view_iterator b);
getIndexFromIterator(b.getIndex()));
} void copy(const_stack_view_iterator a, stack_view_iterator b);
void copy(stack_view_iterator a, stack_view_iterator b) {
CORSIKA_LOG_TRACE("View::copy");
inner_stack_.copy(getIndexFromIterator(a.getIndex()),
getIndexFromIterator(b.getIndex()));
}
void copy(const_stack_view_iterator a, stack_view_iterator b) {
CORSIKA_LOG_TRACE("View::copy");
inner_stack_.copy(getIndexFromIterator(a.getIndex()),
getIndexFromIterator(b.getIndex()));
}
/** /**
* need overwrite Stack::Delete, since we want to call * need overwrite Stack::Delete, since we want to call
...@@ -320,17 +270,7 @@ namespace corsika { ...@@ -320,17 +270,7 @@ namespace corsika {
* remove the last particle. * remove the last particle.
* *
*/ */
void erase(stack_view_iterator p) { void erase(stack_view_iterator p);
CORSIKA_LOG_TRACE("SecondaryView::Delete");
if (isEmpty()) { /*error*/
throw std::runtime_error("Stack, cannot delete entry since size is zero");
}
if (isErased(p.getIndex() - 1)) { /*error*/
throw std::runtime_error("Stack, cannot delete entry since already deleted");
}
inner_stack_.erase(getIndexFromIterator(p.getIndex()));
inner_stack_reference_type::nDeleted_++; // also count in SecondaryView
}
/** /**
* return next particle from stack, need to overwrtie Stack::getNextParticle to get * return next particle from stack, need to overwrtie Stack::getNextParticle to get
...@@ -366,15 +306,7 @@ namespace corsika { ...@@ -366,15 +306,7 @@ namespace corsika {
* if it was marked as deleted before. If this is not the case, * if it was marked as deleted before. If this is not the case,
* the function will just return false and do nothing. * the function will just return false and do nothing.
*/ */
bool purgeLastIfDeleted() { bool purgeLastIfDeleted();
CORSIKA_LOG_TRACE("SecondaryView::purgeLastIfDeleted");
if (!isErased(getSize() - 1))
return false; // the last particle is not marked for deletion. Do nothing.
inner_stack_.purge(getIndexFromIterator(getSize()));
inner_stack_reference_type::nDeleted_--;
indices_.pop_back();
return true;
}
/** /**
* Function to ultimatively remove all entries from the stack * Function to ultimatively remove all entries from the stack
...@@ -384,34 +316,9 @@ namespace corsika { ...@@ -384,34 +316,9 @@ namespace corsika {
* "gaps" in the stack are filled with entries from the back * "gaps" in the stack are filled with entries from the back
* (copied). * (copied).
*/ */
void purge() { void purge() ;
unsigned int iStack = 0;
unsigned int size = getSize();
while (iStack < size) {
if (isErased(iStack)) {
inner_stack_.purge(iStack);
indices_.erase(indices_.begin() + iStack);
}
size = getSize();
iStack++;
}
inner_stack_reference_type::nDeleted_ = 0;
}
std::string as_string() const { std::string as_string() const;
std::string str(fmt::format("size {}\n", getSize()));
// we make our own begin/end since we want ALL entries
std::string new_line = " ";
for (unsigned int iPart = 0; iPart != getSize(); ++iPart) {
const_stack_view_iterator itPart(*this, iPart);
str += fmt::format(
"{}{}{}", new_line, itPart.as_string(),
(inner_stack_.deleted_[getIndexFromIterator(itPart.getIndex())] ? " [deleted]"
: ""));
new_line = "\n ";
}
return str;
}
protected: protected:
friend class StackIteratorInterface< friend class StackIteratorInterface<
...@@ -434,22 +341,7 @@ namespace corsika { ...@@ -434,22 +341,7 @@ namespace corsika {
* stack_view_iterator::addSecondary via ParticleBase * stack_view_iterator::addSecondary via ParticleBase
*/ */
template <typename... Args> template <typename... Args>
stack_view_iterator addSecondary(stack_view_iterator& proj, const Args... v) { stack_view_iterator addSecondary(stack_view_iterator& proj, const Args... v) ;
CORSIKA_LOG_TRACE("SecondaryView::addSecondary(stack_view_iterator&, Args&&)");
// make space on stack
inner_stack_reference_type::getStackData().incrementSize();
inner_stack_.deleted_.push_back(false);
// get current number of secondaries on stack
const unsigned int idSec = getSize();
// determine index on (inner) stack where new particle will be located
const unsigned int index = inner_stack_reference_type::getStackData().getSize() - 1;
indices_.push_back(index);
// NOTE: "+1" is since "0" is special marker here for PROJECTILE, see
// getIndexFromIterator
auto sec = stack_view_iterator(*this, idSec + 1, proj, v...);
MSecondaryProducer<TStackDataType, TParticleInterface>::new_secondary(sec);
return sec;
}
// forward to inner stack // forward to inner stack
// this also checks the allowed bounds of 'i' // this also checks the allowed bounds of 'i'
...@@ -534,4 +426,4 @@ namespace corsika { ...@@ -534,4 +426,4 @@ namespace corsika {
} // namespace corsika } // namespace corsika
//#include <corsika/detail/framework/stack/SecondaryView.inl> #include <corsika/detail/framework/stack/SecondaryView.inl>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment