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()) {
53 for (
auto const& datum: data) {
54 size_t key = size_t(key_of(datum));
55 if (key >= min_size) min_size = key + 1;
56 if (Index.size() <= key) {
66 Index.resize(min_size);
98 template <
typename Coll,
typename KeyOf>
99 auto MakeMap(Coll
const& data, KeyOf key_of = KeyOf())
100 ->
std::vector<decltype(key_of(*(data.begin())))
const*>
102 using Mapped_t = decltype(key_of(*(data.begin())));
103 using Ptr_t = Mapped_t
const*;
104 using Map_t = std::vector<Ptr_t>;
109 Map_t Index(data.size(),
nullptr);
113 for (
auto const& datum: data) {
114 size_t key = size_t(key_of(datum));
115 if (key >= min_size) min_size = key + 1;
116 if (Index.size() <= key) {
118 Index.resize(
std::max(key + 1, Index.size() * 2),
nullptr);
122 Index.resize(min_size);
129 #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.