WIL説明書(C++)  3.0.0
FvArrayAllocator.h
[詳解]
1 // $Revision: 1.1 $
2 /*
3  @file FvArrayAllocator.h
4  @brief CFvArrayAllocator template class
5  @author FAST Corporation
6 */
7 
8 #ifndef _FVARRAYALLOCATOR_H_INCLUDED_
9 #define _FVARRAYALLOCATOR_H_INCLUDED_
10 
11 #include "FvMacros.h"
12 #include "FvDefs.h"
13 #include "FvDebug.h"
14 #include "FvErrors.h"
15 #include "FvObject.h"
16 
17 #include "FvAllocator.h"
18 
19 #pragma pack(push,_FVCL_PACKING)
20 
21 namespace FVCL
22 {
23 
24 // /////////////////////////////////////////////////////////////////
25 // DEFINE
26 
27 // /////////////////////////////////////////////////////////////////
28 // TYPEDEF
29 
30 // /////////////////////////////////////////////////////////////////
31 // CLASS
32 template<class TYPE> class CFvArrayAllocator : public CFvAllocator<TYPE>
33 {
34 public:
35  // DEFINE/TYPEDEF
36  typedef TYPE* pointer;
37  typedef const TYPE* const_pointer;
38  typedef TYPE& reference;
39  typedef const TYPE& const_reference;
40  typedef UINT size_type;
41 
42 public:
43  // ===================================================================
44  CFvArrayAllocator( size_type uiInitial, size_type uiIncrease ) : CFvAllocator<TYPE>( uiInitial, uiIncrease )
45  {
46  this->m_pTable = NULL;
49  }
50 
51  // ===================================================================
53  {
55  }
56 
57 public:
58  //
59  // METHOD
60  //
61 
62  // ===================================================================
63  virtual reference SetItem( size_type uiIndex, const_reference _Val )
64  {
65  if( this->m_uiCount <= uiIndex )
67  this->m_pTable[uiIndex] = _Val;
68  return this->m_pTable[uiIndex];
69  }
70 
71  // ===================================================================
72  virtual reference GetItem( size_type uiIndex ) const
73  {
74  if( this->m_uiCount <= uiIndex )
76  return this->m_pTable[uiIndex];
77  }
78 
79  // ===================================================================
80  virtual pointer GetBuffer( size_type uiIndex = 0 ) const
81  {
82  if( this->m_uiCount <= uiIndex )
84  return &this->m_pTable[uiIndex];
85  }
86 
87  // ===================================================================
88  virtual bool Allocate()
89  {
90  if( this->m_pTable != NULL )
91  {
93  }
94  if( this->m_uiInitial != 0 )
95  {
96  this->m_pTable = new TYPE[this->m_uiInitial];
97  if( this->m_pTable == NULL )
98  return false;
99  }
100  this->m_uiCapacity = this->m_uiInitial;
101  return true;
102  }
103 
104  // ===================================================================
105  virtual void Clear()
106  {
107  if( this->m_pTable != NULL )
108  {
109  delete [] this->m_pTable;
110  this->m_pTable = NULL;
111  }
112  this->m_uiCount = 0;
113  this->m_uiCapacity = 0;
114  }
115 
116  // ===================================================================
117  virtual bool Reserve( size_type uiIncrease )
118  {
119  if( this->m_pTable == NULL )
120  {
121  if( !Allocate() ) return false;
122  }
123  TYPE *pNewTable;
124  pNewTable = new TYPE[this->m_uiCapacity + uiIncrease];
125  if( pNewTable == NULL ) return false;
126 
127  size_type iCnt;
128  for( iCnt=0 ; iCnt<this->m_uiCount ; iCnt++ )
129  pNewTable[iCnt] = this->m_pTable[iCnt];
130 
131  this->m_uiCapacity += uiIncrease;
132 
133  delete [] this->m_pTable;
134  this->m_pTable = pNewTable;
135  return true;
136  }
137 
138  // ===================================================================
139  virtual bool Erase( size_type uiS, size_type uiE )
140  {
141  if( this->m_pTable == NULL ) return false;
142  if( this->m_uiCount<=uiS ) return false;
143  if( this->m_uiCount<=uiE ) uiE = this->m_uiCount-1;
144  if( uiE < uiS ) return false;
145 
146  size_type iCnt, jCnt;
147  for( iCnt=uiS, jCnt=1 ; iCnt<this->m_uiCount ; iCnt++, jCnt++ )
148  {
149  size_type uiP = uiE+jCnt;
150  if( uiP < this->m_uiCount )
151  this->m_pTable[iCnt] = this->m_pTable[uiP];
152  }
153  this->m_uiCount -= (uiE-uiS+1);
154  return true;
155  }
156 
157  // ===================================================================
158  virtual bool Insert( size_type uiPos, size_type uiSize, const_reference _Val )
159  {
160  if( this->m_pTable == NULL ) return false;
161  if( this->m_uiCount <= uiPos ) return false;
162  if( uiSize == 0 ) uiSize = 1;
163  if( this->m_uiCapacity < this->m_uiCount+uiSize )
164  CFvArrayAllocator<TYPE>::Reserve( max(this->m_uiCount + uiSize - this->m_uiCapacity,this->m_uiIncrease) );
165 
166  size_type uiLen = this->m_uiCount - uiPos;
167  size_type iCnt;
168  for( iCnt=1 ; iCnt<=uiLen ; iCnt++ )
169  this->m_pTable[this->m_uiCount+uiSize-iCnt] = this->m_pTable[this->m_uiCount-iCnt];
170  for( iCnt=uiPos ; iCnt<uiPos+uiSize ; iCnt++ )
171  this->m_pTable[iCnt] = _Val;
172  this->m_uiCount += uiSize;
173  return true;
174  }
175 
176  // ===================================================================
177  virtual bool PopBack()
178  {
179  if( this->m_pTable == NULL ) return false;
180  if( this->m_uiCount == 0 ) return false;
181  this->m_uiCount--;
182  return true;
183  }
184 
185  // ===================================================================
186  virtual bool PushBack( const_reference _Val )
187  {
188  if( this->m_pTable == NULL )
189  {
190  if( !Allocate() ) return false;
191  }
192  if( this->m_uiCapacity <= this->m_uiCount )
194  return false;
195  this->m_pTable[this->m_uiCount] = _Val;
196  this->m_uiCount++;
197  return true;
198  }
199 
200  // ===================================================================
201  virtual bool Resize( size_type uiSize, const_pointer pVal=NULL )
202  {
203  if( this->m_pTable == NULL )
204  {
205  if( !Allocate() ) return false;
206  }
207  if( uiSize == 0 )
208  {
210  }
211  else if( uiSize < this->m_uiCount )
212  {
213  TYPE *pNewTable = new TYPE[uiSize];
214  if( pNewTable == NULL ) return false;
215 
216  for( size_type iCnt=0 ; iCnt<uiSize ; iCnt++ )
217  pNewTable[iCnt] = this->m_pTable[iCnt];
218 
220  this->m_pTable = pNewTable;
221  this->m_uiCapacity = uiSize;
222  this->m_uiCount = uiSize;
223  }
224  else if( this->m_uiCount < uiSize )
225  {
226  if( this->m_uiCapacity < uiSize )
227  if( !CFvArrayAllocator<TYPE>::Reserve( max(uiSize - this->m_uiCapacity,this->m_uiIncrease) ) )
228  return false;
229  if( pVal )
230  for( size_type iCnt=this->m_uiCount ; iCnt<uiSize ; iCnt++ )
231  this->m_pTable[iCnt] = static_cast<const_reference>(*pVal);
232  this->m_uiCount = uiSize;
233  }
234  return true;
235  }
236 
237  // ===================================================================
238  virtual bool ChangeOrder( size_type uiPos, INT order )
239  {
240  if( !(0 <= uiPos && uiPos < this->m_uiCount) )
241  return false;
242 
243  INT iPos1 = (INT)uiPos;
244  INT iPos2 = iPos1 + order;
245  if( iPos2 < 0 )
246  iPos2 = 0;
247  if( iPos2 >= (INT)this->m_uiCount )
248  iPos2 = (INT)this->m_uiCount - 1;
249 
250  INT inc = (iPos1<iPos2) ? 1 : -1;
251  TYPE temp = this->m_pTable[iPos1];
252  for( INT i=iPos1 ; i!=iPos2 ; i+=inc )
253  this->m_pTable[i] = this->m_pTable[i+inc];
254  this->m_pTable[iPos2] = temp;
255 
256  return true;
257  }
258 
259  // ===================================================================
260  virtual bool SwapItem( size_type uiPos1, size_type uiPos2 )
261  {
262  if( ! (uiPos1 < this->m_uiCount) ) return false;
263  if( ! (uiPos2 < this->m_uiCount) ) return false;
264 
265  TYPE temp = this->m_pTable[uiPos1];
266  this->m_pTable[uiPos1] = this->m_pTable[uiPos2];
267  this->m_pTable[uiPos2] = temp;
268 
269  return true;
270  }
271 
272 protected:
273  // OBJECT
274  pointer m_pTable;
275 };
276 
277 } // FVCL
278 
279 #pragma pack(pop)
280 
281 #endif // _FVARRAYALLOCATOR_H_INCLUDED_
virtual bool PopBack()
配列末端からの要素の取り出し
Definition: FvArrayAllocator.h:177
size_type m_uiInitial
配列の初期容量
Definition: FvAllocator.h:112
TYPE * pointer
ポインタ型
Definition: FvAllocator.h:38
size_type m_uiIncrease
配列の増分
Definition: FvAllocator.h:113
配列確保テンプレートクラスの基本クラス
Definition: FvAllocator.h:34
FVCLのネームスペース
Definition: EVCbasicDeclare.txt:9
領域確保不可例外クラス
Definition: FveBadAllocException.h:18
virtual ~CFvArrayAllocator()
デストラクタ
Definition: FvArrayAllocator.h:52
virtual bool SwapItem(size_type uiPos1, size_type uiPos2)
配列要素の位置入れ替え
Definition: FvArrayAllocator.h:260
virtual bool Reserve(size_type uiIncrease)
配列容量の追加
Definition: FvArrayAllocator.h:117
const INT FAILED_TO_ALLOCATE
メモリの確保に失敗しました。
Definition: FvErrors.h:15
const INT INVALID_PARAMETER
パラメータが無効です。
Definition: FvErrors.h:24
virtual bool Allocate()
配列の確保
Definition: FvArrayAllocator.h:88
int INT
整数型(32ビット)
Definition: FvDefs.h:36
virtual bool PushBack(const_reference _Val)
配列への要素の追加
Definition: FvArrayAllocator.h:186
マクロ定義
virtual reference SetItem(size_type uiIndex, const_reference _Val)
配列への要素の設定
Definition: FvArrayAllocator.h:63
CFvArrayAllocator(size_type uiInitial, size_type uiIncrease)
コンストラクタ
Definition: FvArrayAllocator.h:44
UINT size_type
サイズ型
Definition: FvAllocator.h:42
size_type m_uiCapacity
配列の許容量
Definition: FvAllocator.h:111
virtual reference GetItem(size_type uiIndex) const
配列の要素の取得
Definition: FvArrayAllocator.h:72
const TYPE * const_pointer
ポインタ型(const付き)
Definition: FvAllocator.h:39
size_type m_uiCount
配列内の要素数
Definition: FvAllocator.h:110
const TYPE & const_reference
参照型(const付き)
Definition: FvAllocator.h:41
アクセス違反例外クラス
Definition: FveBadAccessException.h:18
TYPE & reference
参照型
Definition: FvAllocator.h:40
エラーコード定義
virtual bool Insert(size_type uiPos, size_type uiSize, const_reference _Val)
配列への要素の挿入
Definition: FvArrayAllocator.h:158
virtual bool Resize(size_type uiSize, const_pointer pVal=NULL)
配列の再確保
Definition: FvArrayAllocator.h:201
virtual bool ChangeOrder(size_type uiPos, INT order)
配列要素の順序入れ替え
Definition: FvArrayAllocator.h:238
unsigned int UINT
整数型(32ビット)[符号なし]
Definition: FvDefs.h:37
virtual bool Erase(size_type uiS, size_type uiE)
配列要素の削除
Definition: FvArrayAllocator.h:139
CFvObject クラスのインターフェース
pointer m_pTable
配列
Definition: FvArrayAllocator.h:274
配列確保テンプレートクラスの基本クラス
virtual void Clear()
配列の開放
Definition: FvArrayAllocator.h:105
virtual pointer GetBuffer(size_type uiIndex=0) const
配列の要素へのアドレス取得
Definition: FvArrayAllocator.h:80
変数型と定数の定義
デバッグ用関数のインターフェース
配列確保テンプレートクラス(連続型)
Definition: FvArrayAllocator.h:32

Documentation copyright © 2007 FAST Corporation. [B-001864]
Generated on 2023年11月02日(木) 10時12分53秒 for WIL説明書(C++) by doxygen 1.8.11