Visual Servoing Platform  version 3.2.0 under development (2019-01-22)
vpList.h
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * List data structure.
33  *
34  * Authors:
35  * Eric Marchand
36  * Nicolas Mansard : Toward const-specification respect
37  *
38  *****************************************************************************/
39 
40 #ifndef VP_LIST_H
41 #define VP_LIST_H
42 
48 #include <visp3/core/vpConfig.h>
49 #include <visp3/core/vpDebug.h>
50 #include <visp3/core/vpException.h>
51 
52 #include <stdio.h>
53 
54 #ifndef DOXYGEN_SHOULD_SKIP_THIS
55 
60 template <class type> class vpListElement
61 {
62  // private:
63  // vpListElement(const vpListElement &)
64  // : prev(NULL), next(NULL), val()
65  // {
66  // throw vpException(vpException::functionNotImplementedError,"Not
67  // implemented!");
68  // }
69  // vpListElement &operator=(const vpListElement &){
70  // throw vpException(vpException::functionNotImplementedError,"Not
71  // implemented!"); return *this;
72  // }
73 
74 public:
75  vpListElement() : prev(NULL), next(NULL), val(){};
76  vpListElement<type> *prev;
77  vpListElement<type> *next;
78  type val;
79 };
80 
81 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
82 
113 template <class type> class vpList
114 {
115 private:
116  void init();
117 
118 public:
119  unsigned int nb;
120 
127  vpListElement<type> *first;
135  vpListElement<type> *last;
143  vpListElement<type> *cur; // the current element
144 public:
145  vpList(); // constr.
146  vpList(const vpList &l); // cloning
147  virtual ~vpList(); // destr.
148 
149  inline void next(void); // current element's successor ( cur = cur->next )
150  inline void previous(void); // current element's predecessor ( cur = cur->pred )
151  inline void front(void); // go to the front of the List (cur = first)
152  inline void end(void); // go back to the end of the List ( cur = last )
153  inline bool outside(void) const; // test whether we are outside the List
154 
155  bool empty(void) const; // tests whether the List is empty
156 
157  inline type &value(void); // returns the current element value
158  inline const type &value(void) const; // returns the current element value
159 
160  void suppress(void); // deletes the current item
161  void kill(); // empties the List
162 
163  void display(); // displays the content of the list
164  void print() { display(); } // displays the content of the list
165 
166  inline void addRight(const type &el); // inserts an element on the right
167  inline void addLeft(const type &el); // inserts an element on the left
168  inline void modify(const type &el); // modifies thevalue field of the curr. el.
169  inline void addRight(type &el); // inserts an element on the right
170  inline void addLeft(type &el); // inserts an element on the left
171  inline void swapLeft(); // Switch the current element with the element on the left
172  inline void swapRight(); // Switch the current element with the element on the right
173  inline unsigned int nbElement(void); // returns the number of items currently in the list
174  inline unsigned int nbElements(void); // returns the number of items currently in the list
175 
176  vpList<type> &operator=(const vpList<type> &l);
177  inline void operator+=(vpList<type> &l);
178  inline void operator+=(const type &l);
179 
180  // Other non fundamental member (may be somehow useful)
181  bool nextOutside(void) const; // test whether we are outside the List
182  bool previousOutside(void) const; // test whether we are outside the List
183 
184  type &previousValue(void); // returns the previous element value
185  type &nextValue(void); // returns the next element value
186  type &firstValue(void);
187  type &lastValue(void);
188 };
189 
195 template <class type> void vpList<type>::init()
196 {
197  vpListElement<type> *x = new vpListElement<type>;
198  vpListElement<type> *y = new vpListElement<type>;
199 
200  first = x;
201  last = y;
202 
203  x->prev = NULL;
204  x->next = y;
205  y->prev = x;
206  y->next = NULL;
207 
208  cur = x;
209  nb = 0;
210 }
211 
219 template <class type> vpList<type>::vpList() : nb(0), first(NULL), last(NULL), cur(NULL) { init(); }
220 
225 template <class type> vpList<type>::~vpList()
226 {
227  kill();
228 
229  /*if (first != NULL) */ delete first;
230  /*if (last != NULL) */ delete last;
231 }
232 
236 template <class type> unsigned int vpList<type>::nbElement(void) { return (nb); }
237 
241 template <class type> unsigned int vpList<type>::nbElements(void) { return (nb); }
242 
250 template <class type> void vpList<type>::next(void) { cur = cur->next; }
251 
259 template <class type> void vpList<type>::previous(void) { cur = cur->prev; }
260 
269 template <class type> type &vpList<type>::value(void) { return (cur->val); }
270 
279 template <class type> const type &vpList<type>::value(void) const { return (cur->val); }
280 
289 template <class type> type &vpList<type>::previousValue(void) { return (cur->prev->val); }
290 
298 template <class type> type &vpList<type>::nextValue(void) { return (cur->next->val); }
299 
306 template <class type> type &vpList<type>::firstValue(void) { return (first->next->val); }
307 
313 template <class type> type &vpList<type>::lastValue(void) { return (last->prev->val); }
314 
323 template <class type> void vpList<type>::front(void) { cur = first->next; }
324 
333 template <class type> void vpList<type>::end(void) { cur = last->prev; }
334 
343 template <class type> bool vpList<type>::empty(void) const { return ((first->next == last) && (first == last->prev)); }
344 
356 template <class type> bool vpList<type>::outside(void) const { return ((cur == first) || (cur == last)); }
357 
367 template <class type> bool vpList<type>::nextOutside(void) const
368 {
369  return ((cur->next == first) || (cur->next == last));
370 }
371 
381 template <class type> bool vpList<type>::previousOutside(void) const
382 {
383  return ((cur->prev == first) || (cur->prev == last));
384 }
385 
396 template <class type> void vpList<type>::addRight(const type &v)
397 {
398  vpListElement<type> *x = new vpListElement<type>;
399 
400  x->val = v;
401  if (empty()) {
402  cur = first;
403  } else {
404  if (outside())
405  std::cout << "vpList: outside with addRight " << std::endl;
406  }
407  cur->next->prev = x;
408  x->next = cur->next;
409  x->prev = cur;
410  cur->next = x;
411  cur = x;
412  nb++;
413 }
414 
425 template <class type> void vpList<type>::addLeft(const type &v)
426 {
427  vpListElement<type> *x = new vpListElement<type>;
428 
429  x->val = v;
430 
431  if (empty()) {
432  cur = last;
433  } else {
434  if (outside())
435  std::cout << "vpList: outside with addLeft " << std::endl;
436  }
437  x->next = cur;
438  x->prev = cur->prev;
439  cur->prev->next = x;
440  cur->prev = x;
441  cur = x;
442  nb++;
443 }
444 
455 template <class type> void vpList<type>::addRight(type &v)
456 {
457  vpListElement<type> *x = new vpListElement<type>;
458 
459  x->val = v;
460  if (empty()) {
461  cur = first;
462  } else {
463  if (outside())
464  std::cout << "vpList: outside with addRight " << std::endl;
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  } else {
493  if (outside())
494  std::cout << "vpList: outside with addLeft " << std::endl;
495  }
496  x->next = cur;
497  x->prev = cur->prev;
498  cur->prev->next = x;
499  cur->prev = x;
500  cur = x;
501  nb++;
502 }
503 
512 template <class type> void vpList<type>::modify(const type &v) { cur->val = v; }
513 
522 template <class type> void vpList<type>::swapLeft()
523 {
524  if (cur->prev != first) {
525  cur->prev->prev->next = cur;
526  cur->next->prev = cur->prev;
527 
528  vpListElement<type> *nextTmp;
529  vpListElement<type> *prevTmp;
530 
531  nextTmp = cur->next;
532  prevTmp = cur->prev;
533 
534  cur->next = cur->prev;
535  cur->prev = cur->prev->prev;
536 
537  prevTmp->prev = cur;
538  prevTmp->next = nextTmp;
539  } else {
540  std::cout << "vpList: previous element is outside (swapLeft) " << std::endl;
541  }
542 }
543 
552 template <class type> void vpList<type>::swapRight()
553 {
554  if (cur->next != last) {
555  cur->prev->next = cur->next;
556  cur->next->next->prev = cur;
557 
558  vpListElement<type> *nextTmp;
559  vpListElement<type> *prevTmp;
560 
561  nextTmp = cur->next;
562  prevTmp = cur->prev;
563 
564  cur->next = nextTmp->next;
565  cur->prev = nextTmp;
566 
567  nextTmp->prev = prevTmp;
568  nextTmp->next = cur;
569  } else {
570  std::cout << "vpList: next element is outside (swapRight) " << std::endl;
571  }
572 }
573 
582 template <class type> void vpList<type>::kill()
583 {
584 
585  front();
586  while (!empty()) {
587  suppress();
588  }
589 }
590 
601 template <class type> void vpList<type>::suppress(void)
602 {
603  vpListElement<type> *x;
604 
605  cur->prev->next = cur->next;
606  cur->next->prev = cur->prev;
607  x = cur;
608  cur = cur->next;
609 
610  if (x != NULL)
611  delete x;
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(NULL), last(NULL), cur(NULL)
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  */
vpListElement< type > * last
the last virtualitem in the list
Definition: vpList.h:135
unsigned int nbElements(void)
return the number of element in the list
Definition: vpList.h:241
virtual ~vpList()
vpList destructor
Definition: vpList.h:225
void suppress(void)
suppress the current element
Definition: vpList.h:601
void end(void)
Position the current element on the last element of the list.
Definition: vpList.h:333
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition: vpList.h:356
unsigned int nbElement(void)
return the number of element in the list
Definition: vpList.h:236
Provide simple list management.
Definition: vpList.h:113
vpList()
Basic constructor, initialization, Create an empty list.
Definition: vpList.h:219
void kill()
Destroy the list.
Definition: vpList.h:582
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:381
vpList< type > & operator=(const vpList< type > &l)
Copy constructor const.
Definition: vpList.h:622
void print()
Definition: vpList.h:164
void next(void)
position the current element on the next one
Definition: vpList.h:250
type & previousValue(void)
return the value of the previous element
Definition: vpList.h:289
unsigned int nb
Definition: vpList.h:119
void addLeft(const type &el)
add a new element in the list, at the left of the current one
Definition: vpList.h:425
type & firstValue(void)
return the first element of the list
Definition: vpList.h:306
void modify(const type &el)
Modify the value of the current element.
Definition: vpList.h:512
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:323
type & value(void)
return the value of the current element
Definition: vpList.h:269
type & nextValue(void)
return the value of the next element
Definition: vpList.h:298
vpListElement< type > * first
! number of items in the List
Definition: vpList.h:127
void addRight(const type &el)
add a new element in the list, at the right of the current one
Definition: vpList.h:396
void swapLeft()
Switch the current element with the element on the left.
Definition: vpList.h:522
vpListElement< type > * cur
the current item in the list
Definition: vpList.h:143
type & lastValue(void)
return the last element of the list
Definition: vpList.h:313
void previous(void)
position the current element on the previous one
Definition: vpList.h:259
void display()
Print (std::cout) all the element of the list.
Definition: vpList.h:691
void operator+=(vpList< type > &l)
Append two lists.
Definition: vpList.h:650
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:367
void swapRight()
Switch the current element with the element on the right.
Definition: vpList.h:552
bool empty(void) const
Test if the list is empty.
Definition: vpList.h:343