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