/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/

#ifndef _SCTS_TEMPLATES_H
#define _SCTS_TEMPLATES_H

template <class ItemData> class SctsTree
{
public:
	class Item
	{
		friend SctsTree;
	private:
		Item *parent;
		Item *firstChild;
		Item *lastChild;
		Item *next;
		Item *prev;
	public:
		ItemData data;
		Item(ItemData _data) : data(_data),parent(0),firstChild(0),lastChild(0),next(0),prev(0) {};
		~Item()
		{
			while (firstChild)
				delete firstChild;
			if (data) delete data;
			if (!parent) return;
			if (prev) 
				prev->next=next;
			else
				parent->firstChild=next;
			if (next) 
				next->prev=prev;
			else
				parent->lastChild=prev;
		}
	};

private:
	Item *root;

public:
	Item *InsertSiblingItem(Item *pItem,ItemData data,bool after=true)
	{
		Item *newItem;
		SCTSASSERT(pItem && pItem!=root);
		newItem=new Item(data);
		newItem->parent=pItem->parent;
		if (after)
		{
			newItem->prev=pItem;
			if (pItem->next)
				newItem->next=pItem->next;
			else
				pItem->parent->lastChild=newItem;
			pItem->next=newItem;
		}
		else
		{
			newItem->next=pItem;
			if (pItem->prev)
				newItem->prev=pItem->prev;
			else
				pItem->parent->firstChild=newItem;
			pItem->prev=newItem;
		}
		return newItem;
	}

	Item *InsertChildItem(Item *pItem,ItemData data,bool last=true)
	{
		Item *newItem;
		if (!pItem) pItem=root;
		newItem=new Item(data);
		newItem->parent=pItem;
		if (!pItem->firstChild)
		{
			pItem->lastChild=pItem->firstChild=newItem;
			return newItem;
		}
		if (last)
		{
			newItem->prev=pItem->lastChild;
			pItem->lastChild->next=newItem;
			pItem->lastChild=newItem;
		}
		else
		{
			newItem->next=pItem->firstChild;
			pItem->firstChild->prev=newItem;
			pItem->firstChild=newItem;
		}
		return newItem;
	}

	void DeleteItem(Item *pItem) {SCTSASSERT(pItem && pItem!=root);delete pItem;};
	Item *GetRootItem() {return root;};
	Item *GetNextSiblingItem(Item *pItem) {SCTSASSERT(pItem);return pItem->next;};
	Item *GetPrevSiblingItem(Item *pItem) {SCTSASSERT(pItem);return pItem->prev;};
	Item *GetParentItem(Item *pItem) {SCTSASSERT(pItem);return pItem->parent;};
	Item *GetFirstChildItem(Item *pItem) {SCTSASSERT(pItem);return pItem->firstChild;};
	Item *GetLastChildItem(Item *pItem) {SCTSASSERT(pItem);return pItem->lastChild;};
	SctsTree(ItemData data=0) {root=new Item(data);};
	~SctsTree() {delete root;}
};


#endif
