Main MRPT website > C++ reference for MRPT 1.9.9
xsens_list.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #ifndef _XSENS_LIST_H_2006_06_08
11 #define _XSENS_LIST_H_2006_06_08
12 
13 #include <cstdint>
14 
15 #ifndef _JANITORS_H_2006_05_01
16 #include "xsens_janitors.h"
17 #endif
18 
19 #ifdef _XSENS_COMPILE
20 #ifndef _XSENS_LIST_WITH_MATH
21 #define _XSENS_LIST_WITH_MATH
22 #endif
23 #ifndef _XSENS_LIST_IO
24 #define _XSENS_LIST_IO
25 #endif
26 #endif
27 
28 #ifdef _DEBUG
29 #define _XSENS_LIST_RANGE_CHECKS
30 #endif
31 
32 #ifdef _XSENS_LIST_WITH_MATH
33 #include <math.h>
34 #ifndef _XSENS_MATH_H_2006_05_31
35 #include "xsens_math.h"
36 #endif
37 #endif
38 
39 #define XSENS_LIST_NOTFOUND 0xFFFFFFFF
40 
41 #ifdef _XSENS_LIST_RANGE_CHECKS
42 //# define XSENS_LIST_THROW throw(...) // JLBC: Changed for MRPT in Linux in
43 // Debug
44 #define XSENS_LIST_THROW
45 #else
46 #define XSENS_LIST_THROW
47 #endif
48 
49 namespace xsens
50 {
51 /*! \brief Dynamic list class
52 
53  This class can store items of the given type. If the type supports the <
54  operator
55  it can also be sorted.
56  Items in the list can be accessed through the [] operator or the get()
57  function.
58 
59  Do NOT use any item type that requires a constructor to work correctly.
60  Pointers to these
61  objects can work though.
62 */
63 template <typename T>
64 class List
65 {
66  private:
67  /** intentionally NOT implemented due to ambiguous nature */
68  void operator=(const List& list);
69  //! Sorts the list in an ascending order, using the T::< operator.
70  void qSort(uint32_t left, uint32_t right);
71  //! Sorts the list in an ascending order, using the T::< operator on
72  //! dereferenced list items.
73  void qSortDeref(uint32_t left, uint32_t right);
74 
75  protected:
76  /** The array containing the items */
77  T* m_data;
78  /** The current size of the data array */
80  /** The number of items currently in the list */
82  /** Used to clean up the list on exit */
84  bool m_manage;
85 
86  //! Construct a list as a reference to a raw list
87  List(const uint32_t size, T* src, bool manage);
88 
89  public:
90  //! A comparison function type, should return -1, 0 or 1 for <, == and >
91  typedef int32_t (*cmpFunc)(const T&, const T&);
92 
93  //! Standard constructor, creates an empty list with some room for items.
94  List();
95  //! Construct a list with a capacity of at least the given size.
96  List(const uint32_t size);
97  //! Construct a list as a direct copy of another list
98  List(const List<T>& src);
99  //! Construct a list as a copy of a raw list
100  List(const uint32_t size, const T* src);
101  //! Destroy the list. This does NOT automatically delete items IN the list.
102  ~List();
103 
104  //! Calls delete for all items in the list and then clears the list.
105  void deleteAndClear(void);
106  //! Calls free for all items in the list and then clears the list.
107  void freeAndClear(void);
108  //! Clears the list without explicitly deleting anything.
109  void clear(void);
110  //! Resizes the list to at least the given size.
111  void resize(uint32_t newSize);
112  //! Adds an item to the end of the list.
113  void append(const T& item);
114  //! Adds a number of items to the end of the list.
115  void appendList(uint32_t count, const T* lst);
116  //! Adds the contents of the source list to the end of the list.
117  void appendDeepCopy(const List<T>& source);
118  //! Adds the contents of the source list to the end of the list.
119  void appendShallowCopy(const List<T>& source);
120  //! Adds a copy of a referenced item to the end of the list.
121  template <typename TB>
122  void appendCopy(const TB& item);
123  //! Adds a related item to the end of the list, using the T = TR operator.
124  template <typename TR>
125  void appendRelated(const TR& item);
126  //! Removes an item at the given index in the list.
127  void remove(const uint32_t index) XSENS_LIST_THROW;
128  //! Swaps two items in the list.
129  void swap(const uint32_t i, const uint32_t j) XSENS_LIST_THROW;
130  //! Removes an item at the given index in the list.
132  //! Removes an item at the given index in the list.
134  //! Retrieves the last item.
135  T& last(void) const XSENS_LIST_THROW;
136  //! Retrieves the item at the given index. An index beyond the end returns
137  //! the first item.
138  T& get(const uint32_t index) const XSENS_LIST_THROW;
139  //! Retrieves the item at the given index. An index beyond the end probably
140  //! causes an exception.
141  T& operator[](const uint32_t index) const XSENS_LIST_THROW;
142  //! Inserts an item at the given index, shifting any items below it down one
143  //! spot.
144  void insert(const T& item, const uint32_t index);
145  //! Inserts a copy of the referenced item at the given index, shifting any
146  //! items below it down one spot.
147  template <typename TB>
148  void insertCopy(const TB& item, const uint32_t index);
149  //! Assumes the list is sorted and inserts the item at the appropriate spot.
150  uint32_t insertSorted(const T& item);
151  //! Assumes the list is sorted by dereferenced values and inserts the item
152  //! at the appropriate spot.
153  uint32_t insertSortedDeref(const T& item);
154  //! Assumes the list is sorted and inserts a copy of the referenced item at
155  //! the appropriate spot.
156  template <typename TB>
157  uint32_t insertSortedCopy(const TB& item);
158  //! Returns the number of items currently in the list.
159  uint32_t length(void) const { return m_count; }
160  //! Sorts the list in an ascending order, using the T::< operator.
161  void sortAscending(void);
162  //! Sorts the list in an ascending order, using the T::< operator on
163  //! dereferenced list items.
164  void sortAscendingDeref(void);
165  //! Sorts the first list in an ascending order, using the T::< operator, the
166  //! second list will be updated the same way.
167  template <typename T2>
168  void twinSortAscending(List<T2>& twin);
169  //! Finds an item in an unsorted list (walk over all items) using the T::==
170  //! operator
171  template <typename TB>
172  uint32_t find(const TB& item) const;
173  //! Finds an item in an unsorted list (walk over all items) using the T::==
174  //! operator on dereferenced list items
175  template <typename TB>
176  uint32_t findDeref(const TB& item) const;
177  //! Finds an item in a sorted list (binary search) using the T::== and T::<
178  //! operators
179  template <typename TB>
180  uint32_t findSorted(const TB& item) const;
181  //! Finds an item in a sorted list (binary search) using the T::== and T::<
182  //! operators on dereferenced list items
183  template <typename TB>
184  uint32_t findSortedDeref(const TB& item) const;
185  //! Reverse the order of the list, useful for sorted lists that are
186  //! read/created in the reverse order
187  void reverse(void);
188  //! Removes items from the end of the list.
192 
193  //! Type for an equality compare function, should return true when NOT equal
194  typedef int32_t(__cdecl* InequalityFunction)(const T&, const T&);
195  //! Finds an item in an unsorted list (walk over all items) using the given
196  //! inequality function
197  uint32_t find(const T item, InequalityFunction fnc) const;
198 
199  void deleteItemsOnDestroy(void);
200  void freeItemsOnDestroy(void);
201 
202  //! Removes any duplicate entries and returns the number of items removed.
203  //! Items are compared directly.
205  //! Removes any duplicate entries and returns the number of items removed.
206  //! Items are compared after dereferencing.
208 
209  //! Make a copy of the list, duplicating list items i with: copy[i] = new
210  //! TB(*source[i])
211  template <typename TB>
212  void isDeepCopyOf(const List<T>& source);
213  //! Overwrites the current list with a shallow (memcopy) copy of another
214  //! list.
215  void isShallowCopyOf(const List<T>& source);
216 
217  //! Returns the start of the linear data buffer
218  const T* getBuffer(void) const { return m_data; }
219 #ifdef _XSENS_LIST_IO
220 #ifdef _XSENS_LIST_WITH_MATH
221  //! Saves the list as a matrix in a .mat file. This assumes that T is a
222  //! Vector<T,E>
223  void saveAsMatlab(const char* filename, const char* varname) const;
224 #endif
225 #endif
226 };
227 
228 class IntList : public List<uint32_t>
229 {
230  private:
231  /** intentionally NOT implemented due to ambiguous nature */
232  void operator=(const IntList& list);
233 
234  public:
235  //! Standard constructor, creates an empty list with some room for items.
237  //! Construct a list with a capacity of at least the given size.
239  //! Construct a list as a direct copy of another list
241  //! Construct a list as a reference to a raw list
243  : List<uint32_t>(size, src, false)
244  {
245  }
246 
247  bool operator==(const IntList& lst);
248 
249  void addValue(int32_t value);
250  int32_t deserialize(const char* str);
251  int32_t readFromString(const char* str);
252  int32_t serialize(char* buffer) const;
253  void setIncremental(
254  const uint32_t start, const uint32_t end, const int32_t step);
255  int32_t writeToString(char* buffer) const;
256  int32_t writeToStringHex(char* buffer) const;
257 };
258 } // end of xsens namespace
259 
260 #endif // _XSENS_LIST_H_2006_06_08
void addValue(int32_t value)
Definition: xsens_list.cpp:148
GLuint GLuint GLsizei count
Definition: glext.h:3528
void freeItemsOnDestroy(void)
uint32_t m_count
The number of items currently in the list.
Definition: xsens_list.h:81
void clear(void)
Clears the list without explicitly deleting anything.
Definition: xsens_list.hpp:126
void append(const T &item)
Adds an item to the end of the list.
Definition: xsens_list.hpp:150
int32_t(* cmpFunc)(const T &, const T &)
A comparison function type, should return -1, 0 or 1 for <, == and >
Definition: xsens_list.h:91
void setIncremental(const uint32_t start, const uint32_t end, const int32_t step)
Definition: xsens_list.cpp:77
void appendDeepCopy(const List< T > &source)
Adds the contents of the source list to the end of the list.
Definition: xsens_list.hpp:167
void insertCopy(const TB &item, const uint32_t index)
Inserts a copy of the referenced item at the given index, shifting any items below it down one spot...
Definition: xsens_list.hpp:242
void twinSortAscending(List< T2 > &twin)
Sorts the first list in an ascending order, using the T::< operator, the second list will be updated ...
Definition: xsens_list.hpp:738
int32_t readFromString(const char *str)
Definition: xsens_list.cpp:94
uint32_t find(const TB &item) const
Finds an item in an unsorted list (walk over all items) using the T::== operator. ...
Definition: xsens_list.hpp:914
IntList()
Standard constructor, creates an empty list with some room for items.
Definition: xsens_list.h:236
GLuint buffer
Definition: glext.h:3917
void deleteAndRemoveTail(const uint32_t count) XSENS_LIST_THROW
Definition: xsens_list.hpp:807
void appendShallowCopy(const List< T > &source)
Adds the contents of the source list to the end of the list.
Definition: xsens_list.hpp:177
void appendRelated(const TR &item)
Adds a related item to the end of the list, using the T = TR operator.
Definition: xsens_list.hpp:197
bool operator==(const IntList &lst)
Definition: xsens_list.cpp:154
void freeAndRemoveTail(const uint32_t count) XSENS_LIST_THROW
Definition: xsens_list.hpp:823
void deleteAndClear(void)
Calls delete for all items in the list and then clears the list.
Definition: xsens_list.hpp:110
uint32_t findDeref(const TB &item) const
Finds an item in an unsorted list (walk over all items) using the T::== operator on dereferenced list...
Definition: xsens_list.hpp:933
void reverse(void)
Reverse the order of the list, useful for sorted lists that are read/created in the reverse order...
bool m_manage
Definition: xsens_list.h:84
void appendCopy(const TB &item)
Adds a copy of a referenced item to the end of the list.
Definition: xsens_list.hpp:188
GLuint src
Definition: glext.h:7278
int32_t serialize(char *buffer) const
Definition: xsens_list.cpp:67
uint32_t findSorted(const TB &item) const
Finds an item in a sorted list (binary search) using the T::== and T::< operators.
Definition: xsens_list.hpp:943
T * m_data
The array containing the items.
Definition: xsens_list.h:77
void resize(uint32_t newSize)
Resizes the list to at least the given size.
Definition: xsens_list.hpp:132
IntList(const IntList &src)
Construct a list as a direct copy of another list.
Definition: xsens_list.h:240
uint32_t insertSortedCopy(const TB &item)
Assumes the list is sorted and inserts a copy of the referenced item at the appropriate spot...
void operator=(const IntList &list)
intentionally NOT implemented due to ambiguous nature
void appendList(uint32_t count, const T *lst)
Adds a number of items to the end of the list.
Definition: xsens_list.hpp:158
void deleteAndRemove(const uint32_t index) XSENS_LIST_THROW
Removes an item at the given index in the list.
Definition: xsens_list.hpp:877
List()
Standard constructor, creates an empty list with some room for items.
Definition: xsens_list.hpp:38
void freeAndRemove(const uint32_t index) XSENS_LIST_THROW
Removes an item at the given index in the list.
Definition: xsens_list.hpp:895
T & last(void) const XSENS_LIST_THROW
Retrieves the last item.
Definition: xsens_list.hpp:205
GLuint index
Definition: glext.h:4054
void swap(const uint32_t i, const uint32_t j) XSENS_LIST_THROW
Swaps two items in the list.
GLuint GLuint end
Definition: glext.h:3528
IntList(const uint32_t size)
Construct a list with a capacity of at least the given size.
Definition: xsens_list.h:238
void removeTail(const uint32_t count) XSENS_LIST_THROW
Removes items from the end of the list.
Definition: xsens_list.hpp:792
Class function calling janitor class.
void insert(const T &item, const uint32_t index)
Inserts an item at the given index, shifting any items below it down one spot.
Definition: xsens_list.hpp:227
uint32_t insertSortedDeref(const T &item)
Assumes the list is sorted by dereferenced values and inserts the item at the appropriate spot...
JanitorClassFunc< List< T > > * m_jcf
Used to clean up the list on exit.
Definition: xsens_list.h:83
uint32_t insertSorted(const T &item)
Assumes the list is sorted and inserts the item at the appropriate spot.
Definition: xsens_list.hpp:992
void isShallowCopyOf(const List< T > &source)
Overwrites the current list with a shallow (memcopy) copy of another list.
void isDeepCopyOf(const List< T > &source)
Make a copy of the list, duplicating list items i with: copy[i] = new TB(*source[i]) ...
void operator=(const List &list)
intentionally NOT implemented due to ambiguous nature
void sortAscending(void)
Sorts the list in an ascending order, using the T::< operator.
Definition: xsens_list.hpp:453
int32_t writeToString(char *buffer) const
Definition: xsens_list.cpp:115
void qSort(uint32_t left, uint32_t right)
Sorts the list in an ascending order, using the T::< operator.
Definition: xsens_list.hpp:382
uint32_t removeDuplicateEntriesDeref(void)
Removes any duplicate entries and returns the number of items removed.
Definition: xsens_list.hpp:858
Dynamic list class.
Definition: xsens_list.h:64
__int32 int32_t
Definition: rptypes.h:46
#define XSENS_LIST_THROW
Definition: xsens_list.h:46
void qSortDeref(uint32_t left, uint32_t right)
Sorts the list in an ascending order, using the T::< operator on dereferenced list items...
Definition: xsens_list.hpp:415
const T * getBuffer(void) const
Returns the start of the linear data buffer.
Definition: xsens_list.h:218
int32_t deserialize(const char *str)
Definition: xsens_list.cpp:57
uint32_t length(void) const
Returns the number of items currently in the list.
Definition: xsens_list.h:159
uint32_t m_max
The current size of the data array.
Definition: xsens_list.h:79
uint32_t removeDuplicateEntries(void)
Removes any duplicate entries and returns the number of items removed.
Definition: xsens_list.hpp:839
GLsizei GLsizei GLchar * source
Definition: glext.h:4082
uint32_t findSortedDeref(const TB &item) const
Finds an item in a sorted list (binary search) using the T::== and T::< operators on dereferenced lis...
Definition: xsens_list.hpp:968
The namespace of all Xsens software since 2006.
Definition: cmt1.cpp:95
void freeAndClear(void)
Calls free for all items in the list and then clears the list.
Definition: xsens_list.hpp:118
GLsizei const GLfloat * value
Definition: glext.h:4117
IntList(const uint32_t size, uint32_t *src)
Construct a list as a reference to a raw list.
Definition: xsens_list.h:242
GLsizeiptr size
Definition: glext.h:3923
int32_t writeToStringHex(char *buffer) const
Definition: xsens_list.cpp:131
GLuint start
Definition: glext.h:3528
int32_t(__cdecl * InequalityFunction)(const T &, const T &)
Type for an equality compare function, should return true when NOT equal.
Definition: xsens_list.h:194
unsigned __int32 uint32_t
Definition: rptypes.h:47
T & operator[](const uint32_t index) const XSENS_LIST_THROW
Retrieves the item at the given index.
Definition: xsens_list.hpp:256
void sortAscendingDeref(void)
Sorts the list in an ascending order, using the T::< operator on dereferenced list items...
Definition: xsens_list.hpp:595
void deleteItemsOnDestroy(void)
~List()
Destroy the list. This does NOT automatically delete items IN the list.
Definition: xsens_list.hpp:99



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019