MGCL V10  V10
MGCL V10
 全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 フレンド グループ ページ
Pvector.h
1 /********************************************************************/
2 /* Copyright (c) 2015 DG Technologies Inc. and Yuzi Mizuno */
3 /* All rights reserved. */
4 /********************************************************************/
5 #ifndef _MGPvector_HH_
6 #define _MGPvector_HH_
7 
8 #if _MSC_VER > 1000
9 #pragma once
10 #endif
11 
12 #include <algorithm>
13 #include <vector>
14 #include "mg/MGCL.h"
20 
24 //of MGPvector are transfered to MGPvector, and at the copy and assignment of MGPvector,
26 template <class T>
27 class MGPvector{
28 public:
31  typedef typename std::vector<T*>::iterator iterator;
32  typedef typename std::vector<T*>::const_iterator const_iterator;
33  typedef typename std::vector<T*>::reverse_iterator reverse_iterator;
34  typedef typename std::vector<T*>::const_reverse_iterator const_reverse_iterator;
35  typedef typename std::vector<T*>::reference reference;
36  typedef typename std::vector<T*>::const_reference const_reference;
37  typedef typename std::vector<T*>::size_type size_type;
38 
40  std::vector<T *> m_vector;
41 
43  template <class T>
44  MG_DLL_DECLR friend std::ostream& operator<< (std::ostream& out, const MGPvector<T>& vector);
45 
47 
49  MGPvector(){;}
50 
55  explicit MGPvector(size_type len) : m_vector(len, 0){;}
56 
58  MGPvector(T* ptr) : m_vector(1, ptr){;}
59 
65  MGPvector(const MGPvector<T>& rhs)
66  :m_vector(rhs.m_vector){
67  MGPvector<T>& rhsp=const_cast<MGPvector<T>&>(rhs);
68  rhsp.m_vector.clear();
69  }
70 
72  MGPvector(const std::vector<const T*>& rhs)
73  :m_vector(rhs.size()){
74  int n=rhs.size();
75  for(int i=0; i<n; i++){
76  m_vector[i]=new T(*(rhs[i]));
77  }
78  }
79 
81  template <class InputIter>
82  MGPvector(InputIter first, InputIter last){
83  insert(end(), first, last);
84  }
85 
87 
89  ~MGPvector();
90 
92 
94  const_reference operator[](size_type pos) const{
96  return m_vector[pos];
97  }
98  reference operator[](size_type pos){
100  return m_vector[pos];
101  }
102 
109  MGPvector<T>& operator=(const MGPvector<T>& rhs);
110 
112 
115  template <class InputIter>
116  void assign(InputIter first, InputIter last){
117  iterator cur= begin();
118  for(; first != last && cur != end(); ++first, ++cur){
119  delete *cur; *cur = *first;
120  }
121  (first == last) ? erase(cur, end()) : insert(end(), first, last);
122  }
123 
124  void assign(size_type pos, T* ptr){
125  delete m_vector[pos];
126  m_vector[pos]=ptr;
127  }
128 
130  const_reference at(size_type n) const{
131  return m_vector.at(n);
132  }
133  reference at(size_type n){
134  return m_vector.at(n);
135  }
136 
139  const_reference back() const{
140  return m_vector.back();
141  }
142  reference back(){
143  return m_vector.back();
144  }
145 
147  const_iterator begin() const{
148  return m_vector.begin();
149  }
151  iterator begin(){
152  return m_vector.begin();
153  }
154 
156  size_type capacity() const{
157  return m_vector.capacity();
158  }
159 
161  void clear(){
162  for(iterator i = begin(); i != end(); ++i)
163  delete *i;
164  m_vector.clear();
165  }
166 
169  bool empty() const{
170  return m_vector.empty();
171  }
172 
174  const_iterator end() const{
175  return m_vector.end();
176  }
178  iterator end(){
179  return m_vector.end();
180  }
181 
183  iterator erase(iterator x){
184  delete *x;
185  return m_vector.erase(x);
186  }
187 
189  iterator erase(size_type i){
190  iterator del = m_vector.begin() + i;
191  delete *del;
192  return m_vector.erase(del);
193  }
194 
196  iterator erase(iterator first, iterator last){
197  iterator cur = first;
198  for(; cur != last; ++cur) delete *cur;
199  return m_vector.erase(first, last);
200  }
201 
204  const_reference front() const{
205  return m_vector.front();
206  }
207  reference front(){
208  return m_vector.front();
209  }
210 
213  const_reference get(size_type i) const{
214  return this->operator[](i);
215  }
216  reference get(size_type i){
217  return this->operator[](i);
218  }
219 
221  iterator insert(iterator pos, T* ptr){
222  return m_vector.insert(pos, ptr);
223  }
225  template <class InputIter>
226  void insert(iterator pos, InputIter first, InputIter last){
227  for(; first != last; ++first){
228  pos = insert(pos, *first);
229  ++pos;
230  }
231  }
232 
234  bool is_null(size_type i)const{
235  return m_vector[i]==0;
236  }
237 
239  size_type max_size() const{
240  return m_vector.max_size();
241  }
242 
244  void pop_back(){
245  delete m_vector.back();
246  m_vector.pop_back();
247  }
248 
250  void push_back(T* x){
251  m_vector.push_back(x);
252  }
253 
258  m_vector.insert(m_vector.end(), dst.m_vector.begin(), dst.m_vector.end());
259  dst.m_vector.clear();
260  }
261 
263  const_reverse_iterator rbegin() const{
264  return m_vector.rbegin();
265  }
267  reverse_iterator rbegin(){
268  return m_vector.rbegin();
269  }
270 
273  const_reverse_iterator rend() const{
274  return m_vector.rend();
275  }
278  reverse_iterator rend(){
279  return m_vector.rend();
280  }
281 
285  T* release(size_type i){
286  T* ret = m_vector[i];
287  m_vector[i] = 0;
288  return ret;
289  }
293  void release_all(){
294  m_vector.assign(size(), static_cast<T*>(0));
295  }
296 
299  T* removeAt(iterator i){
300  T* ret = *i;
301  m_vector.erase(i);
302  return ret;
303  }
306  T* removeAt(size_type i){
307  T* ret = m_vector[i];
308  iterator del = m_vector.begin() + i;
309  m_vector.erase(del);
310  return ret;
311  }
312 
315  size_type i,n=size();
316  size_type nhalf=n/2;
317  for(i=0; i<nhalf; i++){
318  size_type nmim1=n-i-1;
319  T* pointer=m_vector[nmim1];
320  m_vector[nmim1]=m_vector[i];
321  m_vector[i]=pointer;
322  }
323  }
324 
327  void reserve(size_type n){
328  m_vector.reserve(n);
329  }
330 
332  void reset(size_type i, T* ptr = 0){
333  delete m_vector[i];
334  m_vector[i] = ptr;
335  }
336 
339  void resize(size_type n);
340 
342  size_type size() const{
343  return m_vector.size();
344  }
345 
346  void swap(MGPvector<T>& x){
347  m_vector.swap(x.m_vector);
348  }
349 };
350 
354 
355 template <class T>
357  for(iterator i = begin(); i != end(); ++i) delete *i;
358 }
359 
360 template <class T>
362  if(this != &rhs){
363  clear();
364  m_vector = rhs.m_vector;
365  MGPvector<T>& rhsp=const_cast<MGPvector<T>&>(rhs);
366  rhsp.m_vector.clear();
367  }
368  return *this;
369 }
370 
373 template <class T>
375  size_type oldn=m_vector.size();
376  if(oldn<n) m_vector.resize(n,0);
377  else if(oldn>n){
378  for(size_type i=n; i<oldn; i++) delete m_vector[i];
379  m_vector.resize(n);
380  }
381 }
382 
383 template <class T>
384 std::ostream& operator<< (std::ostream& out, const MGPvector<T>& vector){
385  out << "MGPvector<T>::";
386  int n=vector.size();
387  out<<"number of entries="<<n<<std::endl;
388  MGPvector<T>::const_iterator itr; int i=0;
389  for(itr=vector.begin(); itr!=vector.end(); itr++){
390  out<<i++<<":"<<(*itr)<<":";
391  if(*itr) out << (**itr) << std::endl;
392  }
393  return out;
394 }
395  // end of BASE group
397 #endif
const_iterator begin() const
Return const_iterator that points to the first element in the sequence.
Definition: Pvector.h:147
std::vector< T * >::reverse_iterator reverse_iterator
Definition: Pvector.h:33
bool empty() const
Definition: Pvector.h:169
MGPvector(const MGPvector< T > &rhs)
Definition: Pvector.h:65
MGPvector< T > & operator=(const MGPvector< T > &rhs)
Definition: Pvector.h:361
MGPvector(InputIter first, InputIter last)
Create a vector with given range [first, last).
Definition: Pvector.h:82
std::vector< T * >::size_type size_type
Definition: Pvector.h:37
const_iterator end() const
Return const_iterator that points one past the last element.
Definition: Pvector.h:174
T * removeAt(size_type i)
Definition: Pvector.h:306
void assign(size_type pos, T *ptr)
Definition: Pvector.h:124
std::vector< T * >::const_iterator const_iterator
Definition: Pvector.h:32
void push_back(MGPvector< T > &dst)
Definition: Pvector.h:257
MGPvector(size_type len)
Definition: Pvector.h:55
void assign(InputIter first, InputIter last)
Definition: Pvector.h:116
const_reference at(size_type n) const
Equivalent to call vector::at(n).
Definition: Pvector.h:130
const_reverse_iterator rbegin() const
Return const_reverse_iterator that points the end of the sequence.
Definition: Pvector.h:263
size_type size() const
Return the number of items that are in the sequence.
Definition: Pvector.h:342
const_reference front() const
Definition: Pvector.h:204
T * release(size_type i)
Definition: Pvector.h:285
void reset(size_type i, T *ptr=0)
delete the i-th element and replace it with ptr.
Definition: Pvector.h:332
void clear()
clear Sequence, that is, erase all the elements in the sequence.
Definition: Pvector.h:161
iterator insert(iterator pos, T *ptr)
Insert ptr into vector at pos.
Definition: Pvector.h:221
size_type capacity() const
Equivalent to call vector::capacity().
Definition: Pvector.h:156
MGPvector(T *ptr)
Construct MGPvector of length 1 that contains ptr.
Definition: Pvector.h:58
const_reference back() const
Definition: Pvector.h:139
size_type max_size() const
Returns the size of maximum size.
Definition: Pvector.h:239
iterator erase(size_type i)
erase i-th element x.
Definition: Pvector.h:189
T * removeAt(iterator i)
Definition: Pvector.h:299
std::vector< T * >::reference reference
Definition: Pvector.h:35
~MGPvector()
Destroy the object and delete all pointers that this object holds.
Definition: Pvector.h:356
reference front()
Definition: Pvector.h:207
std::vector< T * >::iterator iterator
Definition: Pvector.h:31
iterator begin()
Return iterator that points to the first element in the sequence.
Definition: Pvector.h:151
iterator end()
Return iterator that points one past the last element.
Definition: Pvector.h:178
void swap(MGPvector< T > &x)
Definition: Pvector.h:346
void push_back(T *x)
push element x at the end.
Definition: Pvector.h:250
MGPvector()
void constructor
Definition: Pvector.h:49
reference back()
Definition: Pvector.h:142
bool is_null(size_type i) const
Test if i-th element is null.
Definition: Pvector.h:234
reverse_iterator rbegin()
Return reverse_iterator that points the end of the sequence.
Definition: Pvector.h:267
reverse_iterator rend()
Definition: Pvector.h:278
reference operator[](size_type pos)
Definition: Pvector.h:98
void reverse_sequence()
reverse the sequence of the elements.
Definition: Pvector.h:314
const_reverse_iterator rend() const
Definition: Pvector.h:273
iterator erase(iterator x)
erase the element at x.
Definition: Pvector.h:183
void insert(iterator pos, InputIter first, InputIter last)
Insert the range [first, last) into the vector at pos.
Definition: Pvector.h:226
void pop_back()
pop last element.
Definition: Pvector.h:244
reference at(size_type n)
Definition: Pvector.h:133
std::vector< T * > m_vector
Member data.
Definition: Pvector.h:40
iterator erase(iterator first, iterator last)
erase sequence [first, last).
Definition: Pvector.h:196
MGPvector(const std::vector< const T * > &rhs)
Conversion constructor.
Definition: Pvector.h:72
void release_all()
Definition: Pvector.h:293
std::vector< T * >::const_reference const_reference
Definition: Pvector.h:36
Defines Vector of newed object pointers.
Definition: Pvector.h:27
void resize(size_type n)
Definition: Pvector.h:374
void reserve(size_type n)
Definition: Pvector.h:327
std::vector< T * >::const_reverse_iterator const_reverse_iterator
Definition: Pvector.h:34
const_reference operator[](size_type pos) const
Subscript access.
Definition: Pvector.h:94