Jpp  17.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JRouter.hh
Go to the documentation of this file.
1 #ifndef __JTOOLS__JROUTER__
2 #define __JTOOLS__JROUTER__
3 
4 #include <deque>
5 #include <algorithm>
6 
8 #include "JLang/JClass.hh"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JTOOLS {}
16 namespace JPP { using namespace JTOOLS; }
17 
18 namespace JTOOLS {
19 
20  using JLANG::JComparisonAvailable;
21 
22 
23  /**
24  * Direct addressing of elements with unique identifiers.
25  */
26  template<class JAddress_t, bool has_eq = JComparisonAvailable<JAddress_t>::has_eq>
27  class JRouter;
28 
29 
30  /**
31  * Template specialisation of JRouter without default address comparison.
32  */
33  template<class JAddress_t>
34  class JRouter<JAddress_t, false> :
35  protected std::deque<JAddress_t>
36  {
37 
39 
40  public:
41  /**
42  * Default constructor.
43  */
44  JRouter() :
45  std::deque<JAddress_t>()
46  {}
47 
48 
49  /**
50  * Virtual destructor.
51  */
52  virtual ~JRouter()
53  {}
54 
55 
56  /**
57  * Get default address.
58  *
59  * \return default address
60  */
61  virtual const JAddress_t& getDefaultAddress() const
62  {
63  static JAddress_t address;
64 
65  return address;
66  }
67 
68 
69  /**
70  * Clear.
71  */
72  void clear()
73  {
74  static_cast<std::deque<JAddress_t>&>(*this).clear();
75 
76  this->first = 0;
77  }
78 
79 
80  /**
81  * Swap router.
82  *
83  * \param router router
84  */
85  void swap(JRouter& router)
86  {
87  std::swap(this->first, router.first);
88 
89  static_cast<std::deque<JAddress_t>&>(*this).swap(router);
90  }
91 
92 
93  /**
94  * Align router.
95  *
96  * \param router router
97  */
98  void align(const JRouter& router)
99  {
100  this->first = router.first;
101 
102  if (router.size() > this->size()) {
103  this->resize(router.size());
104  }
105  }
106 
107 
108  /**
109  * Store address for given identifier.
110  *
111  * \param id identifier
112  * \param address address
113  */
114  void put(const int id, argument_type address)
115  {
116  if (this->empty()) {
117 
118  this->push_back(address);
119 
120  this->first = id;
121 
122  } else if (id < this->first) {
123 
124  this->insert(this->begin(), this->first - id, getDefaultAddress());
125 
126  this->first = id;
127 
128  this->at(0) = address;
129 
130  } else {
131 
132  const size_t index = (size_t) (id - this->first);
133 
134  if (index >= this->size()) {
135  this->resize(index + 1, getDefaultAddress());
136  }
137 
138  this->at(index) = address;
139  }
140  }
141 
142 
143  /**
144  * Check whether given identifier is in range of this router.
145  *
146  * \param id identifier
147  * \return true if in range; else false
148  */
149  bool in_range(const int id) const
150  {
151  return (id >= this->first && id < this->first + (int) this->size());
152  }
153 
154 
155  /**
156  * Get address of given identifier.
157  *
158  * \param id identifier
159  * \return address
160  */
161  const JAddress_t& get(const int id) const
162  {
163  return this->at(id - this->first);
164  }
165 
166  protected:
167  int first;
168  };
169 
170 
171  /**
172  * Template specialisation of JRouter with default address comparison.
173  */
174  template<class JAddress_t>
175  class JRouter<JAddress_t, true> :
176  public JRouter<JAddress_t, false>
177  {
178 
180 
181  protected:
182  /**
183  * Simple data structure for validation of address.
184  */
185  class JAddress
186  {
187  public:
188  /**
189  * Default constructor.
190  */
192  __is_valid(false),
193  __address ()
194  {}
195 
196 
197  /**
198  * Constructor.
199  *
200  * \param address default address
201  */
203  __is_valid(true),
204  __address (address)
205  {}
206 
207 
208  /**
209  * Check validity.
210  *
211  * \return true if valid; else false
212  */
213  bool is_valid() const
214  {
215  return __is_valid;
216  }
217 
218 
219  /**
220  * Get address.
221  *
222  * \return address
223  */
224  const JAddress_t& getAddress() const
225  {
226  return __address;
227  }
228 
229 
230  /**
231  * Compare to given address.
232  *
233  * \param address address
234  * \return true if valid and equal; else false
235  */
236  bool equals(argument_type address) const
237  {
238  return __is_valid && address == __address;
239  }
240 
241 
242  private:
244  JAddress_t __address;
245  };
246 
247 
248  public:
249  /**
250  * Default constructor.
251  */
253  JRouter<JAddress_t, false>(),
254  defaultAddress()
255  {}
256 
257 
258  /**
259  * Constructor.
260  *
261  * \param address default address
262  */
264  JRouter<JAddress_t, false>(),
265  defaultAddress(address)
266  {}
267 
268 
269  /**
270  * Check availability of default address.
271  *
272  * \return true if available; else false
273  */
274  bool hasDefaultAddress() const
275  {
276  return defaultAddress.is_valid();
277  }
278 
279 
280  /**
281  * Get default address.
282  *
283  * \return default address
284  */
285  virtual const JAddress_t& getDefaultAddress() const override
286  {
287  return defaultAddress.getAddress();
288  }
289 
290 
291  /**
292  * Set default address.
293  *
294  * \param address default address
295  */
297  {
298  defaultAddress = JAddress(address);
299  }
300 
301 
302  /**
303  * Test whether given identifier has valid address (i.e identifier is in range and corresponding address is not equal to default address).
304  *
305  * \param id identifier
306  * \return true if identifier in range and address not equal to default; else false
307  */
308  bool has(const int id) const
309  {
310  return (this->in_range(id) && !this->defaultAddress.equals(this->get(id)));
311  }
312 
313 
314  private:
315  JAddress defaultAddress;
316  };
317 }
318 
319 #endif
const JAddress_t & getAddress() const
Get address.
Definition: JRouter.hh:224
bool in_range(const int id) const
Check whether given identifier is in range of this router.
Definition: JRouter.hh:149
Direct addressing of elements with unique identifiers.
Definition: JRouter.hh:27
JRouter()
Default constructor.
Definition: JRouter.hh:252
bool hasDefaultAddress() const
Check availability of default address.
Definition: JRouter.hh:274
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:179
JRouter()
Default constructor.
Definition: JRouter.hh:44
then usage $script< detector file >< detectorfile > nIf the range of floors is the first detector file is aligned to the second before the comparison nIn this
JRouter(argument_type address)
Constructor.
Definition: JRouter.hh:263
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
virtual const JAddress_t & getDefaultAddress() const override
Get default address.
Definition: JRouter.hh:285
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
void setDefaultAddress(argument_type address)
Set default address.
Definition: JRouter.hh:296
JAddress(argument_type address)
Constructor.
Definition: JRouter.hh:202
void align(const JRouter &router)
Align router.
Definition: JRouter.hh:98
virtual ~JRouter()
Virtual destructor.
Definition: JRouter.hh:52
bool has(const int id) const
Test whether given identifier has valid address (i.e identifier is in range and corresponding address...
Definition: JRouter.hh:308
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:38
bool is_valid() const
Check validity.
Definition: JRouter.hh:213
void put(const int id, argument_type address)
Store address for given identifier.
Definition: JRouter.hh:114
bool equals(argument_type address) const
Compare to given address.
Definition: JRouter.hh:236
void swap(JRouter &router)
Swap router.
Definition: JRouter.hh:85
virtual const JAddress_t & getDefaultAddress() const
Get default address.
Definition: JRouter.hh:61