Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:13:55

0001 /// \file TypeMap.h
0002 /// \author Johannes de Fine Licht (johannes.definelicht@cern.ch)
0003 
0004 #ifndef VECGEOM_BASE_TYPEMAP_H_
0005 #define VECGEOM_BASE_TYPEMAP_H_
0006 
0007 #include "VecGeom/base/Global.h"
0008 
0009 #include <map>
0010 
0011 namespace vecgeom {
0012 inline namespace VECGEOM_IMPL_NAMESPACE {
0013 
0014 /**
0015  * @brief Bidirectional map between two distinct types.
0016  * @details Implemented to accommodate mapping between two representations of
0017  *          geometry without having to store pointers in the geometry objects
0018  *          themselves.
0019  */
0020 template <typename TypeA, typename TypeB>
0021 class BidirectionalTypeMap {
0022 
0023 private:
0024   std::map<TypeA, TypeB> a_to_b;
0025   std::map<TypeB, TypeA> b_to_a;
0026 
0027 public:
0028   BidirectionalTypeMap() : a_to_b(), b_to_a() {}
0029   BidirectionalTypeMap(BidirectionalTypeMap const &other);
0030   BidirectionalTypeMap &operator=(BidirectionalTypeMap const &other);
0031 
0032   /**
0033    * Lookup variable of second type using variable of first type. The accessed
0034    * element is asserted to exist, crashing if it isn't found.
0035    * @return Constant reference to element. Unlike std::map, changes to content
0036    *         should be done using the Set() function.
0037    */
0038   TypeB const &operator[](TypeA const &a) const
0039   {
0040     const typename std::map<TypeA, TypeB>::const_iterator i = a_to_b.find(a);
0041     // Crash if not found. To prevent insertion of new elements by lookup.
0042     assert(i != a_to_b.end());
0043     return i->second;
0044   }
0045 
0046   /**
0047    * Lookup variable of first type using variable of second type. The accessed
0048    * element is asserted to exist, crashing if it isn't found.
0049    * @return Constant reference to element. Unlike std::map, changes to content
0050    *         should be done using the Set() function.
0051    */
0052   TypeA const &operator[](TypeB const &b) const
0053   {
0054     const typename std::map<TypeB, TypeA>::const_iterator i = b_to_a.find(b);
0055     // Crash if not found. To prevent insertion of new elements by lookup.
0056     assert(i != b_to_a.end());
0057     return i->second;
0058   }
0059 
0060   /**
0061    * Maps the two arguments to each other. The order of which arguments are
0062    * passed is inconsequential.
0063    */
0064   void Set(TypeA const &a, TypeB const &b)
0065   {
0066     a_to_b[a] = b;
0067     b_to_a[b] = a;
0068   }
0069 
0070   /**
0071    * Maps the two arguments to each other. The order of which arguments are
0072    * passed is inconsequential.
0073    */
0074   void Set(TypeB const &b, TypeA const &a)
0075   {
0076     a_to_b[a] = b;
0077     b_to_a[b] = a;
0078   }
0079 
0080   /**
0081    * @return Constant iterator to the beginning of the map from the first type
0082    *         to the second. Values of both can be accessed by accessing the
0083    *         fields ->first and ->second of iterator.
0084    */
0085   typename std::map<TypeA, TypeB>::const_iterator begin() const { return a_to_b.begin(); }
0086 
0087   /**
0088    * @return Constant iterator to the end of the map from the first type
0089    *         to the second. Values of both can be accessed by accessing the
0090    *         fields ->first and ->second of iterator.
0091    */
0092   typename std::map<TypeA, TypeB>::const_iterator end() const { return a_to_b.end(); }
0093 
0094   /**
0095    * @return True is the passed element is contined in the bidirectional map,
0096    *         false if it isn't.
0097    */
0098   bool Contains(TypeA const &a) const { return a_to_b.find(a) != a_to_b.end(); }
0099 
0100   /**
0101    * @return True is the passed element is contined in the bidirectional map,
0102    *         false if it isn't.
0103    */
0104   bool Contains(TypeB const &b) const { return b_to_a.find(b) != b_to_a.end(); }
0105 
0106   /**
0107    * remove any content from the containers
0108    */
0109   void Clear()
0110   {
0111     b_to_a.clear();
0112     a_to_b.clear();
0113   }
0114 };
0115 }
0116 } // End global namespace
0117 
0118 #endif // VECGEOM_BASE_TYPEMAP_H_