Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-26 09:15:35

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