Jpp  19.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 <vector>
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::vector<JAddress_t>
36  {
37 
39 
40  public:
41  /**
42  * Default constructor.
43  */
44  JRouter() :
45  std::vector<JAddress_t>(),
46  first(0)
47  {}
48 
49 
50  /**
51  * Virtual destructor.
52  */
53  virtual ~JRouter()
54  {}
55 
56 
57  /**
58  * Get default address.
59  *
60  * \return default address
61  */
62  virtual const JAddress_t& getDefaultAddress() const
63  {
64  static JAddress_t address;
65 
66  return address;
67  }
68 
69 
70  /**
71  * Clear.
72  */
73  void clear()
74  {
75  static_cast<std::vector<JAddress_t>&>(*this).clear();
76 
77  this->first = 0;
78  }
79 
80 
81  /**
82  * Swap router.
83  *
84  * \param router router
85  */
86  void swap(JRouter& router)
87  {
88  std::swap(this->first, router.first);
89 
90  static_cast<std::vector<JAddress_t>&>(*this).swap(router);
91  }
92 
93 
94  /**
95  * Align router.
96  *
97  * \param router router
98  */
99  void align(const JRouter& router)
100  {
101  this->first = router.first;
102 
103  if (router.size() > this->size()) {
104  this->resize(router.size());
105  }
106  }
107 
108 
109  /**
110  * Store address for given identifier.
111  *
112  * \param id identifier
113  * \param address address
114  */
115  void put(const int id, argument_type address)
116  {
117  if (this->empty()) {
118 
119  this->push_back(address);
120 
121  this->first = id;
122 
123  } else if (id < this->first) {
124 
125  this->insert(this->begin(), this->first - id, getDefaultAddress());
126 
127  this->first = id;
128 
129  (*this)[0] = address;
130 
131  } else {
132 
133  const size_t index = (size_t) (id - this->first);
134 
135  if (index >= this->size()) {
136  this->resize(index + 1, getDefaultAddress());
137  }
138 
139  (*this)[index] = address;
140  }
141  }
142 
143 
144  /**
145  * Check whether given identifier is in range of this router.
146  *
147  * \param id identifier
148  * \return true if in range; else false
149  */
150  bool in_range(const int id) const
151  {
152  return (id >= this->first && id < this->first + (int) this->size());
153  }
154 
155 
156  /**
157  * Get address of given identifier.
158  *
159  * \param id identifier
160  * \return address
161  */
162  const JAddress_t& get(const int id) const
163  {
164  return (*this)[id - this->first];
165  }
166 
167  protected:
168  int first;
169  };
170 
171 
172  /**
173  * Template specialisation of JRouter with default address comparison.
174  */
175  template<class JAddress_t>
176  class JRouter<JAddress_t, true> :
177  public JRouter<JAddress_t, false>
178  {
179 
181 
182  protected:
183  /**
184  * Simple data structure for validation of address.
185  */
186  class JAddress
187  {
188  public:
189  /**
190  * Default constructor.
191  */
193  __is_valid(false),
194  __address ()
195  {}
196 
197 
198  /**
199  * Constructor.
200  *
201  * \param address default address
202  */
204  __is_valid(true),
205  __address (address)
206  {}
207 
208 
209  /**
210  * Check validity.
211  *
212  * \return true if valid; else false
213  */
214  bool is_valid() const
215  {
216  return __is_valid;
217  }
218 
219 
220  /**
221  * Get address.
222  *
223  * \return address
224  */
225  const JAddress_t& getAddress() const
226  {
227  return __address;
228  }
229 
230 
231  /**
232  * Compare to given address.
233  *
234  * \param address address
235  * \return true if valid and equal; else false
236  */
237  bool equals(argument_type address) const
238  {
239  return __is_valid && address == __address;
240  }
241 
242 
243  private:
245  JAddress_t __address;
246  };
247 
248 
249  public:
250  /**
251  * Default constructor.
252  */
254  JRouter<JAddress_t, false>(),
255  defaultAddress()
256  {}
257 
258 
259  /**
260  * Constructor.
261  *
262  * \param address default address
263  */
265  JRouter<JAddress_t, false>(),
266  defaultAddress(address)
267  {}
268 
269 
270  /**
271  * Check availability of default address.
272  *
273  * \return true if available; else false
274  */
275  bool hasDefaultAddress() const
276  {
277  return defaultAddress.is_valid();
278  }
279 
280 
281  /**
282  * Get default address.
283  *
284  * \return default address
285  */
286  virtual const JAddress_t& getDefaultAddress() const override
287  {
288  return defaultAddress.getAddress();
289  }
290 
291 
292  /**
293  * Set default address.
294  *
295  * \param address default address
296  */
298  {
299  defaultAddress = JAddress(address);
300  }
301 
302 
303  /**
304  * Test whether given identifier has valid address (i.e identifier is in range and corresponding address is not equal to default address).
305  *
306  * \param id identifier
307  * \return true if identifier in range and address not equal to default; else false
308  */
309  bool has(const int id) const
310  {
311  return (this->in_range(id) && !this->defaultAddress.equals(this->get(id)));
312  }
313 
314 
315  private:
316  JAddress defaultAddress;
317  };
318 }
319 
320 #endif
const JAddress_t & getAddress() const
Get address.
Definition: JRouter.hh:225
bool in_range(const int id) const
Check whether given identifier is in range of this router.
Definition: JRouter.hh:150
Direct addressing of elements with unique identifiers.
Definition: JRouter.hh:27
JRouter()
Default constructor.
Definition: JRouter.hh:253
bool hasDefaultAddress() const
Check availability of default address.
Definition: JRouter.hh:275
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:180
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:264
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:286
JArgument< T >::argument_type argument_type
Definition: JClass.hh:82
void setDefaultAddress(argument_type address)
Set default address.
Definition: JRouter.hh:297
JAddress(argument_type address)
Constructor.
Definition: JRouter.hh:203
void align(const JRouter &router)
Align router.
Definition: JRouter.hh:99
virtual ~JRouter()
Virtual destructor.
Definition: JRouter.hh:53
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:309
JLANG::JClass< JAddress_t >::argument_type argument_type
Definition: JRouter.hh:38
bool is_valid() const
Check validity.
Definition: JRouter.hh:214
void put(const int id, argument_type address)
Store address for given identifier.
Definition: JRouter.hh:115
bool equals(argument_type address) const
Compare to given address.
Definition: JRouter.hh:237
void swap(JRouter &router)
Swap router.
Definition: JRouter.hh:86
virtual const JAddress_t & getDefaultAddress() const
Get default address.
Definition: JRouter.hh:62