10 #ifndef _XSENS_LIST_HPP_2006_06_08
11 #define _XSENS_LIST_HPP_2006_06_08
13 #ifndef _XSENS_LIST_H_2006_06_08
25 # include <malloc/malloc.h>
35 #define CMT_LIST_LINEAR_SEARCH_TRESHOLD 10
42 m_data = (T*) malloc(m_max *
sizeof(T));
55 m_data = (T*) malloc(m_max *
sizeof(T));
66 m_count =
src.m_count;
67 m_data = (T*) malloc(m_max *
sizeof(T));
70 memcpy(m_data,
src.m_data,m_count*
sizeof(T));
81 m_data = (T*) malloc(m_max *
sizeof(T));
103 if (m_manage && m_data != NULL)
109 template <
typename T>
112 for (
unsigned i=0;i<m_count;++i)
117 template <
typename T>
120 for (
unsigned i=0;i<m_count;++i)
125 template <
typename T>
131 template <
typename T>
136 if (newSize == m_max)
138 if (m_count > newSize)
145 m_data = (T*) realloc(m_data,m_max *
sizeof(T));
149 template <
typename T>
152 if (m_count == m_max)
153 resize(m_max + 1 + m_max/2);
154 m_data[m_count++] = item;
157 template <
typename T>
160 if (m_count+
count > m_max)
161 resize(m_max +
count + m_max/2);
162 for (
unsigned i = 0; i <
count; ++i)
163 m_data[m_count++] = lst[i];
166 template <
typename T>
169 if (m_max <
source.m_count + m_count)
170 resize(
source.m_count + m_count);
173 m_data[m_count++] =
new T(*
source.m_data[i]);
176 template <
typename T>
179 if (m_max <
source.m_count + m_count)
180 resize(
source.m_count + m_count);
183 m_data[m_count++] =
source.m_data[i];
186 template <
typename T>
187 template <
typename TB>
190 if (m_count == m_max)
191 resize(m_max + 1 + m_max/2);
192 m_data[m_count++] =
new TB(item);
195 template <
typename T>
196 template <
typename TR>
199 if (m_count == m_max)
200 resize(m_max + 1 + m_max/2);
201 m_data[m_count++] = item;
204 template <
typename T>
207 #ifdef _XSENS_LIST_RANGE_CHECKS
209 throw "List.last: empty list";
211 return m_data[m_count-1];
214 template <
typename T>
217 #ifdef _XSENS_LIST_RANGE_CHECKS
218 if (
index >= m_count)
219 throw "List.get: index out of bounds";
221 if (
index >= m_count)
222 return m_data[m_count-1];
223 return m_data[
index];
226 template <
typename T>
229 if (m_count == m_max)
230 resize(1 + m_max + (m_max >> 1));
231 for (
unsigned i=m_count;i>
index;--i)
232 m_data[i] = m_data[i-1];
233 if (
index <= m_count)
234 m_data[
index] = item;
236 m_data[m_count] = item;
240 template <
typename T>
241 template <
typename TB>
244 if (m_count == m_max)
245 resize(1 + m_max + (m_max >> 1));
246 for (
unsigned i=m_count;i>
index;--i)
247 m_data[i] = m_data[i-1];
248 if (
index <= m_count)
249 m_data[
index] =
new TB(item);
251 m_data[m_count] =
new TB(item);
255 template <
typename T>
258 #ifdef _XSENS_LIST_RANGE_CHECKS
259 if (
index >= m_count)
260 throw "List[]: index out of bounds";
262 return m_data[
index];
265 #if defined(_XSENS_LIST_WITH_MATH) && defined(_XSENS_LIST_IO)
267 template <
typename T>
270 os <<
'[' <<
t.length() <<
"]{ ";
271 for (
unsigned i=0 ; i<
t.length() ; ++i)
277 #ifndef _CMTMATLABHEADERS
278 #define _CMTMATLABHEADERS
279 struct MatlabFileHeader {
280 char description[116];
287 struct MatlabDataHeader {
292 struct MatlabMatrixHeader {
299 int32_t dimensions_m, dimensions_n;
307 template <
typename T>
308 void List<T>::saveAsMatlab(
const char* filename,
const char *varname)
const
313 MatlabFileHeader file_header;
314 MatlabMatrixHeader matrix_header;
316 MatlabDataHeader inner_header;
317 MatlabDataHeader outer_header;
319 FILE* fp =
fopen(filename,
"wb");
323 strcpy(file_header.description,
"cmtMath/Xsens");
324 for (i = strlen(file_header.description); i <116; ++i)
325 file_header.description[i] =
' ';
326 file_header.data_offset1 = 0;
327 file_header.data_offset2 = 0;
328 file_header.version = 0x0100;
329 file_header.endian = (
int16_t)
'M' << 8 |
'I';
333 matrix_header.flags_data_type = 6;
334 matrix_header.flags_data_size = 8;
335 matrix_header.flags0 = 6;
336 matrix_header.flags1 = 0;
337 matrix_header.dimensions_data_type = 5;
338 matrix_header.dimensions_data_size = 8;
339 matrix_header.dimensions_m = m_count;
340 matrix_header.dimensions_n = m_data[0]->size();
341 matrix_header.name_type = 1;
343 if ( varname == (
char *)NULL )
344 matrix_header.name_length = 0;
346 matrix_header.name_length = (
int32_t) strlen(varname);
348 name_pad = matrix_header.name_length & 7;
350 inner_header.data_type = 9;
351 inner_header.n_bytes =
sizeof(double) * m_count * matrix_header.dimensions_n;
353 outer_header.data_type = 14;
354 outer_header.n_bytes =
sizeof(matrix_header) + matrix_header.name_length + name_pad
355 +
sizeof(MatlabDataHeader) + inner_header.n_bytes;
357 fwrite((
char*) &file_header,
sizeof(MatlabFileHeader),1,fp);
358 fwrite((
char*) &outer_header,
sizeof(MatlabDataHeader),1,fp);
359 fwrite((
char*) &matrix_header,
sizeof(MatlabMatrixHeader),1,fp);
360 fwrite(varname,
sizeof(
char),matrix_header.name_length,fp);
363 fwrite(
"\0\0\0\0\0\0\0",
sizeof(
char),8-name_pad,fp);
365 fwrite((
char*) &inner_header,
sizeof(MatlabDataHeader),1,fp);
370 for ( j = 0; j < (size_t) matrix_header.dimensions_n; j++ )
371 for ( i = 0; i < m_count; i++ )
373 tmp = (*m_data[i])[(
unsigned)j];
374 fwrite(&tmp,
sizeof(
double),1,fp);
379 #endif // _XSENS_LIST_WITH_MATH && _XSENS_LIST_IO
381 template <
typename T>
389 pivot = m_data[left];
392 while (!(m_data[right] < pivot) && (left < right))
396 m_data[left] = m_data[right];
399 while (!(pivot < m_data[left]) && (left < right))
401 if (!(left == right))
403 m_data[right] = m_data[left];
407 m_data[left] = pivot;
409 qSort(l_hold, left-1);
411 qSort(left+1, r_hold);
414 template <
typename T>
422 pivot = m_data[left];
425 while (!(*m_data[right] < *pivot) && (left < right))
429 m_data[left] = m_data[right];
432 while (!(*pivot < *m_data[left]) && (left < right))
434 if (!(left == right))
436 m_data[right] = m_data[left];
440 m_data[left] = pivot;
442 qSortDeref(l_hold, left-1);
444 qSortDeref(left+1, r_hold);
449 #define XSENS_LIST_JOBSORT
452 template <
typename T>
457 #if defined(XSENS_LIST_QSORT)
459 #elif defined(XSENS_LIST_COMBSORT)
466 while (gap > 1 || swaps > 0)
470 dgap = floor(((
double) gap) / 1.247330950103979);
472 if (gap == 10 || gap == 9)
478 for (
uint32_t i = 0; i < gappedCount; ++i)
480 if (m_data[i] > m_data[i+gap])
483 m_data[i] = m_data[i+gap];
484 m_data[i+gap] = temp;
489 #elif defined(XSENS_LIST_JOBSORT)
497 Linker* list = (Linker*) malloc(m_count*
sizeof(Linker));
502 list[0].item = m_data[0];
506 for (
uint32_t i = 1; i < m_count; ++i)
509 list[i].item = m_data[i];
510 if (m_data[i] < m_data[curr->index])
512 while (curr->prev != NULL)
515 if (!(m_data[i] < m_data[curr->index]))
518 list[i].next = curr->next;
520 curr->next->prev = &list[i];
521 curr->next = &list[i];
526 if (curr != &list[i])
530 curr->prev = &list[i];
536 while (curr->next != NULL)
539 if (m_data[i] < m_data[curr->index])
543 list[i].prev = curr->prev;
544 curr->prev->next = &list[i];
545 curr->prev = &list[i];
550 if (curr != &list[i])
554 curr->next = &list[i];
561 while (curr->prev != NULL) curr = curr->prev;
564 for (
uint32_t i = 0; i < m_count; ++i)
566 m_data[i] = curr->item;
575 for (
uint32_t i = 1; i < m_count; ++i)
580 if (m_data[j] < m_data[j-1])
583 m_data[j] = m_data[j-1];
594 template <
typename T>
599 #if defined(XSENS_LIST_QSORT)
600 qSortDeref(0,m_count-1);
601 #elif defined(XSENS_LIST_COMBSORT)
608 while (gap > 1 || swaps > 0)
612 dgap = floor(((
double) gap) / 1.247330950103979);
614 if (gap == 10 || gap == 9)
620 for (
uint32_t i = 0; i < gappedCount; ++i)
622 if (*m_data[i+gap] < *m_data[i])
625 m_data[i] = m_data[i+gap];
626 m_data[i+gap] = temp;
631 #elif defined(XSENS_LIST_JOBSORT)
639 Linker* list = (Linker*) malloc(m_count*
sizeof(Linker));
644 list[0].item = m_data[0];
648 for (
uint32_t i = 1; i < m_count; ++i)
651 list[i].item = m_data[i];
652 if (*m_data[i] < *m_data[curr->index])
654 while (curr->prev != NULL)
657 if (!(*m_data[i] < *m_data[curr->index]))
660 list[i].next = curr->next;
662 curr->next->prev = &list[i];
663 curr->next = &list[i];
668 if (curr != &list[i])
672 curr->prev = &list[i];
678 while (curr->next != NULL)
681 if (*m_data[i] < *m_data[curr->index])
685 list[i].prev = curr->prev;
686 curr->prev->next = &list[i];
687 curr->prev = &list[i];
692 if (curr != &list[i])
696 curr->next = &list[i];
703 while (curr->prev != NULL) curr = curr->prev;
706 for (
uint32_t i = 0; i < m_count; ++i)
708 m_data[i] = curr->item;
717 for (
uint32_t i = 1; i < m_count; ++i)
722 if (*(m_data[j]) < *(m_data[j-1]))
725 m_data[j] = m_data[j-1];
736 template <
typename T>
737 template <
typename T2>
743 #ifdef _XSENS_LIST_RANGE_CHECKS
745 throw "List.twinSortAscending: sizes do not match";
752 while (iteration < m_count-1)
755 for (
uint32_t i=iteration+1;i<m_count;++i)
757 if (m_data[i] < m_data[mini])
760 if (mini != iteration)
763 m_data[mini] = m_data[iteration];
764 m_data[iteration] = tmp;
768 twin.
m_data[iteration] = tmp2;
774 template <
typename T>
777 #ifdef _XSENS_LIST_RANGE_CHECKS
778 if (
index >= m_count)
779 throw "List.remove: index out of bounds";
781 if (
index == m_count-1)
787 for (
unsigned i =
index;i < m_count;++i)
788 m_data[i] = m_data[i+1];
791 template <
typename T>
794 #ifdef _XSENS_LIST_RANGE_CHECKS
796 throw "List.removeTail: list size less than remove count";
806 template <
typename T>
809 #ifdef _XSENS_LIST_RANGE_CHECKS
811 throw "List.deleteAndRemoveTail: list size less than remove count";
815 for (
unsigned i = 0;i <
count;++i)
816 delete m_data[--m_count];
822 template <
typename T>
825 #ifdef _XSENS_LIST_RANGE_CHECKS
827 throw "List.freeAndRemoveTail: list size less than remove count";
831 for (
unsigned i = 0;i <
count;++i)
832 free(m_data[--m_count]);
838 template <
typename T>
844 for (
uint32_t j=i+1;j < m_count; ++j)
846 if (m_data[i] == m_data[j])
857 template <
typename T>
863 for (
uint32_t j=i+1;j < m_count; ++j)
865 if (*(m_data[i]) == *(m_data[j]))
876 template <
typename T>
879 #ifdef _XSENS_LIST_RANGE_CHECKS
880 if (
index >= m_count)
881 throw "List.deleteAndRemove: index out of bounds";
883 delete m_data[
index];
884 if (
index == m_count-1)
890 for (
unsigned i =
index;i < m_count;++i)
891 m_data[i] = m_data[i+1];
894 template <
typename T>
897 #ifdef _XSENS_LIST_RANGE_CHECKS
898 if (
index >= m_count)
899 throw "List.freeAndRemove: index out of bounds";
902 if (
index == m_count-1)
908 for (
unsigned i =
index;i < m_count;++i)
909 m_data[i] = m_data[i+1];
912 template <
typename T>
913 template <
typename TB>
917 if (((
const T*)m_data)[i] == item)
922 template <
typename T>
926 if (!fnc(m_data[i],item))
931 template <
typename T>
932 template <
typename TB>
936 if (*(m_data[i]) == item)
941 template <
typename T>
942 template <
typename TB>
956 if (m_data[i-1] == item)
958 if (m_data[i-1] < item)
966 template <
typename T>
967 template <
typename TB>
971 return findDeref(item);
981 if (*(m_data[i-1]) == item)
983 if (*(m_data[i-1]) < item)
991 template <
typename T>
997 for (i=0;i<m_count;++i)
998 if (item < m_data[i])
1015 if (m_data[i-1] == item)
1020 if (m_data[i-1] < item)
1030 template <
typename T>
1036 for (i=0;i<m_count;++i)
1037 if (*item < *m_data[i])
1054 if (*(m_data[i-1]) == *item)
1059 if (*(m_data[i-1]) < *item)
1069 template <
typename T>
1070 template <
typename TB>
1076 for (i=0;i<m_count;++i)
1077 if (item < m_data[i])
1079 insertCopy<TB>(item,i);
1094 if (m_data[i-1] == item)
1096 insertCopy<TB>(item,i-1);
1099 if (m_data[i-1] < item)
1104 insertCopy<TB>(item,
n-1);
1109 template <
typename T>
1120 template <
typename T>
1131 template <
typename T>
1132 template <
typename TB>
1136 if (m_max <
source.m_count)
1138 m_count =
source.m_count;
1140 m_data[i] =
new TB(*
source.m_data[i]);
1143 template <
typename T>
1147 if (m_max <
x.m_count)
1149 m_count =
x.m_count;
1151 m_data[i] =
x.m_data[i];
1154 template <
typename T>
1157 #ifdef _XSENS_LIST_RANGE_CHECKS
1158 if (i >= m_count || j >= m_count)
1159 throw "List.swap: index out of bounds";
1162 m_data[i] = m_data[j];
1166 template <
typename T>
1173 m_data[i] = m_data[
end];
1180 #endif // _XSENS_LIST_HPP_2006_06_08