Jpp  16.0.3
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  * Configure.
51  *
52  * \param numberOfIndices number of indices
53  */
54  void configure(const int numberOfIndices)
55  {
56  zbuf1D.clear();
57 
58  zbuf2D.resize(numberOfIndices);
59 
60  for (int i = 0; i != numberOfIndices; ++i) {
61  zbuf2D[i].resize(numberOfIndices);
62  }
63 
64  for (int i = 0; i != numberOfIndices; ++i) {
65 
66  zbuf2D[i][i] = -1;
67 
68  for (int j = i; ++j != numberOfIndices; ) {
69 
70  zbuf2D[i][j] = zbuf1D.size();
71  zbuf2D[j][i] = zbuf1D.size();
72 
73  zbuf1D.push_back(pair_type(i,j));
74  }
75  }
76  }
77 
78 
79  /**
80  * Get number of indices.
81  *
82  * \return number of indices
83  */
84  size_t getNumberOfIndices() const
85  {
86  return zbuf2D.size();
87  }
88 
89 
90  /**
91  * Get number of pairs.
92  *
93  * \return number of pairs
94  */
95  size_t getNumberOfPairs() const
96  {
97  return zbuf1D.size();
98  }
99 
100 
101  /**
102  * Get index of pair of indices.
103  *
104  * \param first first address
105  * \param second second address
106  * \return index (-1 if first and second address are equal)
107  */
108  int getIndex(const int first, const int second) const
109  {
110  return zbuf2D[first][second];
111  }
112 
113 
114  /**
115  * Get pair of indices for given index.
116  *
117  * \param index index
118  * \return pair of indices
119  */
120  const pair_type& getPair(const int index) const
121  {
122  return zbuf1D[index];
123  }
124 
125 
126  /**
127  * Sort address pairs.
128  *
129  * \param comparator comparator for pairs
130  */
131  template<class JComparator_t>
132  void sort(JComparator_t comparator)
133  {
134  std::stable_sort(zbuf1D.begin(), zbuf1D.end(), comparator);
135 
136  for (int i = 0; i != (int) zbuf1D.size(); ++i) {
137 
138  const pair_type pair = zbuf1D[i];
139 
140  zbuf2D[pair.first][pair.second] = i;
141  zbuf2D[pair.second][pair.first] = i;
142  }
143 
144  for (int i = 0; i != (int) zbuf2D.size(); ++i) {
145  zbuf2D[i][i] = -1;
146  }
147  }
148 
149 
150  /**
151  * Sign of pair of indices.
152  *
153  * \param first first address
154  * \param second second address
155  * \return +1 if second >= first; else -1
156  */
157  static int getSign(const int first, const int second)
158  {
159  return (second >= first ? +1 : -1);
160  }
161 
162 
163  /**
164  * Sign of pair of indices.
165  *
166  * \param pair pair of indices
167  * \return +1 if second >= first; else -1
168  */
169  static int getSign(const pair_type& pair)
170  {
171  return getSign(pair.first, pair.second);
172  }
173 
174 
175  protected:
178  };
179 }
180 
181 #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.
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:682