ViSP  2.6.2
vpList.h
1 /****************************************************************************
2  *
3  * $Id: vpList.h 3530 2012-01-03 10:52:12Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2012 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<type> *prev; //<! pointer to the previous element in the list
69  vpListElement<type> *next; //<! pointer to the next element in the list
70  type val; //<! value of the element
71 } ;
72 
73 #endif /* DOXYGEN_SHOULD_SKIP_THIS */
74 
75 
111 template <class type>
112 class vpList
113 {
114  private:
115  void init() ;
116  public:
117 
118  unsigned int nb; //<! number of items in the List
126  vpListElement<type> *first;
134  vpListElement<type> *last;
142  vpListElement<type> *cur; // the current element
143  public:
144  vpList() ; // constr.
145  vpList(const vpList& l); // cloning
146  virtual ~vpList(); // destr.
147 
148  inline void next(void) ; // current element's successor ( cur = cur->next )
149  inline void previous(void) ; // current element's predecessor ( cur = cur->pred )
150  inline void front(void) ; // go to the front of the List (cur = first)
151  inline void end(void) ; // go back to the end of the List ( cur = last )
152  inline bool outside(void) const; // test whether we are outside the List
153 
154  bool empty(void) const; // tests whether the List is empty
155 
156  inline type& value(void); // returns the current element value
157  inline const type& value(void) const; // returns the current element value
158 
159  void suppress(void); // deletes the current item
160  void kill(); // empties the List
161 
162  void display() ; // displays the content of the list
163  void print() {display() ;} // displays the content of the list
164 
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  void 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 
185  type& previousValue(void); // returns the previous element value
186  type& nextValue(void); // returns the next element value
187  type& firstValue(void) ;
188  type& lastValue(void) ;
189 
190 
191 };
192 
193 
199 template<class type>
200 void vpList<type>::init()
201 {
202  vpListElement<type> *x=new vpListElement<type>;
203  vpListElement<type> *y=new vpListElement<type> ;
204 
205  first = x ;
206  last = y ;
207 
208  x->prev = NULL ;
209  x->next = y ;
210  y->prev = x ;
211  y->next =NULL ;
212 
213  cur = x ;
214  nb = 0 ;
215 }
216 
224 template<class type>
226 {
227  init() ;
228 }
229 
230 
231 
236 template<class type>
238 {
239 
240  kill() ;
241 
242  if (first != NULL) delete first ;
243  if (last != NULL) delete last ;
244 
245 
246 }
247 
251 template<class type>
252 unsigned int vpList<type>::nbElement(void)
253 {
254  return(nb) ;
255 }
256 
260 template<class type>
261 unsigned int vpList<type>::nbElements(void)
262 {
263  return(nb) ;
264 }
265 
266 
274 template<class type>
276 {
277  cur = cur->next ;
278 }
279 
280 
288 template<class type>
290 {
291  cur = cur->prev ;
292 }
293 
302 template<class type>
304 {
305  return(cur->val) ;
306 }
307 
316 template<class type>
317 const type& vpList<type>::value(void) const
318 {
319  return(cur->val) ;
320 }
321 
330 template<class type>
332 {
333  return(cur->prev->val) ;
334 }
335 
343 template<class type>
345 {
346  return(cur->next->val) ;
347 }
348 
349 
350 
357 template<class type>
359 {
360  return(first->next->val) ;
361 }
362 
363 
364 
370 template<class type>
372 {
373  return(last->prev->val) ;
374 }
375 
376 
385 template<class type>
387 {
388  cur = first->next ;
389 }
390 
399 template<class type>
401 {
402  cur = last->prev ;
403 }
404 
413 template<class type>
414 bool vpList<type>::empty(void) const
415 {
416  return((first->next == last) &&( first == last->prev)) ;
417 }
418 
430 template<class type>
431 bool vpList<type>::outside(void) const
432 {
433 
434  return((cur==first)||(cur==last)) ;
435 }
436 
446 template<class type>
448 {
449  return((cur->next==first)||(cur->next==last)) ;
450 }
451 
452 
462 template<class type>
464 {
465  return((cur->prev==first)||(cur->prev==last)) ;
466 }
467 
468 
479 template<class type>
480 void vpList<type>::addRight(const type& v)
481 {
482  vpListElement<type> *x=new vpListElement<type>;
483 
484  x->val = v ;
485  if (empty())
486  {
487  cur = first ;
488  }
489  else
490  {
491  if (outside()) std::cout << "vpList: outside with addRight " << std::endl ;
492  }
493  cur->next->prev = x ;
494  x->next = cur->next ;
495  x->prev = cur ;
496  cur->next = x ;
497  cur = x ;
498  nb++ ;
499 }
500 
501 
512 template<class type>
513 void vpList<type>::addLeft(const type& v)
514 {
515  vpListElement<type> *x=new vpListElement<type>;
516 
517  x->val = v ;
518 
519  if (empty())
520  {
521  cur = last ;
522  }
523  else
524  {
525  if (outside()) std::cout << "vpList: outside with addLeft " << std::endl ;
526  }
527  x->next = cur ;
528  x->prev = cur->prev ;
529  cur->prev->next = x ;
530  cur->prev = x ;
531  cur = x ;
532  nb++ ;
533 
534 }
535 
546 template<class type>
548 {
549  vpListElement<type> *x=new vpListElement<type>;
550 
551  x->val = v ;
552  if (empty())
553  {
554  cur = first ;
555  }
556  else
557  {
558  if (outside()) std::cout << "vpList: outside with addRight " << std::endl ;
559  }
560  cur->next->prev = x ;
561  x->next = cur->next ;
562  x->prev = cur ;
563  cur->next = x ;
564  cur = x ;
565  nb++ ;
566 }
567 
568 
579 template<class type>
581 {
582  vpListElement<type> *x=new vpListElement<type>;
583 
584  x->val = v ;
585 
586  if (empty())
587  {
588  cur = last ;
589  }
590  else
591  {
592  if (outside()) std::cout << "vpList: outside with addLeft " << std::endl ;
593  }
594  x->next = cur ;
595  x->prev = cur->prev ;
596  cur->prev->next = x ;
597  cur->prev = x ;
598  cur = x ;
599  nb++ ;
600 
601 }
602 
611 template<class type>
612 void vpList<type>::modify(const type& v)
613 {
614  cur->val = v ;
615 }
616 
625 template<class type>
627 {
628  if (cur->prev != first)
629  {
630  cur->prev->prev->next = cur;
631  cur->next->prev = cur->prev;
632 
633  vpListElement<type> *nextTmp;
634  vpListElement<type> *prevTmp;
635 
636  nextTmp = cur->next;
637  prevTmp = cur->prev;
638 
639  cur->next = cur->prev;
640  cur->prev = cur->prev->prev;
641 
642  prevTmp->prev = cur;
643  prevTmp->next = nextTmp;
644  }
645  else
646  {
647  std::cout << "vpList: previous element is outside (swapLeft) " << std::endl ;
648  }
649 }
650 
659 template<class type>
661 {
662  if (cur->next != last)
663  {
664  cur->prev->next = cur->next;
665  cur->next->next->prev = cur;
666 
667  vpListElement<type> *nextTmp;
668  vpListElement<type> *prevTmp;
669 
670  nextTmp = cur->next;
671  prevTmp = cur->prev;
672 
673  cur->next = nextTmp->next;
674  cur->prev = nextTmp;
675 
676  nextTmp->prev = prevTmp;
677  nextTmp->next = cur;
678  }
679  else
680  {
681  std::cout << "vpList: next element is outside (swapRight) " << std::endl ;
682  }
683 }
684 
693 template<class type>
695 {
696 
697  front() ;
698  while (!empty())
699  {
700  suppress() ;
701  }
702 
703 }
704 
705 
706 
717 template<class type>
719 {
720  vpListElement<type> *x ;
721 
722  cur->prev->next = cur->next ;
723  cur->next->prev = cur->prev ;
724  x = cur ;
725  cur = cur->next ;
726 
727  if (x!=NULL) delete x ;
728 
729  nb-- ;
730 
731 
732 }
733 
734 
735 
736 
743 template<class type>
745 {
746  type x ;
747  vpListElement<type> *e ;
748 
749  kill() ;
750  e = l.first->next ;
751  front() ;
752  while (e!=l.last)
753  {
754  x = e->val ;
755  addRight(x) ;
756  e = e->next ;
757  }
758 
759  nb = l.nb ;
760  cur = first->next ;
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 {
810  init() ;
811  *this = l;
812 }
813 
817 template<class type>
819 {
820  unsigned int k = 1 ;
821  front() ;
822  while(!outside()) {
823  std::cout<<k<<" ---> "<<value()<<std::endl ;
824  next() ;
825  k++ ;
826  }
827  std::cout<< std::endl << std::endl ;
828 }
829 
830 #endif /* #ifndef VP_LIST_H */
831 
832 
833 
834 /*
835  * Local variables:
836  * c-basic-offset: 2
837  * End:
838  */
vpListElement< type > * last
the last virtualitem in the list
Definition: vpList.h:134
unsigned int nbElements(void)
return the number of element in the list
Definition: vpList.h:261
virtual ~vpList()
vpList destructor
Definition: vpList.h:237
void suppress(void)
suppress the current element
Definition: vpList.h:718
void end(void)
Position the current element on the last element of the list.
Definition: vpList.h:400
bool outside(void) const
Test if the current element is outside the list (on the virtual element)
Definition: vpList.h:431
unsigned int nbElement(void)
return the number of element in the list
Definition: vpList.h:252
Provide simple list management.
Definition: vpList.h:112
vpList()
Basic constructor, initialization, Create an empty list.
Definition: vpList.h:225
void kill()
Destroy the list.
Definition: vpList.h:694
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:463
void print()
Definition: vpList.h:163
void next(void)
position the current element on the next one
Definition: vpList.h:275
type & previousValue(void)
return the value of the previous element
Definition: vpList.h:331
unsigned int nb
Definition: vpList.h:118
void addLeft(const type &el)
add a new element in the list, at the left of the current one
Definition: vpList.h:513
type & firstValue(void)
return the first element of the list
Definition: vpList.h:358
void modify(const type &el)
Modify the value of the current element.
Definition: vpList.h:612
void front(void)
Position the current element on the first element of the list.
Definition: vpList.h:386
type & value(void)
return the value of the current element
Definition: vpList.h:303
type & nextValue(void)
return the value of the next element
Definition: vpList.h:344
vpListElement< type > * first
the first virtual item in the list
Definition: vpList.h:126
void addRight(const type &el)
add a new element in the list, at the right of the current one
Definition: vpList.h:480
void swapLeft()
Switch the current element with the element on the left.
Definition: vpList.h:626
vpListElement< type > * cur
the current item in the list
Definition: vpList.h:142
type & lastValue(void)
return the last element of the list
Definition: vpList.h:371
void previous(void)
position the current element on the previous one
Definition: vpList.h:289
void display()
Print (std::cout) all the element of the list.
Definition: vpList.h:818
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:447
void operator=(const vpList< type > &l)
Copy constructor const.
Definition: vpList.h:744
void swapRight()
Switch the current element with the element on the right.
Definition: vpList.h:660
bool empty(void) const
Test if the list is empty.
Definition: vpList.h:414