home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cset21v1.zip / IBMCPP / IBMCLASS / ITBTRE.H < prev    next >
Text File  |  1993-09-22  |  13KB  |  282 lines

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