commore  1.0.6-SNAPSHOT
All Classes Namespaces Functions Variables Typedefs Enumerations Pages
List.h
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 
6 #ifndef CMR_LIST_INCLUDED
7 #define CMR_LIST_INCLUDED
8 
9 #include "BaseList.h"
10 
11 namespace commore
12 {
23  template< class T, class A> class List : public BaseList
24  {
25  public:
30  {
31  // seems needed for old VC6 on windows ... probably a compiler bug!
32  typedef BaseList::iterator super;
33  public:
34  iterator(const BaseList::iterator& i) : super(i) {}
35  public:
40  {
41  return *(T*)get_data();
42  }
47  {
48  return (List<T, A>&)super::cont();
49  }
55  {
56  *this = cont().erase(*this);
57  return *this;
58  }
64  iterator& insert(const T& v)
65  {
66  iterator i = cont().insert(*this, v);
67  i++;
68  *this = i;
69  return *this;
70  }
75  T& insert()
76  {
77  iterator i = cont().insert(*this);
78  T& r = *i;
79  i++;
80  *this = i;
81  return r;
82  }
88  iterator& add(const T& v)
89  {
90  iterator i = *this;
91  i++;
92  *this = cont().insert(i, v);
93  return *this;
94  }
99  T& add()
100  {
101  iterator i = *this;
102  i++;
103  *this = cont().insert(i);
104  return **this;
105  }
106  };
107 
112  {
113  // seems needed for old VC6 on windows ... probably a compiler bug!
115  public:
116  const_iterator(const super& i)
117  : super(i)
118  {}
119 
120  public:
124  const T& operator *()
125  {
126  return *(const T*)get_data();
127  }
128  };
129 
130  public:
131  List() {}
132  List(const List<T, A>& original)
133  {
134  insert(begin(), original.begin(), original.end());
135  }
136  List<T, A>& operator=(const List<T, A>& l)
137  {
138  return assign(l.begin(), l.end());
139  }
140  ~List()
141  {
142  clear();
143  }
144 
145  public:
146  typedef T value_type;
147 
148  public:
149 
154  {
155  return (iterator)BaseList::begin();
156  }
161  {
162  return (iterator)BaseList::end();
163  }
168  {
170  }
175  {
176  return (const_iterator)BaseList::end();
177  }
183  {
184  return begin();
185  }
191  {
192  return end();
193  }
194 
198  T& front()
199  {
201  return *begin();
202  }
207  const T& front() const
208  {
209  return *begin();
210  }
215  T& back()
216  {
217  return *(--end());
218  }
223  const T& back() const
224  {
225  return *(--end());
226  }
233  iterator insert(iterator pos, const T& value)
234  {
235  return BaseList::insert(pos, sizeof(T), A::constructor, (const Byte*)&value);
236  }
243  {
244  return BaseList::insert(pos, sizeof(T), A::constructor);
245  }
252  iterator insert(size_t pos, const T& value)
253  {
254  return BaseList::insert(pos, sizeof(T), A::constructor, (const Byte*)&value);
255  }
261  iterator insert(size_t pos)
262  {
263  return BaseList::insert(pos, sizeof(T), A::constructor);
264  }
272  {
273  BaseList::insert(pos, first, last, sizeof(T), A::constructor);
274  }
279  void push_back(const T& value)
280  {
281  BaseList::insert(BaseList::end(), sizeof(T), A::constructor, (const Byte*)&value);
282  }
287  void push_front(const T& value)
288  {
289  insert(begin(), value);
290  }
294  void pop_front()
295  {
296  BaseList::erase(begin(), A::destructor);
297  }
301  void pop_back()
302  {
303  iterator tmp = end();
304  BaseList::erase(--tmp , A::destructor);
305  }
311  T& push_back_once(const T& value)
312  {
313  return *(T*)BaseList::push_back_once(sizeof(T), A::constructor, A::comparator, (const Byte*)&value);
314  }
319  T& add()
320  {
322  Byte* data = BaseList::insert(pos, sizeof(T), A::constructor).get_data();
323  return *(T*)data;
324  }
330  T& add(const T& value)
331  {
332  return *(T*)BaseList::insert(BaseList::end(), sizeof(T), A::constructor, (const Byte*)&value).get_data();
333  }
339  bool remove_at(size_t i)
340  {
341  return BaseList::remove_at(i, A::destructor);
342  }
349  {
350  return (iterator)BaseList::erase(pos, A::destructor);
351  }
359  {
360  return (iterator)BaseList::erase(first, last, A::destructor);
361  }
367  List<T, A>& remove(const T& value)
368  {
369  return (List<T, A>&)BaseList::remove((const Byte*)&value, A::comparator, A::destructor);
370  }
376  List<T, A>& remove(const T* pvalue)
377  {
378  return (List<T, A>&)BaseList::remove((const Byte*)pvalue, A::comparator, A::destructor);
379  }
385  List<T, A>& remove_if(bool (*pred)(const T&))
386  {
387  return (List<T, A>&)BaseList::remove((FunctionFilter)pred, A::destructor);
388  }
394  {
395  return (List<T, A>&)BaseList::unique(A::comparator, A::destructor);
396  }
402  List<T, A>& unique(int (*pred)(const T&, const T&))
403  {
404  return (List<T, A>&)BaseList::unique((FunctionComparator)pred, A::destructor);
405  }
412  {
413  return merge(original, A::compare);
414  }
421  List<T, A>& merge(List<T, A>& original, int (*comp)(const T&, const T&))
422  {
423  return (List<T, A>&)BaseList::merge(original, (FunctionComparator)comp);
424  }
430  {
431  return (List<T, A>&)BaseList::sort(A::comparator);
432  }
438  List<T, A>& sort(int (*comp)(const T&, const T&))
439  {
440  return (List<T, A>&)BaseList::sort((FunctionComparator)comp);
441  }
447  {
448  return (List<T, A>&)BaseList::reverse();
449  }
455  const_iterator get_at(size_t i) const
456  {
457  return (const_iterator)BaseList::get_at(i);
458  }
464  iterator get_at(size_t i)
465  {
466  return (iterator)BaseList::get_at(i);
467  }
473  bool find(const T& value) const
474  {
475  return BaseList::find((const Byte*)&value, A::comparator);
476  }
482  bool find(bool (*pred)(const T&)) const
483  {
484  return BaseList::find((FunctionFilter)pred);
485  }
494  int compare(const List<T, A>& l) const
495  {
496  return BaseList::compare(l, A::comparator);
497  }
501  bool operator== (const List<T, A>& l) const
502  {
503  return BaseList::compare(l, A::comparator) == 0;
504  }
508  bool operator< (const List<T, A>& l) const
509  {
510  return BaseList::compare(l, A::comparator) < 0;
511  }
515  bool operator> (const List<T, A>& l) const
516  {
517  return BaseList::compare(l, A::comparator) > 0;
518  }
519 
527  {
528  BaseList::assign(first, last, A::constructor, A::destructor, sizeof(T));
529  return *this;
530  }
537  {
538  return (List<T, A>&)BaseList::swap(original);
539  }
546  {
547  return (List<T, A>&)BaseList::splice(original, A::destructor);
548  }
556  {
557  return (List<T, A>&)BaseList::splice(pos, original);
558  }
562  void clear()
563  {
564  BaseList::clear(A::destructor);
565  }
566 
567  // TODO: currently commented because do not work with old VC6 compiler !
568  // example: provide a conversion from List<T, CmrListAllocator<Tuple>> to List<Tuple, CmrListAllocator<Tuple>>, where T extends Tuple
569  /*
570  typedef A::value_type allocated_value_type;
571  operator List<typename allocated_value_type, A>&() { return (List<typename allocated_value_type, A>&)(*this); }
572  operator const List<typename allocated_value_type, A>&() { return (const List<typename allocated_value_type, A>&)(*this); }
573  */
578  CMREXD long read_xml(IBStream& i);
583  CMREXD long write_xml(OBStream& o) const;
588  CMREXD int write(const Path& file) const;
593  CMREXD int read(const Path& file);
598  CMREXD int write_xml(const Path& file) const;
603  CMREXD int read_xml(const Path& file);
608  CMREXD AString& to_xml_string(AString& s) const;
614  CMREXD long from_xml_string(const AString& s);
615 
616  public:
623  static void transfer(iterator pos, iterator first, iterator last)
624  {
625  BaseList::transfer(pos, first, last);
626  }
627 
628  };
629 }
630 
631 inline bool __trueset(bool& b, bool v)
632 {
633  b = v;
634  return true;
635 }
636 
641 #define cmr_foreach(T, V, L) \
642  if (bool continue__ = true) \
643  for (commore::List<T>::iterator i__ = L.begin(); continue__ && i__; i__++) \
644  if (bool b__ = true) if (__trueset(continue__, false)) \
645  for (T& V = *i__;b__; b__ = false,continue__ = true)
646 
651 #define cmr_const_foreach(T, V, L) \
652  if (bool continue__ = true) \
653  for (commore::List<T>::const_iterator i__ = L.begin(); continue__ && i__; i__++) \
654  if (bool b__ = true) if (__trueset(continue__, false)) \
655  for (const T& V = *i__;b__; b__ = false,continue__ = true)
656 
657 
663 #define cmr_foreach_enum(T, V, L, I) \
664  if (bool continue__ = true) \
665  if (int I = 1) if (I--)\
666  for (commore::List<T>::iterator i__ = L.begin(); continue__ && i__; i__++, I++) \
667  if (bool b__ = true) if (__trueset(continue__, false))\
668  for (T& V = *i__;b__; b__ = false,continue__ = true)
669 
675 #define cmr_const_foreach_enum(T, V, L, I) \
676  if (bool continue__ = true) \
677  if (int I = 1) if (I--)\
678  for (commore::List<T>::const_iterator i__ = L.begin(); continue__ && i__; i__++, I++) \
679  if (bool b__ = true) if (__trueset(continue__, false))\
680  for (const T& V = *i__;b__; b__ = false,continue__ = true)
681 
682 
687 #define cmr_foreach_while(T, V, L, M) \
688  if (bool continue__ = true) \
689  for (commore::List<T>::iterator i__ = L.begin(); M && i__; i__++) \
690  if (bool b__ = true) if (__trueset(continue__, false)) \
691  for (T& V = *i__;b__; b__ = false,continue__ = true)
692 
693 #define cmr_const_foreach_while(T, V, L, M) \
694  if (bool continue__ = true) \
695  for (commore::List<T>::const_iterator i__ = L.begin(); M && i__; i__++) \
696  if (bool b__ = true) if (__trueset(continue__, false)) \
697  for (const T& V = *i__;b__; b__ = false,continue__ = true)
698 
699 
703 #define cmr_remove_where(T, V, L, M) \
704  if (bool keep__ = true) \
705  if (bool continue__ = true) \
706  for (commore::List<T>::iterator i__ = L.begin(); continue__ && i__; keep__ ? i__++ : i__.erase()) \
707  if (bool b__ = true) if (__trueset(continue__, false)) \
708  for (T& V = *i__;b__; b__ = false,continue__ = true,(keep__ = !(M)))\
709  if (M)
710 
714 #define cmr_move_where(T, V, S, D, M) \
715  if (bool keep__ = true) \
716  if (bool continue__ = true) \
717  for (commore::List<T>::iterator i__ = S.begin(); continue__ && i__; keep__ ? i__++ : D.move_before(D.end(), i__++)) \
718  if (bool b__ = true) if (__trueset(continue__, false)) \
719  for (T& V = *i__;b__; b__ = false,continue__ = true,(keep__ = !(M)))\
720  if (M)
721 
722 
723 #endif
void pop_front()
Definition: List.h:294
iterator insert(size_t pos)
Definition: List.h:261
const T & front() const
Definition: List.h:207
const_iterator cbegin() const
Definition: List.h:182
static void transfer(iterator pos, iterator first, iterator last)
Definition: List.h:623
Definition: BaseList.h:21
const_iterator get_at(size_t i) const
Definition: List.h:455
iterator insert(size_t pos, const T &value)
Definition: List.h:252
T & front()
Definition: List.h:198
Definition: AString.h:39
void clear()
Definition: List.h:562
List< T, A > & unique(int(*pred)(const T &, const T &))
Definition: List.h:402
const T & operator*()
Definition: List.h:124
Definition: List.h:23
T & push_back_once(const T &value)
Definition: List.h:311
iterator erase(iterator pos)
Definition: List.h:348
List< T, A > & swap(List< T, A > &original)
Definition: List.h:536
List< T, A > & merge(List< T, A > &original)
Definition: List.h:411
int compare(const List< T, A > &l) const
Definition: List.h:494
iterator erase(iterator first, iterator last)
Definition: List.h:358
List< T, A > & cont()
Definition: List.h:46
Definition: List.h:29
List< T, A > & sort()
Definition: List.h:429
iterator & erase()
Definition: List.h:54
CMREXD int write(const Path &file) const
void insert(iterator pos, const_iterator first, const_iterator last)
Definition: List.h:271
iterator begin()
CMREXD AString & to_xml_string(AString &s) const
T & back()
Definition: List.h:215
Definition: List.h:111
List< T, A > & unique()
Definition: List.h:393
iterator get_at(size_t i)
Definition: List.h:464
iterator insert(iterator pos)
Definition: List.h:242
const_iterator cend() const
Definition: List.h:190
List< T, A > & splice(List< T, A > &original)
Definition: List.h:545
bool find(const T &value) const
Definition: List.h:473
CMREXD int read(const Path &file)
T & operator*()
Definition: List.h:39
void pop_back()
Definition: List.h:301
bool operator>(const List< T, A > &l) const
Definition: List.h:515
T & insert()
Definition: List.h:75
iterator insert(iterator pos, const T &value)
Definition: List.h:233
List< T, A > & remove_if(bool(*pred)(const T &))
Definition: List.h:385
bool operator==(const List< T, A > &l) const
Definition: List.h:501
const_iterator begin() const
Definition: List.h:167
T & add()
Definition: List.h:99
Definition: Path.h:19
Definition: BaseList.h:92
T & add()
Definition: List.h:319
iterator begin()
Definition: List.h:153
List< T, A > & sort(int(*comp)(const T &, const T &))
Definition: List.h:438
CMREXD long from_xml_string(const AString &s)
List< T, A > & assign(const_iterator first, const_iterator last)
Definition: List.h:526
List< T, A > & merge(List< T, A > &original, int(*comp)(const T &, const T &))
Definition: List.h:421
T & add(const T &value)
Definition: List.h:330
CMREXD long write_xml(OBStream &o) const
List< T, A > & reverse()
Definition: List.h:446
iterator & insert(const T &v)
Definition: List.h:64
bool remove_at(size_t i)
Definition: List.h:339
unsigned char Byte
Definition: Type.h:64
iterator end()
Definition: List.h:160
Definition: IOBStream.h:166
void push_back(const T &value)
Definition: List.h:279
const_iterator end() const
Definition: List.h:174
void push_front(const T &value)
Definition: List.h:287
List< T, A > & splice(iterator pos, List< T, A > &original)
Definition: List.h:555
Definition: IOBStream.h:29
bool find(bool(*pred)(const T &)) const
Definition: List.h:482
Definition: BaseList.h:30
const T & back() const
Definition: List.h:223
iterator & add(const T &v)
Definition: List.h:88
CMREXD long read_xml(IBStream &i)