Jpp
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  unsigned int getNumberOfIndices() const
85  {
86  return zbuf2D.size();
87  }
88 
89 
90  /**
91  * Get number of pairs.
92  *
93  * \return number of pairs
94  */
95  unsigned int 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
JTOOLS::JCombinatorics::getSign
static int getSign(const int first, const int second)
Sign of pair of indices.
Definition: JCombinatorics.hh:157
JTOOLS::JCombinatorics::getNumberOfPairs
unsigned int getNumberOfPairs() const
Get number of pairs.
Definition: JCombinatorics.hh:95
JTOOLS::JCombinatorics::configure
void configure(const int numberOfIndices)
Configure.
Definition: JCombinatorics.hh:54
JTOOLS::JCombinatorics::getIndex
int getIndex(const int first, const int second) const
Get index of pair of indices.
Definition: JCombinatorics.hh:108
std::vector
Definition: JSTDTypes.hh:12
JTOOLS::j
int j
Definition: JPolint.hh:634
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
JTOOLS::JCombinatorics::getPair
const pair_type & getPair(const int index) const
Get pair of indices for given index.
Definition: JCombinatorics.hh:120
JTOOLS::JCombinatorics::zbuf2D
std::vector< std::vector< int > > zbuf2D
Definition: JCombinatorics.hh:177
std::pair
Definition: JSTDTypes.hh:15
JTOOLS::JCombinatorics::JCombinatorics
JCombinatorics(const int numberOfIndices)
Constructor.
Definition: JCombinatorics.hh:43
JTOOLS::JCombinatorics
Auxiliary class to convert pair of indices to unique index and back.
Definition: JCombinatorics.hh:22
JTOOLS::JCombinatorics::getNumberOfIndices
unsigned int getNumberOfIndices() const
Get number of indices.
Definition: JCombinatorics.hh:84
JTOOLS::JCombinatorics::zbuf1D
std::vector< pair_type > zbuf1D
Definition: JCombinatorics.hh:176
JTOOLS::JCombinatorics::getSign
static int getSign(const pair_type &pair)
Sign of pair of indices.
Definition: JCombinatorics.hh:169
JTOOLS::JCombinatorics::JCombinatorics
JCombinatorics()
Default constructor.
Definition: JCombinatorics.hh:34
JTOOLS::JCombinatorics::pair_type
std::pair< int, int > pair_type
Data structure for a pair of indices.
Definition: JCombinatorics.hh:28
JTOOLS
Auxiliary classes and methods for multi-dimensional interpolations and histograms.
Definition: JAbstractCollection.hh:9
JTOOLS::JCombinatorics::sort
void sort(JComparator_t comparator)
Sort address pairs.
Definition: JCombinatorics.hh:132