home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
IBMCLASS
/
ITBTRE.IF
< prev
next >
Wrap
Text File
|
1993-09-22
|
22KB
|
542 lines
/*******************************************************************************
* *
* COPYRIGHT: *
* IBM C/C++ Tools Version 2.01 - Collection Class Library *
* Licensed Materials - Property of IBM *
* (C) Copyright IBM Corporation 1992, 1993 *
* All Rights Reserved *
* US Government Users Restricted Rights - Use, duplication, or disclosure *
* restricted by GSA ADP Schedule Contract with IBM Corp. *
* *
*******************************************************************************/
#include <ibexcept.h>
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
checkCursorIsForThis (ITreeCursor const& cursor) const
{
#ifndef INO_CHECKS
ICHECK (((Cursor const&)cursor).isFor (*this),
ICursorInvalidException, ICursorNotForThisText)
#endif
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
checkCursor (ITreeCursor const& cursor) const
{
#ifndef INO_CHECKS
ICHECK (((Cursor const&)cursor).isFor (*this),
ICursorInvalidException, ICursorNotForThisText)
ICHECK (cursor.isValid (), ICursorInvalidException, IInvalidCursorText)
#endif
#ifdef IALL_CHECKS
ICHECK (ivImpl.checkNode(((Cursor const&)cursor).ivNode),
ICursorInvalidException, ICursorNotContainedText)
#endif
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
checkCursorOfTree (IGTabularTree < Element,
ElementOps,
pNumberOfChildren > const& tree,
ITreeCursor const& cursor) const
{
#ifndef INO_CHECKS
ICHECK (((Cursor const&)cursor).ivCollection == &tree,
ICursorInvalidException, ICursorNotForGivenText)
ICHECK (cursor.isValid (), ICursorInvalidException, IInvalidCursorText)
#endif
#ifdef IALL_CHECKS
ICHECK (tree.ivImpl.checkNode(((Cursor const&)cursor).ivNode),
ICursorInvalidException, ICursorNotContainedText)
#endif
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
checkPosition (IPosition position) const
{
#ifndef INO_CHECKS
ICHECK (1 <= position && position <= pNumberOfChildren,
IPositionInvalidException, IInvalidPositionText)
#endif
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
checkRootNotExists () const
{
#ifndef INO_CHECKS
ICHECK (ivImpl.ivRoot == 0,
IRootAlreadyExistsException, IRootAlreadyExistsText)
#endif
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
checkChildNotExists (ITabularTreeImpl::Node* n) const
{
#ifndef INO_CHECKS
ICHECK (n == 0, IChildAlreadyExistsException, IChildAlreadyExistsText)
#endif
}
template < class Element, class ElementOps, int numberOfChildren >
inline void IGTabularTree < Element, ElementOps, numberOfChildren >::
copySubtree (Node* from)
{ if (from == 0)
ivImpl.ivRoot = 0;
else {
copySubtree ((Node*&)ivImpl.ivRoot, from);
ivImpl.ivRoot->ivParent = 0;
}
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IGTabularTree < Element, ElementOps, pNumberOfChildren >::
IGTabularTree ()
: ivImpl (pNumberOfChildren)
{
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IGTabularTree < Element, ElementOps, pNumberOfChildren >::
IGTabularTree (IGTabularTree < Element,
ElementOps,
pNumberOfChildren > const& tabularTree)
: ivImpl (pNumberOfChildren)
{ copySubtree ((Node*)tabularTree.ivImpl.ivRoot);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IGTabularTree < Element, ElementOps, pNumberOfChildren >::
~IGTabularTree ()
{ removeAll ();
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IGTabularTree < Element, ElementOps, pNumberOfChildren >&
IGTabularTree < Element, ElementOps, pNumberOfChildren >::
operator= (IGTabularTree < Element, ElementOps, pNumberOfChildren > const& tree)
{ copy (tree);
return *this;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
copy (IGTabularTree < Element, ElementOps, pNumberOfChildren > const& tree)
{ if (&tree != this)
{ removeAll ();
copySubtree ((Node*)tree.ivImpl.ivRoot);
}
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
copySubtree (IGTabularTree < Element,
ElementOps,
pNumberOfChildren > const& tree,
ITreeCursor const& cursor)
{ checkCursorOfTree (tree, cursor);
if (&tree != this) {
removeAll ();
copySubtree ((Node*)((Cursor const&)cursor).ivNode);
}
else {
IGTabularTree < Element, ElementOps, pNumberOfChildren > t;
t.attachSubtreeAsRoot (*this, cursor);
removeAll ();
attachAsRoot (t);
}
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
addAsRoot (Element const& element)
{ checkRootNotExists ();
ivImpl.ivRoot = (ITabularTreeImpl::Node*) newNode (element);
ivImpl.initNode (ivImpl.ivRoot, 0);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
addAsChild (ITreeCursor const& cursor,
IPosition position,
Element const& element)
{ checkCursor (cursor);
checkPosition (position);
ITabularTreeImpl::Node* parent = ((Cursor const&)cursor).ivNode;
ITabularTreeImpl::Node*& child = parent->ivChildren [position-1];
checkChildNotExists (child);
child = (ITabularTreeImpl::Node*) newNode (element);
ivImpl.initNode (child, parent);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
attachAsRoot (IGTabularTree < Element, ElementOps, pNumberOfChildren >& tree)
{ checkRootNotExists ();
ivImpl.ivRoot = tree.ivImpl.ivRoot;
tree.ivImpl.ivRoot = 0;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
attachAsChild (ITreeCursor const& cursor, IPosition position,
IGTabularTree < Element, ElementOps, pNumberOfChildren >& tree)
{ checkCursor (cursor);
checkPosition (position);
ITabularTreeImpl::Node* parent = ((Cursor const&)cursor).ivNode;
ITabularTreeImpl::Node*& child = parent->ivChildren [position-1];
checkChildNotExists (child);
child = tree.ivImpl.ivRoot;
if (child != 0)
child->ivParent = parent;
tree.ivImpl.ivRoot = 0;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
attachSubtreeAsRoot (IGTabularTree < Element,
ElementOps,
pNumberOfChildren >& tree,
ITreeCursor const& cursor)
{ checkCursorOfTree (tree, cursor);
checkRootNotExists ();
ITabularTreeImpl::Node* node = ((Cursor const&)cursor).ivNode;
ivImpl.attachSubtreeAsRoot (tree.ivImpl, node);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
attachSubtreeAsChild (ITreeCursor const& cursorTo,
IPosition pos,
IGTabularTree < Element,
ElementOps,
pNumberOfChildren >& tree,
ITreeCursor const& cursorFrom)
{ checkCursorOfTree (tree, cursorFrom);
checkCursor (cursorTo);
checkPosition (pos);
ITabularTreeImpl::Node* parent = ((Cursor const&)cursorTo).ivNode;
ITabularTreeImpl::Node*& child = parent->ivChildren [pos-1];
checkChildNotExists (child);
ITabularTreeImpl::Node* nodeFrom = ((Cursor const&)cursorFrom).ivNode;
ivImpl.attachSubtreeAsChild (parent, pos, tree.ivImpl, nodeFrom);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
removeAll ()
{ INumber result;
if (ivImpl.ivRoot != 0) {
result = removeSubtree ((Node*)ivImpl.ivRoot);
ivImpl.ivRoot = 0;
}
else
result = 0;
return result;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
removeSubtree (ITreeCursor const& cursor)
{ checkCursor (cursor);
ITabularTreeImpl::Node* node = ((Cursor const&)cursor).ivNode;
if (node->ivParent == 0)
ivImpl.ivRoot = 0;
else
node->ivParent->ivChildren [position (cursor) - 1] = 0;
return removeSubtree ((Node*)node);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline Element const& IGTabularTree < Element, ElementOps, pNumberOfChildren >::
elementAt (ITreeCursor const& cursor) const
{ checkCursor (cursor);
return ((Node*)((Cursor const&)cursor).ivNode)->ivElement;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline Element& IGTabularTree < Element, ElementOps, pNumberOfChildren >::
elementAt (ITreeCursor const& cursor)
{ checkCursor (cursor);
return ((Node*)((Cursor const&)cursor).ivNode)->ivElement;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline void IGTabularTree < Element, ElementOps, pNumberOfChildren >::
replaceAt (ITreeCursor const& cursor, Element const& element)
{ checkCursor (cursor);
elementOps().assign
(((Node*)((Cursor const&)cursor).ivNode)->ivElement, element);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
numberOfChildren () const
{ return pNumberOfChildren;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
numberOfElements () const
{ if (ivImpl.ivRoot == 0)
return 0;
else
return ivImpl.numberOfSubtreeElements (ivImpl.ivRoot);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
numberOfSubtreeElements (ITreeCursor const& cursor) const
{ checkCursor (cursor);
return ivImpl.numberOfSubtreeElements (((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
numberOfLeaves () const
{ if (ivImpl.ivRoot == 0)
return 0;
else
return ivImpl.numberOfSubtreeLeaves (ivImpl.ivRoot);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
numberOfSubtreeLeaves (ITreeCursor const& cursor) const
{ checkCursor (cursor);
return ivImpl.numberOfSubtreeLeaves (((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
isEmpty () const
{ return ivImpl.ivRoot == 0;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline ITreeCursor* IGTabularTree < Element, ElementOps, pNumberOfChildren >::
newCursor () const
{ ITreeCursor* result = new Cursor (*this);
ICHECK (result != 0, IOutOfMemory, IOutOfMemoryText)
return result;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
isRoot (ITreeCursor const& cursor) const
{ checkCursor (cursor);
return ivImpl.ivRoot == ((Cursor const&)cursor).ivNode;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
isLeaf (ITreeCursor const& cursor) const
{ checkCursor (cursor);
return ivImpl.isLeaf (((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline INumber IGTabularTree < Element, ElementOps, pNumberOfChildren >::
position (ITreeCursor const& cursor) const
{ checkCursor (cursor);
return ivImpl.position (((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
hasChild (IPosition position, ITreeCursor const& cursor) const
{ checkCursor (cursor);
checkPosition (position);
return (((Cursor const&)cursor).ivNode)->ivChildren [position-1] != 0;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToRoot (ITreeCursor& cursor) const
{ checkCursorIsForThis (cursor);
((Cursor&)cursor).ivNode = (ITabularTreeImpl::Node*)ivImpl.ivRoot;
return ivImpl.ivRoot != 0;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToChild (IPosition position, ITreeCursor& cursor) const
{ checkCursor (cursor);
checkPosition (position);
ITabularTreeImpl::Node* node = ((Cursor const&)cursor).ivNode;
((Cursor&)cursor).ivNode = node->ivChildren [position-1];
return ((Cursor const&)cursor).ivNode != 0;
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToParent (ITreeCursor& cursor) const
{ checkCursor (cursor);
return ivImpl.setToParent (((Cursor&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToFirstExistingChild (ITreeCursor& cursor) const
{ checkCursor (cursor);
return ivImpl.setToFirstExistingChild (((Cursor&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToNextExistingChild (ITreeCursor& cursor) const
{ checkCursor (cursor);
return ivImpl.setToNextExistingChild (((Cursor&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToLastExistingChild (ITreeCursor& cursor) const
{ checkCursor (cursor);
return ivImpl.setToLastExistingChild (((Cursor&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToPreviousExistingChild (ITreeCursor& cursor) const
{ checkCursor (cursor);
return ivImpl.setToPreviousExistingChild (((Cursor&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToFirst (ITreeCursor& cursor, ITreeIterationOrder order) const
{ checkCursorIsForThis (cursor);
return ivImpl.setToFirst (((Cursor&)cursor).ivNode, order);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToNext (ITreeCursor& cursor, ITreeIterationOrder order) const
{ checkCursorIsForThis (cursor);
return ivImpl.setToNext (((Cursor&)cursor).ivNode, order);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToLast (ITreeCursor& cursor, ITreeIterationOrder order) const
{ checkCursorIsForThis (cursor);
return ivImpl.setToLast (((Cursor&)cursor).ivNode, order);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
setToPrevious (ITreeCursor& cursor, ITreeIterationOrder order) const
{ checkCursorIsForThis (cursor);
return ivImpl.setToPrevious (((Cursor&)cursor).ivNode, order);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allElementsDo (IBoolean (*function) (Element&, void*),
ITreeIterationOrder order,
void* additionalArgument)
{ if (ivImpl.ivRoot == 0)
return True;
else
return ivImpl.allElementsDo (function, order, additionalArgument,
functionIteration, ivImpl.ivRoot);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allElementsDo (IIterator <Element>& iterator, ITreeIterationOrder order)
{ if (ivImpl.ivRoot == 0)
return True;
else
return ivImpl.allElementsDo (&iterator, order,
iteratorIteration, ivImpl.ivRoot);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allElementsDo (IBoolean (*function) (Element const&, void*),
ITreeIterationOrder order,
void* additionalArgument) const
{ if (ivImpl.ivRoot == 0)
return True;
else
return ivImpl.allElementsDo (function, order, additionalArgument,
constantFunctionIteration, ivImpl.ivRoot);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allElementsDo (IConstantIterator <Element>& iterator,
ITreeIterationOrder order) const
{ if (ivImpl.ivRoot == 0)
return True;
else
return ivImpl.allElementsDo (&iterator, order,
constantIteratorIteration, ivImpl.ivRoot);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
isConsistent () const
{ return ivImpl.isConsistent ();
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allSubtreeElementsDo (ITreeCursor const& cursor,
IBoolean (*function) (Element&, void*),
ITreeIterationOrder order,
void* additionalArgument)
{ checkCursor (cursor);
return ivImpl.allElementsDo
(function, order, additionalArgument,
functionIteration, ((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allSubtreeElementsDo (ITreeCursor const& cursor,
IIterator <Element>& iterator,
ITreeIterationOrder order)
{ checkCursor (cursor);
return ivImpl.allElementsDo (&iterator, order, iteratorIteration,
((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allSubtreeElementsDo (ITreeCursor const& cursor,
IBoolean (*function) (Element const&, void*),
ITreeIterationOrder order,
void* additionalArgument) const
{ checkCursor (cursor);
return ivImpl.allElementsDo
(function, order, additionalArgument,
constantFunctionIteration, ((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IBoolean IGTabularTree < Element, ElementOps, pNumberOfChildren >::
allSubtreeElementsDo (ITreeCursor const& cursor,
IConstantIterator <Element>& iterator,
ITreeIterationOrder order) const
{ checkCursor (cursor);
return ivImpl.allElementsDo (&iterator, order, constantIteratorIteration,
((Cursor const&)cursor).ivNode);
}
template < class Element, class ElementOps, INumber pNumberOfChildren >
inline IGTabularTree < Element, ElementOps, pNumberOfChildren >::Node*
IGTabularTree < Element, ElementOps, pNumberOfChildren >::
newNode (Element const& element)
{ Node *result = new Node (element);
ICHECK (result != 0, IOutOfMemory, IOutOfMemoryText)
return result;
}