TNB Library
TnbTree.h
[詳解]
1#pragma once
14#include "TnbPointerHandle.h"
15#include "TnbMap.h"
17#include "TnbStr.h"
18#include "TnbSerializeAdapter.h"
19
20
21
22//TNB Library
23namespace TNB
24{
25
26
27
45template<typename KEY, typename VAL, typename INK = KEY>
46class CTreeT : public ISerializable
47{
48 #ifndef _TnbDOXYGEN //Document作成用シンボル
49
50 class COneHandle;
51 typedef CMapT<KEY,COneHandle,INK> CMapKey;
53 struct TOne
54 {
55 VAL value;
58 TOne(void) : value(VAL()) {}
59 };
60
62 class COneHandle : public CPointerHandleT<TOne>, public ISerializable
63 {
64 DEFSUPER(CPointerHandleT<TOne>);
65 public:
67 COneHandle(void) {}
72 COneHandle(TOne* P) : _super(P) {}
77 COneHandle(const COneHandle& other) : _super(other) {}
83 virtual void Serialize(ISerializer& _sr) const
84 {
85 _sr << ((*this)->value);
86 size_t l = 0;
87 if ( ! ((*this)->hpMap.IsNull()) )
88 {
89 l = (*this)->hpMap->GetSize();
90 }
91 _sr << l;
92 if ( l > 0 )
93 {
94 _sr << *((*this)->hpMap);
95 }
96 }
102 virtual void Deserialize(const IDeserializer& ds)
103 {
104 COneHandle o = new TOne;
105 ds >> (o->value);
106 size_t l = 0;
107 ds >> l;
108 if ( l > 0 )
109 {
110 o->hpMap = new CMapKey;
111 ds >> *(o->hpMap);
112 }
113 *this = o;
114 }
115 };
116
117 typedef typename CMapKey::CConstKeysAdapter CConstKeysAdapter;
118
119 #endif //_TnbDOXYGEN
120
121 COneHandle m_one;
122
123public:
124
133 struct ICommand
134 {
136 virtual ~ICommand(void) {}
137
147 virtual bool OnFound(int iNestLevel, const KEY& key, const VAL& value, const CTreeT& tree) = 0;
148 };
149
150private:
151
161 bool m_Scan(ICommand* I, const CTreeT& tree, int& _iNestLevel)
162 {
163 _iNestLevel++;
164 CTreeT::CConstKeysAdapter vkey = tree.GetKeysAdapter();
165 loop ( i, vkey.GetSize() )
166 {
167 const KEY& k = vkey.At(i);
168 ASSERTLIB(tree.HasKey(k));
169 if ( ! I->OnFound(_iNestLevel, k, tree.At(k), tree) )
170 {
171 return false;
172 }
173 if ( tree.HasLeaf(k) && ! m_Scan(I, tree(k), _iNestLevel) )
174 {
175 return false;
176 }
177 }
178 _iNestLevel--;
179 return true;
180 }
181
182public:
183
188 CTreeT(void) : m_one(new TOne)
189 {
190 m_one->hpMap = new CMapKey;
191 }
192
198 CTreeT(const CTreeT& other) : m_one(other.m_one)
199 {
200 }
201
203 virtual ~CTreeT(void)
204 {
205 m_one.Null();
206 }
207
214 CTreeT& operator=(const CTreeT& other)
215 {
216 m_one = other.m_one;
217 return *this;
218 }
219
224 const VAL& AtSelf(void) const
225 {
226 return m_one->value;
227 }
228
233 VAL& AtSelf(void)
234 {
235 return m_one->value;
236 }
237
244 bool HasKey(INK key) const
245 {
246 return m_one->hpMap->HasKey(key);
247 }
248
255 const VAL& At(INK key) const
256 {
257 if ( HasKey(key) )
258 {
259 return m_one->hpMap->operator[](key)->value;
260 }
261 throw CEmptyException();
262 }
263
270 const VAL& operator[](INK key) const
271 {
272 return At(key);
273 }
274
281 VAL& At(INK key)
282 {
283 if ( ! HasKey(key) )
284 {
285 COneHandle P = new TOne;
286 P->value = VAL();
287 m_one->hpMap->SetKey(key, P);
288 }
289 return m_one->hpMap->operator[](key)->value;
290 }
291
298 VAL& operator[](INK key)
299 {
300 return At(key);
301 }
302
309 VAL Get(INK key) const
310 {
311 return At(key);
312 }
313
318 size_t GetSize(void) const
319 {
320 return m_one->hpMap->GetSize();
321 }
322
328 {
329 return m_one->hpMap->EnumKeys();
330 }
331
339 bool Set(INK key, VAL v)
340 {
341 if ( HasKey(key) )
342 {
343 m_one->hpMap->operator[](key)->value = v;
344 return true;
345 }
346 return false;
347 }
348
356 bool Add(INK key, VAL v)
357 {
358 if ( HasKey(key) )
359 {
360 return false;
361 }
362 COneHandle P = new TOne;
363 P->value = v;
364 m_one->hpMap->SetKey(key, P);
365 return true;
366 }
367
374 bool Remove(INK key)
375 {
376 if ( ! HasKey(key) )
377 {
378 return false;
379 }
380 return m_one->hpMap->RemoveKey(key);
381 }
382
389 bool HasLeaf(INK key) const
390 {
391 if ( ! HasKey(key) )
392 {
393 return false;
394 }
395 const CPointerHandleT<TOne>& c = m_one->hpMap->operator[](key);
396 if ( c->hpMap.IsNull() )
397 {
398 return false;
399 }
400 return ! c->hpMap->IsEmpty();
401 }
402
412 bool AddLeaf(INK key, CTreeT<KEY,VAL,INK>& other)
413 {
414 if ( HasKey(key) )
415 {
416 return false;
417 }
418 //add 自分の子供、親でないことを確認。
419 m_one->hpMap->SetKey(key, other.m_one);
420 return true;
421 }
422
426 void RemoveAll(void)
427 {
428 m_one = new TOne;
429 m_one->hpMap = new CMapKey;
430 }
431
437 CConstKeysAdapter GetKeysAdapter(void) const
438 {
439 return m_one->hpMap->GetKeysAdapter();
440 }
441
450 {
451 if ( ! HasKey(key) )
452 {
453 COneHandle P = new TOne;
454 P->value = VAL();
455 m_one->hpMap->SetKey(key, P);
456 }
457 COneHandle& c = m_one->hpMap->operator[](key);
458 if ( c->hpMap.IsNull() )
459 {
460 c->hpMap = new CMapKey;
461 }
462 CTreeT t;
463 t.m_one = c;
464 return t;
465 }
466
475 {
476 return Refer(key);
477 }
478
485 const CTreeT<KEY,VAL,INK> Refer(INK key) const
486 {
487 if ( ! HasKey(key) )
488 {
489 throw CEmptyException();
490 }
491 const COneHandle& c = m_one->hpMap->operator[](key);
492 if ( c->hpMap.IsNull() )
493 {
494 throw CEmptyException();
495 }
496 CTreeT t;
497 t.m_one = c;
498 return t;
499 }
500
507 const CTreeT<KEY,VAL,INK> operator()(INK key) const
508 {
509 return Refer(key);
510 }
511
517 virtual void Serialize(ISerializer& _sr) const
518 {
519 _sr << m_one;
520 }
521
527 virtual void Deserialize(const IDeserializer& ds)
528 {
529 ds >> m_one;
530 }
531
538 {
540 CSerializeAdapter sr(&vb);
541 sr << m_one;
542 CTreeT t;
543 CDeserializeAdapter ds(&vb);
544 ds >> t.m_one;
545 return t;
546 }
547
555 bool Scan(ICommand* I)
556 {
557 int iNestLevel = 0;
558 if ( ! I->OnFound(iNestLevel, KEY(), AtSelf(), *this) )
559 {
560 return false;
561 }
562 return m_Scan(I, *this, iNestLevel);
563 }
564
565private:
566 friend class CTreeTest;
567};
568
569
570
577class CStrsTree : public CTreeT<CStr,CStr,LPCTSTR>
578{
580 DEFSUPER(_super);
581
582 #ifndef _TnbDOXYGEN //Document作成用シンボル
583
584 class CToStringCommand : public _super::ICommand
585 {
586 CStr m_str;
596 virtual bool OnFound(int iNestLevel, const CStr& key, const CStr& value, const _super& tree)
597 {
598 m_str += CStr::Fmt(_T("%s[+]%s = %s\n")
599 , CStr::Lineup(_T(" |"), iNestLevel)
600 , key
601 , value);
602 return true;
603 }
604 public:
605 CStr GetString(void) { return m_str; }
606 };
607
608 #endif //_TnbDOXYGEN
609
610public:
613 {
614 }
615
622 {
623 }
624
632 {
633 _super::operator=(other);
634 return *this;
635 }
636
642 {
643 CToStringCommand c;
644 _super::Scan(&c);
645 return c.GetString();
646 }
654 CStrsTree Refer(LPCTSTR key)
655 {
656 return _super::Refer(key);
657 }
658
667 {
668 return Refer(key);
669 }
670
677 const CStrsTree Refer(LPCTSTR key) const
678 {
679 return _super::Refer(key);
680 }
681
688 const CStrsTree operator()(LPCTSTR key) const
689 {
690 return Refer(key);
691 }
692
698 CStrsTree Clone(void) const
699 {
700 return _super::Clone();
701 }
702};
703
704
705
706}; // TNB
707
708
709
情報群管理アダプタ関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
マップ型情報管理関係のヘッダ
ポインタハンドル関係のヘッダ
情報群管理のシリアライザーアダプタ関係のヘッダ
文字列管理関係のヘッダ
デシリアライズアダプタ.
取得要素(空き)無し例外
Definition: TnbException.h:107
マップ型情報管理テンプレート
Definition: TnbMap.h:66
bool IsNull(void) const
[確認] NULLチェック
ポインタ型ハンドルテンプレート
シリアライズアダプタ.
static CStrT Fmt(const TCHAR *lpszFormat,...)
[作成] 書式付き文字列作成
Definition: TnbStr.h:1206
static CStrT Lineup(TCHAR t, size_t length)
[作成] 指定文字を並べた文字列作成
Definition: TnbStr.h:1222
ツリー型文字列情報管理クラス.
Definition: TnbTree.h:578
const CStrsTree Refer(LPCTSTR key) const
[取得] キー下のTree取得
Definition: TnbTree.h:677
const CStrsTree operator()(LPCTSTR key) const
[取得] キー下のTree取得
Definition: TnbTree.h:688
CStrsTree(const CTreeT< CStr, CStr, LPCTSTR > &other)
コピーコンストラクタ
Definition: TnbTree.h:621
CStrsTree(void)
コンストラクタ
Definition: TnbTree.h:612
CStrsTree operator()(LPCTSTR key)
[取得] キー下のTree取得
Definition: TnbTree.h:666
CStrsTree Refer(LPCTSTR key)
[取得] キー下のTree取得
Definition: TnbTree.h:654
CStrsTree Clone(void) const
[複製] 複製作成.
Definition: TnbTree.h:698
CStr ToString(void)
[変換] 単一文字列化
Definition: TnbTree.h:641
CStrsTree & operator=(const CTreeT< CStr, CStr, LPCTSTR > &other)
[代入] コピーオペレータ
Definition: TnbTree.h:631
ツリー型情報管理テンプレートクラス
Definition: TnbTree.h:47
const CTreeT< KEY, VAL, INK > operator()(INK key) const
[取得] キー下のTree取得
Definition: TnbTree.h:507
CTreeT & operator=(const CTreeT &other)
[代入] コピーオペレータ
Definition: TnbTree.h:214
const VAL & At(INK key) const
[取得] キー下の値の参照
Definition: TnbTree.h:255
bool Remove(INK key)
[削除] キー下の値と枝を削除
Definition: TnbTree.h:374
CConstKeysAdapter GetKeysAdapter(void) const
[取得] キーのみの参照アダプタ取得.
Definition: TnbTree.h:437
virtual void Deserialize(const IDeserializer &ds)
[通知] デシリアライズ
Definition: TnbTree.h:527
void RemoveAll(void)
[削除] 値と枝を削除
Definition: TnbTree.h:426
VAL & operator[](INK key)
[取得] キー下の値の参照
Definition: TnbTree.h:298
VAL & At(INK key)
[取得] キー下の値の参照
Definition: TnbTree.h:281
CTreeT< KEY, VAL, INK > operator()(INK key)
[取得] キー下のTree取得
Definition: TnbTree.h:474
CTreeT(const CTreeT &other)
コピーコンストラクタ
Definition: TnbTree.h:198
size_t GetSize(void) const
[取得] キーの数
Definition: TnbTree.h:318
bool HasLeaf(INK key) const
[確認] キーは枝を持つか
Definition: TnbTree.h:389
const VAL & operator[](INK key) const
[取得] キー下の値の参照
Definition: TnbTree.h:270
bool AddLeaf(INK key, CTreeT< KEY, VAL, INK > &other)
[追加] キー下に枝を追加
Definition: TnbTree.h:412
VAL Get(INK key) const
[取得] キー下の値取得
Definition: TnbTree.h:309
CVectorT< KEY > EnumKeys(void) const
[取得] キー一覧
Definition: TnbTree.h:327
bool Set(INK key, VAL v)
[設定] キー下の値設定
Definition: TnbTree.h:339
CTreeT< KEY, VAL, INK > Refer(INK key)
[取得] キー下のTree取得
Definition: TnbTree.h:449
bool Scan(ICommand *I)
[走査] 全要素走査.
Definition: TnbTree.h:555
virtual void Serialize(ISerializer &_sr) const
[通知] シリアライズ
Definition: TnbTree.h:517
bool Add(INK key, VAL v)
[追加] キー下の値追加
Definition: TnbTree.h:356
const CTreeT< KEY, VAL, INK > Refer(INK key) const
[取得] キー下のTree取得
Definition: TnbTree.h:485
bool HasKey(INK key) const
[確認] キー有無
Definition: TnbTree.h:244
virtual ~CTreeT(void)
デストラクタ
Definition: TnbTree.h:203
VAL & AtSelf(void)
[取得] 直下の情報の参照
Definition: TnbTree.h:233
CTreeT< KEY, VAL, INK > Clone(void) const
[複製] 複製作成
Definition: TnbTree.h:537
CTreeT(void)
コンストラクタ
Definition: TnbTree.h:188
const VAL & AtSelf(void) const
[取得] 直下の情報の参照
Definition: TnbTree.h:224
配列型情報管理テンプレート
Definition: TnbVector.h:75
TNB Library
Definition: TnbDoxyTitle.txt:2
ツリー型情報管理用 全キー走査コマンドインターフェース
Definition: TnbTree.h:134
virtual ~ICommand(void)
デストラクタ
Definition: TnbTree.h:136
virtual bool OnFound(int iNestLevel, const KEY &key, const VAL &value, const CTreeT &tree)=0
[通知] キーと値発見
デシリアライザーインターフェースクラス.
シリアライザブルインターフェース.
Definition: TnbSerializer.h:47
シリアライザーインターフェースクラス.