Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 08:59:57

0001 //------------------------------- -*- C++ -*- -------------------------------//
0002 // Copyright Celeritas contributors: see top-level COPYRIGHT file for details
0003 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0004 //---------------------------------------------------------------------------//
0005 //! \file geocel/detail/MakeLabelVector.hh
0006 //---------------------------------------------------------------------------//
0007 #pragma once
0008 
0009 #include <string>
0010 #include <vector>
0011 
0012 #include "corecel/cont/Range.hh"
0013 #include "corecel/io/Label.hh"
0014 
0015 namespace celeritas
0016 {
0017 namespace detail
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Construct a vector of labels from a map of vectors of pointers.
0022  *
0023  * This implementation detail expects a map of string identifiers to a vector
0024  * of pointers. The \c get_id function should convert a dereferenced item to an
0025  * ID.
0026  */
0027 template<class T, class GetId>
0028 std::vector<Label>
0029 make_label_vector(std::unordered_map<std::string, std::vector<T>> const& names,
0030                   GetId&& get_id)
0031 {
0032     std::vector<Label> result;
0033     auto add_label = [&result](std::size_t id, Label&& label) {
0034         if (id >= result.size())
0035         {
0036             result.resize(id + 1);
0037         }
0038         result[id] = std::move(label);
0039     };
0040 
0041     for (auto&& [name, items] : names)
0042     {
0043         CELER_ASSERT(!items.empty());
0044         if (items.size() == 1)
0045         {
0046             // Label is just the name since this item is unique
0047             CELER_ASSERT(items.front());
0048             add_label(get_id(items.front()), Label{name});
0049             continue;
0050         }
0051 
0052         // Set labels in increasing order
0053         for (auto idx : range(items.size()))
0054         {
0055             CELER_ASSERT(items[idx]);
0056             add_label(get_id(items[idx]), Label{name, std::to_string(idx)});
0057         }
0058     }
0059     return result;
0060 }
0061 
0062 //---------------------------------------------------------------------------//
0063 }  // namespace detail
0064 }  // namespace celeritas