#pragma once /**************************************************************************** Copyright (c) 2006, Radon Labs GmbH Copyright (c) 2011-2013,WebJet Business Division,CYOU http://www.genesis-3d.com.cn Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #include "core/types.h" #include "util/array.h" //------------------------------------------------------------------------------ namespace Util { template class SimpleTree { public: /// public node class class Node { public: /// default constructor Node(); /// constructor with parent and value Node(const Node& parent, const VALUETYPE& val); /// destructor ~Node(); /// increment refcount void AddRef(); /// decrement refcount void Release(); /// get read-only child by index const Node& operator[](IndexT i) const; /// get read/write child by index Node& operator[](IndexT i); /// get read-only child element at index const Node& Child(IndexT i) const; /// get read/write child element at index Node& Child(IndexT i); /// return true if the node has a parent bool HasParent() const; /// read/write access to parent Node& Parent(); /// read-only access to parent const Node& Parent() const; /// clear children void Clear(); /// number of children SizeT Size() const; /// return true if empty bool IsEmpty() const; /// return first element Node& Front() const; /// return last element Node& Back() const; /// add element at back of array void Append(const VALUETYPE& val); /// erase at index void Erase(IndexT i); /// insert element before element at index void Insert(IndexT index, const VALUETYPE& val); /// find identical element (slow); IndexT Find(const VALUETYPE& val) const; /// read/write access to value VALUETYPE& Value(); /// read-only access to value const VALUETYPE& Value() const; private: uint refCount; Node* parent; VALUETYPE value; Array > children; }; /// default constructor SimpleTree(); /// read/write access to root element Node& Root(); /// read-only access to root element const Node& Root() const; private: Node rootNode; }; //------------------------------------------------------------------------------ /** */ template SimpleTree::Node::Node() : refCount(0), parent(0) { // empty } //------------------------------------------------------------------------------ /** */ template SimpleTree::Node::Node(const Node& p, const VALUETYPE& val) : refCount(0), parent(const_cast(&p)), value(val) { #if NEBULA3_BOUNDSCHECKS n_assert(0 != this->parent); #endif } //------------------------------------------------------------------------------ /** */ template SimpleTree::Node::~Node() { #if NEBULA3_BOUNDSCHECKS n_assert(0 == this->refCount); #endif } //------------------------------------------------------------------------------ /** */ template void SimpleTree::Node::AddRef() { ++this->refCount; } //------------------------------------------------------------------------------ /** */ template void SimpleTree::Node::Release() { #if NEBULA3_BOUNDSCHECKS n_assert(this->refCount > 0); #endif --this->refCount; if (this->refCount == 0) { n_delete(this); } } //------------------------------------------------------------------------------ /** */ template const typename SimpleTree::Node& SimpleTree::Node::operator[](IndexT i) const { return *(this->children[i]); } //------------------------------------------------------------------------------ /** */ template typename SimpleTree::Node& SimpleTree::Node::operator[](IndexT i) { return *(this->children[i]); } //------------------------------------------------------------------------------ /** */ template const typename SimpleTree::Node& SimpleTree::Node::Child(IndexT i) const { return *(this->children[i]); } //------------------------------------------------------------------------------ /** */ template typename SimpleTree::Node& SimpleTree::Node::Child(IndexT i) { return *(this->children[i]); } //------------------------------------------------------------------------------ /** */ template bool SimpleTree::Node::HasParent() const { return (0 != this->parent); } //------------------------------------------------------------------------------ /** */ template const typename SimpleTree::Node& SimpleTree::Node::Parent() const { #if NEBULA3_BOUNDSCHECKS n_assert(0 != this->parent); #endif return *this->parent; } //------------------------------------------------------------------------------ /** */ template typename SimpleTree::Node& SimpleTree::Node::Parent() { #if NEBULA3_BOUNDSCHECKS n_assert(0 != this->parent); #endif return *this->parent; } //------------------------------------------------------------------------------ /** */ template void SimpleTree::Node::Clear() { this->children.Clear(); } //------------------------------------------------------------------------------ /** */ template SizeT SimpleTree::Node::Size() const { return this->children.Size(); } //------------------------------------------------------------------------------ /** */ template bool SimpleTree::Node::IsEmpty() const { return this->children.IsEmpty(); } //------------------------------------------------------------------------------ /** */ template typename SimpleTree::Node& SimpleTree::Node::Front() const { return *(this->children.Front()); } //------------------------------------------------------------------------------ /** */ template typename SimpleTree::Node& SimpleTree::Node::Back() const { return *(this->children.Back()); } //------------------------------------------------------------------------------ /** */ template void SimpleTree::Node::Append(const VALUETYPE& val) { GPtr newNode = n_new(Node(*this, val)); this->children.Append(newNode); } //------------------------------------------------------------------------------ /** */ template void SimpleTree::Node::Erase(IndexT i) { this->children.EraseIndex(i); } //------------------------------------------------------------------------------ /** */ template const VALUETYPE& SimpleTree::Node::Value() const { return this->value; } //------------------------------------------------------------------------------ /** */ template VALUETYPE& SimpleTree::Node::Value() { return this->value; } //------------------------------------------------------------------------------ /** */ template IndexT SimpleTree::Node::Find(const VALUETYPE& val) const { IndexT i; SizeT num = this->children.Size(); for (i = 0; i < num; i++) { if (val == this->children[i]->Value()) { return i; } } return InvalidIndex; } //------------------------------------------------------------------------------ /** */ template SimpleTree::SimpleTree() { // empty } //------------------------------------------------------------------------------ /** */ template typename SimpleTree::Node& SimpleTree::Root() { return this->rootNode; } //------------------------------------------------------------------------------ /** */ template const typename SimpleTree::Node& SimpleTree::Root() const { return this->rootNode; } } // namespace Util //------------------------------------------------------------------------------