From aad9b5b38145a76e17b371a5b54590bec0c9c0db Mon Sep 17 00:00:00 2001 From: ralfulrich <ralf.ulrich@kit.edu> Date: Tue, 7 Aug 2018 14:48:54 +0200 Subject: [PATCH] dummy particle stack --- Documentation/Examples/CMakeLists.txt | 7 ++- Documentation/Examples/stack_example.cc | 35 ++++++++++++ Framework/CMakeLists.txt | 1 + Framework/ParticleStack/CMakeLists.txt | 18 ++++++ Framework/ParticleStack/StackIterator.h | 70 ++++++++++++++++++++++++ Framework/ParticleStack/StackOne.h | 73 +++++++++++++++++++++++++ Framework/StackInterface/Stack.h | 20 +++---- 7 files changed, 212 insertions(+), 12 deletions(-) create mode 100644 Documentation/Examples/stack_example.cc create mode 100644 Framework/ParticleStack/CMakeLists.txt create mode 100644 Framework/ParticleStack/StackIterator.h create mode 100644 Framework/ParticleStack/StackOne.h diff --git a/Documentation/Examples/CMakeLists.txt b/Documentation/Examples/CMakeLists.txt index 7045e5e0..b7b574f4 100644 --- a/Documentation/Examples/CMakeLists.txt +++ b/Documentation/Examples/CMakeLists.txt @@ -1,10 +1,13 @@ - add_executable (geometry_example geometry_example.cc) target_link_libraries (geometry_example CORSIKAgeometry CORSIKAunits) install (TARGETS geometry_example DESTINATION share/examples) - add_executable (logger_example logger_example.cc) target_link_libraries (logger_example CORSIKAunits CORSIKAlogging) install (TARGETS logger_example DESTINATION share/examples) + +add_executable (stack_example stack_example.cc) +target_link_libraries (stack_example CORSIKAstack CORSIKAunits CORSIKAlogging) +install (TARGETS stack_example DESTINATION share/examples) + diff --git a/Documentation/Examples/stack_example.cc b/Documentation/Examples/stack_example.cc new file mode 100644 index 00000000..d27467f5 --- /dev/null +++ b/Documentation/Examples/stack_example.cc @@ -0,0 +1,35 @@ +#include <ParticleStack/StackOne.h> + +#include <iostream> +#include <iomanip> + +using namespace std; + +void fill(stack::StackOne& s) +{ + for (int i=0; i<11; ++i) { + auto p = s.NewParticle(); + p.SetId(i); + p.SetEnergy(1.5*i); + } +} + +void +read(stack::StackOne& s) +{ + cout << "found Stack with " << s.GetSize() << " particles. " << endl; + double Etot = 0; + for (auto p : s) { + Etot += p.GetEnergy(); + } + cout << "Etot=" << Etot << endl; +} + +int +main() +{ + stack::StackOne s; + fill(s); + read(s); + return 0; +} diff --git a/Framework/CMakeLists.txt b/Framework/CMakeLists.txt index 3081b355..90c136a3 100644 --- a/Framework/CMakeLists.txt +++ b/Framework/CMakeLists.txt @@ -3,3 +3,4 @@ add_subdirectory (Units) add_subdirectory (Geometry) add_subdirectory (Logging) add_subdirectory (StackInterface) +add_subdirectory (Stack) diff --git a/Framework/ParticleStack/CMakeLists.txt b/Framework/ParticleStack/CMakeLists.txt new file mode 100644 index 00000000..93b5280e --- /dev/null +++ b/Framework/ParticleStack/CMakeLists.txt @@ -0,0 +1,18 @@ + +set (STACK_HEADERS StackOne.h) + +add_library (CORSIKAstack INTERFACE) + +#set_target_properties (CORSIKAstack PROPERTIES VERSION ${PROJECT_VERSION}) +#set_target_properties (CORSIKAstack PROPERTIES SOVERSION 1) + +#set_target_properties (CORSIKAstack PROPERTIES PUBLIC_HEADER "${STACK_HEADERS}") + +#target_link_libraries (CORSIKAstackinterface CORSIKAunits) + +#target_include_directories (CORSIKAstack PRIVATE ${EIGEN3_INCLUDE_DIR}) +target_include_directories (CORSIKAstack INTERFACE $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/Framework> + $<INSTALL_INTERFACE:include/Framework> + ) + +install (FILES StackOne.h DESTINATION include/Stack) diff --git a/Framework/ParticleStack/StackIterator.h b/Framework/ParticleStack/StackIterator.h new file mode 100644 index 00000000..87ce66d4 --- /dev/null +++ b/Framework/ParticleStack/StackIterator.h @@ -0,0 +1,70 @@ +#ifndef _include_StackIterator_h__ +#define _include_StackIterator_h__ + +#include <iostream> +#include <iomanip> + +namespace stack { + + template<class Stack, class Particle> class StackIteratorInfo; + + /** + The main interface to iterator over objects on a stack. + */ + + template<typename Stack, typename Particle> + class StackIterator : public Particle + { + friend Stack; + friend Particle; + friend StackIteratorInfo<Stack,Particle>; + + private: + int fIndex; + + //#warning stacks should not be copied because of this: + Stack* fData; + + public: + StackIterator() : fData(0), fIndex(0) { } + StackIterator(Stack& data, const int index) : fData(&data), fIndex(index) { } + StackIterator(const StackIterator& mit) : fData(mit.fData), fIndex(mit.fIndex) { } + + StackIterator& operator++() { ++fIndex; return *this; } + StackIterator operator++(int) { StackIterator tmp(*this); ++fIndex; return tmp; } + bool operator==(const StackIterator& rhs) { return fIndex == rhs.fIndex; } + bool operator!=(const StackIterator& rhs) { return fIndex != rhs.fIndex; } + + StackIterator& operator*() { return *this; } + const StackIterator& operator*() const { return *this; } + + protected: + int GetIndex() const { return fIndex; } + Stack& GetStack() { return *fData; } + const Stack& GetStack() const { return *fData; } + + inline StackIterator<Stack,Particle>& base_ref() { return static_cast<StackIterator<Stack, Particle>&>(*this); } + inline const StackIterator<Stack,Particle>& base_ref() const { return static_cast<const StackIterator<Stack, Particle>&>(*this); } + }; + + + /** + Internal helper class for StackIterator + */ + + template<class _Stack, class Particle> + class StackIteratorInfo { + + friend Particle; + private: + StackIteratorInfo() {} + + protected: + inline _Stack& Stack() { return static_cast<StackIterator<_Stack, Particle>*>(this)->GetStack(); } + inline int Index() const { return static_cast<const StackIterator<_Stack, Particle>*>(this)->GetIndex(); } + inline const _Stack& Stack() const { return static_cast<const StackIterator<_Stack, Particle>*>(this)->GetStack(); } + }; + +} // end namespace stack + +#endif diff --git a/Framework/ParticleStack/StackOne.h b/Framework/ParticleStack/StackOne.h new file mode 100644 index 00000000..6245994c --- /dev/null +++ b/Framework/ParticleStack/StackOne.h @@ -0,0 +1,73 @@ +#ifndef _include_stackone_h_ +#define _include_stackone_h_ + +#include <vector> +#include <string> + +#include <StackInterface/Stack.h> + + +namespace stack { + + + /** + Example of a particle object on the stack. + */ + + template<typename _Stack> + class ParticleReadOne : public StackIteratorInfo<_Stack, ParticleReadOne<_Stack> > + { + using StackIteratorInfo<_Stack, ParticleReadOne>::Index; + using StackIteratorInfo<_Stack, ParticleReadOne>::Stack; + + public: + void SetId(const int id) { Stack().SetId(Index(), id); } + void SetEnergy(const double e) { Stack().SetEnergy(Index(), e); } + + int GetId() const { Stack().GetId(Index()); } + double GetEnergy() const { Stack().GetEnergy(Index()); } + + double GetPDG() const { return 0; } // ConvertToPDG(GetId()); } + void SetPDG(double v) { Stack().SetId(0, 0); } //fIndex, ConvertFromPDG(v)); } + }; + + + /** + Definition of one most simple particle stack object. + */ + + class StackOneImpl + { + private: + std::vector<int> fId; + std::vector<double> fData; + + public: + void Clear() { fData.clear(); } + + int GetSize() const { return fData.size(); } + int GetCapacity() const { return fData.size(); } + + + void SetId(const int i, const int id) { fId[i] = id; } + void SetEnergy(const int i, const double e) { fData[i] = e; } + + const int GetId(const int i) const { return fId[i]; } + const double GetEnergy(const int i) const { return fData[i]; } + + void Copy(const int i1, const int i2) { + fData[i2] = fData[i1]; + fId[i2] = fId[i1]; + } + + protected: + void IncrementSize() { fData.push_back(0.); fId.push_back(0.); } + void DecrementSize() { if (fData.size()>0) { fData.pop_back(); fId.pop_back(); } } + }; + + typedef StackIterator<StackOneImpl, ParticleReadOne<StackOneImpl> > ParticleOne; + typedef Stack<StackOneImpl, ParticleOne> StackOne; + +} // end namespace + +#endif diff --git a/Framework/StackInterface/Stack.h b/Framework/StackInterface/Stack.h index 3bf7a3c2..6fa19e2d 100644 --- a/Framework/StackInterface/Stack.h +++ b/Framework/StackInterface/Stack.h @@ -1,7 +1,7 @@ #ifndef _include_Stack_h__ #define _include_Stack_h__ -#include <StackIterator.h> // to help application programmres +#include <StackInterface/StackIterator.h> // to help application programmres namespace stack { @@ -13,8 +13,8 @@ namespace stack { class Stack : public DataImpl { public: - using DataImpl::Capacity; - using DataImpl::Size; + using DataImpl::GetCapacity; + using DataImpl::GetSize; using DataImpl::Clear; using DataImpl::Copy; @@ -26,15 +26,15 @@ namespace stack { typedef Particle iterator; typedef const Particle const_iterator; - iterator Begin() { return iterator(*this, 0); } - iterator End() { return iterator(*this, Size()); } - iterator Last() { return iterator(*this, Size()-1); } + iterator begin() { return iterator(*this, 0); } + iterator end() { return iterator(*this, GetSize()); } + iterator last() { return iterator(*this, GetSize()-1); } - const_iterator CBegin() const { return const_iterator(*this, 0); } - const_iterator CEnd() const { return const_iterator(*this, Size()); } - const_iterator CLast() const { return const_iterator(*this, Size()-1); } + const_iterator cbegin() const { return const_iterator(*this, 0); } + const_iterator cend() const { return const_iterator(*this, GetSize()); } + const_iterator clast() const { return const_iterator(*this, GetSize()-1); } - iterator NewParticle() { IncrementSize(); return iterator(*this, Size()-1); } + iterator NewParticle() { IncrementSize(); return iterator(*this, GetSize()-1); } void DeleteLast() { DecrementSize(); } }; -- GitLab