home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) IBM Corp. 1992 */
- #ifndef _ITBTRE_H
- #define _ITBTRE_H
-
- #include <iglobals.h>
- #include <itcursor.h>
- #include <iitbtre.h>
-
- template < class Element, class ElementOps, INumber pNumberOfChildren >
- class IGTabularTree
- {
- static ElementOps cvElementOps;
-
- class Node;
- class Cursor;
-
- friend class Node;
- friend class Cursor;
-
- class Node
- { friend class IGTabularTree < Element, ElementOps, pNumberOfChildren >;
- friend class Cursor;
-
- // must be layout compatible with ITabularTreeImpl::Node
- // cannot derive because of unknown pNumberOfChildren in impl
-
- Node** ivParent;
- Node* ivChildren [pNumberOfChildren];
- Element ivElement;
-
- public :
- Node (Element const& element)
- : ivElement (element) {};
-
- void* operator new (size_t s)
- { return cvElementOps.allocate (s); }
-
- void operator delete (void* p)
- { cvElementOps.deallocate (p); }
- };
-
- ITabularTreeImpl ivImpl;
-
- public :
-
- class Cursor : public ITreeCursor {
- protected:
- IGTabularTree < Element, ElementOps,
- pNumberOfChildren > const* ivCollection;
- ITabularTreeImpl::Node **ivNode;
- friend class IGTabularTree < Element, ElementOps, pNumberOfChildren >;
-
- public:
- Cursor (IGTabularTree < Element, ElementOps,
- pNumberOfChildren > const& collection)
- : ivCollection (&collection), ivNode (0) {};
-
- Boolean setToRoot ();
- Boolean setToChild (IPosition);
- Boolean setToFirstExistingChild ();
- Boolean setToNextExistingChild ();
- Boolean setToLastExistingChild ();
- Boolean setToPreviousExistingChild ();
- Boolean isValid () const;
- void invalidate ();
-
- Element const& element ()
- { return ivCollection->elementAt (*this); }
-
- Boolean operator== (Cursor const& cursor)
- { return ivCollection == cursor.ivCollection &&
- ivNode == cursor.ivNode; }
- Boolean operator!= (Cursor const& cursor)
- { return ! operator== (cursor); }
- };
-
- IGTabularTree ();
- IGTabularTree (IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren > const&);
- ~IGTabularTree ();
-
- IGTabularTree < Element, ElementOps, pNumberOfChildren >&
- operator= (IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren > const&);
-
- void copy (IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren > const&);
- void copySubtree (IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren > const&,
- ITreeCursor const&);
-
- void addAsRoot (Element const&);
- void addAsChild (ITreeCursor const&,
- IPosition,
- Element const&);
-
- void attachAsRoot (IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren >&);
- void attachAsChild (ITreeCursor const&,
- IPosition,
- IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren >&);
- void attachSubtreeAsRoot (IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren >&,
- ITreeCursor const&);
- void attachSubtreeAsChild (ITreeCursor const&,
- IPosition,
- IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren >&,
- ITreeCursor const&);
-
- INumber removeAll ();
- INumber removeSubtree (ITreeCursor const&);
-
- Element const& elementAt (ITreeCursor const&) const;
- Element& elementAt (ITreeCursor const&);
-
- void replaceAt (ITreeCursor const&,
- Element const&);
-
- INumber numberOfChildren () const;
- INumber numberOfElements () const;
- INumber numberOfSubtreeElements (ITreeCursor const&) const;
- INumber numberOfLeaves () const;
- INumber numberOfSubtreeLeaves (ITreeCursor const&) const;
-
- Boolean isEmpty () const;
-
- ITreeCursor* newCursor () const;
-
- Boolean isRoot (ITreeCursor const&) const;
- Boolean isLeaf (ITreeCursor const&) const;
- INumber position (ITreeCursor const&) const;
- Boolean hasChild (IPosition,
- ITreeCursor const&) const;
-
- Boolean setToRoot (ITreeCursor&) const;
- Boolean setToChild (IPosition, ITreeCursor&) const;
- Boolean setToParent (ITreeCursor&) const;
- Boolean setToFirstExistingChild (ITreeCursor&) const;
- Boolean setToNextExistingChild (ITreeCursor&) const;
- Boolean setToLastExistingChild (ITreeCursor&) const;
- Boolean setToPreviousExistingChild (ITreeCursor&) const;
-
- Boolean setToFirst (ITreeCursor&,
- ITreeIterationOrder) const;
- Boolean setToNext (ITreeCursor&,
- ITreeIterationOrder) const;
- Boolean setToLast (ITreeCursor&,
- ITreeIterationOrder) const;
- Boolean setToPrevious (ITreeCursor&,
- ITreeIterationOrder) const;
-
- Boolean allElementsDo (Boolean (*function)
- (Element&, void*),
- ITreeIterationOrder,
- void* additionalArgument = 0);
-
- Boolean allElementsDo (IIterator <Element>&,
- ITreeIterationOrder);
-
- Boolean allElementsDo (Boolean (*function)
- (Element const&, void*),
- ITreeIterationOrder,
- void* additionalArgument = 0)
- const;
-
- Boolean allElementsDo (IConstantIterator <Element>&,
- ITreeIterationOrder) const;
-
- Boolean allSubtreeElementsDo (ITreeCursor const&,
- Boolean (*function)
- (Element&, void*),
- ITreeIterationOrder,
- void* additionalArgument = 0);
-
- Boolean allSubtreeElementsDo (ITreeCursor const&,
- IIterator <Element>&,
- ITreeIterationOrder);
-
- Boolean allSubtreeElementsDo (ITreeCursor const&,
- Boolean (*function)
- (Element const&, void*),
- ITreeIterationOrder,
- void* additionalArgument = 0)
- const;
-
- Boolean allSubtreeElementsDo (ITreeCursor const&,
- IConstantIterator <Element>&,
- ITreeIterationOrder) const;
-
- private:
-
- void copySubtree (Node*& to, Node* from);
- INumber removeSubtree (Node*);
- Node* newNode (Element const&);
-
- void checkNotEmpty () const;
- void checkCursorIsForThis (ITreeCursor const&) const;
- void checkCursor (ITreeCursor const&) const;
- void checkCursorOfTree (IGTabularTree
- < Element,
- ElementOps,
- pNumberOfChildren > const&,
- ITreeCursor const&) const;
- void checkPosition (IPosition position) const;
- void checkRootNotExists () const;
- void checkChildNotExists (ITabularTreeImpl::Node*) const;
-
- static
- Boolean constantFunctionIteration (void *function,
- void* env,
- void const* node);
- static
- Boolean functionIteration (void *function,
- void* env,
- void* node);
- static
- Boolean constantIteratorIteration (void* iterator,
- void const* node);
- static
- Boolean iteratorIteration (void* iterator,
- void* node);
- };
-
- template < class Element, INumber pNumberOfChildren >
- class ITabularTree :
- public IGTabularTree < Element, IStdOps < Element >, pNumberOfChildren > {
- };
-
- #include <itbtre.if>
-
- #ifdef __IBMCPP__
- #include <itbtre.c>
- #endif
-
- #endif