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.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 //
  2 // ********************************************************************
  3 // * License and Disclaimer                                           *
  4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.                             *
 10 // *                                                                  *
 11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                                                  *
 18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // ********************************************************************
 25 //
 26 
 27 // Class template for accumulable unordered_maps handled by Geant4 analysis
 28 //
 29 // Author: Ivana Hrivnacova, IJCLab IN2P3/CNRS, 19/07/2024
 30 
 31 #ifndef G4AccUnorderedMap_h
 32 #define G4AccUnorderedMap_h 1
 33 
 34 #include "G4VAccumulable.hh"
 35 #include "G4MergeMode.hh"
 36 
 37 #include "globals.hh"
 38 
 39 #include <unordered_map>
 40 
 41 template <class Key,
 42           class T,
 43           class Hash = std::hash<Key>,
 44           class KeyEqual = std::equal_to<Key>,
 45           class Allocator = std::allocator<std::pair<const Key, T>>>
 46 class G4AccUnorderedMap : public G4VAccumulable
 47 {
 48   public:
 49     // ctors to be supported
 50     // (https://en.cppreference.com/w/cpp/container/unordered_map/unordered_map)
 51     //
 52     // Default constructor (1) - Constructs an empty container.
 53     // unordered_map();
 54     //
 55     // Constructor (2) - Constructs an empty container.
 56     // explicit unordered_map( size_type bucket_count,
 57     //                         const Hash& hash = Hash(),
 58     //                         const KeyEqual& equal = KeyEqual(),
 59     //                         const Allocator& alloc = Allocator() );
 60     //
 61     // Constructor (3) - Constructs an empty container.
 62     // unordered_map( size_type bucket_count,
 63     //                const Allocator& alloc )
 64     //   : unordered_map(bucket_count, Hash(), KeyEqual(), alloc) {}
 65     //
 66     // Constructor (4)  (since C++14)
 67     // Constructs empty container.
 68     // unordered_map( size_type bucket_count,
 69     //                const Hash& hash,
 70     //                const Allocator& alloc )
 71     //     : unordered_map(bucket_count, hash, KeyEqual(), alloc) {}
 72     //
 73     // Constructor  (5) (since C++11)
 74     // Constructs empty container.
 75     // explicit unordered_map( const Allocator& alloc );
 76     //
 77     // (6,7,8) (since C++11)
 78     // Constructs the container with the contents of the range
 79     // - skipped
 80     //
 81     // (9) (since C++11)  - Copy constructor.
 82     // unordered_map( const unordered_map& other );
 83     //
 84     // (10) (since C++11) - Copy constructor with provided allocator
 85     // unordered_map( const unordered_map& other, const Allocator& alloc );
 86     //
 87     // (11)  (since C++11) - Move constructor.
 88     // unordered_map( unordered_map&& other );
 89     //
 90     // (12)  (since C++11) - Move constructor with provided allocator
 91     // unordered_map( unordered_map&& other, const Allocator& alloc );
 92     //
 93     // (13)  (since C++11) - Initializer-list constructor.
 94     // Constructs the container with the contents of the initializer list init
 95     // unordered_map( std::initializer_list<value_type> init,
 96     //                size_type bucket_count = /* implementation-defined */,
 97     //                const Hash& hash = Hash(),
 98     //                const KeyEqual& equal = KeyEqual(),
 99     //                const Allocator& alloc = Allocator() );
100     //
101     // (14)  (since C++11) - Initializer-list constructor.
102     // Constructs the container with the contents of the initializer list init
103     // unordered_map( std::initializer_list<value_type> init,
104     //                size_type bucket_count,
105     //                const Allocator& alloc )
106     //     : unordered_map(init, bucket_count,
107     //                     Hash(), KeyEqual(), alloc) {}
108     //
109     // (15)  (since C++14) - Initializer-list constructor.
110     // unordered_map( std::initializer_list<value_type> init,
111     //                size_type bucket_count,
112     //                const Hash& hash,
113     //                const Allocator& alloc )
114     //     : unordered_map(init, bucket_count,
115     //                     hash, KeyEqual(), alloc) {}
116     //
117     // (16,17)  (since C++23)
118     // Constructs the container with the contents of rg.
119     // - skipped
120 
121 
122     // Default constructor (1)
123     // Constructs an empty container with all defaults.
124     G4AccUnorderedMap(const G4String& name = "",
125                       G4MergeMode mergeMode = G4MergeMode::kAddition);
126 
127     // Constructor (2)
128     // Constructs an empty container with the given bucket_count
129     G4AccUnorderedMap(std::size_t bucket_count,
130                       G4MergeMode mergeMode = G4MergeMode::kAddition,
131                       const Allocator& alloc = Allocator());
132 
133     // Constructor (2) with name
134     // Constructs an empty container with the given bucket_count and name
135     G4AccUnorderedMap(const G4String& name,
136                       std::size_t bucket_count,
137                       G4MergeMode mergeMode = G4MergeMode::kAddition,
138                       const Allocator& alloc = Allocator());
139 
140     // Constructor (3)
141     // Constructs an empty container with the given bucket_count and allocator
142     G4AccUnorderedMap(std::size_t bucket_count,
143                       const Allocator& alloc,
144                       G4MergeMode mergeMode = G4MergeMode::kAddition);
145 
146     // Constructor (3) with name
147     // Constructs an empty container with the given bucket_count, allocator and name
148     G4AccUnorderedMap(const G4String& name,
149                       std::size_t bucket_count,
150                       const Allocator& alloc,
151                       G4MergeMode mergeMode = G4MergeMode::kAddition);
152     // Constructor (4)
153     // Constructs an empty container with the given bucket_count, allocator and hash
154     G4AccUnorderedMap(std::size_t bucket_count,
155                       const Hash& hash,
156                       const Allocator& alloc,
157                       G4MergeMode mergeMode = G4MergeMode::kAddition);
158 
159     // Constructor (4) with name
160     // Constructs an empty container with the given bucket_count, allocator, hash and name
161     G4AccUnorderedMap(const G4String& name,
162                       std::size_t bucket_count,
163                       const Hash& hash,
164                       const Allocator& alloc,
165                       G4MergeMode mergeMode = G4MergeMode::kAddition);
166     // Constructor (5)
167     // Constructs an empty container with the given allocator alloc.
168     G4AccUnorderedMap(const Allocator& alloc,
169                       G4MergeMode mergeMode = G4MergeMode::kAddition);
170 
171     // Constructor (5) with name
172     // Constructs an empty container with the given allocator alloc and name
173     G4AccUnorderedMap(const G4String& name,
174                       const Allocator& alloc,
175                       G4MergeMode mergeMode = G4MergeMode::kAddition);
176 
177     // Constructor (13)
178     // Constructs the container with the contents of the initializer list init.
179     G4AccUnorderedMap(std::initializer_list<std::pair<const Key,T>> init,
180                       G4MergeMode mergeMode = G4MergeMode::kAddition,
181                       std::size_t bucket_count = 0,
182                       const Hash& hash = Hash(),
183                       const KeyEqual& equal = KeyEqual(),
184                       const Allocator& alloc = Allocator() );
185 
186     // Constructor (13) with name
187     // Constructs the container with the contents of the initializer list init.
188     G4AccUnorderedMap(const G4String& name,
189                       std::initializer_list<std::pair<const Key,T>> init,
190                       G4MergeMode mergeMode = G4MergeMode::kAddition,
191                       std::size_t bucket_count = 0,
192                       const Hash& hash = Hash(),
193                       const KeyEqual& equal = KeyEqual(),
194                       const Allocator& alloc = Allocator() );
195 
196     // Copy constructor
197     G4AccUnorderedMap(const G4AccUnorderedMap& rhs) = default;
198     G4AccUnorderedMap(const G4AccUnorderedMap& rhs, const Allocator& allocator);
199     // Move constructor
200     G4AccUnorderedMap(G4AccUnorderedMap&& rhs) = default;
201     G4AccUnorderedMap(G4AccUnorderedMap&& rhs, const Allocator& allocator);
202 
203     // Destructor
204     ~G4AccUnorderedMap() override = default;
205 
206     // std::unordered_map functions, operators
207     // operator []
208     inline T& operator[](const Key& key) { return fUMap[key]; }
209     inline T& operator[](Key&& key) { return fUMap[std::move(key)]; }
210     // at
211     inline T& at(const Key& key) { return fUMap[key]; }
212     inline const T& at(const Key& key ) const { return fUMap[key]; }
213     // size
214     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::size_type size() const { return fUMap.size(); }
215     // begin, cbegin
216     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator begin() { return fUMap.begin(); }
217     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator begin() const { return fUMap.begin(); }
218     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator cbegin() const { return fUMap.cbegin(); }
219     // end, cend
220     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator end() { return fUMap.end(); }
221     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator end() const { return fUMap.end(); }
222     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator cend() const { return fUMap.cend(); }
223     // clear
224     inline void clear() { fUMap.clear(); }
225     // insert
226     inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert(const T& value) { return fUMap.insert(value); }
227     template< class P >
228     inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert( P&& value ) { return fUMap.insert(std::move(value)); }
229     inline std::pair<typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator, bool> insert( T&& value ) { return fUMap.insert(std::move(value)); }   
230     // find
231     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::iterator find( const Key& key ) { return fUMap.find(key); }
232     inline typename std::unordered_map<Key, T, Hash, KeyEqual, Allocator>::const_iterator find( const Key& key ) const { return fUMap.find(key); }
233 
234     // Methods
235     void Merge(const G4VAccumulable& other) final;
236     void Reset() final;
237     void Print(G4PrintOptions options = G4PrintOptions()) const final;
238     void SetMergeMode(G4MergeMode value) final;
239     void SetInitValue(const T& value);
240 
241     // Get methods
242     G4AccType GetType() const final { return G4AccType::kUnorderedMap; }
243     std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& GetUnorderedMap() { return fUMap; }
244     const std::unordered_map<Key, T, Hash, KeyEqual, Allocator>& GetUnorderedMap() const { return fUMap; }
245 
246   private:
247     // Data members
248     std::unordered_map<Key, T, Hash, KeyEqual, Allocator> fUMap;
249     T fInitValue = 0;
250     G4MergeFunction<T> fMergeFunction;
251  };
252 
253 // inline functions
254 
255 #include "G4AccUnorderedMap.icc"
256 
257 #endif
258