CMS 3D CMS Logo

L1MuGMTMatrix.h
Go to the documentation of this file.
1 //-------------------------------------------------
2 //
8 //
9 //
10 // Author :
11 // N. Neumeister CERN EP
12 //
13 // Migrated to CMSSW:
14 // I. Mikulec
15 //
16 //--------------------------------------------------
17 #ifndef L1TriggerGlobalMuonTrigger_L1MuGMTMatrix_h
18 #define L1TriggerGlobalMuonTrigger_L1MuGMTMatrix_h
19 
20 //---------------
21 // C++ Headers --
22 //---------------
23 
24 #include <iostream>
25 #include <cassert>
26 #include <string>
27 #include <sstream>
28 #include <memory>
29 
30 //----------------------
31 // Base Class Headers --
32 //----------------------
33 
34 //------------------------------------
35 // Collaborating Class Declarations --
36 //------------------------------------
37 
39 
40 // ---------------------
41 // -- Class Interface --
42 // ---------------------
43 
44 template <class T>
46 public:
48  L1MuGMTMatrix(int r, int c, T v);
49 
52 
53  L1MuGMTMatrix(L1MuGMTMatrix<T>&&) = default;
54 
56  virtual ~L1MuGMTMatrix() = default;
57 
59  T& operator()(int r, int c);
60 
62  const T& operator()(int r, int c) const;
63 
65  void reset(T v);
66 
68  void set(int r, int c, T v);
69 
72 
75 
77  L1MuGMTMatrix& operator+=(const T& s);
78 
80  L1MuGMTMatrix& operator*=(const T& s);
81 
83  bool isMax(int r, int c) const;
84 
86  bool isMin(int r, int c) const;
87 
89  int colAny(int c) const;
90 
92  int rowAny(int r) const;
93 
95  void print() const;
96 
97 private:
98  T& get(int r, int c) { return p_[r * c_size + c]; }
99 
100  T const& get(int r, int c) const { return p_[r * c_size + c]; }
101 
103 
104  std::unique_ptr<T[]> p_;
105 };
106 
107 template <class T>
108 L1MuGMTMatrix<T>::L1MuGMTMatrix(int r, int c, T v) : r_size(r), c_size(c), p_(new T[r * c]) {
109  for (int i = 0; i < r_size * c_size; ++i) {
110  p_[i] = v;
111  }
112 }
113 
114 // copy constructor
115 
116 template <class T>
118  : r_size(mat.r_size), c_size(mat.c_size), p_(new T[r_size * c_size]) {
119  for (int i = 0; i < r_size * c_size; ++i) {
120  p_[i] = mat.p_[i];
121  }
122 }
123 
124 //
125 //
126 //
127 template <class T>
129  assert(r >= 0 && r < r_size && c >= 0 && c < c_size);
130 
131  return get(r, c);
132 }
133 
134 //
135 //
136 //
137 template <class T>
138 const T& L1MuGMTMatrix<T>::operator()(int r, int c) const {
139  assert(r >= 0 && r < r_size && c >= 0 && c < c_size);
140 
141  return get(r, c);
142 }
143 
144 //
145 // reset elements
146 //
147 template <class T>
149  for (int i = 0; i < r_size * c_size; ++i) {
150  p_[i] = v;
151  }
152 }
153 
154 //
155 // set matrix element
156 //
157 template <class T>
158 void L1MuGMTMatrix<T>::set(int r, int c, T v) {
159  assert(r >= 0 && r < r_size && c >= 0 && c < c_size);
160 
161  get(r, c) = v;
162 }
163 
164 //
165 // assignment operator
166 //
167 template <class T>
169  if (this != &m) {
170  assert(m.c_size == c_size && m.r_size == r_size);
171 
172  for (int i = 0; i < r_size * c_size; ++i) {
173  p_[i] = m.p_[i];
174  }
175  }
176 
177  return (*this);
178 }
179 
180 //
181 // matrix addition
182 //
183 template <class T>
185  assert(m.c_size == c_size && m.r_size == r_size);
186 
187  for (int i = 0; i < r_size * c_size; ++i) {
188  p_[i] += m.p_[i];
189  }
190 
191  return *this;
192 }
193 
194 //
195 // scalar addition
196 //
197 template <class T>
199  for (int i = 0; i < r_size * c_size; ++i) {
200  p_[i] += s;
201  }
202 
203  return *this;
204 }
205 
206 //
207 // scalar multiplication
208 //
209 template <class T>
211  for (int i = 0; i < r_size * c_size; ++i) {
212  p_[i] *= s;
213  }
214 
215  return *this;
216 }
217 
218 //
219 // is the element (r,c) the max. entry in its row and column?
220 //
221 template <class T>
222 bool L1MuGMTMatrix<T>::isMax(int r, int c) const {
223  bool max = true;
224 
225  for (int i = 0; i < c; i++) {
226  max = max && (this->get(r, c) > this->get(r, i));
227  }
228  for (int i = c + 1; i < c_size; i++) {
229  max = max && (this->get(r, c) >= this->get(r, i));
230  }
231 
232  for (int j = 0; j < r; j++) {
233  max = max && (this->get(r, c) > this->get(j, c));
234  }
235  for (int j = r + 1; j < r_size; j++) {
236  max = max && (this->get(r, c) >= this->get(j, c));
237  }
238 
239  return max;
240 }
241 
242 //
243 // is the element (r,c) the min. entry in its row and column?
244 //
245 template <class T>
246 bool L1MuGMTMatrix<T>::isMin(int r, int c) const {
247  bool min = true;
248  for (int i = 0; i < c_size; i++) {
249  min = min && (this->get(r, c) <= this->get(r, i));
250  }
251  for (int j = 0; j < r_size; j++) {
252  min = min && (this->get(r, c) <= this->get(j, c));
253  }
254 
255  return min;
256 }
257 
258 //
259 // is any element in column c > 0 ? return index or -1
260 //
261 template <class T>
262 int L1MuGMTMatrix<T>::colAny(int c) const {
263  int stat = -1;
264  for (int i = 0; i < r_size; i++) {
265  if (this->get(i, c) > 0)
266  stat = i;
267  }
268 
269  return stat;
270 }
271 
272 //
273 // is any element in row r > 0 ? return index or -1
274 //
275 template <class T>
276 int L1MuGMTMatrix<T>::rowAny(int r) const {
277  int stat = -1;
278  for (int i = 0; i < c_size; i++) {
279  if (this->get(r, i) > 0)
280  stat = i;
281  }
282 
283  return stat;
284 }
285 
286 //
287 // print matrix
288 //
289 template <class T>
291  for (int r = 0; r < r_size; r++) {
292  std::stringstream output;
293  for (int c = 0; c < c_size; c++)
294  output << get(r, c) << "\t";
295  edm::LogVerbatim("GMTMatrix_print") << output.str();
296  }
297  edm::LogVerbatim("GMTMatrix_print");
298 }
299 
300 #endif
L1MuGMTMatrix & operator=(const L1MuGMTMatrix &m)
assignment operator
std::unique_ptr< T[]> p_
bool isMax(int r, int c) const
is the element (r,c) the max. entry in its row and column?
L1MuGMTMatrix & operator*=(const T &s)
scalar multiplication
void set(int r, int c, T v)
set matrix element
bool isMin(int r, int c) const
is the element (r,c) the min. entry in its row and column?
int rowAny(int r) const
is any element in row r > 0 ? return index or -1
T & operator()(int r, int c)
void reset(T v)
reset all elements
void print() const
print matrix
T min(T a, T b)
Definition: MathUtil.h:58
L1MuGMTMatrix(int r, int c, T v)
constructor
L1MuGMTMatrix & operator+=(const L1MuGMTMatrix &m)
matrix addition
virtual ~L1MuGMTMatrix()=default
destructor
long double T
int colAny(int c) const
is any element in column c > 0 ? return index or -1