ViSP  2.10.0
vpList.h
1 /****************************************************************************
2  *
3  * $Id: vpList.h 4632 2014-02-03 17:06:40Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * List data structure.
36  *
37  * Authors:
38  * Eric Marchand
39  * Nicolas Mansard : Toward const-specification respect
40  *
41  *****************************************************************************/
42 
43 
44 #ifndef VP_LIST_H
45 #define VP_LIST_H
46 
47 
53 #include <visp/vpConfig.h>
54 #include <visp/vpDebug.h>
55 
56 #include <stdio.h>
57 
58 #ifndef DOXYGEN_SHOULD_SKIP_THIS
59 
64 template <class type>
65 class vpListElement
66 {
67  public:
68  vpListElement() : prev(NULL), next(NULL), val() {};
69  vpListElement<type> *prev; //<! pointer to the previous element in the list
70  vpListElement<type> *next; //<! pointer to the next element in the list
71  type val; //<! value of the element
72 } ;
73 
74 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
75 
76 
112 template <class type>
113 class vpList
114 {
115  private:
116  void init() ;
117  public:
118 
119  unsigned int nb; //<! number of items in the List
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 
167  inline void addRight(const type& el); // inserts an element on the right
168  inline void addLeft(const type& el); // inserts an element on the left
169  inline void modify(const type& el); // modifies thevalue field of the curr. el.
170  inline void addRight(type& el); // inserts an element on the right
171  inline void addLeft(type& el); // inserts an element on the left
172  inline void swapLeft(); // Switch the current element with the element on the left
173  inline void swapRight(); // Switch the current element with the element on the right
174  inline unsigned int nbElement(void); // returns the number of items currently in the list
175  inline unsigned int nbElements(void); // returns the number of items currently in the list
176 
178  inline void operator+=(vpList<type>& l);
179  inline void operator+=(const type& l);
180 
181  // Other non fundamental member (may be somehow useful)
182  bool nextOutside(void) const; // test whether we are outside the List
183  bool previousOutside(void) const;// test whether we are outside the List
184 
185 
186  type& previousValue(void); // returns the previous element value
187  type& nextValue(void); // returns the next element value
188  type& firstValue(void) ;
189  type& lastValue(void) ;
190 
191 
192 };
193 
194 
200 template<class type>
201 void vpList<type>::init()
202 {
203  vpListElement<type> *x=new vpListElement<type>;
204  vpListElement<type> *y=new vpListElement<type> ;
205 
206  first = x ;
207  last = y ;
208 
209  x->prev = NULL ;
210  x->next = y ;
211  y->prev = x ;
212  y->next =NULL ;
213 
214  cur = x ;
215  nb = 0 ;
216 }
217 
225 template<class type>
226 vpList<type>::vpList() : nb(0), first(NULL), last(NULL), cur(NULL)
227 {
228  init() ;
229 }
230 
231 
232 
237 template<class type>
239 {
240  kill() ;
241 
242  /*if (first != NULL) */ delete first ;
243  /*if (last != NULL) */ delete last ;
244 }
245 
249 template<class type>
250 unsigned int vpList<type>::nbElement(void)
251 {
252  return(nb) ;
253 }
254 
258 template<class type>
259 unsigned int vpList<type>::nbElements(void)
260 {
261  return(nb) ;
262 }
263 
264 
272 template<class type>
274 {
275  cur = cur->next ;
276 }
277 
278 
286 template<class type>
288 {
289  cur = cur->prev ;
290 }
291 
300 template<class type>
302 {
303  return(cur->val) ;
304 }
305 
314 template<class type>
315 const type& vpList<type>::value(void) const
316 {
317  return(cur->val) ;
318 }
319 
328 template<class type>
330 {
331  return(cur->prev->val) ;
332 }
333 
341 template<class type>
343 {
344  return(cur->next->val) ;
345 }
346 
347 
348 
355 template<class type>
357 {
358  return(first->next->val) ;
359 }
360 
361 
362 
368 template<class type>
370 {
371  return(last->prev->val) ;
372 }
373 
374 
383 template<class type>
385 {
386  cur = first->next ;
387 }
388 
397 template<class type>
399 {
400  cur = last->prev ;
401 }
402 
411 template<class type>
412 bool vpList<type>::empty(void) const
413 {
414  return((first->next == last) &&( first == last->prev)) ;
415 }
416 
428 template<class type>
429 bool vpList<type>::outside(void) const
430 {
431 
432  return((cur==first)||(cur==last)) ;
433 }
434 
444 template<class type>
446 {
447  return((cur->next==first)||(cur->next==last)) ;
448 }
449 
450 
460 template<class type>
462 {
463  return((cur->prev==first)||(cur->prev==last)) ;
464 }
465 
466 
477 template<class type>
478 void vpList<type>::addRight(const type& v)
479 {
480  vpListElement<type> *x=new vpListElement<type>;
481 
482  x->val = v ;
483  if (empty())
484  {
485  cur = first ;
486  }
487  else
488  {
489  if (outside()) std::cout << "vpList: outside with addRight " << std::endl ;
490  }
491  cur->next->prev = x ;
492  x->next = cur->next ;
493  x->prev = cur ;
494  cur->next = x ;
495  cur = x ;
496  nb++ ;
497 }
498 
499 
510 template<class type>
511 void vpList<type>::addLeft(const type& v)
512 {
513  vpListElement<type> *x=new vpListElement<type>;
514 
515  x->val = v ;
516 
517  if (empty())
518  {
519  cur = last ;
520  }
521  else
522  {
523  if (outside()) std::cout << "vpList: outside with addLeft " << std::endl ;
524  }
525  x->next = cur ;
526  x->prev = cur->prev ;
527  cur->prev->next = x ;
528  cur->prev = x ;
529  cur = x ;
530  nb++ ;
531 
532 }
533 
544 template<class type>
546 {
547  vpListElement<type> *x=new vpListElement<type>;
548 
549  x->val = v ;
550  if (empty())
551  {
552  cur = first ;
553  }
554  else
555  {
556  if (outside()) std::cout << "vpList: outside with addRight " << std::endl ;
557  }
558  cur->next->prev = x ;
559  x->next = cur->next ;
560  x->prev = cur ;
561  cur->next = x ;
562  cur = x ;
563  nb++ ;
564 }
565 
566 
577 template<class type>
579 {
580  vpListElement<type> *x=new vpListElement<type>;
581 
582  x->val = v ;
583 
584  if (empty())
585  {
586  cur = last ;
587  }
588  else
589  {
590  if (outside()) std::cout << "vpList: outside with addLeft " << std::endl ;
591  }
592  x->next = cur ;
593  x->prev = cur->prev ;
594  cur->prev->next = x ;
595  cur->prev = x ;
596  cur = x ;
597  nb++ ;
598 
599 }
600 
609 template<class type>
610 void vpList<type>::modify(const type& v)
611 {
612  cur->val = v ;
613 }
614 
623 template<class type>
625 {
626  if (cur->prev != first)
627  {
628  cur->prev->prev->next = cur;
629  cur->next->prev = cur->prev;
630 
631  vpListElement<type> *nextTmp;
632  vpListElement<type> *prevTmp;
633 
634  nextTmp = cur->next;
635  prevTmp = cur->prev;
636 
637  cur->next = cur->prev;
638  cur->prev = cur->prev->prev;
639 
640  prevTmp->prev = cur;
641  prevTmp->next = nextTmp;
642  }
643  else
644  {
645  std::cout << "vpList: previous element is outside (swapLeft) " << std::endl ;
646  }
647 }
648 
657 template<class type>
659 {
660  if (cur->next != last)
661  {
662  cur->prev->next = cur->next;
663  cur->next->next->prev = cur;
664 
665  vpListElement<type> *nextTmp;
666  vpListElement<type> *prevTmp;
667 
668  nextTmp = cur->next;
669  prevTmp = cur->prev;
670 
671  cur->next = nextTmp->next;
672  cur->prev = nextTmp;
673 
674  nextTmp->prev = prevTmp;
675  nextTmp->next = cur;
676  }
677  else
678  {
679  std::cout << "vpList: next element is outside (swapRight) " << std::endl ;
680  }
681 }
682 
691 template<class type>
693 {
694 
695  front() ;
696  while (!empty())
697  {
698  suppress() ;
699  }
700 
701 }
702 
703 
704 
715 template<class type>
717 {
718  vpListElement<type> *x ;
719 
720  cur->prev->next = cur->next ;
721  cur->next->prev = cur->prev ;
722  x = cur ;
723  cur = cur->next ;
724 
725  if (x!=NULL) delete x ;
726 
727  nb-- ;
728 
729 
730 }
731 
732 
733 
734 
741 template<class type>
743 {
744  type x ;
745  vpListElement<type> *e ;
746 
747  kill() ;
748  e = l.first->next ;
749  front() ;
750  while (e!=l.last)
751  {
752  x = e->val ;
753  addRight(x) ;
754  e = e->next ;
755  }
756 
757  nb = l.nb ;
758  cur = first->next ;
759 
760  return *this;
761 }
762 
771 template<class type>
773 {
774  type x ;
775 
776  l.front() ;
777  end() ;
778  while (!l.outside())
779  {
780  x = l.value() ;
781  addRight(x) ;
782  l.next() ;
783  }
784 }
785 
794 template<class type>
795 void vpList<type>::operator += (const type& l)
796 {
797  end() ;
798  addRight(l) ;
799 }
800 
801 
807 template<class type>
809  : nb(0), first(NULL), last(NULL), cur(NULL)
810 {
811  init() ;
812  *this = l;
813 }
814 
818 template<class type>
820 {
821  unsigned int k = 1 ;
822  front() ;
823  while(!outside()) {
824  std::cout<<k<<" ---> "<<value()<<std::endl ;
825  next() ;
826  k++ ;
827  }
828  std::cout<< std::endl << std::endl ;
829 }
830 
831 #endif /* #ifndef VP_LIST_H */
832 
833 
834 
835 /*
836  * Local variables:
837  * c-basic-offset: 2
838  * End:
839  */
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:259
virtual ~vpList()
vpList destructor
Definition: vpList.h:238
void suppress(void)
suppress the current element
Definition: vpList.h:716
void end(void)
Position the current element on the last element of the list.
Definition: vpList.h:398
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition: vpList.h:429
unsigned int nbElement(void)
return the number of element in the list
Definition: vpList.h:250
Provide simple list management.
Definition: vpList.h:113
vpList()
Basic constructor, initialization, Create an empty list.
Definition: vpList.h:226
void kill()
Destroy the list.
Definition: vpList.h:692
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:461
vpList< type > & operator=(const vpList< type > &l)
Copy constructor const.
Definition: vpList.h:742
void print()
Definition: vpList.h:164
void next(void)
position the current element on the next one
Definition: vpList.h:273
type & previousValue(void)
return the value of the previous element
Definition: vpList.h:329
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:511
type & firstValue(void)
return the first element of the list
Definition: vpList.h:356
void modify(const type &el)
Modify the value of the current element.
Definition: vpList.h:610
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:384
type & value(void)
return the value of the current element
Definition: vpList.h:301
type & nextValue(void)
return the value of the next element
Definition: vpList.h:342
vpListElement< type > * first
the first virtual item 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:478
void swapLeft()
Switch the current element with the element on the left.
Definition: vpList.h:624
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:369
void previous(void)
position the current element on the previous one
Definition: vpList.h:287
void display()
Print (std::cout) all the element of the list.
Definition: vpList.h:819
void operator+=(vpList< type > &l)
Append two lists.
Definition: vpList.h:772
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:445
void swapRight()
Switch the current element with the element on the right.
Definition: vpList.h:658
bool empty(void) const
Test if the list is empty.
Definition: vpList.h:412