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 unordered_maps handled by Geant4 analysis
0028 //
0029 // Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024
0030 
0031 #ifndef G4AccUnorderedMap_h
0032 #define G4AccUnorderedMap_h 1
0033 
0034 #include "G4VAccumulable.hh"
0035 #include "G4MergeMode.hh"
0036 
0037 #include "globals.hh"
0038 
0039 #include <unordered_map>
0040 
0041 template <class Key,
0042           class T,
0043           class Hash = std::hash<Key>,
0044           class KeyEqual = std::equal_to<Key>,
0045           class Allocator = std::allocator<std::pair<const Key, T>>>
0046 class G4AccUnorderedMap : public G4VAccumulable
0047 {
0048   public:
0049     // ctors to be supported
0050     // (https://en.cppreference.com/w/cpp/container/unordered_map/unordered_map)
0051     //
0052     // Default constructor (1) - Constructs an empty container.
0053     // unordered_map();
0054     //
0055     // Constructor (2) - Constructs an empty container.
0056     // explicit unordered_map( size_type bucket_count,
0057     //                         const Hash& hash = Hash(),
0058     //                         const KeyEqual& equal = KeyEqual(),
0059     //                         const Allocator& alloc = Allocator() );
0060     //
0061     // Constructor (3) - Constructs an empty container.
0062     // unordered_map( size_type bucket_count,
0063     //                const Allocator& alloc )
0064     //   : unordered_map(bucket_count, Hash(), KeyEqual(), alloc) {}
0065     //
0066     // Constructor (4)  (since C++14)
0067     // Constructs empty container.
0068     // unordered_map( size_type bucket_count,
0069     //                const Hash& hash,
0070     //                const Allocator& alloc )
0071     //     : unordered_map(bucket_count, hash, KeyEqual(), alloc) {}
0072     //
0073     // Constructor  (5) (since C++11)
0074     // Constructs empty container.
0075     // explicit unordered_map( const Allocator& alloc );
0076     //
0077     // (6,7,8) (since C++11)
0078     // Constructs the container with the contents of the range
0079     // - skipped
0080     //
0081     // (9) (since C++11)  - Copy constructor.
0082     // unordered_map( const unordered_map& other );
0083     //
0084     // (10) (since C++11) - Copy constructor with provided allocator
0085     // unordered_map( const unordered_map& other, const Allocator& alloc );
0086     //
0087     // (11)  (since C++11) - Move constructor.
0088     // unordered_map( unordered_map&& other );
0089     //
0090     // (12)  (since C++11) - Move constructor with provided allocator
0091     // unordered_map( unordered_map&& other, const Allocator& alloc );
0092     //
0093     // (13)  (since C++11) - Initializer-list constructor.
0094     // Constructs the container with the contents of the initializer list init
0095     // unordered_map( std::initializer_list<value_type> init,
0096     //                size_type bucket_count = /* implementation-defined */,
0097     //                const Hash& hash = Hash(),
0098     //                const KeyEqual& equal = KeyEqual(),
0099     //                const Allocator& alloc = Allocator() );
0100     //
0101     // (14)  (since C++11) - Initializer-list constructor.
0102     // Constructs the container with the contents of the initializer list init
0103     // unordered_map( std::initializer_list<value_type> init,
0104     //                size_type bucket_count,
0105     //                const Allocator& alloc )
0106     //     : unordered_map(init, bucket_count,
0107     //                     Hash(), KeyEqual(), alloc) {}
0108     //
0109     // (15)  (since C++14) - Initializer-list constructor.
0110     // unordered_map( std::initializer_list<value_type> init,
0111     //                size_type bucket_count,
0112     //                const Hash& hash,
0113     //                const Allocator& alloc )
0114     //     : unordered_map(init, bucket_count,
0115     //                     hash, KeyEqual(), alloc) {}
0116     //
0117     // (16,17)  (since C++23)
0118     // Constructs the container with the contents of rg.
0119     // - skipped
0120 
0121 
0122     // Default constructor (1)
0123     // Constructs an empty container with all defaults.
0124     G4AccUnorderedMap(const G4String& name = "",
0125                       G4MergeMode mergeMode = G4MergeMode::kAddition);
0126 
0127     // Constructor (2)
0128     // Constructs an empty container with the given bucket_count
0129     G4AccUnorderedMap(std::size_t bucket_count,
0130                       G4MergeMode mergeMode = G4MergeMode::kAddition,
0131                       const Allocator& alloc = Allocator());
0132 
0133     // Constructor (2) with name
0134     // Constructs an empty container with the given bucket_count and name
0135     G4AccUnorderedMap(const G4String& name,
0136                       std::size_t bucket_count,
0137                       G4MergeMode mergeMode = G4MergeMode::kAddition,
0138                       const Allocator& alloc = Allocator());
0139 
0140     // Constructor (3)
0141     // Constructs an empty container with the given bucket_count and allocator
0142     G4AccUnorderedMap(std::size_t bucket_count,
0143                       const Allocator& alloc,
0144                       G4MergeMode mergeMode = G4MergeMode::kAddition);
0145 
0146     // Constructor (3) with name
0147     // Constructs an empty container with the given bucket_count, allocator and name
0148     G4AccUnorderedMap(const G4String& name,
0149                       std::size_t bucket_count,
0150                       const Allocator& alloc,
0151                       G4MergeMode mergeMode = G4MergeMode::kAddition);
0152     // Constructor (4)
0153     // Constructs an empty container with the given bucket_count, allocator and hash
0154     G4AccUnorderedMap(std::size_t bucket_count,
0155                       const Hash& hash,
0156                       const Allocator& alloc,
0157                       G4MergeMode mergeMode = G4MergeMode::kAddition);
0158 
0159     // Constructor (4) with name
0160     // Constructs an empty container with the given bucket_count, allocator, hash and name
0161     G4AccUnorderedMap(const G4String& name,
0162                       std::size_t bucket_count,
0163                       const Hash& hash,
0164                       const Allocator& alloc,
0165                       G4MergeMode mergeMode = G4MergeMode::kAddition);
0166     // Constructor (5)
0167     // Constructs an empty container with the given allocator alloc.
0168     G4AccUnorderedMap(const Allocator& alloc,
0169                       G4MergeMode mergeMode = G4MergeMode::kAddition);
0170 
0171     // Constructor (5) with name
0172     // Constructs an empty container with the given allocator alloc and name
0173     G4AccUnorderedMap(const G4String& name,
0174                       const Allocator& alloc,
0175                       G4MergeMode mergeMode = G4MergeMode::kAddition);
0176 
0177     // Constructor (13)
0178     // Constructs the container with the contents of the initializer list init.
0179     G4AccUnorderedMap(std::initializer_list<std::pair<const Key,T>> init,
0180                       G4MergeMode mergeMode = G4MergeMode::kAddition,
0181                       std::size_t bucket_count = 0,
0182                       const Hash& hash = Hash(),
0183                       const KeyEqual& equal = KeyEqual(),
0184                       const Allocator& alloc = Allocator() );
0185 
0186     // Constructor (13) with name
0187     // Constructs the container with the contents of the initializer list init.
0188     G4AccUnorderedMap(const G4String& name,
0189                       std::initializer_list<std::pair<const Key,T>> init,
0190                       G4MergeMode mergeMode = G4MergeMode::kAddition,
0191                       std::size_t bucket_count = 0,
0192                       const Hash& hash = Hash(),
0193                       const KeyEqual& equal = KeyEqual(),
0194                       const Allocator& alloc = Allocator() );
0195 
0196     // Copy constructor
0197     G4AccUnorderedMap(const G4AccUnorderedMap& rhs) = default;
0198     G4AccUnorderedMap(const G4AccUnorderedMap& rhs, const Allocator& allocator);
0199     // Move constructor
0200     G4AccUnorderedMap(G4AccUnorderedMap&& rhs) = default;
0201     G4AccUnorderedMap(G4AccUnorderedMap&& rhs, const Allocator& allocator);
0202 
0203     // Destructor
0204     ~G4AccUnorderedMap() override = default;
0205 
0206     // std::unordered_map functions, operators
0207     // operator []
0208     inline T& operator[](const Key& key) { return fUMap[key]; }
0209     inline T& operator[](Key&& key) { return fUMap[std::move(key)]; }
0210     // at
0211     inline T& at(const Key& key) { return fUMap[key]; }
0212     inline const T& at(const Key& key ) const { return fUMap[key]; }
0213     // size
0214     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size() const { return fUMap.size(); }
0215     // begin, cbegin
0216     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator begin() { return fUMap.begin(); }
0217     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator begin() const { return fUMap.begin(); }
0218     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator cbegin() const { return fUMap.cbegin(); }
0219     // end, cend
0220     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator end() { return fUMap.end(); }
0221     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator end() const { return fUMap.end(); }
0222     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator cend() const { return fUMap.cend(); }
0223     // clear
0224     inline void clear() { fUMap.clear(); }
0225     // insert
0226     inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert(const T& value) { return fUMap.insert(value); }
0227     template< class P >
0228     inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert( P&& value ) { return fUMap.insert(std::move(value)); }
0229     inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert( T&& value ) { return fUMap.insert(std::move(value)); }   
0230     // find
0231     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator find( const Key& key ) { return fUMap.find(key); }
0232     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator find( const Key& key ) const { return fUMap.find(key); }
0233 
0234     // Methods
0235     void Merge(const G4VAccumulable& other) final;
0236     void Reset() final;
0237     void Print(G4PrintOptions options = G4PrintOptions()) const final;
0238     void SetMergeMode(G4MergeMode value) final;
0239     void SetInitValue(const T& value);
0240 
0241     // Get methods
0242     G4AccType GetType() const final { return G4AccType::kUnorderedMap; }
0243     std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& GetUnorderedMap() { return fUMap; }
0244     const std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& GetUnorderedMap() const { return fUMap; }
0245 
0246   private:
0247     // Data members
0248     std::unordered_map<Key, T, Hash, KeyEqual, Allocator> fUMap;
0249     T fInitValue = 0;
0250     G4MergeFunction<T> fMergeFunction;
0251  };
0252 
0253 // inline functions
0254 
0255 #include "G4AccUnorderedMap.icc"
0256 
0257 #endif