ATLAS Offline Software
Loading...
Searching...
No Matches
KFromNItr.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3*/
4
6#include <numeric>
7#include <stdexcept>
8
9namespace TrigCompositeUtils {
10 KFromNItr::KFromNItr(std::size_t k, std::size_t N)
11 : m_N(N),
12 m_current(k, 0)
13 {
14 // Fill the iterator with the first combination
15 reset();
16 }
17
19 {
20 // The first combination is just an ascending sequence starting from 0
21 std::iota(m_current.begin(), m_current.end(), 0);
22 }
23
25 {
26 return size() == 0 || m_current.back() >= m_N;
27 }
28
30 {
31 if (exhausted())
32 throw std::runtime_error("Dereferencing past-the-end iterator!");
33 return m_current;
34 }
35
37 {
38 if (exhausted())
39 throw std::runtime_error("Dereferencing past-the-end iterator!");
40 return &m_current;
41 }
42
44 {
45 if (exhausted())
46 // Don't iterate an iterator that is already past the end
47 return *this;
48 // All index combinations pointed to by this iterator are in ascending order.
49 // In order to generate the next combination, go through the following steps,
50 // starting with the highest (last) index
51 //
52 // 1. Increment the current index
53 // 2. If it is less than its end point then go to step 4
54 // 3. If it is equal to its end point then shift the current index to be the
55 // before this one and go to step 1.
56 // 4. Set each index after the current one to larger than its preceding
57 // index's value
58 //
59 // For an example, consider picking 3 values from the range 0 - 4.
60 // The maximum value here is '5'
61 // The combination starts at (0, 1, 2). For the first two increments only the
62 // third index changes as the comparison in step 2 is always true and the
63 // iterator yields the sequence (0, 1, 3), (0, 1, 4).
64 // After this, the third index increments to 5 (the max value) and so we
65 // increment the second index. As this is now less than its maximum value (4)
66 // we reset all following indices in an ascending sequence, yielding (0, 2, 3)
67
68 // Start from the last index
69 auto backItr = m_current.rbegin();
70 // the end point decreases as we go back through the sequence
71 std::size_t end = m_N;
72 while (backItr != m_current.rend())
73 {
74 // This statement increments the value pointed to by backItr, then checks
75 // that it's still below its maximum value
76 if (++(*backItr) < end)
77 {
78 // step 4 - this value is now OK. We have to set each following element
79 // in increasing sequence
80 // backItr.base() returns an iterator pointing to the element *after* this
81 // one
82 std::iota(backItr.base(), m_current.end(), *backItr + 1);
83 break;
84 }
85 ++backItr;
86 --end;
87 }
88 return *this;
89 }
90
92 {
94 KFromNItr ret(*this);
95 // step us past this point
96 this->operator++();
97 return ret;
98 }
99
100 bool KFromNItr::operator==(const KFromNItr &other) const
101 {
102 // All past-the-end iterators compare equal
103 if (exhausted() && other.exhausted())
104 return true;
105 // Otherwise make sure that the iterators point to the same combination
106 return m_current == other.m_current;
107 }
108
109 bool KFromNItr::operator!=(const KFromNItr &other) const
110 {
111 return !(*this == other);
112 }
113} //> end namespace TrigCompositeUtils
bool exhausted() const
True if this iterator is past the end.
Definition KFromNItr.cxx:24
bool operator==(const KFromNItr &other) const
Iterator comparison functions.
bool operator!=(const KFromNItr &other) const
const value_type & reference
Definition KFromNItr.h:40
reference operator*() const
Dereference.
Definition KFromNItr.cxx:29
KFromNItr()=default
Default constructor creates a generic past-the-end iterator.
KFromNItr & operator++()
Pre-increment operator.
Definition KFromNItr.cxx:43
std::size_t size() const
The size of each combination (k)
Definition KFromNItr.h:51
void reset()
Reset the iterator to its start position.
Definition KFromNItr.cxx:18
std::vector< std::size_t > m_current
The current combination.
Definition KFromNItr.h:77
std::size_t m_N
The number of indices.
Definition KFromNItr.h:75
const value_type * pointer
Definition KFromNItr.h:41