#ifndef _Tree_Tree_h #define _Tree_Tree_h #include NAMESPACE_UPP template class Tree : protected Array, MoveableAndDeepCopyOption > { protected: typedef Array B; T* parent; T* root; inline void DoLink(T& t) { t.root = root; t.parent = (T *)this; } inline void Unlink(T& t) { t.root = NULL; t.parent = NULL; } public: void Relink() { for(int i = 0; i < B::GetCount(); i++) { T& t = B::operator[](i); DoLink(t); t.Relink();} } public: T* GetPtr() { return (T*) this; } const T* GetPtr() const { return (const T *) this; } T* GetParent() { return parent; } const T* GetParent() const { return parent; } T* GetRoot() { return root; } const T* GetRoot() const { return root; } void SetAsRoot() { ASSERT(parent == NULL); root = (T*)this; Relink(); } // Array interface T& Add() { T& t = B::Add(); DoLink(t); return t; } T& Add(const T& x) { T& t = B::Add(x); DoLink(t); t.Relink(); return t; } T& AddPick(pick_ T& x) { T& t = B::Add(x); DoLink(t); t.Relink(); return t; } T& Add(T *newt) { ASSERT(newt->parent == NULL); T& t = B::Add(newt); DoLink(t); t.Relink(); return t; } using B::operator[]; using B::GetCount; using B::IsEmpty; using B::Trim; void SetCount(int n) { B::SetCount(n); for(int i = 0; i < B::GetCount(); i++) DoLink(B::operator[](i)); } void SetCountR(int n) { B::SetCountR(n); for(int i = 0; i < B::GetCount(); i++) DoLink(B::operator[](i)); } using B::Clear; T& At(int i) { if(i >= GetCount()) SetCountR(i + 1); return B::Get(i); } T& At(int i, const T& x) { if(i >= GetCount()) SetCountR(i + 1, x); return B::Get(i); } using B::Remove; T& Insert(int i) { T& t = B::Insert(i); DoLink(t); return t; } void InsertPick(int i, pick_ T& x) { DoLink(x); B::InsertPick(i, x); } using B::GetIndex; using B::Swap; using B::Move; T* Detach(int i) { T* t = B::Detach(i); Unlink(*t); return t;} T& Set(int i, T *newt) { ASSERT(newt->parent == NULL); T& t = B::Set(i, newt); DoLink(t); return t; } void Insert(int i, T *newt) { ASSERT(newt->parent == NULL); B::Insert(i, newt); DoLink(*newt); } using B::Drop; using B::Top; // using B::Pop; T* PopDetach() { T* t = B::PopDetach(); Unlink(*t); return t; } void Swap(Tree& b) { B::Swap(b); for(int i = 0; i < B::GetCount(); i++) DoLink(B::operator[](i)); for(int i = 0; i < b.GetCount(); i++) b.DoLink(b[i]); } Tree& operator<<(const T& x) { Add(x); return *this; } Tree& operator<<(T *newt) { Add(newt); return *this; } Tree& operator|(pick_ T& x) { AddPick(x); return *this; } using B::IsPicked; #ifdef UPP void Serialize(Stream& s) { StreamContainer(s, *this); } #endif Tree() : parent(NULL) , root(NULL) {} //pick Tree(pick_ Tree& v) : parent(NULL) , root(NULL) , B(v) { Relink(); } void operator=(pick_ Tree& v) { (B&)(*this) = v; Relink(); } // Deep copy Tree(const Tree& v, int) : parent(NULL) , root(NULL) , B(v, 0) { Relink(); } using B::ConstIterator; using B::Iterator; using B::ValueType; using B::Begin; using B::End; using B::GetIter; // Array Interface end public: #ifdef _DEBUG void Dump() { for(int i = 0; i < GetCount(); i++) LOG((*this)[i]); LOG("-------------------------------------"); } #endif }; template inline void Xmlize(XmlIO& xml, Tree& data) { XmlizeContainer(xml, "tree", data); } template inline void DumpContainer(Stream& s, const Tree& c) { DumpContainer(s, c.Begin(), c.End()); } //Tree Node helper class template class Node : public Tree > { public: typedef Node CLASSNAME; typedef Tree > R; Node() {} Node(const T& leaf) : leaf(leaf) {} T leaf; }; template inline Stream& operator%(Stream& s, Node& x) { s % x.leaf % (Tree >&)x; return s; } template inline void Xmlize(XmlIO& xml, Node& a) { xml("leaf", a.leaf); Xmlize(xml, (Tree >&)a); } template //B conflicts with Tree::B class NodeB : public Tree > , public BB //leaf { public: typedef NodeB CLASSNAME; typedef Tree > R; }; template inline Stream& operator%(Stream& s, NodeB& x) { s % (BB&)x % (Tree >&)x; return s; } template inline void Xmlize(XmlIO& xml, NodeB& a) { xml("leaf", (BB&)a); Xmlize(xml, (Tree >&)a); } //// template > class TreeMap : protected ArrayMap, MoveableAndDeepCopyOption > { protected: typedef ArrayMap B; T* parent; T* root; inline void DoLink(T& t) { t.root = root; t.parent = (T *)this; } inline void Unlink(T& t) { t.root = NULL; t.parent = NULL; } public: void Relink() { for(int i = 0; i < B::GetCount(); i++) { T& t = B::operator[](i); DoLink(t); t.Relink();} } public: T* GetPtr() { return (T*) this; } const T* GetPtr() const { return (const T *) this; } T* GetParent() { return parent; } const T* GetParent() const { return parent; } T* GetRoot() { return root; } const T* GetRoot() const { return root; } void SetAsRoot() { ASSERT(parent == NULL); root = (T*)this; Relink(); } // AMap interface T& Add(const K& k, const T& x) { T& t = B::Add(k, x); DoLink(t); t.Relink(); return t; } T& Add(const K& k) { T& t = B::Add(k); DoLink(t); return t; } T& AddPick(const K& k, pick_ T& x) { T& t = B::AddPick(); DoLink(t); t.Relink(); return t; } using B::Find; using B::FindNext; using B::FindLast; using B::FindPrev; int FindAdd(const K& k) { int i = B::FindAdd(k); DoLink(operator[](i)); return i; } int FindAdd(const K& k, const T& init) { int i = B::FindAdd(k, init); DoLink(operator[](i)); return i; } int FindAddPick(const K& k, pick_ T& init) { int i = B::FindAddPick(k, init); DoLink(operator[](i)); return i; } int Put(const K& k, const T& x) { int i = B::Put(k, x); DoLink(operator[](i)); return i; } int PutPick(const K& k, pick_ T& x) { int i = B::PutPick(k, x); DoLink(operator[](i)); return i; } T& Put(const K& k) { T& t = B::Put(k); DoLink(t); return t; } int FindPut(const K& k) { int i = B::FindPut(k); DoLink(operator[](i)); return i; } int FindPut(const K& k, const T& init) { int i = B::FindPut(k, init); DoLink(operator[](i)); return i; } int FindPutPick(const K& k, pick_ T& init) { int i = B::FindPutPick(k, init); DoLink(operator[](i)); return i; } using B::Get; T& GetAdd(const K& k) {T& t = B::GetAdd(k); DoLink(t); return t; } T& GetAdd(const K& k, const T& x) {T& t = B::GetAdd(k, x); DoLink(t); return t; } T& GetAddPick(const K& k, pick_ T& x) {T& t = B::GetAddPick(k, x); DoLink(t); return t; } T& GetPut(const K& k) {T& t = B::GetPut(k); DoLink(t); return t; } T& GetPut(const K& k, const T& x) {T& t = B::GetPut(k, x); DoLink(t); return t; } T& GetPutPick(const K& k, pick_ T& x) {T& t = B::GetPutPick(k, x); DoLink(t); return t; } using B::SetKey; using B::FindPtr; using B::Unlink; using B::UnlinkKey; using B::IsUnlinked; using B::Sweep; T& Insert(int i, const K& k) { T& t = B::Insert(i, k); DoLink(t); return t; } T& Insert(int i, const K& k, const T& x) { T& t = B::Insert(i, k, x); DoLink(t); return t; } using B::Remove; using B::RemoveKey; using B::operator[]; using B::GetCount; using B::IsEmpty; using B::Clear; using B::Shrink; using B::Reserve; using B::GetAlloc; using B::GetHash; using B::Drop; using B::Top; using B::TopKey; // using B::Pop; using B::PopKey; using B::Trim; using B::GetKey; #ifdef UPP void Serialize(Stream& s) { int version = 0; s / version % B::key % B::value; for(int i = 0; i < B::GetCount(); i++) { T& t = B::operator[](i); DoLink(t); //t.Relink(); //serialize will recurse } } #endif void Swap(TreeMap& b) { B::Swap(b); for(int i = 0; i < B::GetCount(); i++) DoLink(B::operator[](i)); for(int i = 0; i < b.GetCount(); i++) b.DoLink(b[i]); } using B::GetIndex; using B::PickIndex; using B::GetKeys; using B::PickKeys; using B::GetValues; Vector PickValues() pick_ { Vector v = B::PickValues(); for(int i = 0; i < v.GetCount(); i++) v[i].SetAsRoot(); return v; } using B::IsPicked; using B::KeyType; using B::KeyConstIterator; using B::KeyBegin; using B::KeyEnd; using B::KeyGetIter; using B::ValueType; using B::ConstIterator; using B::Iterator; using B::Begin; using B::End; using B::GetIter; // ArrayMap interface T& Add(const K& k, T *newt) { T& t = B::Add(k, newt); DoLink(t); t.Relink(); return t; } template TT& CreateB(const K& k) { TT *q = B::Create(k); DoLink(q); return *q; } T& Set(int i, T *ptr) { ASSERT(ptr->parent == NULL); T& t = B::Set(i, ptr); DoLink(t); return t; } T* PopDetach() { T* t = B::PopDetach(); Unlink(*t); return t; } T* Detach(int i) { T* t = B::Detach(i); Unlink(*t); return t;} TreeMap(const TreeMap& s, int) : parent(NULL) , root(NULL) , B(s, 1) { Relink(); } #if 0 ArrayMap(pick_ Index& ndx, pick_ Array& val) : AMap, HashFn>(ndx, val) {} ArrayMap(pick_ Vector& ndx, pick_ Array& val) : AMap, HashFn>(ndx, val) {} #endif TreeMap() : parent(NULL) , root(NULL) {} friend void Swap(TreeMap& a, TreeMap& b) { a.B::Swap(b); for(int i = 0; i < a.GetCount(); i++) DoLink(a[i]); for(int i = 0; i < b.GetCount(); i++) b.DoLink(b[i]); } }; template inline void Xmlize(XmlIO& xml, TreeMap& data) { XmlizeMap(xml, "tkey", "tvalue", data); } template inline void DumpContainer(Stream& s, const TreeMap& c) { DumpContainer(s, c.Begin(), c.End()); } template > class MapNode : public TreeMap, H> { public: typedef MapNode CLASSNAME; typedef TreeMap, H> R; MapNode() {} MapNode(const T& leaf) : leaf(leaf) {} T leaf; }; template inline Stream& operator%(Stream& s, MapNode& x) { s % x.leaf % (TreeMap, H>&)x; return s; } template inline void Xmlize(XmlIO& xml, MapNode& a) { xml("leaf", a.leaf); Xmlize(xml, (TreeMap, H>&)a); } template > //B conflicts with Tree::B class MapNodeB : public TreeMap, H> , public BB //leaf { public: typedef MapNodeB CLASSNAME; typedef TreeMap, H> R; }; template inline Stream& operator%(Stream& s, MapNodeB& x) { s % (BB&)x % (TreeMap, H>&)x; return s; } template inline void Xmlize(XmlIO& xml, MapNodeB& a) { xml("leaf", (BB&)a); Xmlize(xml, (TreeMap, H>&)a); } END_UPP_NAMESPACE #endif