Jpp 19.3.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JCombinatorics.hh
Go to the documentation of this file.
1#ifndef __JTOOLS__JCOMBINATORICS__
2#define __JTOOLS__JCOMBINATORICS__
3
4#include <utility>
5#include <vector>
6#include <algorithm>
7
8
9/**
10 * \author mdejong
11 */
12
13namespace JTOOLS {}
14namespace JPP { using namespace JTOOLS; }
15
16namespace JTOOLS {
17
18
19 /**
20 * Auxiliary class to convert pair of indices to unique index and back.
21 */
23 public:
24 /**
25 * Data structure for a pair of indices.
26 */
27 struct pair_type :
28 public std::pair<int, int>
29 {
30 /**
31 * Default constructor.
32 */
34 std::pair<int, int>(-1, -1)
35 {}
36
37
38 /**
39 * Constructor.
40 *
41 * \param first first index
42 * \param second second index
43 */
44 pair_type(const int first, const int second) :
45 std::pair<int, int>(first, second)
46 {}
47
48
49 /**
50 * Less-than operator.
51 *
52 * \param first first pair
53 * \param second second pair
54 * \return true if first pair less than second; else false
55 */
56 friend inline bool operator<(const pair_type& first, const pair_type& second)
57 {
58 if (first.first == second.first)
59 return first.second < second.second;
60 else
61 return first.first < second.first;
62 }
63 };
64
65
66 /**
67 * Default constructor.
68 */
71
72
73 /**
74 * Constructor.
75 *
76 * \param numberOfIndices number of indices
77 */
78 JCombinatorics(const int numberOfIndices)
79 {
80 configure(numberOfIndices);
81 }
82
83
84 /**
85 * Get combinatorics.
86 *
87 * \return combinatorics
88 */
90 {
91 return *this;
92 }
93
94
95 /**
96 * Configure.
97 *
98 * \param numberOfIndices number of indices
99 */
100 void configure(const int numberOfIndices)
101 {
102 zbuf1D.clear();
103
104 zbuf2D.resize(numberOfIndices);
105
106 for (int i = 0; i != numberOfIndices; ++i) {
107 zbuf2D[i].resize(numberOfIndices);
108 }
109
110 for (int i = 0; i != numberOfIndices; ++i) {
111
112 zbuf2D[i][i] = -1;
113
114 for (int j = i; ++j != numberOfIndices; ) {
115
116 zbuf2D[i][j] = zbuf1D.size();
117 zbuf2D[j][i] = zbuf1D.size();
118
119 zbuf1D.push_back(pair_type(i,j));
120 }
121 }
122 }
123
124
125 /**
126 * Get number of indices.
127 *
128 * \return number of indices
129 */
130 size_t getNumberOfIndices() const
131 {
132 return zbuf2D.size();
133 }
134
135
136 /**
137 * Get number of pairs.
138 *
139 * \return number of pairs
140 */
141 size_t getNumberOfPairs() const
142 {
143 return zbuf1D.size();
144 }
145
146
147 /**
148 * Get index of pair of indices.
149 *
150 * \param first first address
151 * \param second second address
152 * \return index (-1 if first and second address are equal)
153 */
154 int getIndex(const int first, const int second) const
155 {
156 return zbuf2D[first][second];
157 }
158
159
160 /**
161 * Get index of pair.
162 *
163 * \param pair pair
164 * \return index (-1 if first and second address are equal)
165 */
166 int getIndex(const pair_type& pair) const
167 {
168 return getIndex(pair.first, pair.second);
169 }
170
171
172 /**
173 * Get pair of indices for given index.
174 *
175 * \param index index
176 * \return pair of indices
177 */
178 const pair_type& getPair(const int index) const
179 {
180 return zbuf1D[index];
181 }
182
183
184 /**
185 * Sort address pairs.
186 *
187 * \param comparator comparator for pairs
188 */
189 template<class JComparator_t>
190 void sort(JComparator_t comparator)
191 {
192 std::stable_sort(zbuf1D.begin(), zbuf1D.end(), comparator);
193
194 for (int i = 0; i != (int) zbuf1D.size(); ++i) {
195
196 const pair_type pair = zbuf1D[i];
197
198 zbuf2D[pair.first][pair.second] = i;
199 zbuf2D[pair.second][pair.first] = i;
200 }
201
202 for (int i = 0; i != (int) zbuf2D.size(); ++i) {
203 zbuf2D[i][i] = -1;
204 }
205 }
206
207
208 /**
209 * Sign of pair of indices.
210 *
211 * \param first first address
212 * \param second second address
213 * \return +1 if second >= first; else -1
214 */
215 static int getSign(const int first, const int second)
216 {
217 return (second >= first ? +1 : -1);
218 }
219
220
221 /**
222 * Sign of pair of indices.
223 *
224 * \param pair pair of indices
225 * \return +1 if second >= first; else -1
226 */
227 static int getSign(const pair_type& pair)
228 {
229 return getSign(pair.first, pair.second);
230 }
231
232
233 protected:
236 };
237}
238
239#endif
Auxiliary class to convert pair of indices to unique index and back.
JCombinatorics()
Default constructor.
const pair_type & getPair(const int index) const
Get pair of indices for given index.
static int getSign(const pair_type &pair)
Sign of pair of indices.
int getIndex(const pair_type &pair) const
Get index of pair.
void configure(const int numberOfIndices)
Configure.
JCombinatorics(const int numberOfIndices)
Constructor.
size_t getNumberOfIndices() const
Get number of indices.
int getIndex(const int first, const int second) const
Get index of pair of indices.
std::vector< pair_type > zbuf1D
const JCombinatorics & getCombinatorics() const
Get combinatorics.
static int getSign(const int first, const int second)
Sign of pair of indices.
void sort(JComparator_t comparator)
Sort address pairs.
size_t getNumberOfPairs() const
Get number of pairs.
std::vector< std::vector< int > > zbuf2D
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
int j
Definition JPolint.hh:801
Data structure for a pair of indices.
friend bool operator<(const pair_type &first, const pair_type &second)
Less-than operator.
pair_type(const int first, const int second)
Constructor.