Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMultiKey.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JMULTIKEY__
2 #define __JTOOLS__JMULTIKEY__
3 
4 #include <istream>
5 #include <ostream>
6 #include <utility>
7 #include <cmath>
8 
9 #include "JLang/JClass.hh"
10 #include "JLang/JComparable.hh"
11 
12 
13 /**
14  * \author mdejong
15  */
16 
17 namespace JTOOLS {}
18 namespace JPP { using namespace JTOOLS; }
19 
20 namespace JTOOLS {
21 
22  using JLANG::JClass;
23 
24 
25  /**
26  * Forward declaration of template JMultiKey class.
27  */
28  template<unsigned int N, class JKey_t>
29  class JMultiKey;
30 
31 
32  namespace {
33  /**
34  * Auxiliary class for copying between const and non-const key types.
35  */
36  template<unsigned int N, class JKey_t>
37  struct JArgument {
38  typedef const JMultiKey<N, const JKey_t>& argument_type;
39  };
40 
41 
42  template<unsigned int N, class JKey_t>
43  struct JArgument<N, const JKey_t> {
44  typedef const JMultiKey<N, JKey_t>& argument_type;
45  };
46  }
47 
48 
49  /**
50  * Multidimensional key.
51  *
52  * This class reproduces the key of a multidimensional map.
53  * The individual data members can be accessed as, e.g:
54  * <pre>
55  * JMultiKey<3, key_type> key;
56  *
57  * key[[.second].second].first;
58  * </pre>
59  */
60  template<unsigned int N, class JKey_t>
61  class JMultiKey :
62  public std::pair<JKey_t, JMultiKey<N-1, JKey_t> >,
63  public JLANG::JComparable< JMultiKey<N, JKey_t> >
64  {
65  public:
66 
67  typedef JKey_t key_type;
68  typedef JMultiKey<N-1, JKey_t> mapped_type;
70 
71 
72  /**
73  * Default constructor.
74  */
76  pair()
77  {}
78 
79 
80  /**
81  * Constructor.
82  * The secondary key is appended to the end of the primary keys.
83  *
84  * \param __first primary keys
85  * \param __second secondary key
86  */
88  typename JClass<key_type> ::argument_type __second) :
89  pair(__first.first, mapped_type(__first.second, __second))
90  {}
91 
92 
93  /**
94  * Constructor.
95  * The primary key is inserted at the start of the secondary keys.
96  *
97  * \param __first primary key
98  * \param __second secondary keys
99  */
101  typename JClass<mapped_type>::argument_type __second) :
102  pair(__first, __second)
103  {}
104 
105 
106  /**
107  * Copy constructor.
108  *
109  * \param key key
110  */
112  pair(key.first, key.second)
113  {}
114 
115 
116  /**
117  * Less than method.
118  *
119  * \param key key
120  * \return true if this key less than given key; else false
121  */
122  bool less(const JMultiKey<N, JKey_t>& key) const
123  {
124  if (this->first == key.first)
125  return this->second.less(key.second);
126  else
127  return this->first < key.first;
128  }
129 
130 
131  /**
132  * Get length squared.
133  *
134  * \return square of length
135  */
136  double getLengthSquared() const
137  {
138  return this->first*this->first + this->second.getLengthSquared();
139  }
140 
141 
142  /**
143  * Get length.
144  *
145  * \return length
146  */
147  double getLength() const
148  {
149  return sqrt(this->getLengthSquared());
150  }
151 
152 
153  /**
154  * Get frontend key.
155  *
156  * \return frontend key
157  */
158  JMultiKey<N-1, JKey_t> front() const
159  {
160  return JMultiKey<N-1, JKey_t>(this->first, this->second.front());
161  }
162 
163 
164  /**
165  * Get backend key.
166  *
167  * \return backend key
168  */
169  key_type back() const
170  {
171  return this->second.back();
172  }
173 
174 
175  /**
176  * Read key from input.
177  *
178  * \param in input stream
179  * \param key key
180  * \return input stream
181  */
182  friend inline std::istream& operator>>(std::istream& in, JMultiKey<N, JKey_t>& key)
183  {
184  in >> key.first;
185  in >> key.second;
186 
187  return in;
188  }
189 
190 
191  /**
192  * Write key to output.
193  *
194  * \param out output stream
195  * \param key key
196  * \return output stream
197  */
198  friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey<N, JKey_t>& key)
199  {
200  out << key.first;
201  out << ' ';
202  out << key.second;
203 
204  return out;
205  }
206  };
207 
208 
209  /**
210  * Two-dimensional key.
211  */
212  template<class JKey_t>
213  class JMultiKey<2, JKey_t> :
214  public std::pair<JKey_t, JMultiKey<1, JKey_t> >,
215  public JLANG::JComparable< JMultiKey<2, JKey_t> >
216  {
217  public:
218 
219  typedef JKey_t key_type;
222 
223 
224  /**
225  * Default constructor.
226  */
228  pair()
229  {}
230 
231 
232  /**
233  * Constructor.
234  * The secondary key is appended to the end of the primary key.
235  *
236  * \param __first primary key
237  * \param __second secondary key
238  */
240  typename JClass<key_type> ::argument_type __second) :
241  pair(__first.first, __second)
242  {}
243 
244 
245  /**
246  * Constructor.
247  * The primary key is inserted at the start of the secondary key.
248  *
249  * \param __first primary key
250  * \param __second secondary key
251  */
253  typename JClass<mapped_type>::argument_type __second) :
254  pair(__first, __second.first)
255  {}
256 
257 
258  /**
259  * Copy constructor.
260  *
261  * \param key key
262  */
264  pair(key.first, key.second)
265  {}
266 
267 
268  /**
269  * Less than method.
270  *
271  * \param key key
272  * \return true if this key less than given key; else false
273  */
274  bool less(const JMultiKey<2, JKey_t>& key) const
275  {
276  if (this->first == key.first)
277  return this->second.less(key.second);
278  else
279  return this->first < key.first;
280  }
281 
282 
283  /**
284  * Get length squared.
285  *
286  * \return square of length
287  */
288  double getLengthSquared() const
289  {
290  return this->first*this->first + this->second.getLengthSquared();
291  }
292 
293 
294  /**
295  * Get length.
296  *
297  * \return length
298  */
299  double getLength() const
300  {
301  return sqrt(this->getLengthSquared());
302  }
303 
304 
305  /**
306  * Get frontend key.
307  *
308  * \return frontend key
309  */
311  {
312  return JMultiKey<1, JKey_t>(this->first);
313  }
314 
315 
316  /**
317  * Get backend key.
318  *
319  * \return backend key
320  */
321  key_type back() const
322  {
323  return this->second.back();
324  }
325 
326 
327  /**
328  * Read key from input.
329  *
330  * \param in input stream
331  * \param key key
332  * \return input stream
333  */
334  friend inline std::istream& operator>>(std::istream& in, JMultiKey<2, JKey_t>& key)
335  {
336  in >> key.first;
337  in >> key.second;
338 
339  return in;
340  }
341 
342 
343  /**
344  * Write key to output.
345  *
346  * \param out output stream
347  * \param key key
348  * \return output stream
349  */
350  friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey<2, JKey_t>& key)
351  {
352  out << key.first;
353  out << ' ';
354  out << key.second;
355 
356  return out;
357  }
358  };
359 
360 
361  /**
362  * One-dimensional key.
363  */
364  template<class JKey_t>
365  class JMultiKey<1, JKey_t> :
366  public JLANG::JComparable< JMultiKey<1, JKey_t> >
367  {
368  public:
369 
370  typedef JKey_t key_type;
372 
373 
374  /**
375  * Default constructor.
376  */
378  first()
379  {}
380 
381 
382  /**
383  * Constructor.
384  * The secondary key is appended to the end of the primary key.
385  *
386  * \param __first primary key
387  * \param __second secondary key
388  */
390  typename JClass<key_type> ::argument_type __second) :
391  first(__second)
392  {}
393 
394 
395  /**
396  * Constructor.
397  * The primary key is inserted at the start of the secondary key.
398  *
399  * \param __first primary key
400  * \param __second secondary key
401  */
403  typename JClass<mapped_type>::argument_type __second) :
404  first(__first)
405  {}
406 
407 
408  /**
409  * Constructor.
410  *
411  * \param __first key
412  */
414  first(__first)
415  {}
416 
417 
418  /**
419  * Copy constructor.
420  *
421  * \param key key
422  */
424  first(key.first)
425  {}
426 
427 
428  /**
429  * Less than method.
430  *
431  * \param key key
432  * \return true if this key less than given key; else false
433  */
434  bool less(const JMultiKey<1, JKey_t>& key) const
435  {
436  return this->first < key.first;
437  }
438 
439 
440  /**
441  * Get length squared.
442  *
443  * \return square of length
444  */
445  double getLengthSquared() const
446  {
447  return this->first*this->first;
448  }
449 
450 
451  /**
452  * Get length.
453  *
454  * \return length
455  */
456  double getLength() const
457  {
458  return sqrt(this->getLengthSquared());
459  }
460 
461 
462  /**
463  * Get backend key.
464  *
465  * \return backend key
466  */
467  key_type back() const
468  {
469  return this->first;
470  }
471 
472 
473  /**
474  * Read key from input.
475  *
476  * \param in input stream
477  * \param key key
478  * \return input stream
479  */
480  friend inline std::istream& operator>>(std::istream& in, JMultiKey<1, JKey_t>& key)
481  {
482  in >> key.first;
483 
484  return in;
485  }
486 
487 
488  /**
489  * Write key to output.
490  *
491  * \param out output stream
492  * \param key key
493  * \return output stream
494  */
495  friend inline std::ostream& operator<<(std::ostream& out, const JMultiKey<1, JKey_t>& key)
496  {
497  out << key.first;
498 
499  return out;
500  }
501 
502 
504  };
505 
506 
507  /**
508  * Empty key.
509  */
510  template<class JKey_t>
511  class JMultiKey<0, JKey_t>
512  {
513  public:
514 
515  typedef JKey_t key_type;
516 
517 
518  /**
519  * Default constructor.
520  */
522  {}
523  };
524 }
525 
526 #endif
std::pair< key_type, mapped_type > pair
Definition: JMultiKey.hh:69
double getLengthSquared() const
Get length squared.
Definition: JMultiKey.hh:136
bool less(const JMultiKey< 2, JKey_t > &key) const
Less than method.
Definition: JMultiKey.hh:274
Two-dimensional key.
Definition: JMultiKey.hh:213
double getLengthSquared() const
Get length squared.
Definition: JMultiKey.hh:445
std::pair< key_type, mapped_type > pair
Definition: JMultiKey.hh:221
JMultiKey< 0, JKey_t > mapped_type
Definition: JMultiKey.hh:371
key_type back() const
Get backend key.
Definition: JMultiKey.hh:169
JMultiKey(typename JClass< key_type >::argument_type __first)
Constructor.
Definition: JMultiKey.hh:413
friend std::istream & operator>>(std::istream &in, JMultiKey< 1, JKey_t > &key)
Read key from input.
Definition: JMultiKey.hh:480
friend std::istream & operator>>(std::istream &in, JMultiKey< 2, JKey_t > &key)
Read key from input.
Definition: JMultiKey.hh:334
JMultiKey()
Default constructor.
Definition: JMultiKey.hh:521
double getLength() const
Get length.
Definition: JMultiKey.hh:299
JMultiKey(typename JArgument< 1, JKey_t >::argument_type key)
Copy constructor.
Definition: JMultiKey.hh:423
JMultiKey(typename JClass< key_type >::argument_type __first, typename JClass< mapped_type >::argument_type __second)
Constructor.
Definition: JMultiKey.hh:402
JMultiKey()
Default constructor.
Definition: JMultiKey.hh:75
double getLength() const
Get length.
Definition: JMultiKey.hh:147
JMultiKey(typename JClass< mapped_type >::argument_type __first, typename JClass< key_type >::argument_type __second)
Constructor.
Definition: JMultiKey.hh:87
Forward declaration of template JMultiKey class.
Definition: JMultiKey.hh:29
JMultiKey(typename JArgument< 2, JKey_t >::argument_type key)
Copy constructor.
Definition: JMultiKey.hh:263
JArgument< T >::argument_type argument_type
Definition: JClass.hh:64
JMultiKey(typename JArgument< N, JKey_t >::argument_type key)
Copy constructor.
Definition: JMultiKey.hh:111
JMultiKey(typename JClass< mapped_type >::argument_type __first, typename JClass< key_type >::argument_type __second)
Constructor.
Definition: JMultiKey.hh:239
double getLength() const
Get length.
Definition: JMultiKey.hh:456
JMultiKey()
Default constructor.
Definition: JMultiKey.hh:227
JMultiKey< N-1, JKey_t > mapped_type
Definition: JMultiKey.hh:68
key_type back() const
Get backend key.
Definition: JMultiKey.hh:467
JMultiKey(typename JClass< key_type >::argument_type __first, typename JClass< mapped_type >::argument_type __second)
Constructor.
Definition: JMultiKey.hh:100
Template definition of auxiliary base class for comparison of data structures.
Definition: JComparable.hh:24
One-dimensional key.
Definition: JMultiKey.hh:365
JMultiKey()
Default constructor.
Definition: JMultiKey.hh:377
JMultiKey< N-1, JKey_t > front() const
Get frontend key.
Definition: JMultiKey.hh:158
double getLengthSquared() const
Get length squared.
Definition: JMultiKey.hh:288
JMultiKey(typename JClass< key_type >::argument_type __first, typename JClass< mapped_type >::argument_type __second)
Constructor.
Definition: JMultiKey.hh:252
Template for generic class types.
Definition: JClass.hh:62
JMultiKey< 1, JKey_t > mapped_type
Definition: JMultiKey.hh:220
JMultiKey< 1, JKey_t > front() const
Get frontend key.
Definition: JMultiKey.hh:310
JMultiKey(typename JClass< mapped_type >::argument_type __first, typename JClass< key_type >::argument_type __second)
Constructor.
Definition: JMultiKey.hh:389
bool less(const JMultiKey< N, JKey_t > &key) const
Less than method.
Definition: JMultiKey.hh:122
bool less(const JMultiKey< 1, JKey_t > &key) const
Less than method.
Definition: JMultiKey.hh:434
key_type back() const
Get backend key.
Definition: JMultiKey.hh:321
friend std::istream & operator>>(std::istream &in, JMultiKey< N, JKey_t > &key)
Read key from input.
Definition: JMultiKey.hh:182