Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-04-19 09:13:36

0001 // -*- C++ -*-
0002 //
0003 // This file is part of YODA -- Yet more Objects for Data Analysis
0004 // Copyright (C) 2008-2024 The YODA collaboration (see AUTHORS for details)
0005 //
0006 #ifndef YODA_INDEXEDSET_H
0007 #define YODA_INDEXEDSET_H
0008 
0009 #include <set>
0010 #include <stdexcept>
0011 #include <iostream>
0012 
0013 namespace YODA {
0014   namespace Utils {
0015 
0016 
0017     /// @brief Specialisation of std::set to allow indexed access to ordered set elements
0018     ///
0019     /// The STL set is already implemented in an ordered style, so this specialisation is
0020     /// implemented in a super-simple way by just iterating over the set elements until
0021     /// the desired index is reached. This is not scalable, of course, but this implementation
0022     /// is for use with histograms, which will not have squillions of entries.
0023     template <typename T>
0024     class indexedset : public std::set<T> {
0025     public:
0026 
0027 
0028       /// @brief Const index-access operator
0029       ///
0030       /// Non-const is not permitted with sets (== self-keyed maps) as it would change ordering.
0031       const T& operator[](size_t index) const {
0032         if (index >= this->size()) {
0033           throw std::range_error("Requested index larger than indexed set size");
0034         }
0035         size_t i = 0;
0036         for (typename indexedset<T>::const_iterator it = this->begin(); it != this->end(); ++it) {
0037           // std::cout << i << "/" << index << ", " << (*it) << std::endl;
0038           if (i == index) return *it;
0039           i += 1;
0040         }
0041         // This should never be called: just keeping the compiler happy:
0042         return (*this)[0];
0043       }
0044 
0045 
0046     };
0047 
0048 
0049   }
0050 }
0051 
0052 #endif