home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / runnable / ibmc / ibmclass / itbtre.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-26  |  8.9 KB  |  254 lines

  1. /* Copyright (c) IBM Corp. 1992 */
  2. #ifndef _ITBTRE_H
  3. #define _ITBTRE_H
  4.  
  5. #include <iglobals.h>
  6. #include <itcursor.h>
  7. #include <iitbtre.h>
  8.  
  9. template < class Element, class ElementOps, INumber pNumberOfChildren >
  10. class IGTabularTree
  11.   static ElementOps cvElementOps;
  12.  
  13.   class Node;
  14.   class Cursor;
  15.  
  16.   friend class Node;
  17.   friend class Cursor;
  18.  
  19.   class Node
  20.   { friend class IGTabularTree < Element, ElementOps, pNumberOfChildren >;
  21.     friend class Cursor;
  22.  
  23.     // must be layout compatible with ITabularTreeImpl::Node
  24.     // cannot derive because of unknown pNumberOfChildren in impl
  25.  
  26.     Node** ivParent;
  27.     Node* ivChildren [pNumberOfChildren];
  28.     Element ivElement;      
  29.  
  30.   public :
  31.                      Node                (Element const& element)
  32.              : ivElement (element) {};
  33.  
  34.     void*            operator new        (size_t s)
  35.              { return cvElementOps.allocate (s); }
  36.  
  37.     void             operator delete     (void* p)
  38.                      { cvElementOps.deallocate (p); }
  39.   };
  40.  
  41.   ITabularTreeImpl ivImpl;
  42.  
  43. public :
  44.  
  45.   class Cursor : public ITreeCursor {
  46.   protected:
  47.     IGTabularTree < Element, ElementOps,
  48.             pNumberOfChildren > const* ivCollection;
  49.     ITabularTreeImpl::Node **ivNode;
  50.     friend class IGTabularTree < Element, ElementOps, pNumberOfChildren >;
  51.  
  52.   public:
  53.     Cursor (IGTabularTree < Element, ElementOps,
  54.                 pNumberOfChildren > const& collection)
  55.     : ivCollection (&collection), ivNode (0) {};
  56.  
  57.     Boolean setToRoot ();
  58.     Boolean setToChild (IPosition);
  59.     Boolean setToFirstExistingChild ();
  60.     Boolean setToNextExistingChild ();
  61.     Boolean setToLastExistingChild ();
  62.     Boolean setToPreviousExistingChild ();
  63.     Boolean isValid () const;
  64.     void    invalidate ();
  65.  
  66.     Element const& element      ()
  67.     { return ivCollection->elementAt (*this); }
  68.  
  69.     Boolean operator== (Cursor const& cursor)
  70.     { return ivCollection == cursor.ivCollection &&
  71.              ivNode == cursor.ivNode; }
  72.     Boolean operator!= (Cursor const& cursor)
  73.     { return ! operator== (cursor); }
  74.   };
  75.  
  76.                   IGTabularTree               ();
  77.                   IGTabularTree               (IGTabularTree
  78.                          < Element,
  79.                            ElementOps,
  80.                            pNumberOfChildren > const&);
  81.                  ~IGTabularTree               ();
  82.  
  83.    IGTabularTree < Element, ElementOps, pNumberOfChildren >&
  84.           operator=                   (IGTabularTree
  85.                          < Element,
  86.                            ElementOps,
  87.                            pNumberOfChildren > const&);
  88.  
  89.    void           copy                        (IGTabularTree
  90.                          < Element,
  91.                            ElementOps,
  92.                            pNumberOfChildren > const&);
  93.    void           copySubtree                 (IGTabularTree
  94.                              < Element,
  95.                            ElementOps,
  96.                            pNumberOfChildren > const&,
  97.                            ITreeCursor const&);
  98.  
  99.    void           addAsRoot                   (Element const&);
  100.    void           addAsChild                  (ITreeCursor const&,
  101.                                IPosition,
  102.                                Element const&);
  103.  
  104.    void           attachAsRoot                (IGTabularTree
  105.                          < Element,
  106.                            ElementOps,
  107.                            pNumberOfChildren >&);
  108.    void           attachAsChild               (ITreeCursor const&,
  109.                                    IPosition,
  110.                                    IGTabularTree
  111.                          < Element,
  112.                            ElementOps,
  113.                            pNumberOfChildren >&);
  114.    void           attachSubtreeAsRoot         (IGTabularTree
  115.                          < Element,
  116.                            ElementOps,
  117.                            pNumberOfChildren >&,
  118.                                ITreeCursor const&);
  119.    void           attachSubtreeAsChild        (ITreeCursor const&,
  120.                                IPosition,
  121.                                IGTabularTree
  122.                          < Element,
  123.                            ElementOps,
  124.                            pNumberOfChildren >&,
  125.                                ITreeCursor const&);
  126.  
  127.    INumber        removeAll                   ();
  128.    INumber        removeSubtree               (ITreeCursor const&);
  129.  
  130.    Element const& elementAt                   (ITreeCursor const&) const;
  131.    Element&       elementAt                   (ITreeCursor const&);
  132.  
  133.    void           replaceAt                   (ITreeCursor const&,
  134.                            Element const&);
  135.  
  136.    INumber        numberOfChildren            () const;
  137.    INumber        numberOfElements            () const;
  138.    INumber        numberOfSubtreeElements     (ITreeCursor const&) const;
  139.    INumber        numberOfLeaves              () const;
  140.    INumber        numberOfSubtreeLeaves       (ITreeCursor const&) const;
  141.  
  142.    Boolean        isEmpty                     () const;
  143.  
  144.    ITreeCursor*   newCursor                   () const;
  145.  
  146.    Boolean        isRoot                      (ITreeCursor const&) const;
  147.    Boolean        isLeaf                      (ITreeCursor const&) const;
  148.    INumber        position                    (ITreeCursor const&) const;
  149.    Boolean        hasChild                    (IPosition,
  150.                            ITreeCursor const&) const;
  151.  
  152.    Boolean        setToRoot                   (ITreeCursor&) const;
  153.    Boolean        setToChild                  (IPosition, ITreeCursor&) const;
  154.    Boolean        setToParent                 (ITreeCursor&) const;
  155.    Boolean        setToFirstExistingChild     (ITreeCursor&) const;
  156.    Boolean        setToNextExistingChild      (ITreeCursor&) const;
  157.    Boolean        setToLastExistingChild      (ITreeCursor&) const;
  158.    Boolean        setToPreviousExistingChild  (ITreeCursor&) const;
  159.  
  160.    Boolean        setToFirst                  (ITreeCursor&,
  161.                            ITreeIterationOrder) const;
  162.    Boolean        setToNext                   (ITreeCursor&,
  163.                            ITreeIterationOrder) const;
  164.    Boolean        setToLast                   (ITreeCursor&,
  165.                            ITreeIterationOrder) const;
  166.    Boolean        setToPrevious               (ITreeCursor&,
  167.                            ITreeIterationOrder) const;
  168.  
  169.    Boolean        allElementsDo               (Boolean (*function)
  170.                                     (Element&, void*),
  171.                            ITreeIterationOrder,
  172.                                    void* additionalArgument = 0);
  173.  
  174.    Boolean        allElementsDo               (IIterator <Element>&,
  175.                            ITreeIterationOrder);
  176.  
  177.    Boolean        allElementsDo               (Boolean (*function)
  178.                                     (Element const&, void*),
  179.                            ITreeIterationOrder,
  180.                                    void* additionalArgument = 0)
  181.                           const;
  182.  
  183.    Boolean        allElementsDo               (IConstantIterator <Element>&,
  184.                            ITreeIterationOrder) const;
  185.  
  186.    Boolean        allSubtreeElementsDo        (ITreeCursor const&,
  187.                                Boolean (*function)
  188.                                 (Element&, void*),
  189.                                ITreeIterationOrder,
  190.                                void* additionalArgument = 0);
  191.  
  192.    Boolean        allSubtreeElementsDo        (ITreeCursor const&,
  193.                                IIterator <Element>&,
  194.                                ITreeIterationOrder);
  195.  
  196.    Boolean        allSubtreeElementsDo        (ITreeCursor const&,
  197.                                Boolean (*function)
  198.                                 (Element const&, void*),
  199.                                ITreeIterationOrder,
  200.                                void* additionalArgument = 0)
  201.                           const;
  202.  
  203.    Boolean        allSubtreeElementsDo        (ITreeCursor const&,
  204.                                IConstantIterator <Element>&,
  205.                                ITreeIterationOrder) const;
  206.  
  207. private:
  208.  
  209.    void           copySubtree                 (Node*& to, Node* from);
  210.    INumber        removeSubtree               (Node*);
  211.    Node*          newNode                     (Element const&);
  212.  
  213.    void           checkNotEmpty               () const;
  214.    void           checkCursorIsForThis        (ITreeCursor const&) const;
  215.    void           checkCursor                 (ITreeCursor const&) const;
  216.    void           checkCursorOfTree           (IGTabularTree
  217.                          < Element,
  218.                            ElementOps,
  219.                            pNumberOfChildren > const&,
  220.                            ITreeCursor const&) const;
  221.    void           checkPosition               (IPosition position) const;
  222.    void           checkRootNotExists          () const;
  223.    void           checkChildNotExists         (ITabularTreeImpl::Node*) const;
  224.  
  225.    static
  226.    Boolean        constantFunctionIteration   (void *function,
  227.                                                void* env,
  228.                                                void const* node);
  229.    static
  230.    Boolean        functionIteration           (void *function,
  231.                                                void* env,
  232.                                                void* node);
  233.    static
  234.    Boolean        constantIteratorIteration   (void* iterator,
  235.                                                void const* node);
  236.    static
  237.    Boolean        iteratorIteration           (void* iterator,
  238.                                                void* node);
  239. };
  240.  
  241. template < class Element, INumber pNumberOfChildren >
  242. class ITabularTree :
  243.   public IGTabularTree < Element, IStdOps < Element >, pNumberOfChildren > {
  244. };
  245.  
  246. #include <itbtre.if>
  247.  
  248. #ifdef __IBMCPP__
  249. #include <itbtre.c>
  250. #endif
  251.  
  252. #endif
  253.