Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-02 08:28:13

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 
0027 // Class template for accumulable maps handled by Geant4 analysis
0028 //
0029 // Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024
0030 
0031 #ifndef G4AccMap_h
0032 #define G4AccMap_h 1
0033 
0034 #include "G4VAccumulable.hh"
0035 #include "G4MergeMode.hh"
0036 
0037 #include "globals.hh"
0038 
0039 #include <map>
0040 
0041 template <class Key,
0042           class T,
0043           class Compare = std::less<Key>,
0044           class Allocator = std::allocator<std::pair<const Key, T>>>
0045 class G4AccMap : public G4VAccumulable
0046 {
0047   public:
0048     // ctors to be supported
0049     // (https://en.cppreference.com/w/cpp/container/map/map)
0050     //
0051     // Default constructor (1) - Constructs an empty container.
0052     // map();
0053     //
0054     // Constructor (2) - Constructs an empty container.
0055     // explicit map(const Compare& comp,
0056     //              const Allocator& alloc = Allocator());
0057     //
0058     // Constructor (3) - Constructs an empty container.
0059     // explicit map(const Allocator& alloc);
0060     //
0061     // 4,5) Constructs the container with the contents of the range [first, last).
0062     // - skipped
0063     //
0064     // Constructor (6) - Copy constructor.
0065     // Constructs the container with the copy of the contents of other.
0066     // map( const map& other );
0067     //
0068     // Constructor (7) - Copy constructor with provided allocator
0069     // map( const map& other, const Allocator& alloc );
0070     //
0071     // Constructor (8) - Move constructor.
0072     // Constructs the container with the contents of other using move semantics.
0073     // map(map&& other);
0074     //
0075     // Constructor (9) - Move constructor with provided allocator
0076     // Constructs the container with the contents of other using move semantics.
0077     // map(map&& other, const Allocator& alloc);
0078     //
0079     // Constructor (10) - Initializer-list constructor.
0080     // Constructs the container with the contents of the initializer list init
0081     // map(std::initializer_list<value_type> init,
0082     //     const Compare& comp = Compare(),
0083     //     const Allocator& alloc = Allocator());
0084     //
0085     // Constructor (11) - Initializer-list constructor.
0086     // Constructs the container with the contents of the initializer list init
0087     // map(std::initializer_list<value_type> init,
0088     //     const Allocator& alloc);
0089     // - skipped
0090     //
0091     // (since C++20)
0092     // (12,13) Constructs the container with the contents of rg.
0093     // - skipped
0094 
0095     // Default constructor (1)
0096     // Constructs an empty container with all defaults.
0097     G4AccMap(const G4String& name = "",
0098              G4MergeMode mergeMode = G4MergeMode::kAddition);
0099 
0100     // Constructor (2)
0101     // Constructs an empty container with the given compare.
0102     G4AccMap(const Compare& comp,
0103              G4MergeMode mergeMode = G4MergeMode::kAddition,
0104              const Allocator& alloc = Allocator());
0105 
0106     // Constructor (2) with name
0107     // Constructs an empty container with the given compare with name
0108     G4AccMap(const G4String& name,
0109              const Compare& comp,
0110              G4MergeMode mergeMode = G4MergeMode::kAddition,
0111              const Allocator& alloc = Allocator());
0112 
0113     // Constructor (3)
0114     // Constructs an empty container with the given allocator alloc.
0115     G4AccMap(const Allocator& alloc,
0116              G4MergeMode mergeMode = G4MergeMode::kAddition);
0117 
0118     // Constructor (3) with name
0119     // Constructs an empty container with the given allocator alloc and name
0120     G4AccMap(const G4String& name,
0121              const Allocator& alloc,
0122              G4MergeMode mergeMode = G4MergeMode::kAddition);
0123 
0124     // Constructor (10)
0125     // Constructs the container with the contents of the initializer list init.
0126     G4AccMap(std::initializer_list<std::pair<const Key,T>> init,
0127              G4MergeMode mergeMode = G4MergeMode::kAddition,
0128              const Compare& comp = Compare(),
0129              const Allocator& alloc = Allocator());
0130 
0131     // Constructor (10) with name
0132     // Constructs the container with the contents of the initializer list init.
0133     G4AccMap(const G4String& name,
0134              std::initializer_list<std::pair<const Key,T>> init,
0135              G4MergeMode mergeMode = G4MergeMode::kAddition,
0136              const Compare& comp = Compare(),
0137              const Allocator& alloc = Allocator());
0138 
0139     // Copy constructor
0140     G4AccMap(const G4AccMap& rhs) = default;
0141     G4AccMap(const G4AccMap& rhs, const Allocator& allocator);
0142     // Move constructor
0143     G4AccMap(G4AccMap&& rhs) = default;
0144     G4AccMap(G4AccMap&& rhs, const Allocator& allocator);
0145 
0146     // Destructor
0147     ~G4AccMap() override = default;
0148 
0149     // std::map functions, operators
0150     // operator []
0151     inline T& operator[](const Key& key) { return fMap[key]; }
0152     inline T& operator[](Key&& key) { return fMap[std::move(key)]; }
0153     // at
0154     inline T& at(const Key& key) { return fMap[key]; }
0155     inline const T& at(const Key& key ) const { return fMap[key]; }
0156     // size
0157     inline typename std::map<Key, T, Compare, Allocator>::size_type size() const { return fMap.size(); }
0158     // begin, cbegin
0159     inline typename std::map<Key, T, Compare, Allocator>::iterator begin() { return fMap.begin(); }
0160     inline typename std::map<Key, T, Compare, Allocator>::const_iterator begin() const { return fMap.begin(); }
0161     inline typename std::map<Key, T, Compare, Allocator>::const_iterator cbegin() const { return fMap.cbegin(); }
0162     // end, cend
0163     inline typename std::map<Key, T, Compare, Allocator>::iterator end() { return fMap.end(); }
0164     inline typename std::map<Key, T, Compare, Allocator>::const_iterator end() const { return fMap.end(); }
0165     inline typename std::map<Key, T, Compare, Allocator>::const_iterator cend() const { return fMap.cend(); }
0166     // clear
0167     inline void clear() { fMap.clear(); }
0168     // insert
0169     inline std::pair<typename std::map<Key, T, Compare, Allocator>::iterator, bool> insert(const T& value) { return fMap.insert(value); }
0170     template< class P >
0171     inline std::pair<typename std::map<Key, T, Compare, Allocator>::iterator, bool> insert( P&& value ) { return fMap.insert(std::move(value)); }
0172     inline std::pair<typename std::map<Key, T, Compare, Allocator>::iterator, bool> insert( T&& value ) { return fMap.insert(std::move(value)); }   
0173     // find
0174     inline typename std::map<Key, T, Compare, Allocator>::iterator find( const Key& key ) { return fMap.find(key); }
0175     inline typename std::map<Key, T, Compare, Allocator>::const_iterator find( const Key& key ) const { return fMap.find(key); }
0176 
0177     // Methods
0178     void Merge(const G4VAccumulable& other) final;
0179     void Reset() final;
0180     void Print(G4PrintOptions options = G4PrintOptions()) const final;
0181     void SetMergeMode(G4MergeMode value) final;
0182     void SetInitValue(const T& value);
0183 
0184     // Get methods
0185     G4AccType GetType() const final { return G4AccType::kMap; }
0186     std::map<Key, T, Compare, Allocator>& GetMap() { return fMap; }
0187     const std::map<Key, T, Compare, Allocator>& GetMap() const { return fMap; }
0188 
0189   private:
0190     // Data members
0191     std::map<Key, T, Compare, Allocator> fMap {};
0192     T fInitValue = 0;
0193     G4MergeFunction<T> fMergeFunction;
0194  };
0195 
0196 // inline functions
0197 
0198 #include "G4AccMap.icc"
0199 
0200 #endif