8 #ifndef LARDATA_UTILITIES_MAKEINDEX_H 9 #define LARDATA_UTILITIES_MAKEINDEX_H 42 template <
typename Coll,
typename KeyOf>
43 std::vector<size_t>
MakeIndex(Coll
const& data, KeyOf key_of = KeyOf())
49 std::vector<size_t> Index(data.size(), std::numeric_limits<size_t>::max());
54 for (
auto const& datum : data) {
55 size_t key = size_t(key_of(datum));
56 if (key >= min_size) min_size = key + 1;
57 if (Index.size() <= key) {
59 Index.resize(std::max(key + 1, Index.size() * 2), std::numeric_limits<size_t>::max());
64 Index.resize(min_size);
95 template <
typename Coll,
typename KeyOf>
96 auto MakeMap(Coll
const& data, KeyOf key_of = KeyOf())
97 ->
std::vector<decltype(key_of(*(data.begin())))
const*>
99 using Mapped_t = decltype(key_of(*(data.begin())));
100 using Ptr_t = Mapped_t
const*;
101 using Map_t = std::vector<Ptr_t>;
106 Map_t Index(data.size(),
nullptr);
110 for (
auto const& datum : data) {
111 size_t key = size_t(key_of(datum));
112 if (key >= min_size) min_size = key + 1;
113 if (Index.size() <= key) {
115 Index.resize(std::max(key + 1, Index.size() * 2),
nullptr);
119 Index.resize(min_size);
125 #endif // LARDATA_UTILITIES_MAKEINDEX_H Namespace for general, non-LArSoft-specific utilities.
std::vector< size_t > MakeIndex(Coll const &data, KeyOf key_of=KeyOf())
Creates a map of indices from an existing collection.
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
auto MakeMap(Coll const &data, KeyOf key_of=KeyOf()) -> std::vector< decltype(key_of(*(data.begin()))) const * >
Creates a map of objects from an existing collection.