rangequeue.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification,
6 // are permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice,
9 // this list of conditions, and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice,
12 // this list of conditions, and the following disclaimer in the documentation
13 // and/or other materials provided with the distribution.
14 //
15 // 3. Neither the names of the copyright holders nor the names of their contributors
16 // may be used to endorse or promote products derived from this software without
17 // specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
24 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
26 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
28 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
29 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
30 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
31 //
32 
33 
34 // Copyright (c) 2003-2021 Xsens Technologies B.V. or subsidiaries worldwide.
35 // All rights reserved.
36 //
37 // Redistribution and use in source and binary forms, with or without modification,
38 // are permitted provided that the following conditions are met:
39 //
40 // 1. Redistributions of source code must retain the above copyright notice,
41 // this list of conditions, and the following disclaimer.
42 //
43 // 2. Redistributions in binary form must reproduce the above copyright notice,
44 // this list of conditions, and the following disclaimer in the documentation
45 // and/or other materials provided with the distribution.
46 //
47 // 3. Neither the names of the copyright holders nor the names of their contributors
48 // may be used to endorse or promote products derived from this software without
49 // specific prior written permission.
50 //
51 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
52 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
53 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
54 // THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 // SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
56 // OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY OR
58 // TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.THE LAWS OF THE NETHERLANDS
60 // SHALL BE EXCLUSIVELY APPLICABLE AND ANY DISPUTES SHALL BE FINALLY SETTLED UNDER THE RULES
61 // OF ARBITRATION OF THE INTERNATIONAL CHAMBER OF COMMERCE IN THE HAGUE BY ONE OR MORE
62 // ARBITRATORS APPOINTED IN ACCORDANCE WITH SAID RULES.
63 //
64 
65 #ifndef RANGE_QUEUE_H
66 #define RANGE_QUEUE_H
67 
68 #include <list>
69 #include <xscommon/xsens_mutex.h>
70 
74 template <class T>
76 {
77 public:
81  {
82  }
83 
84  virtual ~RangeQueue()
85  {
86  }
87 
90  void clear()
91  {
92  xsens::Lock locky(&m_mutex);
93  m_queue.clear();
94  m_count = 0;
95  }
96 
103  void pushBack(T start, T end)
104  {
105  if (end < start)
106  return;
107 
108  xsens::Lock locky(&m_mutex);
109  if (!empty() && (start <= last()))
110  return;
111 
112  Range newRange(start, end);
113  JLTRACEG("Adding range: [" << start << " - " << end << "]");
114  m_queue.push_back(newRange);
115  m_count += 1 + end - start;
116  }
117 
122  void remove(T index)
123  {
124  xsens::Lock locky(&m_mutex);
125  if (empty())
126  return;
127 
128  for (auto r = m_queue.rbegin(); r != m_queue.rend(); ++r)
129  {
130  if ((index > r->m_end) || (index < r->m_start))
131  continue;
132 
133  --m_count;
134  if (r->m_start == r->m_end)
135  m_queue.erase(--(r.base()));
136  else if (r->m_start == index)
137  r->m_start++;
138  else if (r->m_end == index)
139  r->m_end--;
140  else
141  {
142  Range split(r->m_start, index - 1);
143  r->m_start = index + 1;
144  m_queue.insert(--(r.base()), split);
145  }
146  return;
147  }
148  }
149 
152  void popFront(T upto)
153  {
154  xsens::Lock locky(&m_mutex);
155 
156  while (!m_queue.empty() && (m_queue.front().m_end <= upto))
157  {
158  auto const& i = m_queue.front();
159  m_count -= (i.m_end - i.m_start) + 1;
160  m_queue.pop_front();
161  }
162 
163  if (empty())
164  {
165  JLDEBUGG("Removed upto: " << upto << ", now empty");
166  m_count = 0;
167  return;
168  }
169  if (m_queue.front().m_start <= upto)
170  {
171  m_count -= 1 + upto - m_queue.front().m_start;
172  m_queue.front().m_start = upto + 1;
173  }
174  JLDEBUGG("Removed upto: " << upto << ", new first = " << m_queue.front().m_start);
175  }
176 
179  void popBack(T upto)
180  {
181  xsens::Lock locky(&m_mutex);
182 
183  while (!m_queue.empty() && (m_queue.back().m_start >= upto))
184  {
185  auto const& i = m_queue.back();
186  m_count -= (i.m_end - i.m_start) + 1;
187  m_queue.pop_back();
188  }
189 
190  if (empty())
191  {
192  JLDEBUGG("Removed upto: " << upto << ", now empty");
193  m_count = 0;
194  return;
195  }
196  if (m_queue.back().m_end >= upto)
197  {
198  m_count -= 1 + m_queue.back().m_end - upto;
199  m_queue.back().m_end = upto - 1;
200  }
201  JLDEBUGG("Removed upto: " << upto << ", new last = " << m_queue.back().m_end);
202  }
203 
206  T first() const
207  {
208  xsens::Lock locky(&m_mutex);
209  if (empty())
210  return illegalIndex();
211  return m_queue.front().m_start;
212  }
213 
216  T last() const
217  {
218  xsens::Lock locky(&m_mutex);
219  if (empty())
220  return illegalIndex();
221 
222  return m_queue.back().m_end;
223  }
224 
236  const T operator[](std::size_t index) const
237  {
238  xsens::Lock locky(&m_mutex);
239 
240  std::size_t range = index / 2;
241  uint32_t end = index % 2;
242 
243  if (m_queue.size() < range)
244  return illegalIndex();
245 
246  auto it = m_queue.begin();
247 
248  for (uint32_t i = 0; i < range; ++i)
249  ++it;
250 
251  if (end)
252  return it->m_end;
253  else
254  return it->m_start;
255  }
256 
259  bool contains(uint32_t index) const
260  {
261  xsens::Lock locky(&m_mutex);
262  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
263  {
264  if ((index >= i->m_start) && (index <= i->m_end))
265  return true;
266  }
267  return false;
268  }
269 
272  T count() const
273  {
274  xsens::Lock locky(&m_mutex);
275  return m_count;
276  }
277 
280  {
281  xsens::Lock locky(&m_mutex);
282  m_count = 0;
283  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
284  m_count += (i->m_end - i->m_start) + 1;
285  return m_count;
286  }
287 
290  bool empty() const
291  {
292  return m_queue.empty();
293  }
294 
297  static const T illegalIndex()
298  {
299  return (T) - 1;
300  }
301 
307  void copy(RangeQueue<T>& destination, T start, T end)
308  {
309  xsens::Lock locky(&m_mutex);
310 
311  for (auto i = m_queue.begin(); i != m_queue.end(); ++i)
312  {
313  if ((i->m_start >= start && start <= i->m_end) && (i->m_start <= end))
314  destination.pushBack(i->m_start, std::min<T>(i->m_end, end));
315  else if (i->m_start < start && i->m_start < end)
316  destination.pushBack(start, std::min<T>(i->m_end, end));
317  }
318  }
319 
320 private:
321  struct Range
322  {
324  T m_end;
325  Range(T start, T end)
326  : m_start(start)
327  , m_end(end)
328  {}
329  };
331 
332  std::list<Range> m_queue;
334 };
335 
336 #endif
RangeQueue::empty
bool empty() const
Returns true if the queue is empty.
Definition: rangequeue.h:290
RangeQueue::Range::Range
Range(T start, T end)
Definition: rangequeue.h:325
RangeQueue::clear
void clear()
Clears the queue.
Definition: rangequeue.h:90
RangeQueue::pushBack
void pushBack(T start, T end)
Adds a range to the back of the queue Only a range newer than the most recent range in the queue is a...
Definition: rangequeue.h:103
RangeQueue::Range
Definition: rangequeue.h:321
RangeQueue::copy
void copy(RangeQueue< T > &destination, T start, T end)
Copy all ranges within the given limits [start, end]. If the limit falls in a certain range,...
Definition: rangequeue.h:307
RangeQueue::Range::m_end
T m_end
Definition: rangequeue.h:324
RangeQueue::operator[]
const T operator[](std::size_t index) const
Definition: rangequeue.h:236
RangeQueue::Range::m_start
T m_start
Definition: rangequeue.h:323
xsens_mutex.h
RangeQueue::contains
bool contains(uint32_t index) const
Checks if the given index is contained in the queue.
Definition: rangequeue.h:259
uint32_t
unsigned int uint32_t
Definition: pstdint.h:485
RangeQueue
A range queue.
Definition: rangequeue.h:75
RangeQueue::m_count
T m_count
Definition: rangequeue.h:330
RangeQueue::m_queue
std::list< Range > m_queue
Definition: rangequeue.h:332
RangeQueue::last
T last() const
Returns the last (highest) index in the queue.
Definition: rangequeue.h:216
JLTRACEG
#define JLTRACEG(msg)
Definition: journaller.h:279
xsens::Mutex
A base mutex class.
Definition: xsens_mutex.h:132
RangeQueue::popBack
void popBack(T upto)
Removes all the indices upto and including upto from the queue.
Definition: rangequeue.h:179
start
ROSCPP_DECL void start()
RangeQueue::count
T count() const
Returns the total number of indices captured by the ranges in the queue.
Definition: rangequeue.h:272
RangeQueue::~RangeQueue
virtual ~RangeQueue()
Definition: rangequeue.h:84
JLDEBUGG
#define JLDEBUGG(msg)
Definition: journaller.h:280
xsens::Lock
A base class for a Lock.
Definition: xsens_mutex.h:947
RangeQueue::popFront
void popFront(T upto)
Removes all the indices upto and including upto from the queue.
Definition: rangequeue.h:152
RangeQueue::first
T first() const
Returns the first index of the first range in the queue.
Definition: rangequeue.h:206
RangeQueue::RangeQueue
RangeQueue()
Default constructor.
Definition: rangequeue.h:80
RangeQueue::illegalIndex
static const T illegalIndex()
Returns the value for an illegal index.
Definition: rangequeue.h:297
RangeQueue::m_mutex
xsens::Mutex m_mutex
Definition: rangequeue.h:333
RangeQueue::remove
void remove(T index)
Removes a specific index from the queue.
Definition: rangequeue.h:122
RangeQueue::recount
T recount()
Returns the recounted total number of indices captured by the ranges in the queue.
Definition: rangequeue.h:279


xsens_mti_driver
Author(s):
autogenerated on Sun Sep 3 2023 02:43:20