Visual Servoing Platform  version 3.6.1 under development (2024-02-13)
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 
54 template <class type> class vpListElement
55 {
56  // private:
57  // vpListElement(const vpListElement &)
58  // : prev(nullptr), next(nullptr), 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 
68 public:
69  vpListElement() : prev(nullptr), next(nullptr), val() { };
70  vpListElement<type> *prev;
71  vpListElement<type> *next;
72  type val;
73 };
74 
75 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
76 
107 template <class type> class vpList
108 {
109 private:
110  void init();
111 
112 public:
113  unsigned int nb;
121  vpListElement<type> *first;
129  vpListElement<type> *last;
137  vpListElement<type> *cur; // the current element
138 public:
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 
189 template <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 = nullptr;
198  x->next = y;
199  y->prev = x;
200  y->next = nullptr;
201 
202  cur = x;
203  nb = 0;
204 }
205 
213 template <class type> vpList<type>::vpList() : nb(0), first(nullptr), last(nullptr), cur(nullptr) { init(); }
218 template <class type> vpList<type>::~vpList()
219 {
220  kill();
221 
222  /*if (first != nullptr) */ delete first;
223  /*if (last != nullptr) */ delete last;
224 }
225 
229 template <class type> unsigned int vpList<type>::nbElement(void) { return (nb); }
230 
234 template <class type> unsigned int vpList<type>::nbElements(void) { return (nb); }
235 
243 template <class type> void vpList<type>::next(void) { cur = cur->next; }
244 
252 template <class type> void vpList<type>::previous(void) { cur = cur->prev; }
253 
262 template <class type> type &vpList<type>::value(void) { return (cur->val); }
263 
272 template <class type> const type &vpList<type>::value(void) const { return (cur->val); }
273 
282 template <class type> type &vpList<type>::previousValue(void) { return (cur->prev->val); }
283 
291 template <class type> type &vpList<type>::nextValue(void) { return (cur->next->val); }
292 
299 template <class type> type &vpList<type>::firstValue(void) { return (first->next->val); }
300 
306 template <class type> type &vpList<type>::lastValue(void) { return (last->prev->val); }
307 
316 template <class type> void vpList<type>::front(void) { cur = first->next; }
317 
326 template <class type> void vpList<type>::end(void) { cur = last->prev; }
327 
336 template <class type> bool vpList<type>::empty(void) const { return ((first->next == last) && (first == last->prev)); }
337 
349 template <class type> bool vpList<type>::outside(void) const { return ((cur == first) || (cur == last)); }
350 
360 template <class type> bool vpList<type>::nextOutside(void) const
361 {
362  return ((cur->next == first) || (cur->next == last));
363 }
364 
374 template <class type> bool vpList<type>::previousOutside(void) const
375 {
376  return ((cur->prev == first) || (cur->prev == last));
377 }
378 
389 template <class type> void vpList<type>::addRight(const type &v)
390 {
391  vpListElement<type> *x = new vpListElement<type>;
392 
393  x->val = v;
394  if (empty()) {
395  cur = first;
396  }
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 
419 template <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  }
428  else {
429  if (outside())
430  std::cout << "vpList: outside with addLeft " << std::endl;
431  }
432  x->next = cur;
433  x->prev = cur->prev;
434  cur->prev->next = x;
435  cur->prev = x;
436  cur = x;
437  nb++;
438 }
439 
450 template <class type> void vpList<type>::addRight(type &v)
451 {
452  vpListElement<type> *x = new vpListElement<type>;
453 
454  x->val = v;
455  if (empty()) {
456  cur = first;
457  }
458  else {
459  if (outside())
460  std::cout << "vpList: outside with addRight " << std::endl;
461  }
462  cur->next->prev = x;
463  x->next = cur->next;
464  x->prev = cur;
465  cur->next = x;
466  cur = x;
467  nb++;
468 }
469 
480 template <class type> void vpList<type>::addLeft(type &v)
481 {
482  vpListElement<type> *x = new vpListElement<type>;
483 
484  x->val = v;
485 
486  if (empty()) {
487  cur = last;
488  }
489  else {
490  if (outside())
491  std::cout << "vpList: outside with addLeft " << std::endl;
492  }
493  x->next = cur;
494  x->prev = cur->prev;
495  cur->prev->next = x;
496  cur->prev = x;
497  cur = x;
498  nb++;
499 }
500 
509 template <class type> void vpList<type>::modify(const type &v) { cur->val = v; }
510 
519 template <class type> void vpList<type>::swapLeft()
520 {
521  if (cur->prev != first) {
522  cur->prev->prev->next = cur;
523  cur->next->prev = cur->prev;
524 
525  vpListElement<type> *nextTmp;
526  vpListElement<type> *prevTmp;
527 
528  nextTmp = cur->next;
529  prevTmp = cur->prev;
530 
531  cur->next = cur->prev;
532  cur->prev = cur->prev->prev;
533 
534  prevTmp->prev = cur;
535  prevTmp->next = nextTmp;
536  }
537  else {
538  std::cout << "vpList: previous element is outside (swapLeft) " << std::endl;
539  }
540 }
541 
550 template <class type> void vpList<type>::swapRight()
551 {
552  if (cur->next != last) {
553  cur->prev->next = cur->next;
554  cur->next->next->prev = cur;
555 
556  vpListElement<type> *nextTmp;
557  vpListElement<type> *prevTmp;
558 
559  nextTmp = cur->next;
560  prevTmp = cur->prev;
561 
562  cur->next = nextTmp->next;
563  cur->prev = nextTmp;
564 
565  nextTmp->prev = prevTmp;
566  nextTmp->next = cur;
567  }
568  else {
569  std::cout << "vpList: next element is outside (swapRight) " << std::endl;
570  }
571 }
572 
581 template <class type> void vpList<type>::kill()
582 {
583 
584  front();
585  while (!empty()) {
586  suppress();
587  }
588 }
589 
600 template <class type> void vpList<type>::suppress(void)
601 {
602  vpListElement<type> *x;
603 
604  cur->prev->next = cur->next;
605  cur->next->prev = cur->prev;
606  x = cur;
607  cur = cur->next;
608 
609  if (x != nullptr) {
610  delete x;
611  }
612 
613  nb--;
614 }
615 
622 template <class type> vpList<type> &vpList<type>::operator=(const vpList<type> &l)
623 {
624  type x;
625  vpListElement<type> *e;
626 
627  kill();
628  e = l.first->next;
629  front();
630  while (e != l.last) {
631  x = e->val;
632  addRight(x);
633  e = e->next;
634  }
635 
636  nb = l.nb;
637  cur = first->next;
638 
639  return *this;
640 }
641 
650 template <class type> void vpList<type>::operator+=(vpList<type> &l)
651 {
652  type x;
653 
654  l.front();
655  end();
656  while (!l.outside()) {
657  x = l.value();
658  addRight(x);
659  l.next();
660  }
661 }
662 
671 template <class type> void vpList<type>::operator+=(const type &l)
672 {
673  end();
674  addRight(l);
675 }
676 
682 template <class type> vpList<type>::vpList(const vpList<type> &l) : nb(0), first(nullptr), last(nullptr), cur(nullptr)
683 {
684  init();
685  *this = l;
686 }
687 
691 template <class type> void vpList<type>::display()
692 {
693  unsigned int k = 1;
694  front();
695  while (!outside()) {
696  std::cout << k << " ---> " << value() << std::endl;
697  next();
698  k++;
699  }
700  std::cout << std::endl << std::endl;
701 }
702 
703 #endif /* #ifndef VP_LIST_H */
704 
705 /*
706  * Local variables:
707  * c-basic-offset: 2
708  * End:
709  */
Provide simple list management.
Definition: vpList.h:108
void next(void)
position the current element on the next one
Definition: vpList.h:243
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:389
void kill()
Destroy the list.
Definition: vpList.h:581
void swapRight()
Switch the current element with the element on the right.
Definition: vpList.h:550
bool empty(void) const
Test if the list is empty.
Definition: vpList.h:336
void modify(const type &el)
Modify the value of the current element.
Definition: vpList.h:509
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:326
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:316
void display()
Print (std::cout) all the element of the list.
Definition: vpList.h:691
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:374
type & previousValue(void)
return the value of the previous element
Definition: vpList.h:282
void operator+=(vpList< type > &l)
Append two lists.
Definition: vpList.h:650
unsigned int nb
Definition: vpList.h:113
unsigned int nbElements(void)
return the number of element in the list
Definition: vpList.h:234
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:360
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition: vpList.h:349
void previous(void)
position the current element on the previous one
Definition: vpList.h:252
virtual ~vpList()
vpList destructor
Definition: vpList.h:218
type & value(void)
return the value of the current element
Definition: vpList.h:262
type & nextValue(void)
return the value of the next element
Definition: vpList.h:291
vpList< type > & operator=(const vpList< type > &l)
Copy constructor const.
Definition: vpList.h:622
type & lastValue(void)
return the last element of the list
Definition: vpList.h:306
void swapLeft()
Switch the current element with the element on the left.
Definition: vpList.h:519
void suppress(void)
suppress the current element
Definition: vpList.h:600
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:229
type & firstValue(void)
return the first element of the list
Definition: vpList.h:299