Visual Servoing Platform  version 3.0.0
vpList.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  * Nicolas Mansard : Toward const-specification respect
36  *
37  *****************************************************************************/
38 
39 
40 #ifndef VP_LIST_H
41 #define VP_LIST_H
42 
43 
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpDebug.h>
51 #include <visp3/core/vpException.h>
52 
53 #include <stdio.h>
54 
55 #ifndef DOXYGEN_SHOULD_SKIP_THIS
56 
61 template <class type>
62 class vpListElement
63 {
64 //private:
65 // vpListElement(const vpListElement &)
66 // : prev(NULL), next(NULL), val()
67 // {
68 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
69 // }
70 // vpListElement &operator=(const vpListElement &){
71 // throw vpException(vpException::functionNotImplementedError,"Not implemented!");
72 // return *this;
73 // }
74 
75 public:
76  vpListElement() : prev(NULL), next(NULL), val() {};
77  vpListElement<type> *prev; //<! pointer to the previous element in the list
78  vpListElement<type> *next; //<! pointer to the next element in the list
79  type val; //<! value of the element
80 } ;
81 
82 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
83 
84 
117 template <class type>
118 class vpList
119 {
120  private:
121  void init() ;
122  public:
123 
124  unsigned int nb; //<! number of items in the List
132  vpListElement<type> *first;
140  vpListElement<type> *last;
148  vpListElement<type> *cur; // the current element
149  public:
150  vpList() ; // constr.
151  vpList(const vpList& l); // cloning
152  virtual ~vpList(); // destr.
153 
154  inline void next(void) ; // current element's successor ( cur = cur->next )
155  inline void previous(void) ; // current element's predecessor ( cur = cur->pred )
156  inline void front(void) ; // go to the front of the List (cur = first)
157  inline void end(void) ; // go back to the end of the List ( cur = last )
158  inline bool outside(void) const; // test whether we are outside the List
159 
160  bool empty(void) const; // tests whether the List is empty
161 
162  inline type& value(void); // returns the current element value
163  inline const type& value(void) const; // returns the current element value
164 
165  void suppress(void); // deletes the current item
166  void kill(); // empties the List
167 
168  void display() ; // displays the content of the list
169  void print() {display() ;} // displays the content of the list
170 
171 
172  inline void addRight(const type& el); // inserts an element on the right
173  inline void addLeft(const type& el); // inserts an element on the left
174  inline void modify(const type& el); // modifies thevalue field of the curr. el.
175  inline void addRight(type& el); // inserts an element on the right
176  inline void addLeft(type& el); // inserts an element on the left
177  inline void swapLeft(); // Switch the current element with the element on the left
178  inline void swapRight(); // Switch the current element with the element on the right
179  inline unsigned int nbElement(void); // returns the number of items currently in the list
180  inline unsigned int nbElements(void); // returns the number of items currently in the list
181 
183  inline void operator+=(vpList<type>& l);
184  inline void operator+=(const type& l);
185 
186  // Other non fundamental member (may be somehow useful)
187  bool nextOutside(void) const; // test whether we are outside the List
188  bool previousOutside(void) const;// test whether we are outside the List
189 
190 
191  type& previousValue(void); // returns the previous element value
192  type& nextValue(void); // returns the next element value
193  type& firstValue(void) ;
194  type& lastValue(void) ;
195 
196 
197 };
198 
199 
205 template<class type>
206 void vpList<type>::init()
207 {
208  vpListElement<type> *x=new vpListElement<type>;
209  vpListElement<type> *y=new vpListElement<type> ;
210 
211  first = x ;
212  last = y ;
213 
214  x->prev = NULL ;
215  x->next = y ;
216  y->prev = x ;
217  y->next =NULL ;
218 
219  cur = x ;
220  nb = 0 ;
221 }
222 
230 template<class type>
231 vpList<type>::vpList() : nb(0), first(NULL), last(NULL), cur(NULL)
232 {
233  init() ;
234 }
235 
236 
237 
242 template<class type>
244 {
245  kill() ;
246 
247  /*if (first != NULL) */ delete first ;
248  /*if (last != NULL) */ delete last ;
249 }
250 
254 template<class type>
255 unsigned int vpList<type>::nbElement(void)
256 {
257  return(nb) ;
258 }
259 
263 template<class type>
264 unsigned int vpList<type>::nbElements(void)
265 {
266  return(nb) ;
267 }
268 
269 
277 template<class type>
279 {
280  cur = cur->next ;
281 }
282 
283 
291 template<class type>
293 {
294  cur = cur->prev ;
295 }
296 
305 template<class type>
307 {
308  return(cur->val) ;
309 }
310 
319 template<class type>
320 const type& vpList<type>::value(void) const
321 {
322  return(cur->val) ;
323 }
324 
333 template<class type>
335 {
336  return(cur->prev->val) ;
337 }
338 
346 template<class type>
348 {
349  return(cur->next->val) ;
350 }
351 
352 
353 
360 template<class type>
362 {
363  return(first->next->val) ;
364 }
365 
366 
367 
373 template<class type>
375 {
376  return(last->prev->val) ;
377 }
378 
379 
388 template<class type>
390 {
391  cur = first->next ;
392 }
393 
402 template<class type>
404 {
405  cur = last->prev ;
406 }
407 
416 template<class type>
417 bool vpList<type>::empty(void) const
418 {
419  return((first->next == last) &&( first == last->prev)) ;
420 }
421 
433 template<class type>
434 bool vpList<type>::outside(void) const
435 {
436 
437  return((cur==first)||(cur==last)) ;
438 }
439 
449 template<class type>
451 {
452  return((cur->next==first)||(cur->next==last)) ;
453 }
454 
455 
465 template<class type>
467 {
468  return((cur->prev==first)||(cur->prev==last)) ;
469 }
470 
471 
482 template<class type>
483 void vpList<type>::addRight(const type& v)
484 {
485  vpListElement<type> *x=new vpListElement<type>;
486 
487  x->val = v ;
488  if (empty())
489  {
490  cur = first ;
491  }
492  else
493  {
494  if (outside()) std::cout << "vpList: outside with addRight " << std::endl ;
495  }
496  cur->next->prev = x ;
497  x->next = cur->next ;
498  x->prev = cur ;
499  cur->next = x ;
500  cur = x ;
501  nb++ ;
502 }
503 
504 
515 template<class type>
516 void vpList<type>::addLeft(const type& v)
517 {
518  vpListElement<type> *x=new vpListElement<type>;
519 
520  x->val = v ;
521 
522  if (empty())
523  {
524  cur = last ;
525  }
526  else
527  {
528  if (outside()) std::cout << "vpList: outside with addLeft " << std::endl ;
529  }
530  x->next = cur ;
531  x->prev = cur->prev ;
532  cur->prev->next = x ;
533  cur->prev = x ;
534  cur = x ;
535  nb++ ;
536 
537 }
538 
549 template<class type>
551 {
552  vpListElement<type> *x=new vpListElement<type>;
553 
554  x->val = v ;
555  if (empty())
556  {
557  cur = first ;
558  }
559  else
560  {
561  if (outside()) std::cout << "vpList: outside with addRight " << std::endl ;
562  }
563  cur->next->prev = x ;
564  x->next = cur->next ;
565  x->prev = cur ;
566  cur->next = x ;
567  cur = x ;
568  nb++ ;
569 }
570 
571 
582 template<class type>
584 {
585  vpListElement<type> *x=new vpListElement<type>;
586 
587  x->val = v ;
588 
589  if (empty())
590  {
591  cur = last ;
592  }
593  else
594  {
595  if (outside()) std::cout << "vpList: outside with addLeft " << std::endl ;
596  }
597  x->next = cur ;
598  x->prev = cur->prev ;
599  cur->prev->next = x ;
600  cur->prev = x ;
601  cur = x ;
602  nb++ ;
603 
604 }
605 
614 template<class type>
615 void vpList<type>::modify(const type& v)
616 {
617  cur->val = v ;
618 }
619 
628 template<class type>
630 {
631  if (cur->prev != first)
632  {
633  cur->prev->prev->next = cur;
634  cur->next->prev = cur->prev;
635 
636  vpListElement<type> *nextTmp;
637  vpListElement<type> *prevTmp;
638 
639  nextTmp = cur->next;
640  prevTmp = cur->prev;
641 
642  cur->next = cur->prev;
643  cur->prev = cur->prev->prev;
644 
645  prevTmp->prev = cur;
646  prevTmp->next = nextTmp;
647  }
648  else
649  {
650  std::cout << "vpList: previous element is outside (swapLeft) " << std::endl ;
651  }
652 }
653 
662 template<class type>
664 {
665  if (cur->next != last)
666  {
667  cur->prev->next = cur->next;
668  cur->next->next->prev = cur;
669 
670  vpListElement<type> *nextTmp;
671  vpListElement<type> *prevTmp;
672 
673  nextTmp = cur->next;
674  prevTmp = cur->prev;
675 
676  cur->next = nextTmp->next;
677  cur->prev = nextTmp;
678 
679  nextTmp->prev = prevTmp;
680  nextTmp->next = cur;
681  }
682  else
683  {
684  std::cout << "vpList: next element is outside (swapRight) " << std::endl ;
685  }
686 }
687 
696 template<class type>
698 {
699 
700  front() ;
701  while (!empty())
702  {
703  suppress() ;
704  }
705 
706 }
707 
708 
709 
720 template<class type>
722 {
723  vpListElement<type> *x ;
724 
725  cur->prev->next = cur->next ;
726  cur->next->prev = cur->prev ;
727  x = cur ;
728  cur = cur->next ;
729 
730  if (x!=NULL) delete x ;
731 
732  nb-- ;
733 
734 
735 }
736 
737 
738 
739 
746 template<class type>
748 {
749  type x ;
750  vpListElement<type> *e ;
751 
752  kill() ;
753  e = l.first->next ;
754  front() ;
755  while (e!=l.last)
756  {
757  x = e->val ;
758  addRight(x) ;
759  e = e->next ;
760  }
761 
762  nb = l.nb ;
763  cur = first->next ;
764 
765  return *this;
766 }
767 
776 template<class type>
778 {
779  type x ;
780 
781  l.front() ;
782  end() ;
783  while (!l.outside())
784  {
785  x = l.value() ;
786  addRight(x) ;
787  l.next() ;
788  }
789 }
790 
799 template<class type>
800 void vpList<type>::operator += (const type& l)
801 {
802  end() ;
803  addRight(l) ;
804 }
805 
806 
812 template<class type>
814  : nb(0), first(NULL), last(NULL), cur(NULL)
815 {
816  init() ;
817  *this = l;
818 }
819 
823 template<class type>
825 {
826  unsigned int k = 1 ;
827  front() ;
828  while(!outside()) {
829  std::cout<<k<<" ---> "<<value()<<std::endl ;
830  next() ;
831  k++ ;
832  }
833  std::cout<< std::endl << std::endl ;
834 }
835 
836 #endif /* #ifndef VP_LIST_H */
837 
838 
839 
840 /*
841  * Local variables:
842  * c-basic-offset: 2
843  * End:
844  */
vpListElement< type > * last
the last virtualitem in the list
Definition: vpList.h:140
unsigned int nbElements(void)
return the number of element in the list
Definition: vpList.h:264
virtual ~vpList()
vpList destructor
Definition: vpList.h:243
void suppress(void)
suppress the current element
Definition: vpList.h:721
void end(void)
Position the current element on the last element of the list.
Definition: vpList.h:403
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition: vpList.h:434
unsigned int nbElement(void)
return the number of element in the list
Definition: vpList.h:255
Provide simple list management.
Definition: vpList.h:118
vpList()
Basic constructor, initialization, Create an empty list.
Definition: vpList.h:231
void kill()
Destroy 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:466
vpList< type > & operator=(const vpList< type > &l)
Copy constructor const.
Definition: vpList.h:747
void print()
Definition: vpList.h:169
void next(void)
position the current element on the next one
Definition: vpList.h:278
type & previousValue(void)
return the value of the previous element
Definition: vpList.h:334
unsigned int nb
Definition: vpList.h:124
void addLeft(const type &el)
add a new element in the list, at the left of the current one
Definition: vpList.h:516
type & firstValue(void)
return the first element of the list
Definition: vpList.h:361
void modify(const type &el)
Modify the value of the current element.
Definition: vpList.h:615
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:389
type & value(void)
return the value of the current element
Definition: vpList.h:306
type & nextValue(void)
return the value of the next element
Definition: vpList.h:347
vpListElement< type > * first
the first virtual item in the list
Definition: vpList.h:132
void addRight(const type &el)
add a new element in the list, at the right of the current one
Definition: vpList.h:483
void swapLeft()
Switch the current element with the element on the left.
Definition: vpList.h:629
vpListElement< type > * cur
the current item in the list
Definition: vpList.h:148
type & lastValue(void)
return the last element of the list
Definition: vpList.h:374
void previous(void)
position the current element on the previous one
Definition: vpList.h:292
void display()
Print (std::cout) all the element of the list.
Definition: vpList.h:824
void operator+=(vpList< type > &l)
Append two lists.
Definition: vpList.h:777
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:450
void swapRight()
Switch the current element with the element on the right.
Definition: vpList.h:663
bool empty(void) const
Test if the list is empty.
Definition: vpList.h:417