TNB Library
TnbBlockLinkVector.h
[詳解]
1#pragma once
15#include "TnbCollection.h"
16#include "TnbVector.h"
17
18
19
20//TNB Library
21namespace TNB
22{
23
24
25
51template<typename TYP>
53{
54 DEFSUPER(ICollectionMidT<TYP>);
55public:
56
59 {
60 const TYP* pData;
61 size_t size;
62 };
63
64
65 //-------
66
67
72 CBlockLinkVectorT(void) : m_totalSize(0)
73 {
74 m_blockShift = 10;
75 m_blockSize = down_cast<size_t>(_BIT(m_blockShift));
76 m_blockMask = m_blockSize - 1;
77 }
78
83 CBlockLinkVectorT(size_t bs) : m_totalSize(0)
84 {
85 m_blockShift = 1;
86 m_blockSize = 1 << m_blockShift;
87 while ( m_blockSize < bs )
88 {
89 m_blockSize <<= 1;
90 m_blockShift++;
91 if ( m_blockSize == 0 )
92 {
93 m_blockShift = 7;
94 m_blockSize = 1 << m_blockShift;
95 }
96 }
97 m_blockMask = m_blockSize - 1;
98 }
99
102 {
103 RemoveAll();
104 }
105
112 virtual bool Lock(DWORD dwTime = INFINITE) const
113 {
114 return m_sync.Lock(dwTime);
115 }
116
118 virtual void Unlock(void) const
119 {
120 m_sync.Unlock();
121 }
122
128 virtual size_t GetSize(void) const
129 {
130 return m_totalSize;
131 }
132
141 virtual INDEX Add(const TYP& t)
142 {
143 size_t i = m_totalSize;
144 INDEX bn = i >> m_blockShift;
145 INDEX idx = i & m_blockMask;
146 if ( bn >= m_blocks.GetSize() )
147 {
148 TYP* P = new TYP[m_blockSize];
149 m_blocks.Add(P);
150 }
151 TYP* B = m_blocks[bn];
152 B[idx] = t;
153 m_totalSize++;
154 return i;
155 }
156
165 virtual size_t AddElements(size_t size, const TYP* P = NULL)
166 {
167 INDEX is = m_totalSize;
168 size_t i = m_totalSize - 1 + size;
169 INDEX bn = i >> m_blockShift;
170 while ( bn >= m_blocks.GetSize() )
171 {
172 TYP* P = new TYP[m_blockSize];
173 m_blocks.Add(P);
174 }
175 m_totalSize += size;
176 if ( P != NULL )
177 {
178 loop ( i, size )
179 {
180 Set(is + i, *P++);
181 }
182 }
183 else
184 {
185 loop ( i, min(m_blockSize, size) ) //memo 全部やる必要はないので、一ブロック分のみ
186 {
187 Set(is + i, TYP());
188 }
189 }
190 return size;
191 }
192
198 virtual bool RemoveAll(void)
199 {
200 m_totalSize = 0;
201 m_blocks.RemoveAll();
202 return true;
203 }
204
213 virtual bool Remove(INDEX index)
214 {
215 if ( index >= m_totalSize )
216 {
217 return false;
218 }
219 size_t d = m_totalSize - index;
220 if ( d > 1 )
221 {
222 loop ( i, d - 1 )
223 {
224 Set(index + i, At(index + i + 1));
225 }
226 }
227 m_totalSize--;
228 if ( m_totalSize == 0 )
229 {
230 m_blocks.RemoveAll();
231 }
232 else
233 {
234 INDEX bn = (m_totalSize - 1) >> m_blockShift;
235 if ( bn < m_blocks.GetSize() )
236 {
237 m_blocks.SetSize(bn + 1);
238 }
239 }
240 return true;
241 }
242
250 virtual size_t RemoveElements(INDEX index, size_t size = 0)
251 {
252 EXCLUSIVE( &m_sync );
253 size_t sz = GetSize();
254 if ( sz < index )
255 {
256 return INVALID_SIZE;
257 }
258 if ( size == 0 || sz < index + size)
259 {
260 SetSize(index);
261 return sz - index;
262 }
263 size_t z = sz - size - index;
264 loop ( i, z )
265 {
266 Set(index + i, Get(index + size + i));
267 }
268 SetSize(index + z);
269 return size;
270 }
271
279 size_t GetBlockCount(void) const
280 {
281 return m_blocks.GetSize();
282 }
283
294 bool GetBlockInformation(TBlockInformation& _bi, INDEX index) const
295 {
296 if ( m_blocks.IsInRange(index ) )
297 {
298 _bi.size = m_blockSize;
299 if ( index == m_blocks.GetSize() - 1 )
300 {
301 _bi.size = (m_totalSize & m_blockMask);
302 }
303 _bi.pData = m_blocks[index];
304 return true;
305 }
306 return false;
307 }
308
309protected:
310
317 virtual const TYP* m_GetConstPointer(INDEX index) const
318 {
319 if ( index >= m_totalSize )
320 {
321 return NULL;
322 }
323 INDEX bn = index >> m_blockShift;
324 INDEX idx = index & m_blockMask;
325 TYP* B = m_blocks[bn];
326 return &B[idx];
327 }
328
335 virtual TYP* m_GetPointer(INDEX index)
336 {
337 if ( index >= m_totalSize )
338 {
339 return NULL;
340 }
341 INDEX bn = index >> m_blockShift;
342 INDEX idx = index & m_blockMask;
343 TYP* B = m_blocks[bn];
344 return &B[idx];
345 }
346
347private:
348 CSyncSection m_sync;
349 size_t m_totalSize;
351 size_t m_blockSize;
352 int m_blockShift;
353 INDEX m_blockMask;
354 friend class CBlockListVectorTest;
355};
356
357
358
359}; // TNB
360
情報群管理関係のヘッダ
#define _BIT(X)
BIT演算
Definition: TnbDef.h:307
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
配列型情報管理関係のヘッダ
配列型情報管理テンプレート
virtual size_t GetSize(void) const
[取得] 要素数取得.
virtual size_t RemoveElements(INDEX index, size_t size=0)
[削除] 要素削除.
CBlockLinkVectorT(void)
コンストラクタ.
CBlockLinkVectorT(size_t bs)
コンストラクタ.
virtual bool Remove(INDEX index)
[削除] 要素一つ削除.
virtual bool Lock(DWORD dwTime=INFINITE) const
[排他] ロック
size_t GetBlockCount(void) const
[取得] ブロック数取得.
bool GetBlockInformation(TBlockInformation &_bi, INDEX index) const
[取得] ブロック情報取得.
virtual bool RemoveAll(void)
[削除] 全要素削除 .
virtual size_t AddElements(size_t size, const TYP *P=NULL)
[追加] 複数要素追加.
friend class CBlockListVectorTest
フレンドクラス宣言
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
virtual void Unlock(void) const
[排他] アンロック
virtual const TYP * m_GetConstPointer(INDEX index) const
[取得] 要素アドレス取得
~CBlockLinkVectorT(void)
デストラクタ
virtual TYP * m_GetPointer(INDEX index)
[取得] 要素アドレス取得
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
Section排他管理クラス
Definition: TnbSync.h:125
virtual bool Lock(DWORD dwTime=INFINITE) const
[排他] ロック
Definition: TnbSync.h:148
virtual void Unlock(void) const
[排他] アンロック
Definition: TnbSync.h:155
配列型情報管理テンプレート
Definition: TnbVector.h:75
情報群管理操作インターフェース拡張テンプレート
virtual TYP Get(INDEX index) const
[取得] 要素の取得.
virtual const TYP & At(INDEX index) const
[取得] 要素の参照取得.
virtual bool Set(INDEX index, const TYP &t)
[設定] 要素の設定.
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
Definition: TnbSync.h:788
TNB Library
Definition: TnbDoxyTitle.txt:2
virtual bool SetSize(size_t size)
[操作] サイズ指定