MGCL V10  V10
MGCL V10
 全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 フレンド グループ ページ
Plist.h
1 /********************************************************************/
2 /* Copyright (c) 2015 DG Technologies Inc. and Yuzi Mizuno */
3 /* All rights reserved. */
4 /********************************************************************/
5 #ifndef _MGPlist_HH_
6 #define _MGPlist_HH_
7 
8 // Plist.h
9 //
10 
11 #if _MSC_VER > 1000
12 #pragma once
13 #endif // _MSC_VER > 1000
14 
15 #include <algorithm>
16 #include <list>
17 #include "mg/MGCL.h"
18 
24 
28 //of MGPlist are transfered to MGPlist, and at the copy and assignment of MGPlist,
30 template <class T>
31 class MGPlist{
32 public:
35  typedef typename std::list<T*>::iterator iterator;
36  typedef typename std::list<T*>::const_iterator const_iterator;
37  typedef typename std::list<T*>::reverse_iterator reverse_iterator;
38  typedef typename std::list<T*>::const_reverse_iterator const_reverse_iterator;
39  typedef typename std::list<T*>::reference reference;
40  typedef typename std::list<T*>::const_reference const_reference;
41  typedef typename std::list<T*>::size_type size_type;
42 
44  std::list<T*> m_list;
45 
47  MG_DLL_DECLR friend std::ostream& operator<< (std::ostream& out, const MGPlist<T>& lst);
48 
50 
52  MGPlist(){;}
53 
59  MGPlist(const MGPlist<T>& rhs) : m_list(rhs.m_list){
60  MGPlist<T>& rhsp = const_cast<MGPlist<T>&>(rhs);
61  rhsp.m_list.clear();
62  }
63 
65  explicit MGPlist(size_type n) : m_list(n, 0){;}
66 
68  template <class InputIter>
69  MGPlist(InputIter first, InputIter last){
70  insert(begin(), first, last);
71  }
72 
75  ~MGPlist();
76 
78 
85  MGPlist<T>& operator=(const MGPlist<T>& rhs);
86 
88 
91  template <class InputIter>
92  void assign(InputIter first, InputIter last){
93  iterator cur= begin(), finish = end();
94  for(; first != last && cur != finish; ++first, ++cur){
95  delete *cur; *cur = *first;
96  }
97  (first == last) ? erase(cur, finish) : insert(finish, first, last);
98  }
99 
102  const_reference back() const{
103  return m_list.back();
104  }
105  reference back(){
106  return m_list.back();
107  }
108 
110  const_iterator begin() const{
111  return m_list.begin();
112  }
113  iterator begin(){
114  return m_list.begin();
115  }
116 
118  void clear(){
119  iterator first = begin(), last = end();
120  for(; first != last; ++first)
121  delete *first;
122  m_list.clear();
123  }
124 
127  bool empty() const{
128  return m_list.empty();
129  }
130 
132  const_iterator end() const{
133  return m_list.end();
134  }
135  iterator end(){
136  return m_list.end();
137  }
138 
140  iterator erase(iterator x){
141  delete *x;
142  return m_list.erase(x);
143  }
145  iterator erase(iterator first, iterator last){
146  for(; first != last; ++first) delete *first;
147  return m_list.erase(first, last);
148  }
149 
152  const_iterator find(T* obj) const{
153  return std::find(begin(),end(),obj);
154  }
155  iterator find(T* obj){
156  return std::find(begin(),end(),obj);
157  }
158 
161  const_reference front() const{
162  return m_list.front();
163  }
164  reference front(){
165  return m_list.front();
166  }
167 
170  iterator insert(iterator it, T* x){
171  return m_list.insert(it,x);
172  }
173 
175  template <class InputIter>
176  void insert(iterator pos, InputIter first, InputIter last){
177  for(; first != last; ++first) insert(pos, *first);
178  }
179 
181  size_type max_size() const{
182  return m_list.max_size();
183  }
184 
186  void merge(MGPlist<T>& rhs){
187  m_list.merge(rhs.m_list);
188  }
189 
191  void pop_back(){
192  delete m_list.back();
193  m_list.pop_back();
194  }
195 
197  void pop_front(){
198  delete m_list.front();
199  m_list.pop_front();
200  }
201 
203  void push_back(T* x){
204  m_list.push_back(x);
205  }
207  insert(end(), x.begin(), x.end());
208  x.m_list.clear();
209  }
210 
212  void push_front(T* x){
213  m_list.push_front(x);
214  }
215 
217  const_reverse_iterator rbegin() const{
218  return m_list.rbegin();
219  }
220  reverse_iterator rbegin(){
221  return m_list.rbegin();
222  }
223 
225  const_reverse_iterator rend() const{
226  return m_list.rend();
227  }
228  reverse_iterator rend(){
229  return m_list.rend();
230  }
231 
234  iterator release(iterator i){
235  return m_list.erase(i);
236  }
237 
239  void remove(T* x){
240  m_list.remove(x);
241  }
242 
245  template <class Pred>
246  void remove_if(Pred pred){
247  iterator first = begin(), last = end();
248  while(first != last){
249  iterator next = first;
250  ++next;
251  if(pred(*first)) m_list.erase(first);
252  first = next;
253  }
254  }
255 
259  T* removeAt(iterator x){
260  T* ret = *x;
261  m_list.erase(x);
262  return ret;
263  }
264 
266  void reverse(){ m_list.reverse();}
267 
269  size_type size() const{ return m_list.size();}
270 
272  void sort(){ m_list.sort();}
273 
274  template<class Traits>
275  void sort(Traits _Comp){m_list.sort(_Comp);}
276 
277  void splice(iterator pos, MGPlist<T>& ls){
278  m_list.splice(pos, ls.m_list);
279  }
280  void splice(iterator pos, MGPlist<T>& ls, iterator i){
281  m_list.splice(pos, ls.m_list, i);
282  }
283  void splice(iterator pos, MGPlist<T>& ls, iterator first, iterator last){
284  m_list.splice(pos, ls.m_list, first, last);
285  }
286 
288  void swap(MGPlist<T>& x){
289  m_list.swap(x.m_list);
290  }
291 
293  void unique(){ m_list.unique();}
294 };
295 
297 
298 template <class T>
300  iterator first = begin(), last = end();
301  for(; first != last; ++first) delete *first;
302 }
303 
304 template <class T>
306  if(this != &rhs){
307  clear();
308  m_list = rhs.m_list;
309  MGPlist<T>& rhsp=const_cast<MGPlist<T>&>(rhs);
310  rhsp.m_list.clear();
311  }
312  return *this;
313 }
314 
315 template <class T>
316 std::ostream& operator<< (std::ostream& out, const MGPlist<T>& lst){
317  out << "MGPlist<T>::";
318  int n=lst.size();
319  out<<"number of entries="<<n<<std::endl;
320  MGPlist<T>::const_iterator itr; int i=0;
321  for(itr=lst.begin(); itr!=lst.end(); itr++){
322  out<<i++<<":"<<(*itr)<<std::endl;
323  if(*itr) out << (**itr) << std::endl;
324  }
325  return out;
326 }
327  // end of BASE group
329 #endif
void pop_front()
pop first element.
Definition: Plist.h:197
iterator end()
Definition: Plist.h:135
std::list< T * >::iterator iterator
Definition: Plist.h:35
bool empty() const
Definition: Plist.h:127
std::list< T * >::size_type size_type
Definition: Plist.h:41
MGPlist()
Constructors.
Definition: Plist.h:52
const_reverse_iterator rend() const
Return const_reverse_iterator at the end of list.
Definition: Plist.h:225
T * removeAt(iterator x)
Definition: Plist.h:259
std::list< T * > m_list
Member Data.
Definition: Plist.h:44
std::list< T * >::reverse_iterator reverse_iterator
Definition: Plist.h:37
void clear()
clear list, that is, erase all the elements in the list.
Definition: Plist.h:118
const_iterator find(T *obj) const
Definition: Plist.h:152
Defines List of newed object pointers.
Definition: Plist.h:31
void splice(iterator pos, MGPlist< T > &ls, iterator first, iterator last)
Definition: Plist.h:283
const_reference back() const
Definition: Plist.h:102
std::list< T * >::const_reverse_iterator const_reverse_iterator
Definition: Plist.h:38
MGPlist< T > & operator=(const MGPlist< T > &rhs)
operator overload
Definition: Plist.h:305
std::list< T * >::const_iterator const_iterator
Definition: Plist.h:36
void reverse()
reverse sequence.
Definition: Plist.h:266
void sort(Traits _Comp)
Definition: Plist.h:275
reference back()
Definition: Plist.h:105
void insert(iterator pos, InputIter first, InputIter last)
Insert given range denoted by [first, last) into one before pos.
Definition: Plist.h:176
const_iterator end() const
Return const_iterator at the end of list.
Definition: Plist.h:132
MGPlist(const MGPlist< T > &rhs)
Definition: Plist.h:59
size_type max_size() const
Returns the size of maximum size.
Definition: Plist.h:181
iterator find(T *obj)
Definition: Plist.h:155
void merge(MGPlist< T > &rhs)
Equivalent to call std::list::merge().
Definition: Plist.h:186
iterator erase(iterator x)
erase element x.
Definition: Plist.h:140
iterator release(iterator i)
Definition: Plist.h:234
void push_back(T *x)
push element x at the end.
Definition: Plist.h:203
MGPlist(size_type n)
Create a list whose size is n.
Definition: Plist.h:65
void unique()
Equivalent to call std::list::unique().
Definition: Plist.h:293
void splice(iterator pos, MGPlist< T > &ls)
Definition: Plist.h:277
reference front()
Definition: Plist.h:164
reverse_iterator rend()
Definition: Plist.h:228
void sort()
Equivalent to call std::list::sort().
Definition: Plist.h:272
~MGPlist()
Destroy the object and delete all pointers that this holds.
Definition: Plist.h:299
void remove_if(Pred pred)
Definition: Plist.h:246
std::list< T * >::const_reference const_reference
Definition: Plist.h:40
std::list< T * >::reference reference
Definition: Plist.h:39
size_type size() const
Return the number of items that are in the list.
Definition: Plist.h:269
iterator insert(iterator it, T *x)
Definition: Plist.h:170
void swap(MGPlist< T > &x)
Equivalent to call std::list::swap().
Definition: Plist.h:288
MGPlist(InputIter first, InputIter last)
Create a list with a range. The ownership will be transfered.
Definition: Plist.h:69
void push_back(MGPlist< T > &x)
Definition: Plist.h:206
void pop_back()
pop last element.
Definition: Plist.h:191
iterator erase(iterator first, iterator last)
erase sequence [first, last).
Definition: Plist.h:145
reverse_iterator rbegin()
Definition: Plist.h:220
void push_front(T *x)
push element x at the first.
Definition: Plist.h:212
const_reference front() const
Definition: Plist.h:161
void splice(iterator pos, MGPlist< T > &ls, iterator i)
Definition: Plist.h:280
const_iterator begin() const
Return const_iterator at the beginning of list.
Definition: Plist.h:110
iterator begin()
Definition: Plist.h:113
void assign(InputIter first, InputIter last)
member functions
Definition: Plist.h:92
const_reverse_iterator rbegin() const
Return const_reverse_iterator at the beginning of list.
Definition: Plist.h:217