SCL  1.0
Standard Control Library : Control, dynamics, physics, and simulation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Macros Groups Pages
CMappedList.hpp
1 /* This file is part of sUtil, a random collection of utilities.
2 
3 sUtil is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 
8 Alternatively, you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 sUtil is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU Lesser General Public
19 License and a copy of the GNU General Public License along with
20 sUtil. If not, see <http://www.gnu.org/licenses/>.
21  */
22 /* \file CMappedList.hpp
23  *
24  * Created on: Jul 12, 2010
25  *
26  * Copyright (C) 2010, Samir Menon <smenon@stanford.edu>
27  */
28 
29 #ifndef CMAPPEDLIST_HPP_
30 #define CMAPPEDLIST_HPP_
31 
32 #include <map>
33 #include <cstddef>
34 #include <vector>
35 
36 #ifdef DEBUG
37 #include <iostream>
38 #include <cassert>
39 #endif
40 
41 namespace sutil
42 {
43 
45  template <typename IdxS, typename TS>
46  class SMLNode
47  {
48  public:
49  TS* data_;
50  IdxS* id_;
51 
52  //For the linked list
53  SMLNode<IdxS,TS> *next_,*prev_;
54 
55  SMLNode()
56  {
57  data_=NULL;
58  id_=NULL;
59  next_=NULL;
60  prev_=NULL;
61  }
62  };
63 
84  template <typename Idx, typename T>
86  {
87  public:
98  typedef size_t size_type;
99  typedef ptrdiff_t difference_type;
100  typedef T* pointer;
101  typedef const T* const_pointer;
102  typedef T& reference;
103  typedef const T& const_reference;
104  typedef T value_type;
105 
109  class iterator; //Forward declaration
110  class const_iterator;
111 
116  CMappedList() : front_(NULL), back_(NULL), size_(0), flag_is_sorted_(false) {}
117 
118  protected:
121  virtual bool deepCopy(const CMappedList<Idx,T>* const arg_pmap);
122 
123  public:
128  explicit CMappedList(const CMappedList<Idx,T>& arg_pm)
129  {
130  front_ = NULL; back_ = NULL; null_.prev_ = NULL; size_ = 0;
131  deepCopy(&arg_pm);
132  }
133 
137  {
138  deepCopy(&arg_rhs);
139  return *this;
140  }
141 
144  virtual ~CMappedList();
145 
148  bool operator == (const CMappedList<Idx,T>& rhs);
149 
152  bool operator != (const CMappedList<Idx,T>& rhs);
153 
155  void swap(CMappedList<Idx,T>& arg_swap_obj);
156 
157 // /** Example usage:
158 // * first.assign (7,100); // 7 ints with value 100
159 // * second.assign (first.begin(),first.end()); // a copy of first */
160 // void assign ( iterator first, iterator last );
161 // void assign ( size_type n, const T& u );
162 
166  public:
167 
170  virtual T* create(const Idx & arg_idx, const bool insert_at_start=true);
171 
177  virtual T* create(const Idx & arg_idx, const T& arg_t, const bool insert_at_start=true);
178 
185  virtual T* insert(const Idx & arg_idx, T* arg_t, const bool insert_at_start=true);
186 
192  virtual T* at(const std::size_t arg_idx);
193 
197  virtual T* at(const Idx & arg_idx);
198 
203  virtual const Idx* getIndexAt(const std::size_t arg_idx) const;
204 
209  virtual int getIndexNumericAt(const Idx& arg_idx) const;
210 
215  virtual int getIndexNumericAt(const T* const arg_node) const;
216 
222  virtual const T* at_const(const std::size_t arg_idx) const;
223 
227  virtual const T* at_const(const Idx & arg_idx) const;
228 
231  virtual bool erase(T* arg_t);
232 
237  virtual bool erase(const Idx& arg_idx);
238 
240  virtual std::size_t size() const
241  { return size_; }
242 
244  virtual bool empty() const
245  { return (size_ == 0); }
246 
248  virtual bool clear();
249 
251  virtual T* operator[](const std::size_t arg_idx)
252  { return at(arg_idx); }
253 
257  protected:
258 
261 
264 
268 
270  std::map<Idx, SMLNode<Idx,T>*> map_;
271 
273  std::size_t size_;
274 
275  public:
277  class iterator : public std::iterator<std::forward_iterator_tag, T>
278  {
279  //To allow : const_iterator x = iterator();
280  friend class const_iterator;
281 
282  SMLNode<Idx,T> *pos_;
283  public:
284  explicit iterator(): pos_(NULL){}
285 
287  iterator(const iterator& other)
288  { pos_ = other.pos_; }
289 
290  explicit iterator(SMLNode<Idx,T>* node_ptr)
291  { pos_ = node_ptr; }
292 
293  iterator&
294  operator = (const iterator& other)
295  { pos_ = other.pos_; return (*this); }
296 
297  bool
298  operator == (const iterator& other)
299  { return (pos_ == other.pos_); }
300 
301  bool
302  operator != (const iterator& other)
303  { return (pos_ != other.pos_); }
304 
305  T&
306  operator * ()
307  { return *(pos_->data_); }
308 
309  T*
310  operator -> ()
311  { return pos_->data_; }
312 
313  Idx&
314  operator ! ()
315  { return *(pos_->id_); }
316 
318  iterator&
319  operator ++(int unused)
320  {
321  if(NULL!= pos_->next_)
322  { pos_ = pos_->next_; }
323  return *this;
324  }
325 
327  iterator&
329  {
330  if(NULL!= pos_->next_)
331  { pos_ = pos_->next_; }
332  return *this;
333  }
334 
335  iterator
336  operator +(int offset)
337  {
338  SMLNode<Idx,T> *ptr = this->pos_;
339  for(int i=0; i <offset; ++i)
340  {
341  if(NULL== ptr->next_) { break; }
342  ptr = ptr->next_;
343  }
344 
345  return iterator(ptr);
346  }
347 
349  iterator&
350  operator --(int unused)
351  {
352  if(NULL!= pos_)
353  { pos_ = pos_->prev_; }
354  return *this;
355  }
356 
358  iterator&
360  {
361  if(NULL!= pos_)
362  { pos_ = pos_->prev_; }
363  return *this;
364  }
365 
366  iterator
367  operator -(int offset)
368  {
369  SMLNode<Idx,T> *ptr = this->pos_;
370  for(int i=0; i < offset; ++i)
371  {
372  if(NULL== ptr->prev_) { break; }
373  ptr = ptr->prev_;
374  }
375 
376  return iterator(ptr);
377  }
378  };
379 
381  class const_iterator : public std::iterator<std::forward_iterator_tag, T>
382  {
383  const SMLNode<Idx,T> *pos_;
384  public:
385  explicit const_iterator(): pos_(NULL){}
386 
389  { pos_ = other.pos_; }
390 
391  explicit const_iterator(const SMLNode<Idx,T>* node_ptr)
392  { pos_ = node_ptr; }
393 
394  const const_iterator& operator = (const const_iterator& other)
395  { pos_ = other.pos_; return (*this); }
396 
397  const const_iterator& operator = (const iterator& other)
398  { pos_ = static_cast<const SMLNode<Idx,T> *>(other.pos_); return (*this); }
399 
400  bool
401  operator == (const const_iterator& other)
402  { return (pos_ == other.pos_); }
403 
404  bool
405  operator != (const const_iterator& other)
406  { return (pos_ != other.pos_); }
407 
408  const T&
409  operator * ()
410  { return *(pos_->data_); }
411 
412  const T*
413  operator -> ()
414  { return pos_->data_; }
415 
416  const Idx&
417  operator ! ()
418  { return *(pos_->id_); }
419 
421  const_iterator&
422  operator ++(int unused)
423  {
424  if(NULL!= pos_->next_)
425  { pos_ = pos_->next_; }
426  return *this;
427  }
428 
432  {
433  if(NULL!= pos_->next_)
434  { pos_ = pos_->next_; }
435  return *this;
436  }
437 
439  operator +(int offset)
440  {
441  const SMLNode<Idx,T> *ptr = this->pos_;
442  for(int i=0; i <offset; ++i)
443  {
444  if(NULL== ptr->next_) { break; }
445  ptr = ptr->next_;
446  }
447 
448  return const_iterator(ptr);
449  }
450 
452  const_iterator&
453  operator --(int unused)
454  {
455  if(NULL!= pos_)
456  { pos_ = pos_->prev_; }
457  return *this;
458  }
459 
463  {
464  if(NULL!= pos_)
465  { pos_ = pos_->prev_; }
466  return *this;
467  }
468 
470  operator -(int offset)
471  {
472  const SMLNode<Idx,T> *ptr = this->pos_;
473  for(int i=0; i <offset; --i)
474  {
475  if(NULL== ptr) { break; }
476  ptr = ptr->prev_;
477  }
478 
479  return const_iterator(ptr);
480  }
481  };
482 
486  iterator begin()
487  {
488  if(NULL!=front_){ return iterator(front_); }
489  else{ return iterator(&null_); }
490  }
491 
492  const_iterator begin() const
493  {
494  if(NULL!=front_){ return const_iterator(front_); }
495  else{ return const_iterator(&null_); }
496  }
497 
498  iterator end()
499  { return iterator(&null_); }
500 
501  const_iterator end() const
502  { return const_iterator(&null_); }
503 
507  public:
518  virtual bool sort(const std::vector<Idx> &arg_order);
519 
521  virtual bool sort_get_order(std::vector<Idx>& ret_order) const
522  {
523  if(flag_is_sorted_)
524  { ret_order = sorting_order_; return true; }
525  else
526  { return false; }
527  }
528 
530  virtual bool isSorted() const
531  { return flag_is_sorted_; }
532 
533  protected:
535  std::vector<Idx> sorting_order_;
536 
539  }; //End of class.
540 
548  template <typename Idx, typename T, bool ManageMemory>
549  class CMappedPointerList : public CMappedList<Idx,T*>
550  {
551  public:
552  typedef typename CMappedList<Idx,T*>::iterator iterator;
553  typedef typename CMappedList<Idx,T*>::const_iterator const_iterator;
554 
555  virtual ~CMappedPointerList()
556  {
557  if(ManageMemory)
558  {
559  typename CMappedList<Idx,T*>::iterator it,ite;
560  for(it = this->begin(), ite = this->end();
561  it!=ite; ++it)
562  {
563  T*& tmp = *it;
564  delete tmp;
565  tmp = NULL;
566  }
567  }
568  }
569  };
570 
571  template <typename Idx, typename T>
573  {//Deep copy.
574  //No need if both are identical
575  if(this == arg_pmap)
576  { return true; }
577 
578  this->~CMappedList(); //Delete everything in the mappedlist
579 
581  if(0 == arg_pmap->size_)
582  {
583  front_ = NULL; back_ = NULL; null_.prev_ = NULL;
584  map_.clear();
585  size_ = 0;
586  }
587  else
588  {
590  for(it = arg_pmap->begin(), ite = arg_pmap->end(); it!=ite; ++it)
591  {
592  T* tmp = CMappedList<Idx,T>::create(!it,*it,false);
593  if(NULL == tmp)
594  {
595 #ifdef DEBUG
596  std::cerr<<"\nCMappedList<Idx,T>::CMappedList(const CMappedList<Idx,T>& arg_pmap) : ";
597  std::cerr<<"ERROR : Deep copy failed to duplicate a node. Resetting mappedlist.";
598 #endif
599  this->~CMappedList();//Reset the mappedlist.
600  return false;
601  }
602  }
603  }
604  return true;
605  }
606 
607  template <typename Idx, typename T>
609  {
610  SMLNode<Idx,T> *t;
611 
612  //Nothing to do if already empty
613  if(0==size_) { return; }
614 
615  //Terminate the end (just in case)
616  null_.next_ = NULL;
617 
618  //Now start deleting everything
619  t = front_->next_;
620  while(NULL!=t)
621  {
622  if(NULL!=t->prev_->data_)
623  { delete t->prev_->data_; }
624  if(NULL!=t->prev_->id_)
625  { delete t->prev_->id_; }
626  delete t->prev_;
627 
628  t = t->next_;
629  }
630 
631  front_ = NULL; back_ = NULL; null_.prev_ = NULL;
632  map_.clear();
633  size_ = 0;
634  flag_is_sorted_ = false;
635  }
636 
637  template <typename Idx, typename T>
639  {
640  CMappedList<Idx,T>::const_iterator it, ite, it2, it2e;
641  for(it = begin(), ite = end(),
642  it2 = rhs.begin(), it2e = rhs.end();
643  it!=ite && it2!=it2e; ++it, ++it2)
644  {
645  if(*it != *it2)
646  { return false; }
647  }
648  //Would exit loop if atleast one was at the end.
649  //If both are at the end then the two mapped lists are equal
650  if((it == ite) && (it2 == it2e))
651  { return true; }
652  //If both aren't at the end, return false;
653  else
654  { return false; }
655  }
656 
657  template <typename Idx, typename T>
659  { return !(*this == rhs);}
660 
661 
662  template <typename Idx, typename T>
664  {
665  //Be lazy
666  CMappedList<Idx,T> *lhs, *rhs;
667  if(size_ >= arg_swap_obj.size_)
668  { lhs = this; rhs = &arg_swap_obj; }
669  else
670  { lhs = &arg_swap_obj; rhs = this; }
671 
672  //List status.
673  SMLNode<Idx,T> *tf = lhs->front_;
674  SMLNode<Idx,T> *tb = lhs->back_;
675  std::map<Idx, SMLNode<Idx,T>*> tmap(lhs->map_);
676  size_t ts = lhs->size_;
677 
678  //Sorting status
679  bool tmp_lhs_sorted = lhs->flag_is_sorted_;
680  std::vector<Idx> tmp_lhs_sorting_order;
681  if(lhs->flag_is_sorted_)
682  { tmp_lhs_sorting_order = lhs->sorting_order_; }
683 
684  //Copy list status
685  lhs->front_ = rhs->front_;
686  lhs->back_ = rhs->back_;
687  lhs->map_ = rhs->map_;
688  lhs->size_ = rhs->size_;
689  lhs->null_.prev_ = lhs->back_;
690  if(0 < lhs->size_) { lhs->back_->next_ = &(lhs->null_); }
691 
692  //Copy sorting status
693  lhs->flag_is_sorted_ = rhs->flag_is_sorted_;
694  if(lhs->flag_is_sorted_)
695  { lhs->sorting_order_ = rhs->sorting_order_; }
696  else { lhs->sorting_order_.clear(); }
697 
698  //Copy list status from tmp
699  rhs->front_ = tf;
700  rhs->back_ = tb;
701  rhs->map_ = tmap;
702  rhs->size_ = ts;
703  rhs->null_.prev_ = rhs->back_;
704  if(0 < rhs->size_) { rhs->back_->next_ = &(rhs->null_); }
705 
706  //Copy sorting status from tmp
707  rhs->flag_is_sorted_ = tmp_lhs_sorted;
708  if(rhs->flag_is_sorted_)
709  { rhs->sorting_order_ = tmp_lhs_sorting_order; }
710  else { rhs->sorting_order_.clear(); }
711  }
712 
713  template <typename Idx, typename T>
714  T* CMappedList<Idx,T>::create(const Idx & arg_idx, const bool insert_at_start)
715  { return CMappedList<Idx,T>::create(arg_idx,T(),insert_at_start); }
716 
717  template <typename Idx, typename T>
718  T* CMappedList<Idx,T>::create(const Idx & arg_idx, const T& arg_t, const bool insert_at_start)
719  {
720  SMLNode<Idx,T> * tmp = new SMLNode<Idx,T>();
721 
722  if(NULL==tmp) //Memory not allocated
723  { return NULL; }
724 
725  //Make sure the idx hasn't already been registered.
726  if(map_.find(arg_idx) != map_.end())
727  {
728 #ifdef DEBUG
729  std::cerr<<"\nCMappedList<Idx,T>::create() ERROR : Idx exists. Tried to add duplicate entry";
730 #endif
731  return NULL;
732  }
733 
734  tmp->data_ = new T(arg_t);
735  tmp->id_ = new Idx(arg_idx);
736 
738  if(0 == size_)
739  {
740  back_ = tmp;
741  front_ = tmp;
742  front_->prev_ = NULL;
743  front_->next_ = &null_;
744  null_.prev_ = back_;
745  tmp = NULL;
746  }
747  else if(insert_at_start)
748  {
749  tmp->next_ = front_;
750  front_->prev_ = tmp;
751  front_ = tmp;
752  front_->prev_ = NULL;
753  tmp = NULL;
754  }
755  else
756  {
757  back_->next_ = tmp;
758  tmp->next_ = &null_;
759  tmp->prev_ = back_;
760  back_ = tmp;
761  null_.prev_ = back_;
762  back_->next_ = &null_;
763  tmp = NULL;
764  }
765 
766  size_++;
767 
768  map_.insert( std::pair<Idx, SMLNode<Idx,T> *>(arg_idx, front_) );
769  flag_is_sorted_ = false;
770 
771  return front_->data_;
772  }
773 
774  template <typename Idx, typename T>
775  T* CMappedList<Idx,T>::insert(const Idx & arg_idx, T* arg_t, const bool insert_at_start)
776  {
777  SMLNode<Idx,T> * tmp = new SMLNode<Idx,T>();
778 
779  if(NULL==tmp) //Memory not allocated
780  { return NULL; }
781 
782  //Make sure the idx hasn't already been registered.
783  if(map_.find(arg_idx) != map_.end())
784  {
785 #ifdef DEBUG
786  std::cerr<<"\nCMappedList<Idx,T>::create() ERROR : Idx exists. Tried to add duplicate entry";
787 #endif
788  return NULL;
789  }
790 
791  tmp->data_ = arg_t;
792  tmp->id_ = new Idx(arg_idx);
793 
795  if(0 == size_)
796  {
797  back_ = tmp;
798  front_ = tmp;
799  front_->prev_ = NULL;
800  front_->next_ = &null_;
801  null_.prev_ = back_;
802  tmp = NULL;
803  }
804  else if(insert_at_start)
805  {
806  tmp->next_ = front_;
807  front_->prev_ = tmp;
808  front_ = tmp;
809  front_->prev_ = NULL;
810  tmp = NULL;
811  }
812  else
813  {
814  back_->next_ = tmp;
815  tmp->next_ = &null_;
816  tmp->prev_ = back_;
817  back_ = tmp;
818  null_.prev_ = back_;
819  back_->next_ = &null_;
820  tmp = NULL;
821  }
822 
823  size_++;
824 
825  map_.insert( std::pair<Idx, SMLNode<Idx,T> *>(arg_idx, front_) );
826  flag_is_sorted_ = false;
827 
828  return front_->data_;
829  }
830 
831  template <typename Idx, typename T>
832  T* CMappedList<Idx,T>::at(const std::size_t arg_idx)
833  {
834  if(NULL==front_)
835  { return NULL; }
836  else
837  {
838  if(arg_idx > size_)
839  { return NULL; }
840  SMLNode<Idx,T> * t = front_;
841 
842  for(std::size_t i=0; i<arg_idx; ++i)
843  {
844 #ifdef DEBUG
845  assert(i<=size_);
846 #endif
847 
848  if(NULL==t)
849  { return NULL; }
850  t = t->next_;
851  }
852  if(NULL==t)
853  { return NULL; }
854 
855  return t->data_;
856  }
857  }
858 
859  template <typename Idx, typename T>
860  T* CMappedList<Idx,T>::at(const Idx & arg_idx)
861  {
862  if(NULL==front_)
863  { return NULL; }
864  else
865  {
866  if(map_.find(arg_idx) == map_.end())
867  {
868  return NULL;
869  }
870 
871  SMLNode<Idx,T> * t = map_[arg_idx];
872 
873  if(NULL==t)
874  { return NULL; }
875  else
876  { return t->data_; }
877  }
878  }
879 
880  template <typename Idx, typename T>
881  const Idx* CMappedList<Idx,T>::getIndexAt(const std::size_t arg_idx) const
882  {
883  if(NULL==front_)
884  { return NULL; }
885  else
886  {
887  if(arg_idx > size_)
888  { return NULL; }
889  SMLNode<Idx,T> * t = front_;
890 
891  for(std::size_t i=0; i<arg_idx; ++i)
892  {
893 #ifdef DEBUG
894  assert(i<=size_);
895 #endif
896 
897  if(NULL==t)
898  { return NULL; }
899  t = t->next_;
900  }
901  if(NULL==t)
902  { return NULL; }
903 
904  return t->id_;
905  }
906  }
907 
909  template <typename Idx, typename T>
910  int CMappedList<Idx,T>::getIndexNumericAt(const Idx& arg_idx) const
911  {
912  const T *tdes = at_const(arg_idx);
913  return getIndexNumericAt(tdes);
914  }
915 
917  template <typename Idx, typename T>
918  int CMappedList<Idx,T>::getIndexNumericAt(const T* const arg_node) const
919  {
920  if(NULL == arg_node) { return -1; }
921 
922  SMLNode<Idx,T> *t = front_;
923 
924  int idx = 0;
925  while(t->data_ != arg_node && NULL!=t)
926  { t = t->next_; idx++; }
927 
928  if(NULL == t){idx = -1;}
929 
930  return idx;
931  }
932 
933 
934  template <typename Idx, typename T>
935  const T* CMappedList<Idx,T>::at_const(const std::size_t arg_idx) const
936  {
937  if(NULL==front_)
938  { return NULL; }
939  else
940  {
941  if(arg_idx > size_)
942  { return NULL; }
943  const SMLNode<Idx,T> * t = front_;
944 
945  for(std::size_t i=0; i<arg_idx; ++i)
946  {
947 #ifdef DEBUG
948  assert(i<=size_);
949 #endif
950 
951  if(NULL==t)
952  { return NULL; }
953  t = t->next_;
954  }
955  if(NULL==t)
956  { return NULL; }
957 
958  return (const T*) t->data_;
959  }
960  }
961 
962  template <typename Idx, typename T>
963  const T* CMappedList<Idx,T>::at_const(const Idx & arg_idx) const
964  {
965  if(NULL==front_)
966  { return NULL; }
967  else
968  {
969  if(map_.find(arg_idx) == map_.end())
970  {
971  return NULL;
972  }
973 
974  const SMLNode<Idx,T> * t = map_.at(arg_idx);
975 
976  if(NULL==t)
977  { return NULL; }
978  else
979  { return (const T*) t->data_; }
980  }
981  }
982 
983 
984  template <typename Idx, typename T>
986  {
987  if((NULL==front_) || (NULL==arg_t))
988  { return false; }
989 
990  SMLNode<Idx,T> * t, *tpre;
991 
992  //Head is a special case
993  if(front_->data_ == arg_t)
994  {
995  t = front_;
996  front_ = front_->next_;
997 
998  if(NULL!= t->data_)
999  {
1000  delete t->data_;
1001  if(NULL!= t->id_)
1002  {
1003  map_.erase(*(t->id_));
1004  delete t->id_;
1005  }
1006  delete t;
1007  size_--;
1008 
1009  if(0 == size_)
1010  { back_ = NULL; null_.prev_=NULL; }
1011 
1012  flag_is_sorted_ = false;
1013 
1014  return true; // Deleted head.
1015  }
1016 
1017  return false;//Head was NULL --> Error condition.
1018  }
1019  else
1020  {
1021  //The head doesn't match.
1022  tpre = front_;
1023  t = front_->next_;
1024 
1025  //Find the node
1026  while(NULL!=t)
1027  {
1028  if(t->data_ == arg_t)
1029  {
1030  tpre->next_ = t->next_;
1031  if(NULL!= t->data_)
1032  {
1033  delete t->data_;
1034  if(NULL!= t->id_)
1035  {
1036  map_.erase(*(t->id_));
1037  delete t->id_;
1038  }
1039  delete t;
1040  size_--;
1041 
1042  if(0 == size_)
1043  { back_ = NULL; null_.prev_=NULL; }
1044 
1045  flag_is_sorted_ = false;
1046 
1047  return true; // Deleted node.
1048  }
1049  else
1050  { return false; }//Node to delete was NULL --> Error condition.
1051  }
1052  tpre = t;
1053  t = t->next_;
1054  }
1055  return false; // Didn't delete anything.
1056  }//End of if/else
1057  }
1058 
1059 
1060 
1061  template <typename Idx, typename T>
1062  bool CMappedList<Idx,T>::erase(const Idx& arg_idx)
1063  {
1064  if(0>=size_)
1065  { return false; }
1066 
1067  //Make sure the node exists
1068  if(map_.find(arg_idx) == map_.end())
1069  {
1070 #ifdef DEBUG
1071  std::cerr<<"\nCMappedList<Idx,T>::erase() WARNING : Tried to erase a nonexistent entry";
1072 #endif
1073  return false;
1074  }
1075 
1076  SMLNode<Idx,T> * node = map_[arg_idx];
1077 
1078  if(1==size_)
1079  {
1080  if(front_!=node) { return false; } //This should never happen when Size 1 + idx exists.
1081 
1082  if(NULL!= front_->data_)
1083  { delete front_->data_; }
1084  if(NULL!= front_->id_)
1085  { delete front_->id_; }
1086  delete front_;
1087 
1088  front_ = NULL; back_ = NULL; null_.prev_ = NULL;
1089  node = NULL;
1090  }
1091  else
1092  {//At least two nodes. Remove the node
1093  if(NULL!= node->data_)
1094  { delete node->data_; }
1095  if(NULL!= node->id_)
1096  { delete node->id_; }
1097 
1098  if(front_ == node)
1099  {
1100  front_ = front_->next_;
1101  front_->prev_ = NULL;
1102  }
1103  else if(back_ == node)
1104  {
1105  back_ = back_->prev_;
1106  back_->next_ = &null_;
1107  null_.prev_ = back_;
1108  }
1109  else
1110  {
1111  node->prev_->next_ = node->next_;
1112  node->next_->prev_ = node->prev_;
1113  }
1114 
1115  delete node;
1116  node = NULL;
1117  }
1118 
1119  size_--;
1120  map_.erase(arg_idx);
1121  flag_is_sorted_ = false;
1122 
1123  return true; // Deleted head.
1124  }
1125 
1126  template <typename Idx, typename T>
1128  {
1129  SMLNode<Idx,T> *tpre;
1130  tpre = front_;
1131 
1132  if(tpre == NULL)
1133  {
1134  size_=0;
1135  flag_is_sorted_ = false;
1136  return true;
1137  } //Nothing in the list.
1138 
1139  front_ = front_->next_;
1140 
1141  while(&null_ != tpre)
1142  {
1143  if(NULL!=tpre->data_)
1144  { delete tpre->data_; }
1145  if(NULL!=tpre->id_)
1146  { delete tpre->id_; }
1147  delete tpre;
1148 
1149  tpre = front_;
1150  if(&null_ == tpre)//Reached the end.
1151  { break; }
1152  front_ = front_->next_;
1153  }
1154 
1155  size_=0;
1156  front_ = NULL; back_ = NULL; null_.prev_ = NULL;
1157  map_.clear(); // Clear the map.
1158  flag_is_sorted_ = false; //Not ordered anymore
1159  return true;
1160  }
1161 
1162  template <typename Idx, typename T>
1163  bool CMappedList<Idx,T>::sort(const std::vector<Idx> &arg_order)
1164  {
1165  if(1>=size_)
1166  {//Already sorted.
1167  flag_is_sorted_ = true;
1168  sorting_order_ = arg_order;
1169  return true;
1170  }//Now we know for sure that there are atleast 2 elements, and that front, and back are set.
1171 
1172  // Check that size matches the passed order
1173  if(arg_order.size() != size_)
1174  {
1175 #ifdef DEBUG
1176  std::cout<<"\nCMappedList<Idx,T>::sort() ERROR : Number of indices in passed order doesn't match mapped list.";
1177 #endif
1178  return false;
1179  }
1180 
1181  //First check all the indices actually appear in the list.
1182  typename std::vector<Idx>::const_iterator it,ite;
1183  for(it = arg_order.begin(), ite = arg_order.end(); it!=ite; ++it)
1184  {
1185  T* ptr = this->at(*it);
1186  if(NULL == ptr)
1187  {
1188 #ifdef DEBUG
1189  std::cout<<"\nCMappedList<Idx,T>::sort() ERROR : Passed order contains invalid index element.";
1190 #endif
1191  return false;
1192  }
1193  }
1194 
1195  // Verified that all the elements are in the mapped list
1196  // So proceed to insertion sort.
1197  for(it = arg_order.begin(), ite = arg_order.end(); it!=ite; ++it)
1198  {
1199  // Find the node, starting from idx 0 to end.
1200  SMLNode<Idx,T>* node = map_[*it]; //Idx is already tested to exist in the map.
1201 
1202  if(node==back_) { continue; } //Nothing to do. Node is already in the right place.
1203 
1204  // Detach the node from the list.
1205  if(node==front_)
1206  {
1207  node->next_->prev_ = NULL;
1208  front_ = node->next_;
1209  }
1210  else
1211  {
1212  node->prev_->next_ = node->next_;
1213  node->next_->prev_ = node->prev_;
1214  }
1215 
1216  // Re-add the node at the end.
1217  back_->next_ = node;
1218  node->prev_ = back_;
1219  back_ = node;
1220  null_.prev_ = node;
1221  node->next_ = &null_;
1222  }
1223 
1224  flag_is_sorted_ = true;
1225  sorting_order_ = arg_order;
1226  return true;
1227  }
1228 }
1229 
1230 #endif /* CMAPPEDLIST_HPP_ */
1231 
virtual bool sort(const std::vector< Idx > &arg_order)
Definition: CMappedList.hpp:1163
const_iterator(const const_iterator &other)
Definition: CMappedList.hpp:388
void swap(CMappedList< Idx, T > &arg_swap_obj)
Definition: CMappedList.hpp:663
virtual const Idx * getIndexAt(const std::size_t arg_idx) const
Definition: CMappedList.hpp:881
CMappedList(const CMappedList< Idx, T > &arg_pm)
Definition: CMappedList.hpp:128
virtual bool clear()
Definition: CMappedList.hpp:1127
bool operator!=(const CMappedList< Idx, T > &rhs)
Definition: CMappedList.hpp:658
virtual const T * at_const(const std::size_t arg_idx) const
Definition: CMappedList.hpp:935
std::vector< Idx > sorting_order_
Definition: CMappedList.hpp:535
CMappedList()
Definition: CMappedList.hpp:116
Definition: CMappedList.hpp:85
virtual CMappedList< Idx, T > & operator=(const CMappedList< Idx, T > &arg_rhs)
Definition: CMappedList.hpp:136
Definition: CMappedList.hpp:277
const_iterator & operator++()
Definition: CMappedList.hpp:431
bool operator==(const CMappedList< Idx, T > &rhs)
Definition: CMappedList.hpp:638
virtual bool deepCopy(const CMappedList< Idx, T > *const arg_pmap)
Definition: CMappedList.hpp:572
std::size_t size_
Definition: CMappedList.hpp:273
virtual bool sort_get_order(std::vector< Idx > &ret_order) const
Definition: CMappedList.hpp:521
size_t size_type
Definition: CMappedList.hpp:98
virtual T * at(const std::size_t arg_idx)
Definition: CMappedList.hpp:832
Definition: CMappedList.hpp:549
iterator & operator++()
Definition: CMappedList.hpp:328
virtual bool erase(T *arg_t)
Definition: CMappedList.hpp:985
SMLNode< Idx, T > null_
Definition: CMappedList.hpp:267
SMLNode< Idx, T > * front_
Definition: CMappedList.hpp:260
bool flag_is_sorted_
Definition: CMappedList.hpp:538
std::map< Idx, SMLNode< Idx, T > * > map_
Definition: CMappedList.hpp:270
iterator(const iterator &other)
Definition: CMappedList.hpp:287
Definition: SActuatorSetParsed.hpp:44
virtual bool empty() const
Definition: CMappedList.hpp:244
iterator begin()
Definition: CMappedList.hpp:486
Definition: CMappedList.hpp:46
virtual bool isSorted() const
Definition: CMappedList.hpp:530
SMLNode< Idx, T > * back_
Definition: CMappedList.hpp:263
virtual T * create(const Idx &arg_idx, const bool insert_at_start=true)
Definition: CMappedList.hpp:714
virtual T * operator[](const std::size_t arg_idx)
Definition: CMappedList.hpp:251
Definition: CMappedList.hpp:381
virtual T * insert(const Idx &arg_idx, T *arg_t, const bool insert_at_start=true)
Definition: CMappedList.hpp:775
const_iterator & operator--()
Definition: CMappedList.hpp:462
iterator & operator--()
Definition: CMappedList.hpp:359
virtual std::size_t size() const
Definition: CMappedList.hpp:240
virtual ~CMappedList()
Definition: CMappedList.hpp:608
virtual int getIndexNumericAt(const Idx &arg_idx) const
Definition: CMappedList.hpp:910