Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file orange/orangeinp/detail/ProtoMap.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include <unordered_map>
0011 #include <vector>
0012 
0013 #include "orange/OrangeTypes.hh"
0014 
0015 namespace celeritas
0016 {
0017 namespace orangeinp
0018 {
0019 class ProtoInterface;
0020 
0021 namespace detail
0022 {
0023 //---------------------------------------------------------------------------//
0024 /*!
0025  * Set up and access universe ordering.
0026  *
0027  * On construction this builds a breadth-first ordered list of protos:
0028  * the input "global" universe will always be at the front of the list, and
0029  * universes may only depend on a universe with a larger ID.
0030  *
0031  * This is used by \c ProtoInterface::build as two-way map
0032  * between universe IDs and pointers to Proto interfaces. It \em must not
0033  * exceed the lifetime of any of the protos.
0034  */
0035 class ProtoMap
0036 {
0037   public:
0038     // Construct with global proto for ordering
0039     explicit ProtoMap(ProtoInterface const& global);
0040 
0041     // Get the proto corresponding to a universe ID
0042     inline ProtoInterface const* at(UniverseId) const;
0043 
0044     // Find the universe ID for a given proto pointer (or raise)
0045     inline UniverseId find(ProtoInterface const*) const;
0046 
0047     //! Get the number of protos to build
0048     UniverseId::size_type size() const { return protos_.size(); }
0049 
0050   private:
0051     std::vector<ProtoInterface const*> protos_;
0052     std::unordered_map<ProtoInterface const*, UniverseId> uids_;
0053 };
0054 
0055 //---------------------------------------------------------------------------//
0056 // INLINE DEFINITIONS
0057 //---------------------------------------------------------------------------//
0058 /*!
0059  * Get the proto corresponding to a universe ID.
0060  */
0061 ProtoInterface const* ProtoMap::at(UniverseId uid) const
0062 {
0063     CELER_EXPECT(uid < this->size());
0064     return protos_[uid.unchecked_get()];
0065 }
0066 
0067 //---------------------------------------------------------------------------//
0068 /*!
0069  * Find the universe ID for a given proto pointer (or raise).
0070  */
0071 UniverseId ProtoMap::find(ProtoInterface const* proto) const
0072 {
0073     CELER_EXPECT(proto);
0074     auto iter = uids_.find(proto);
0075     CELER_EXPECT(iter != uids_.end());
0076     return iter->second;
0077 }
0078 
0079 //---------------------------------------------------------------------------//
0080 }  // namespace detail
0081 }  // namespace orangeinp
0082 }  // namespace celeritas