Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
vpList.h
1/*
2 * ViSP, open source Visual Servoing Platform software.
3 * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4 *
5 * This software is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * See the file LICENSE.txt at the root directory of this source
10 * distribution for additional information about the GNU GPL.
11 *
12 * For using ViSP with software that can not be combined with the GNU
13 * GPL, please contact Inria about acquiring a ViSP Professional
14 * Edition License.
15 *
16 * See https://visp.inria.fr for more information.
17 *
18 * This software was developed at:
19 * Inria Rennes - Bretagne Atlantique
20 * Campus Universitaire de Beaulieu
21 * 35042 Rennes Cedex
22 * France
23 *
24 * If you have questions regarding the use of this file, please contact
25 * Inria at visp@inria.fr
26 *
27 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Description:
31 * List data structure.
32 */
33
34#ifndef VP_LIST_H
35#define VP_LIST_H
36
42#include <visp3/core/vpConfig.h>
43#include <visp3/core/vpDebug.h>
44#include <visp3/core/vpException.h>
45
46#include <stdio.h>
47
48#ifndef DOXYGEN_SHOULD_SKIP_THIS
49
54template <class type> class vpListElement
55{
56 // private:
57 // vpListElement(const vpListElement &)
58 // : prev(NULL), next(NULL), val()
59 // {
60 // throw vpException(vpException::functionNotImplementedError,"Not
61 // implemented!");
62 // }
63 // vpListElement &operator=(const vpListElement &){
64 // throw vpException(vpException::functionNotImplementedError,"Not
65 // implemented!"); return *this;
66 // }
67
68public:
69 vpListElement() : prev(NULL), next(NULL), val(){};
70 vpListElement<type> *prev;
71 vpListElement<type> *next;
72 type val;
73};
74
75#endif /* DOXYGEN_SHOULD_SKIP_THIS */
76
107template <class type> class vpList
108{
109private:
110 void init();
111
112public:
113 unsigned int nb;
121 vpListElement<type> *first;
129 vpListElement<type> *last;
137 vpListElement<type> *cur; // the current element
138public:
139 vpList(); // constr.
140 vpList(const vpList &l); // cloning
141 virtual ~vpList(); // destr.
142
143 inline void next(void); // current element's successor ( cur = cur->next )
144 inline void previous(void); // current element's predecessor ( cur = cur->pred )
145 inline void front(void); // go to the front of the List (cur = first)
146 inline void end(void); // go back to the end of the List ( cur = last )
147 inline bool outside(void) const; // test whether we are outside the List
148
149 bool empty(void) const; // tests whether the List is empty
150
151 inline type &value(void); // returns the current element value
152 inline const type &value(void) const; // returns the current element value
153
154 void suppress(void); // deletes the current item
155 void kill(); // empties the List
156
157 void display(); // displays the content of the list
158 void print() { display(); } // displays the content of the list
159
160 inline void addRight(const type &el); // inserts an element on the right
161 inline void addLeft(const type &el); // inserts an element on the left
162 inline void modify(const type &el); // modifies thevalue field of the curr. el.
163 inline void addRight(type &el); // inserts an element on the right
164 inline void addLeft(type &el); // inserts an element on the left
165 inline void swapLeft(); // Switch the current element with the element on the left
166 inline void swapRight(); // Switch the current element with the element on the right
167 inline unsigned int nbElement(void); // returns the number of items currently in the list
168 inline unsigned int nbElements(void); // returns the number of items currently in the list
169
171 inline void operator+=(vpList<type> &l);
172 inline void operator+=(const type &l);
173
174 // Other non fundamental member (may be somehow useful)
175 bool nextOutside(void) const; // test whether we are outside the List
176 bool previousOutside(void) const; // test whether we are outside the List
177
178 type &previousValue(void); // returns the previous element value
179 type &nextValue(void); // returns the next element value
180 type &firstValue(void);
181 type &lastValue(void);
182};
183
189template <class type> void vpList<type>::init()
190{
191 vpListElement<type> *x = new vpListElement<type>;
192 vpListElement<type> *y = new vpListElement<type>;
193
194 first = x;
195 last = y;
196
197 x->prev = NULL;
198 x->next = y;
199 y->prev = x;
200 y->next = NULL;
201
202 cur = x;
203 nb = 0;
204}
205
213template <class type> vpList<type>::vpList() : nb(0), first(NULL), last(NULL), cur(NULL) { init(); }
214
219template <class type> vpList<type>::~vpList()
220{
221 kill();
222
223 /*if (first != NULL) */ delete first;
224 /*if (last != NULL) */ delete last;
225}
226
230template <class type> unsigned int vpList<type>::nbElement(void) { return (nb); }
231
235template <class type> unsigned int vpList<type>::nbElements(void) { return (nb); }
236
244template <class type> void vpList<type>::next(void) { cur = cur->next; }
245
253template <class type> void vpList<type>::previous(void) { cur = cur->prev; }
254
263template <class type> type &vpList<type>::value(void) { return (cur->val); }
264
273template <class type> const type &vpList<type>::value(void) const { return (cur->val); }
274
283template <class type> type &vpList<type>::previousValue(void) { return (cur->prev->val); }
284
292template <class type> type &vpList<type>::nextValue(void) { return (cur->next->val); }
293
300template <class type> type &vpList<type>::firstValue(void) { return (first->next->val); }
301
307template <class type> type &vpList<type>::lastValue(void) { return (last->prev->val); }
308
317template <class type> void vpList<type>::front(void) { cur = first->next; }
318
327template <class type> void vpList<type>::end(void) { cur = last->prev; }
328
337template <class type> bool vpList<type>::empty(void) const { return ((first->next == last) && (first == last->prev)); }
338
350template <class type> bool vpList<type>::outside(void) const { return ((cur == first) || (cur == last)); }
351
361template <class type> bool vpList<type>::nextOutside(void) const
362{
363 return ((cur->next == first) || (cur->next == last));
364}
365
375template <class type> bool vpList<type>::previousOutside(void) const
376{
377 return ((cur->prev == first) || (cur->prev == last));
378}
379
390template <class type> void vpList<type>::addRight(const type &v)
391{
392 vpListElement<type> *x = new vpListElement<type>;
393
394 x->val = v;
395 if (empty()) {
396 cur = first;
397 } else {
398 if (outside())
399 std::cout << "vpList: outside with addRight " << std::endl;
400 }
401 cur->next->prev = x;
402 x->next = cur->next;
403 x->prev = cur;
404 cur->next = x;
405 cur = x;
406 nb++;
407}
408
419template <class type> void vpList<type>::addLeft(const type &v)
420{
421 vpListElement<type> *x = new vpListElement<type>;
422
423 x->val = v;
424
425 if (empty()) {
426 cur = last;
427 } else {
428 if (outside())
429 std::cout << "vpList: outside with addLeft " << std::endl;
430 }
431 x->next = cur;
432 x->prev = cur->prev;
433 cur->prev->next = x;
434 cur->prev = x;
435 cur = x;
436 nb++;
437}
438
449template <class type> void vpList<type>::addRight(type &v)
450{
451 vpListElement<type> *x = new vpListElement<type>;
452
453 x->val = v;
454 if (empty()) {
455 cur = first;
456 } else {
457 if (outside())
458 std::cout << "vpList: outside with addRight " << std::endl;
459 }
460 cur->next->prev = x;
461 x->next = cur->next;
462 x->prev = cur;
463 cur->next = x;
464 cur = x;
465 nb++;
466}
467
478template <class type> void vpList<type>::addLeft(type &v)
479{
480 vpListElement<type> *x = new vpListElement<type>;
481
482 x->val = v;
483
484 if (empty()) {
485 cur = last;
486 } else {
487 if (outside())
488 std::cout << "vpList: outside with addLeft " << std::endl;
489 }
490 x->next = cur;
491 x->prev = cur->prev;
492 cur->prev->next = x;
493 cur->prev = x;
494 cur = x;
495 nb++;
496}
497
506template <class type> void vpList<type>::modify(const type &v) { cur->val = v; }
507
516template <class type> void vpList<type>::swapLeft()
517{
518 if (cur->prev != first) {
519 cur->prev->prev->next = cur;
520 cur->next->prev = cur->prev;
521
522 vpListElement<type> *nextTmp;
523 vpListElement<type> *prevTmp;
524
525 nextTmp = cur->next;
526 prevTmp = cur->prev;
527
528 cur->next = cur->prev;
529 cur->prev = cur->prev->prev;
530
531 prevTmp->prev = cur;
532 prevTmp->next = nextTmp;
533 } else {
534 std::cout << "vpList: previous element is outside (swapLeft) " << std::endl;
535 }
536}
537
546template <class type> void vpList<type>::swapRight()
547{
548 if (cur->next != last) {
549 cur->prev->next = cur->next;
550 cur->next->next->prev = cur;
551
552 vpListElement<type> *nextTmp;
553 vpListElement<type> *prevTmp;
554
555 nextTmp = cur->next;
556 prevTmp = cur->prev;
557
558 cur->next = nextTmp->next;
559 cur->prev = nextTmp;
560
561 nextTmp->prev = prevTmp;
562 nextTmp->next = cur;
563 } else {
564 std::cout << "vpList: next element is outside (swapRight) " << std::endl;
565 }
566}
567
576template <class type> void vpList<type>::kill()
577{
578
579 front();
580 while (!empty()) {
581 suppress();
582 }
583}
584
595template <class type> void vpList<type>::suppress(void)
596{
597 vpListElement<type> *x;
598
599 cur->prev->next = cur->next;
600 cur->next->prev = cur->prev;
601 x = cur;
602 cur = cur->next;
603
604 if (x != NULL)
605 delete x;
606
607 nb--;
608}
609
616template <class type> vpList<type> &vpList<type>::operator=(const vpList<type> &l)
617{
618 type x;
619 vpListElement<type> *e;
620
621 kill();
622 e = l.first->next;
623 front();
624 while (e != l.last) {
625 x = e->val;
626 addRight(x);
627 e = e->next;
628 }
629
630 nb = l.nb;
631 cur = first->next;
632
633 return *this;
634}
635
644template <class type> void vpList<type>::operator+=(vpList<type> &l)
645{
646 type x;
647
648 l.front();
649 end();
650 while (!l.outside()) {
651 x = l.value();
652 addRight(x);
653 l.next();
654 }
655}
656
665template <class type> void vpList<type>::operator+=(const type &l)
666{
667 end();
668 addRight(l);
669}
670
676template <class type> vpList<type>::vpList(const vpList<type> &l) : nb(0), first(NULL), last(NULL), cur(NULL)
677{
678 init();
679 *this = l;
680}
681
685template <class type> void vpList<type>::display()
686{
687 unsigned int k = 1;
688 front();
689 while (!outside()) {
690 std::cout << k << " ---> " << value() << std::endl;
691 next();
692 k++;
693 }
694 std::cout << std::endl << std::endl;
695}
696
697#endif /* #ifndef VP_LIST_H */
698
699/*
700 * Local variables:
701 * c-basic-offset: 2
702 * End:
703 */
Provide simple list management.
Definition vpList.h:108
void next(void)
position the current element on the next one
Definition vpList.h:244
void addLeft(const type &el)
add a new element in the list, at the left of the current one
Definition vpList.h:419
void addRight(const type &el)
add a new element in the list, at the right of the current one
Definition vpList.h:390
void kill()
Destroy the list.
Definition vpList.h:576
void swapRight()
Switch the current element with the element on the right.
Definition vpList.h:546
bool empty(void) const
Test if the list is empty.
Definition vpList.h:337
void modify(const type &el)
Modify the value of the current element.
Definition vpList.h:506
vpList()
Basic constructor, initialization, Create an empty list.
Definition vpList.h:213
void end(void)
Position the current element on the last element of the list.
Definition vpList.h:327
void front(void)
Position the current element on the first element of the list.
Definition vpList.h:317
void display()
Print (std::cout) all the element of the list.
Definition vpList.h:685
bool previousOutside(void) const
Test if the previous element is outside the list (ie if the current element is the firts one)
Definition vpList.h:375
type & previousValue(void)
return the value of the previous element
Definition vpList.h:283
void operator+=(vpList< type > &l)
Append two lists.
Definition vpList.h:644
unsigned int nb
Definition vpList.h:113
unsigned int nbElements(void)
return the number of element in the list
Definition vpList.h:235
vpListElement< type > * cur
the current item in the list
Definition vpList.h:137
void print()
Definition vpList.h:158
bool nextOutside(void) const
Test if the next element is outside the list (ie if the current element is the last one)
Definition vpList.h:361
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition vpList.h:350
void previous(void)
position the current element on the previous one
Definition vpList.h:253
virtual ~vpList()
vpList destructor
Definition vpList.h:219
type & value(void)
return the value of the current element
Definition vpList.h:263
type & nextValue(void)
return the value of the next element
Definition vpList.h:292
vpList< type > & operator=(const vpList< type > &l)
Copy constructor const.
Definition vpList.h:616
type & lastValue(void)
return the last element of the list
Definition vpList.h:307
void swapLeft()
Switch the current element with the element on the left.
Definition vpList.h:516
void suppress(void)
suppress the current element
Definition vpList.h:595
vpListElement< type > * first
! number of items in the List
Definition vpList.h:121
vpListElement< type > * last
the last virtualitem in the list
Definition vpList.h:129
unsigned int nbElement(void)
return the number of element in the list
Definition vpList.h:230
type & firstValue(void)
return the first element of the list
Definition vpList.h:300