29 #ifndef CMAPPEDLIST_HPP_
30 #define CMAPPEDLIST_HPP_
45 template <
typename IdxS,
typename TS>
84 template <
typename Idx,
typename T>
99 typedef ptrdiff_t difference_type;
110 class const_iterator;
170 virtual T*
create(
const Idx & arg_idx,
const bool insert_at_start=
true);
177 virtual T*
create(
const Idx & arg_idx,
const T& arg_t,
const bool insert_at_start=
true);
185 virtual T*
insert(
const Idx & arg_idx, T* arg_t,
const bool insert_at_start=
true);
192 virtual T*
at(
const std::size_t arg_idx);
197 virtual T*
at(
const Idx & arg_idx);
203 virtual const Idx*
getIndexAt(
const std::size_t arg_idx)
const;
222 virtual const T*
at_const(
const std::size_t arg_idx)
const;
227 virtual const T*
at_const(
const Idx & arg_idx)
const;
231 virtual bool erase(T* arg_t);
237 virtual bool erase(
const Idx& arg_idx);
240 virtual std::size_t
size()
const
245 {
return (
size_ == 0); }
248 virtual bool clear();
252 {
return at(arg_idx); }
270 std::map<Idx, SMLNode<Idx,T>*>
map_;
277 class iterator :
public std::iterator<std::forward_iterator_tag, T>
288 { pos_ = other.pos_; }
294 operator = (
const iterator& other)
295 { pos_ = other.pos_;
return (*
this); }
298 operator == (
const iterator& other)
299 {
return (pos_ == other.pos_); }
302 operator != (
const iterator& other)
303 {
return (pos_ != other.pos_); }
307 {
return *(pos_->data_); }
311 {
return pos_->data_; }
315 {
return *(pos_->id_); }
321 if(NULL!= pos_->next_)
322 { pos_ = pos_->next_; }
330 if(NULL!= pos_->next_)
331 { pos_ = pos_->next_; }
336 operator +(
int offset)
339 for(
int i=0; i <offset; ++i)
341 if(NULL== ptr->next_) {
break; }
345 return iterator(ptr);
353 { pos_ = pos_->prev_; }
362 { pos_ = pos_->prev_; }
367 operator -(
int offset)
370 for(
int i=0; i < offset; ++i)
372 if(NULL== ptr->prev_) {
break; }
376 return iterator(ptr);
389 { pos_ = other.pos_; }
394 const const_iterator& operator = (
const const_iterator& other)
395 { pos_ = other.pos_;
return (*
this); }
397 const const_iterator& operator = (
const iterator& other)
398 { pos_ =
static_cast<const SMLNode<Idx,T> *
>(other.pos_);
return (*
this); }
401 operator == (
const const_iterator& other)
402 {
return (pos_ == other.pos_); }
405 operator != (
const const_iterator& other)
406 {
return (pos_ != other.pos_); }
410 {
return *(pos_->data_); }
414 {
return pos_->data_; }
418 {
return *(pos_->id_); }
424 if(NULL!= pos_->next_)
425 { pos_ = pos_->next_; }
433 if(NULL!= pos_->next_)
434 { pos_ = pos_->next_; }
439 operator +(
int offset)
442 for(
int i=0; i <offset; ++i)
444 if(NULL== ptr->next_) {
break; }
448 return const_iterator(ptr);
456 { pos_ = pos_->prev_; }
465 { pos_ = pos_->prev_; }
470 operator -(
int offset)
473 for(
int i=0; i <offset; --i)
475 if(NULL== ptr) {
break; }
479 return const_iterator(ptr);
489 else{
return iterator(&
null_); }
492 const_iterator
begin()
const
495 else{
return const_iterator(&
null_); }
499 {
return iterator(&
null_); }
501 const_iterator end()
const
502 {
return const_iterator(&
null_); }
518 virtual bool sort(
const std::vector<Idx> &arg_order);
548 template <
typename Idx,
typename T,
bool ManageMemory>
560 for(it = this->
begin(), ite = this->end();
571 template <
typename Idx,
typename T>
581 if(0 == arg_pmap->
size_)
583 front_ = NULL; back_ = NULL; null_.prev_ = NULL;
590 for(it = arg_pmap->
begin(), ite = arg_pmap->end(); it!=ite; ++it)
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.";
607 template <
typename Idx,
typename T>
613 if(0==size_) {
return; }
622 if(NULL!=t->prev_->data_)
623 {
delete t->prev_->data_; }
624 if(NULL!=t->prev_->id_)
625 {
delete t->prev_->id_; }
631 front_ = NULL; back_ = NULL; null_.prev_ = NULL;
634 flag_is_sorted_ =
false;
637 template <
typename Idx,
typename T>
641 for(it = begin(), ite = end(),
642 it2 = rhs.
begin(), it2e = rhs.end();
643 it!=ite && it2!=it2e; ++it, ++it2)
650 if((it == ite) && (it2 == it2e))
657 template <
typename Idx,
typename T>
659 {
return !(*
this == rhs);}
662 template <
typename Idx,
typename T>
667 if(size_ >= arg_swap_obj.
size_)
668 { lhs =
this; rhs = &arg_swap_obj; }
670 { lhs = &arg_swap_obj; rhs =
this; }
675 std::map<Idx, SMLNode<Idx,T>*> tmap(lhs->
map_);
676 size_t ts = lhs->
size_;
680 std::vector<Idx> tmp_lhs_sorting_order;
713 template <
typename Idx,
typename T>
717 template <
typename Idx,
typename T>
726 if(map_.find(arg_idx) != map_.end())
729 std::cerr<<
"\nCMappedList<Idx,T>::create() ERROR : Idx exists. Tried to add duplicate entry";
734 tmp->data_ =
new T(arg_t);
735 tmp->id_ =
new Idx(arg_idx);
742 front_->prev_ = NULL;
743 front_->next_ = &null_;
747 else if(insert_at_start)
752 front_->prev_ = NULL;
762 back_->next_ = &null_;
769 flag_is_sorted_ =
false;
771 return front_->data_;
774 template <
typename Idx,
typename T>
783 if(map_.find(arg_idx) != map_.end())
786 std::cerr<<
"\nCMappedList<Idx,T>::create() ERROR : Idx exists. Tried to add duplicate entry";
792 tmp->id_ =
new Idx(arg_idx);
799 front_->prev_ = NULL;
800 front_->next_ = &null_;
804 else if(insert_at_start)
809 front_->prev_ = NULL;
819 back_->next_ = &null_;
826 flag_is_sorted_ =
false;
828 return front_->data_;
831 template <
typename Idx,
typename T>
842 for(std::size_t i=0; i<arg_idx; ++i)
859 template <
typename Idx,
typename T>
866 if(map_.find(arg_idx) == map_.end())
880 template <
typename Idx,
typename T>
891 for(std::size_t i=0; i<arg_idx; ++i)
909 template <
typename Idx,
typename T>
912 const T *tdes = at_const(arg_idx);
913 return getIndexNumericAt(tdes);
917 template <
typename Idx,
typename T>
920 if(NULL == arg_node) {
return -1; }
925 while(t->data_ != arg_node && NULL!=t)
926 { t = t->next_; idx++; }
928 if(NULL == t){idx = -1;}
934 template <
typename Idx,
typename T>
945 for(std::size_t i=0; i<arg_idx; ++i)
958 return (
const T*) t->data_;
962 template <
typename Idx,
typename T>
969 if(map_.find(arg_idx) == map_.end())
979 {
return (
const T*) t->data_; }
984 template <
typename Idx,
typename T>
987 if((NULL==front_) || (NULL==arg_t))
993 if(front_->data_ == arg_t)
996 front_ = front_->next_;
1003 map_.erase(*(t->id_));
1010 { back_ = NULL; null_.prev_=NULL; }
1012 flag_is_sorted_ =
false;
1028 if(t->data_ == arg_t)
1030 tpre->next_ = t->next_;
1036 map_.erase(*(t->id_));
1043 { back_ = NULL; null_.prev_=NULL; }
1045 flag_is_sorted_ =
false;
1061 template <
typename Idx,
typename T>
1068 if(map_.find(arg_idx) == map_.end())
1071 std::cerr<<
"\nCMappedList<Idx,T>::erase() WARNING : Tried to erase a nonexistent entry";
1080 if(front_!=node) {
return false; }
1082 if(NULL!= front_->data_)
1083 {
delete front_->data_; }
1084 if(NULL!= front_->id_)
1085 {
delete front_->id_; }
1088 front_ = NULL; back_ = NULL; null_.prev_ = NULL;
1093 if(NULL!= node->data_)
1094 {
delete node->data_; }
1095 if(NULL!= node->id_)
1096 {
delete node->id_; }
1100 front_ = front_->next_;
1101 front_->prev_ = NULL;
1103 else if(back_ == node)
1105 back_ = back_->prev_;
1106 back_->next_ = &null_;
1107 null_.prev_ = back_;
1111 node->prev_->next_ = node->next_;
1112 node->next_->prev_ = node->prev_;
1120 map_.erase(arg_idx);
1121 flag_is_sorted_ =
false;
1126 template <
typename Idx,
typename T>
1135 flag_is_sorted_ =
false;
1139 front_ = front_->next_;
1141 while(&null_ != tpre)
1143 if(NULL!=tpre->data_)
1144 {
delete tpre->data_; }
1146 {
delete tpre->id_; }
1152 front_ = front_->next_;
1156 front_ = NULL; back_ = NULL; null_.prev_ = NULL;
1158 flag_is_sorted_ =
false;
1162 template <
typename Idx,
typename T>
1167 flag_is_sorted_ =
true;
1168 sorting_order_ = arg_order;
1173 if(arg_order.size() != size_)
1176 std::cout<<
"\nCMappedList<Idx,T>::sort() ERROR : Number of indices in passed order doesn't match mapped list.";
1182 typename std::vector<Idx>::const_iterator it,ite;
1183 for(it = arg_order.begin(), ite = arg_order.end(); it!=ite; ++it)
1185 T* ptr = this->at(*it);
1189 std::cout<<
"\nCMappedList<Idx,T>::sort() ERROR : Passed order contains invalid index element.";
1197 for(it = arg_order.begin(), ite = arg_order.end(); it!=ite; ++it)
1202 if(node==back_) {
continue; }
1207 node->next_->prev_ = NULL;
1208 front_ = node->next_;
1212 node->prev_->next_ = node->next_;
1213 node->next_->prev_ = node->prev_;
1217 back_->next_ = node;
1218 node->prev_ = back_;
1221 node->next_ = &null_;
1224 flag_is_sorted_ =
true;
1225 sorting_order_ = arg_order;
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