Visual Servoing Platform  version 3.6.1 under development (2024-10-15)
vpList.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2024 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 
39 #ifndef VP_LIST_H
40 #define VP_LIST_H
41 
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 BEGIN_VISP_NAMESPACE
49 #ifndef DOXYGEN_SHOULD_SKIP_THIS
54 template <class type> class vpListElement
55 {
56  /*
57  // private:
58  // vpListElement(const vpListElement &)
59  // : prev(nullptr), next(nullptr), val()
60  // {
61  // throw vpException(vpException::functionNotImplementedError,"Not
62  // implemented!");
63  // }
64  // vpListElement &operator=(const vpListElement &){
65  // throw vpException(vpException::functionNotImplementedError,"Not
66  // implemented!"); return *this;
67  // }
68  */
69 public:
70  vpListElement() : prev(nullptr), next(nullptr), val() { };
71  vpListElement<type> *prev;
72  vpListElement<type> *next;
73  type val;
74 };
75 
76 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
77 
108 template <class type> class vpList
109 {
110 private:
111  void init();
112 
113 public:
114  unsigned int nb;
122  vpListElement<type> *first;
130  vpListElement<type> *last;
138  vpListElement<type> *cur; // the current element
139 public:
140  vpList(); // constr.
141  vpList(const vpList &l); // cloning
142  virtual ~vpList(); // destr.
143 
144  inline void next(void); // current element's successor ( cur = cur->next )
145  inline void previous(void); // current element's predecessor ( cur = cur->pred )
146  inline void front(void); // go to the front of the List (cur = first)
147  inline void end(void); // go back to the end of the List ( cur = last )
148  inline bool outside(void) const; // test whether we are outside the List
149 
150  bool empty(void) const; // tests whether the List is empty
151 
152  inline type &value(void); // returns the current element value
153  inline const type &value(void) const; // returns the current element value
154 
155  void suppress(void); // deletes the current item
156  void kill(); // empties the List
157 
158  void display(); // displays the content of the list
159  void print() { display(); } // displays the content of the list
160 
161  inline void addRight(const type &el); // inserts an element on the right
162  inline void addLeft(const type &el); // inserts an element on the left
163  inline void modify(const type &el); // modifies thevalue field of the curr. el.
164  inline void addRight(type &el); // inserts an element on the right
165  inline void addLeft(type &el); // inserts an element on the left
166  inline void swapLeft(); // Switch the current element with the element on the left
167  inline void swapRight(); // Switch the current element with the element on the right
168  inline unsigned int nbElement(void); // returns the number of items currently in the list
169  inline unsigned int nbElements(void); // returns the number of items currently in the list
170 
172  inline void operator+=(vpList<type> &l);
173  inline void operator+=(const type &l);
174 
175  // Other non fundamental member (may be somehow useful)
176  bool nextOutside(void) const; // test whether we are outside the List
177  bool previousOutside(void) const; // test whether we are outside the List
178 
179  type &previousValue(void); // returns the previous element value
180  type &nextValue(void); // returns the next element value
181  type &firstValue(void);
182  type &lastValue(void);
183 };
184 
190 template <class type> void vpList<type>::init()
191 {
192  vpListElement<type> *x = new vpListElement<type>;
193  vpListElement<type> *y = new vpListElement<type>;
194 
195  first = x;
196  last = y;
197 
198  x->prev = nullptr;
199  x->next = y;
200  y->prev = x;
201  y->next = nullptr;
202 
203  cur = x;
204  nb = 0;
205 }
206 
214 template <class type> vpList<type>::vpList() : nb(0), first(nullptr), last(nullptr), cur(nullptr) { init(); }
219 template <class type> vpList<type>::~vpList()
220 {
221  kill();
222 
223  /*if (first != nullptr) */ delete first;
224  /*if (last != nullptr) */ delete last;
225 }
226 
230 template <class type> unsigned int vpList<type>::nbElement(void) { return nb; }
231 
235 template <class type> unsigned int vpList<type>::nbElements(void) { return nb; }
236 
244 template <class type> void vpList<type>::next(void) { cur = cur->next; }
245 
253 template <class type> void vpList<type>::previous(void) { cur = cur->prev; }
254 
263 template <class type> type &vpList<type>::value(void) { return (cur->val); }
264 
273 template <class type> const type &vpList<type>::value(void) const { return (cur->val); }
274 
283 template <class type> type &vpList<type>::previousValue(void) { return (cur->prev->val); }
284 
292 template <class type> type &vpList<type>::nextValue(void) { return (cur->next->val); }
293 
300 template <class type> type &vpList<type>::firstValue(void) { return (first->next->val); }
301 
307 template <class type> type &vpList<type>::lastValue(void) { return (last->prev->val); }
308 
317 template <class type> void vpList<type>::front(void) { cur = first->next; }
318 
327 template <class type> void vpList<type>::end(void) { cur = last->prev; }
328 
337 template <class type> bool vpList<type>::empty(void) const { return ((first->next == last) && (first == last->prev)); }
338 
350 template <class type> bool vpList<type>::outside(void) const { return ((cur == first) || (cur == last)); }
351 
361 template <class type> bool vpList<type>::nextOutside(void) const
362 {
363  return ((cur->next == first) || (cur->next == last));
364 }
365 
375 template <class type> bool vpList<type>::previousOutside(void) const
376 {
377  return ((cur->prev == first) || (cur->prev == last));
378 }
379 
390 template <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  }
398  else {
399  if (outside()) {
400  std::cout << "vpList: outside with addRight " << std::endl;
401  }
402  }
403  cur->next->prev = x;
404  x->next = cur->next;
405  x->prev = cur;
406  cur->next = x;
407  cur = x;
408  nb++;
409 }
410 
421 template <class type> void vpList<type>::addLeft(const type &v)
422 {
423  vpListElement<type> *x = new vpListElement<type>;
424 
425  x->val = v;
426 
427  if (empty()) {
428  cur = last;
429  }
430  else {
431  if (outside()) {
432  std::cout << "vpList: outside with addLeft " << std::endl;
433  }
434  }
435  x->next = cur;
436  x->prev = cur->prev;
437  cur->prev->next = x;
438  cur->prev = x;
439  cur = x;
440  ++nb;
441 }
442 
453 template <class type> void vpList<type>::addRight(type &v)
454 {
455  vpListElement<type> *x = new vpListElement<type>;
456 
457  x->val = v;
458  if (empty()) {
459  cur = first;
460  }
461  else {
462  if (outside()) {
463  std::cout << "vpList: outside with addRight " << std::endl;
464  }
465  }
466  cur->next->prev = x;
467  x->next = cur->next;
468  x->prev = cur;
469  cur->next = x;
470  cur = x;
471  ++nb;
472 }
473 
484 template <class type> void vpList<type>::addLeft(type &v)
485 {
486  vpListElement<type> *x = new vpListElement<type>;
487 
488  x->val = v;
489 
490  if (empty()) {
491  cur = last;
492  }
493  else {
494  if (outside()) {
495  std::cout << "vpList: outside with addLeft " << std::endl;
496  }
497  }
498  x->next = cur;
499  x->prev = cur->prev;
500  cur->prev->next = x;
501  cur->prev = x;
502  cur = x;
503  ++nb;
504 }
505 
514 template <class type> void vpList<type>::modify(const type &v) { cur->val = v; }
515 
524 template <class type> void vpList<type>::swapLeft()
525 {
526  if (cur->prev != first) {
527  cur->prev->prev->next = cur;
528  cur->next->prev = cur->prev;
529 
530  vpListElement<type> *nextTmp;
531  vpListElement<type> *prevTmp;
532 
533  nextTmp = cur->next;
534  prevTmp = cur->prev;
535 
536  cur->next = cur->prev;
537  cur->prev = cur->prev->prev;
538 
539  prevTmp->prev = cur;
540  prevTmp->next = nextTmp;
541  }
542  else {
543  std::cout << "vpList: previous element is outside (swapLeft) " << std::endl;
544  }
545 }
546 
555 template <class type> void vpList<type>::swapRight()
556 {
557  if (cur->next != last) {
558  cur->prev->next = cur->next;
559  cur->next->next->prev = cur;
560 
561  vpListElement<type> *nextTmp;
562  vpListElement<type> *prevTmp;
563 
564  nextTmp = cur->next;
565  prevTmp = cur->prev;
566 
567  cur->next = nextTmp->next;
568  cur->prev = nextTmp;
569 
570  nextTmp->prev = prevTmp;
571  nextTmp->next = cur;
572  }
573  else {
574  std::cout << "vpList: next element is outside (swapRight) " << std::endl;
575  }
576 }
577 
586 template <class type> void vpList<type>::kill()
587 {
588 
589  front();
590  while (!empty()) {
591  suppress();
592  }
593 }
594 
605 template <class type> void vpList<type>::suppress(void)
606 {
607  vpListElement<type> *x;
608 
609  cur->prev->next = cur->next;
610  cur->next->prev = cur->prev;
611  x = cur;
612  cur = cur->next;
613 
614  if (x != nullptr) {
615  delete x;
616  }
617 
618  --nb;
619 }
620 
627 template <class type> vpList<type> &vpList<type>::operator=(const vpList<type> &l)
628 {
629  type x;
630  vpListElement<type> *e;
631 
632  kill();
633  e = l.first->next;
634  front();
635  while (e != l.last) {
636  x = e->val;
637  addRight(x);
638  e = e->next;
639  }
640 
641  nb = l.nb;
642  cur = first->next;
643 
644  return *this;
645 }
646 
655 template <class type> void vpList<type>::operator+=(vpList<type> &l)
656 {
657  type x;
658 
659  l.front();
660  end();
661  while (!l.outside()) {
662  x = l.value();
663  addRight(x);
664  l.next();
665  }
666 }
667 
676 template <class type> void vpList<type>::operator+=(const type &l)
677 {
678  end();
679  addRight(l);
680 }
681 
687 template <class type> vpList<type>::vpList(const vpList<type> &l) : nb(0), first(nullptr), last(nullptr), cur(nullptr)
688 {
689  init();
690  *this = l;
691 }
692 
696 template <class type> void vpList<type>::display()
697 {
698  unsigned int k = 1;
699  front();
700  while (!outside()) {
701  std::cout << k << " ---> " << value() << std::endl;
702  next();
703  ++k;
704  }
705  std::cout << std::endl << std::endl;
706 }
707 END_VISP_NAMESPACE
708 #endif /* #ifndef VP_LIST_H */
709 
710 /*
711  * Local variables:
712  * c-basic-offset: 2
713  * End:
714  */
Provide simple list management.
Definition: vpList.h:109
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:421
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:586
void swapRight()
Switch the current element with the element on the right.
Definition: vpList.h:555
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:514
vpList()
Basic constructor, initialization, Create an empty list.
Definition: vpList.h:214
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:696
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:655
unsigned int nb
Definition: vpList.h:114
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:138
void print()
Definition: vpList.h:159
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:627
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:524
void suppress(void)
suppress the current element
Definition: vpList.h:605
vpListElement< type > * first
! number of items in the List
Definition: vpList.h:122
vpListElement< type > * last
the last virtualitem in the list
Definition: vpList.h:130
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