1 #ifndef XSENS_MONOLITHIC
34 #ifndef XSENS_MONOLITHIC
35 #ifndef PSTDINT_H_INCLUDED
40 #ifndef XSENS_EXCEPTION_H
44 #ifndef XSENS_JANITORS_H
49 #define XSENS_LIST_NOTFOUND 0xFFFFFFFF
51 #ifdef _XSENS_LIST_RANGE_CHECKS
53 # define XSENS_LIST_THROW throw(...)
55 # define XSENS_LIST_THROW
58 # define XSENS_LIST_THROW
67 #define XSENS_LIST_LINEAR_SEARCH_TRESHOLD 10
82 void operator = (
const List& list);
83 void qSort(uint32_t left, uint32_t right);
86 void qSortDeref(uint32_t left, uint32_t right);
96 List(
const uint32_t size, T* src,
bool manage);
100 typedef int32_t (*
cmpFunc) (
const T&,
const T&);
105 List(
const uint32_t size);
109 List(
const uint32_t size,
const T* src);
120 void resize(uint32_t newSize);
122 void append(
const T& item);
130 template <
typename TB>
133 template <
typename TR>
146 T&
minVal(
void) const XSENS_LIST_THROW;
148 T&
maxVal(
void) const XSENS_LIST_THROW;
150 T&
get(const uint32_t index) const XSENS_LIST_THROW;
152 T& operator [] (const uint32_t index) const XSENS_LIST_THROW;
154 void insert(const T& item, const uint32_t index);
156 template <typename TB>
157 void insertCopy(const TB& item, const uint32_t index);
163 template <typename TB>
174 template <
typename T2>
177 template <
typename TB>
178 uint32_t
find(
const TB& item)
const;
180 template <
typename TB>
181 uint32_t
findDeref(
const TB& item)
const;
183 template <
typename TB>
186 template <
typename TB>
190 template <
typename TB>
193 template <
typename TB>
197 template <
typename TB>
200 template <
typename TB>
224 template <
typename TB>
232 template <
typename TB>
237 template <
typename T>
242 m_data = (T*) malloc(m_max *
sizeof(T));
244 throw std::bad_alloc();
249 template <
typename T>
256 m_data = (T*) malloc(m_max *
sizeof(T));
258 throw std::bad_alloc();
263 template <
typename T>
270 m_data = (T*) malloc(m_max *
sizeof(T));
272 throw std::bad_alloc();
275 memcpy(m_data,src.
m_data,m_count*
sizeof(T));
279 template <
typename T>
286 m_data = (T*) malloc(m_max *
sizeof(T));
288 throw std::bad_alloc();
291 memcpy(m_data,src,m_count *
sizeof(T));
295 template <
typename T>
305 template <
typename T>
310 if (m_manage && m_data != NULL)
316 template <
typename T>
319 for (
unsigned i=0;i<m_count;++i)
324 template <
typename T>
327 for (
unsigned i=0;i<m_count;++i)
332 template <
typename T>
338 template <
typename T>
343 if (newSize == m_max)
345 if (m_count > newSize)
352 m_data = (T*) realloc(m_data,m_max *
sizeof(T));
356 template <
typename T>
359 if (m_count == m_max)
360 resize(m_max + 1 + m_max/2);
361 m_data[m_count++] = item;
364 template <
typename T>
367 if (m_count+count > m_max)
368 resize(m_max + count + m_max/2);
369 for (
unsigned i = 0; i < count; ++i)
370 m_data[m_count++] = lst[i];
373 template <
typename T>
376 if (m_max < source.
m_count + m_count)
377 resize(source.
m_count + m_count);
379 for (uint32_t i = 0;i<source.
m_count;++i)
380 m_data[m_count++] =
new T(*source.
m_data[i]);
383 template <
typename T>
386 if (m_max < source.
m_count + m_count)
387 resize(source.
m_count + m_count);
389 for (uint32_t i = 0;i<source.
m_count;++i)
390 m_data[m_count++] = source.
m_data[i];
393 template <
typename T>
394 template <
typename TB>
397 if (m_count == m_max)
398 resize(m_max + 1 + m_max/2);
399 m_data[m_count++] =
new TB(item);
402 template <
typename T>
403 template <
typename TR>
406 if (m_count == m_max)
407 resize(m_max + 1 + m_max/2);
408 m_data[m_count++] = item;
411 template <
typename T>
414 #ifdef _XSENS_LIST_RANGE_CHECKS
416 throw Exception(
"List.last: empty list");
418 return m_data[m_count-1];
421 template <
typename T>
424 #ifdef _XSENS_LIST_RANGE_CHECKS
426 throw Exception(
"List.maxVal: empty list");
428 T* item = &m_data[0];
429 for (uint32_t i = 1; i < m_count; ++i)
430 if (*item < m_data[i])
435 template <
typename T>
438 #ifdef _XSENS_LIST_RANGE_CHECKS
440 throw Exception(
"List.minVal: empty list");
442 T* item = &m_data[0];
443 for (uint32_t i = 1; i < m_count; ++i)
444 if (m_data[i] < *item)
449 template <
typename T>
452 #ifdef _XSENS_LIST_RANGE_CHECKS
453 if (index >= m_count)
454 throw Exception(
"List.get: index out of bounds");
456 if (index >= m_count)
457 return m_data[m_count-1];
458 return m_data[index];
461 template <
typename T>
464 if (m_count == m_max)
465 resize(1 + m_max + (m_max >> 1));
466 for (
unsigned i=m_count;i>index;--i)
467 m_data[i] = m_data[i-1];
468 if (index <= m_count)
469 m_data[index] = item;
471 m_data[m_count] = item;
475 template <
typename T>
476 template <
typename TB>
479 if (m_count == m_max)
480 resize(1 + m_max + (m_max >> 1));
481 for (
unsigned i=m_count;i>index;--i)
482 m_data[i] = m_data[i-1];
483 if (index <= m_count)
484 m_data[index] =
new TB(item);
486 m_data[m_count] =
new TB(item);
490 template <
typename T>
493 #ifdef _XSENS_LIST_RANGE_CHECKS
494 if (index >= m_count)
495 throw Exception(
"List[]: index out of bounds");
497 return m_data[index];
501 template <
typename T>
504 uint32_t l_hold, r_hold;
509 pivot = m_data[left];
512 while (!(m_data[right] < pivot) && (left < right))
516 m_data[left] = m_data[right];
519 while (!(pivot < m_data[left]) && (left < right))
521 if (!(left == right))
523 m_data[right] = m_data[left];
527 m_data[left] = pivot;
529 qSort(l_hold, left-1);
531 qSort(left+1, r_hold);
534 template <
typename T>
535 void List<T>::qSortDeref(uint32_t left, uint32_t right)
537 uint32_t l_hold, r_hold;
542 pivot = m_data[left];
545 while (!(*m_data[right] < *pivot) && (left < right))
549 m_data[left] = m_data[right];
552 while (!(*pivot < *m_data[left]) && (left < right))
554 if (!(left == right))
556 m_data[right] = m_data[left];
560 m_data[left] = pivot;
562 qSortDeref(l_hold, left-1);
564 qSortDeref(left+1, r_hold);
567 template <
typename T>
579 Linker* list = (Linker*) malloc(m_count*
sizeof(Linker));
581 throw std::bad_alloc();
586 list[0].item = m_data[0];
590 for (uint32_t i = 1; i < m_count; ++i)
593 list[i].item = m_data[i];
594 if (m_data[i] < m_data[curr->index])
596 while (curr->prev != NULL)
599 if (!(m_data[i] < m_data[curr->index]))
602 list[i].next = curr->next;
604 curr->next->prev = &list[i];
605 curr->next = &list[i];
610 if (curr != &list[i])
614 curr->prev = &list[i];
620 while (curr->next != NULL)
623 if (m_data[i] < m_data[curr->index])
627 list[i].prev = curr->prev;
628 curr->prev->next = &list[i];
629 curr->prev = &list[i];
634 if (curr != &list[i])
638 curr->next = &list[i];
645 while (curr->prev != NULL) curr = curr->prev;
648 for (uint32_t i = 0; i < m_count; ++i)
650 m_data[i] = curr->item;
657 template <
typename T>
670 Linker* list = (Linker*) malloc(m_count*
sizeof(Linker));
672 throw std::bad_alloc();
677 list[0].item = m_data[0];
681 for (uint32_t i = 1; i < m_count; ++i)
684 list[i].item = m_data[i];
685 if (*m_data[i] < *m_data[curr->index])
687 while (curr->prev != NULL)
690 if (!(*m_data[i] < *m_data[curr->index]))
693 list[i].next = curr->next;
695 curr->next->prev = &list[i];
696 curr->next = &list[i];
701 if (curr != &list[i])
705 curr->prev = &list[i];
711 while (curr->next != NULL)
714 if (*m_data[i] < *m_data[curr->index])
718 list[i].prev = curr->prev;
719 curr->prev->next = &list[i];
720 curr->prev = &list[i];
725 if (curr != &list[i])
729 curr->next = &list[i];
736 while (curr->prev != NULL) curr = curr->prev;
739 for (uint32_t i = 0; i < m_count; ++i)
741 m_data[i] = curr->item;
748 template <
typename T>
749 template <
typename T2>
755 #ifdef _XSENS_LIST_RANGE_CHECKS
757 throw Exception(
"List.twinSortAscending: sizes do not match");
759 uint32_t iteration = 0;
764 while (iteration < m_count-1)
767 for (uint32_t i=iteration+1;i<m_count;++i)
769 if (m_data[i] < m_data[mini])
772 if (mini != iteration)
775 m_data[mini] = m_data[iteration];
776 m_data[iteration] = tmp;
780 twin.
m_data[iteration] = tmp2;
786 template <
typename T>
789 #ifdef _XSENS_LIST_RANGE_CHECKS
790 if (index >= m_count)
791 throw Exception(
"List.remove: index out of bounds");
793 if (index == m_count-1)
799 for (
unsigned i = index;i < m_count;++i)
800 m_data[i] = m_data[i+1];
803 template <
typename T>
806 #ifdef _XSENS_LIST_RANGE_CHECKS
808 throw Exception(
"List.removeTail: list size less than remove count");
818 template <
typename T>
821 #ifdef _XSENS_LIST_RANGE_CHECKS
823 throw Exception(
"List.deleteAndRemoveTail: list size less than remove count");
827 for (
unsigned i = 0;i < count;++i)
828 delete m_data[--m_count];
834 template <
typename T>
837 #ifdef _XSENS_LIST_RANGE_CHECKS
839 throw Exception(
"List.freeAndRemoveTail: list size less than remove count");
843 for (
unsigned i = 0;i < count;++i)
844 free(m_data[--m_count]);
850 template <
typename T>
853 uint32_t removed = 0;
854 for (uint32_t i=0;i < m_count; ++i)
856 for (uint32_t j=i+1;j < m_count; ++j)
858 if (m_data[i] == m_data[j])
869 template <
typename T>
872 uint32_t removed = 0;
873 for (uint32_t i=0;i < m_count; ++i)
875 for (uint32_t j=i+1;j < m_count; ++j)
877 if (*(m_data[i]) == *(m_data[j]))
888 template <
typename T>
891 #ifdef _XSENS_LIST_RANGE_CHECKS
892 if (index >= m_count)
893 throw Exception(
"List.deleteAndRemove: index out of bounds");
895 delete m_data[index];
896 if (index == m_count-1)
902 for (
unsigned i = index;i < m_count;++i)
903 m_data[i] = m_data[i+1];
906 template <
typename T>
909 #ifdef _XSENS_LIST_RANGE_CHECKS
910 if (index >= m_count)
911 throw Exception(
"List.freeAndRemove: index out of bounds");
914 if (index == m_count-1)
920 for (
unsigned i = index;i < m_count;++i)
921 m_data[i] = m_data[i+1];
924 template <
typename T>
925 template <
typename TB>
928 for (uint32_t i=0;i<m_count;++i)
929 if (((
const T*)m_data)[i] == item)
934 template <
typename T>
937 for (uint32_t i=0;i<m_count;++i)
938 if (!fnc(m_data[i],item))
943 template <
typename T>
944 template <
typename TB>
947 for (uint32_t i=0;i<m_count;++i)
948 if (*(m_data[i]) == item)
953 template <
typename T>
954 template <
typename TB>
958 for (uint32_t i=m_count-1 ; i<m_count ; --i)
959 if (((
const T*)m_data)[i] == item)
964 template <
typename T>
965 template <
typename TB>
969 for (uint32_t i=m_count-1 ; i<m_count ; --i)
970 if (*(m_data[i]) == item)
975 template <
typename T>
976 template <
typename TB>
982 uint32_t x = m_count;
990 if (m_data[i-1] == item)
992 if (m_data[i-1] < item)
1000 template <
typename T>
1001 template <
typename TB>
1005 return findDeref(item);
1007 uint32_t x = m_count;
1015 if (*(m_data[i-1]) < item)
1017 else if (*(m_data[i-1]) == item)
1025 template <
typename T>
1026 template <
typename TB>
1032 for (i=0;i<m_count;++i)
1033 if (item <= m_data[i])
1039 uint32_t x = m_count;
1046 if (m_data[i-1] < item)
1048 else if (m_data[i-1] == item)
1057 template <
typename T>
1058 template <
typename TB>
1064 for (i=0;i<m_count;++i)
1065 if (item <= *m_data[i])
1071 uint32_t x = m_count;
1078 if (*(m_data[i-1]) < item)
1080 else if (*(m_data[i-1]) == item)
1089 template <
typename T>
1092 uint32_t i = findSortedForInsert(item);
1095 else if (item == m_data[i])
1102 template <
typename T>
1105 uint32_t i = findSortedDerefForInsert(*item);
1108 else if (*item == *m_data[i])
1115 template <
typename T>
1116 template <
typename TB>
1119 uint32_t i = findSortedDerefForInsert(item);
1121 appendCopy<TB>(item);
1122 else if (item == m_data[i])
1125 insertCopy<TB>(item,i);
1129 template <
typename T>
1140 template <
typename T>
1151 template <
typename T>
1152 template <
typename TB>
1159 for (uint32_t i = 0;i<m_count;++i)
1160 m_data[i] =
new TB(*source.
m_data[i]);
1163 template <
typename T>
1170 for (uint32_t i = 0;i<m_count;++i)
1174 template <
typename T>
1177 #ifdef _XSENS_LIST_RANGE_CHECKS
1178 if (i >= m_count || j >= m_count)
1179 throw Exception(
"List.swap: index out of bounds");
1182 m_data[i] = m_data[j];
1186 template <
typename T>
1189 uint32_t half = m_count / 2;
1190 for (uint32_t i = 0, end=m_count-1; i < half; ++i,--end)
1193 m_data[i] = m_data[end];
1198 template <
typename T>
1199 template <
typename TB>
1204 for (uint32_t i = 0; i < m_count; ++i)
1205 if (!(m_data[i] == lst.
m_data[i]))
1212 #endif // XSENS_LIST_H
void remove(const uint32_t index) XSENS_LIST_THROW
Removes an item at the given index in the list.
Definition: xsens_list.h:787
void deleteItemsOnDestroy(void)
Definition: xsens_list.h:1130
void swap(const uint32_t i, const uint32_t j) XSENS_LIST_THROW
Swaps two items in the list.
Definition: xsens_list.h:1175
void freeAndRemoveTail(const uint32_t count) XSENS_LIST_THROW
Definition: xsens_list.h:835
T & get(const uint32_t index) const XSENS_LIST_THROW
Retrieves the item at the given index. An index beyond the end returns the first item.
Definition: xsens_list.h:450
void freeAndClear(void)
Calls free for all items in the list and then clears the list.
Definition: xsens_list.h:325
Definition: xsens_exception.h:9
uint32_t findSortedForInsert(const TB &item) const
Finds an item in a sorted list (binary search) using the T::== and T::< operators. If not found, it does not return XSENS_LIST_NOTFOUND but the insert position if this item would be inserted in the list.
Definition: xsens_list.h:1027
uint32_t reverseFindDeref(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.h:966
Contains the Janitor class-interfaces and implementations.
bool operator==(const List< TB > &lst)
Compare each item of the lists using the T == TB operator. If they're all identical, returns true.
Definition: xsens_list.h:1200
void deleteAndClear(void)
Calls delete for all items in the list and then clears the list.
Definition: xsens_list.h:317
uint32_t length(void) const
Returns the number of items currently in the list.
Definition: xsens_list.h:166
#define __cdecl
Definition: cmtdef.h:51
void deleteAndRemove(const uint32_t index) XSENS_LIST_THROW
Removes an item at the given index in the list.
Definition: xsens_list.h:889
uint32_t count(void) const
Returns the number of items currently in the list.
Definition: xsens_list.h:168
uint32_t findSortedDerefForInsert(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.h:1059
Dynamic list class.
Definition: xsens_list.h:79
void reverse(void)
Reverse the order of the list, useful for sorted lists that are read/created in the reverse order...
Definition: xsens_list.h:1187
List()
Standard constructor, creates an empty list with some room for items.
Definition: xsens_list.h:238
int32_t(__cdecl * InequalityFunction)(const T &, const T &)
Type for an equality compare function, should return true when NOT equal.
Definition: xsens_list.h:211
T & maxVal(void) const XSENS_LIST_THROW
Retrieves the largest item, using the T::< operator.
Definition: xsens_list.h:422
JanitorClassFunc< List< T > > * m_jcf
Used to clean up the list on exit.
Definition: xsens_list.h:92
void sortAscendingDeref(void)
Sorts the list in an ascending order, using the T::< operator on dereferenced list items...
Definition: xsens_list.h:658
const T * getBuffer(void) const
Returns the start of the linear data buffer.
Definition: xsens_list.h:230
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.h:945
Class function calling janitor class.
Definition: xsens_janitors.h:45
void clear(void)
Clears the list without explicitly deleting anything.
Definition: xsens_list.h:333
uint32_t reverseFind(const TB &item) const
Finds an item in an unsorted list (walk over all items) using the T::== operator, starting at the end...
Definition: xsens_list.h:955
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.h:462
T & minVal(void) const XSENS_LIST_THROW
Retrieves the smallest item, using the T::< operator.
Definition: xsens_list.h:436
uint32_t insertSorted(const T &item)
Assumes the list is sorted and inserts the item at the appropriate spot.
Definition: xsens_list.h:1090
~List()
Destroy the list. This does NOT automatically delete items IN the list.
Definition: xsens_list.h:306
void appendDeepCopy(const List< T > &source)
Adds the contents of the source list to the end of the list.
Definition: xsens_list.h:374
bool m_manage
Definition: xsens_list.h:93
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.h:926
#define XSENS_LIST_THROW
Definition: xsens_list.h:58
void appendCopy(const TB &item)
Adds a copy of a referenced item to the end of the list using newItem = new TB(item).
Definition: xsens_list.h:395
#define XSENS_LIST_NOTFOUND
Definition: xsens_list.h:49
uint32_t m_count
The number of items currently in the list.
Definition: xsens_list.h:91
void deleteAndRemoveTail(const uint32_t count) XSENS_LIST_THROW
Definition: xsens_list.h:819
uint32_t removeDuplicateEntriesDeref(void)
Removes any duplicate entries and returns the number of items removed. Items are compared after deref...
Definition: xsens_list.h:870
void appendList(uint32_t count, const T *lst)
Adds a number of items to the end of the list.
Definition: xsens_list.h:365
uint32_t insertSortedCopy(const TB &item)
Assumes the list is sorted and inserts a copy of the referenced item at the appropriate spot...
Definition: xsens_list.h:1117
void freeAndRemove(const uint32_t index) XSENS_LIST_THROW
Removes an item at the given index in the list.
Definition: xsens_list.h:907
void resize(uint32_t newSize)
Resizes the list to at least the given size.
Definition: xsens_list.h:339
T & last(void) const XSENS_LIST_THROW
Retrieves the last item.
Definition: xsens_list.h:412
void appendShallowCopy(const List< T > &source)
Adds the contents of the source list to the end of the list.
Definition: xsens_list.h:384
T * m_data
The array containing the items.
Definition: xsens_list.h:89
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.h:1002
void isShallowCopyOf(const List< T > &source)
Overwrites the current list with a direct copy (a=b) of another list.
Definition: xsens_list.h:1164
uint32_t removeDuplicateEntries(void)
Removes any duplicate entries and returns the number of items removed. Items are compared directly...
Definition: xsens_list.h:851
#define XSENS_LIST_LINEAR_SEARCH_TRESHOLD
Definition: xsens_list.h:67
void sortAscending(void)
Sorts the list in an ascending order, using the T::< operator.
Definition: xsens_list.h:568
void removeTail(const uint32_t count) XSENS_LIST_THROW
Removes items from the end of the list.
Definition: xsens_list.h:804
uint32_t insertSortedDeref(const T &item)
Assumes the list is sorted by dereferenced values and inserts the item at the appropriate spot...
Definition: xsens_list.h:1103
void freeItemsOnDestroy(void)
Definition: xsens_list.h:1141
void append(const T &item)
Adds an item to the end of the list.
Definition: xsens_list.h:357
T & operator[](const uint32_t index) const XSENS_LIST_THROW
Retrieves the item at the given index. An index beyond the end probably causes an exception...
Definition: xsens_list.h:491
uint32_t m_max
The current size of the data array.
Definition: xsens_list.h:90
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.h:750
void appendRelated(const TR &item)
Adds a related item to the end of the list, using the T = TR operator.
Definition: xsens_list.h:404
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.h:477
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.h:977
int32_t(* cmpFunc)(const T &, const T &)
A comparison function type, should return -1, 0 or 1 for <, == and >
Definition: xsens_list.h:100
void isDeepCopyOf(const List< T > &source)
Make a copy of the list, duplicating list items i with: copy[i] = new TB(*source[i]) ...
Definition: xsens_list.h:1153