Jpp  17.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
13 namespace JTOOLS {}
14 namespace JPP { using namespace JTOOLS; }
15 
16 namespace JTOOLS {
17 
18 
19  /**
20  * Auxiliary class to convert pair of indices to unique index and back.
21  */
23  public:
24 
25  /**
26  * Data structure for a pair of indices.
27  */
29 
30 
31  /**
32  * Default constructor.
33  */
35  {}
36 
37 
38  /**
39  * Constructor.
40  *
41  * \param numberOfIndices number of indices
42  */
43  JCombinatorics(const int numberOfIndices)
44  {
45  configure(numberOfIndices);
46  }
47 
48 
49  /**
50  * Get combinatorics.
51  *
52  * \return combinatorics
53  */
55  {
56  return *this;
57  }
58 
59 
60  /**
61  * Configure.
62  *
63  * \param numberOfIndices number of indices
64  */
65  void configure(const int numberOfIndices)
66  {
67  zbuf1D.clear();
68 
69  zbuf2D.resize(numberOfIndices);
70 
71  for (int i = 0; i != numberOfIndices; ++i) {
72  zbuf2D[i].resize(numberOfIndices);
73  }
74 
75  for (int i = 0; i != numberOfIndices; ++i) {
76 
77  zbuf2D[i][i] = -1;
78 
79  for (int j = i; ++j != numberOfIndices; ) {
80 
81  zbuf2D[i][j] = zbuf1D.size();
82  zbuf2D[j][i] = zbuf1D.size();
83 
84  zbuf1D.push_back(pair_type(i,j));
85  }
86  }
87  }
88 
89 
90  /**
91  * Get number of indices.
92  *
93  * \return number of indices
94  */
95  size_t getNumberOfIndices() const
96  {
97  return zbuf2D.size();
98  }
99 
100 
101  /**
102  * Get number of pairs.
103  *
104  * \return number of pairs
105  */
106  size_t getNumberOfPairs() const
107  {
108  return zbuf1D.size();
109  }
110 
111 
112  /**
113  * Get index of pair of indices.
114  *
115  * \param first first address
116  * \param second second address
117  * \return index (-1 if first and second address are equal)
118  */
119  int getIndex(const int first, const int second) const
120  {
121  return zbuf2D[first][second];
122  }
123 
124 
125  /**
126  * Get pair of indices for given index.
127  *
128  * \param index index
129  * \return pair of indices
130  */
131  const pair_type& getPair(const int index) const
132  {
133  return zbuf1D[index];
134  }
135 
136 
137  /**
138  * Sort address pairs.
139  *
140  * \param comparator comparator for pairs
141  */
142  template<class JComparator_t>
143  void sort(JComparator_t comparator)
144  {
145  std::stable_sort(zbuf1D.begin(), zbuf1D.end(), comparator);
146 
147  for (int i = 0; i != (int) zbuf1D.size(); ++i) {
148 
149  const pair_type pair = zbuf1D[i];
150 
151  zbuf2D[pair.first][pair.second] = i;
152  zbuf2D[pair.second][pair.first] = i;
153  }
154 
155  for (int i = 0; i != (int) zbuf2D.size(); ++i) {
156  zbuf2D[i][i] = -1;
157  }
158  }
159 
160 
161  /**
162  * Sign of pair of indices.
163  *
164  * \param first first address
165  * \param second second address
166  * \return +1 if second >= first; else -1
167  */
168  static int getSign(const int first, const int second)
169  {
170  return (second >= first ? +1 : -1);
171  }
172 
173 
174  /**
175  * Sign of pair of indices.
176  *
177  * \param pair pair of indices
178  * \return +1 if second >= first; else -1
179  */
180  static int getSign(const pair_type& pair)
181  {
182  return getSign(pair.first, pair.second);
183  }
184 
185 
186  protected:
189  };
190 }
191 
192 #endif
const pair_type & getPair(const int index) const
Get pair of indices for given index.
Auxiliary class to convert pair of indices to unique index and back.
void configure(const int numberOfIndices)
Configure.
const JCombinatorics & getCombinatorics() const
Get combinatorics.
JCombinatorics(const int numberOfIndices)
Constructor.
void sort(JComparator_t comparator)
Sort address pairs.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
JCombinatorics()
Default constructor.
size_t getNumberOfPairs() const
Get number of pairs.
std::vector< std::vector< int > > zbuf2D
std::pair< int, int > pair_type
Data structure for a pair of indices.
int getIndex(const int first, const int second) const
Get index of pair of indices.
std::vector< pair_type > zbuf1D
static int getSign(const pair_type &pair)
Sign of pair of indices.
static int getSign(const int first, const int second)
Sign of pair of indices.
size_t getNumberOfIndices() const
Get number of indices.
int j
Definition: JPolint.hh:703