home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / ibmcli / iset.hp_ / ISET.HPP
Encoding:
C/C++ Source or Header  |  1992-10-26  |  8.6 KB  |  187 lines

  1. #if !defined(_ISet_)
  2. #define _ISet_
  3.  
  4. /*--------------------------------------------------------------*/
  5. /* CLASS NAMES:   ISet                                          */
  6. /*                                                              */
  7. /* DESCRIPTION  : This file defines a class used to implement   */
  8. /*                a set mechanism.                              */
  9. /*                                                              */
  10. /*                The collection classes in ICLUI are a      */
  11. /*                merger between the SList/SLink generic        */
  12. /*                collection classes described by Bjarne        */
  13. /*                Stroustrup in Vol 1 of "The C++ Programming   */
  14. /*                Language" and the original typeless,          */
  15. /*                shallow-copy classes designed by Mike Chan    */
  16. /*                and Kevin Leong in Dallas.                    */
  17. /*                                                              */
  18. /*                Hungarian form: "set"                         */
  19. /*                                                              */
  20. /* CHANGE ACTIVITY:                                             */
  21. /* ---------------                                              */
  22. /*   DATE:     INITIAL:       DESCRIPTION:                      */
  23. /*                                                              */
  24. /* 03/01/91    KKL            Design & architecture             */
  25. /* 03/28/91    WC             Init declaration                  */
  26. /* 05/01/91    KKL            Extend to support IGENOBJ         */
  27. /* 06/08/91    KKL            Tidy-up and re-organize methods.  */
  28. /* 02/17/92    CAJ            Re-work to Stroustrup             */
  29. /* 09/29/92    RDL            c/IGSet/IGSet1 - BB Conflict      */
  30. /*--------------------------------------------------------------*/
  31. /* Copyright (c) IBM Corporation 1991                           */
  32. /*--------------------------------------------------------------*/
  33.  
  34. #include <ilinklst.hpp>
  35.  
  36. class ISet;
  37.  
  38.  
  39. /*--------------------------------------------------------------*/
  40. /* Define the Set Class                                         */
  41. /*--------------------------------------------------------------*/
  42. class ISet : public ILinkList
  43. {
  44.    public:
  45.    /*-----------------------------------------------------------*/
  46.    /* Define the Cursor Class                                   */
  47.    /*    Note: The set cursor only "sees" objects which have not*/
  48.    /*          been filtered out of the set.                    */
  49.    /*-----------------------------------------------------------*/
  50.    class Cursor : public ILinkListCursor
  51.    {
  52.    public:
  53.    
  54.          /************************************************************/
  55.          /* Normal Set Cursor. Use to move forward and               */
  56.          /* backward through the set.                                */
  57.          /************************************************************/
  58.          Cursor(ISet& iset);
  59.    
  60.    
  61.          /*********************************************************/
  62.          /*  Return items in the set (or set view) & move cursor. */
  63.          /*********************************************************/
  64.              void* first();
  65.              void* last();
  66.              void* next();
  67.              void* prior();
  68.  
  69.   virtual Boolean setToFirst()     { if (first()!=0) 
  70.                                         return true;
  71.                                      return false;}
  72.   virtual Boolean setToNext()      { if (next()!=0 ) 
  73.                                         return true; 
  74.                                      return false;}
  75.   virtual Boolean isValid() const  { if (current()!=0) 
  76.                                         return true;
  77.                                      return false; }
  78.   virtual void    invalidate()     { reset();  }
  79.  
  80.    private:
  81.              PFNCOMPARE pfnClCompare;
  82.              PFNFILTER  pfnClFilter;
  83.              void*      pClMask;
  84.              ISet*      set()  { return (ISet*)plnklCl; }
  85.    };
  86.   friend class Cursor;
  87. protected:
  88.       /************************************************************/
  89.       /*  CONSTRUCTORS                                            */
  90.       /************************************************************/
  91.           ISet(Boolean fDestruct=false,unsigned long ulBase=0)
  92.               : ILinkList(fDestruct,ulBase) {}
  93. //          ISet(void* pObj,Boolean fDestruct=false, unsigned long ulBase=0)
  94. //              : ILinkList(pObj, fDestruct, ulBase) {}
  95.  
  96.           // Copy Constructor
  97.           ISet(const ISet& iset)
  98.               : ILinkList((const ILinkList&)iset) {}
  99.  
  100.       /************************************************************/
  101.       /*  Filter/sort set                                         */
  102.       /************************************************************/
  103.              void  filter(PFNFILTER pfnFilter);
  104.           Boolean  includes(void* pobj);
  105.  
  106.       /************************************************************/
  107.       /*  Add to Set                                              */
  108.       /************************************************************/
  109.           Boolean  add(void* pobj);
  110.           Boolean  addFirst(void* pobj);
  111.           Boolean  addLast(void* pobj);
  112.           Boolean  addAfter(void* pobj, unsigned long lIndex);
  113.  
  114.       /************************************************************/
  115.       /*  Set Assignment                                          */
  116.       /************************************************************/
  117.           ISet& operator=(const ISet& iset);
  118.  
  119.       /************************************************************/
  120.       /*  "Destructive" Set Union and Intersection                */
  121.       /*   - result is left in receiver set                       */
  122.       /************************************************************/
  123.           void unionWith(const ISet& iset);
  124.           ISet&  operator|=(const ISet& iset);
  125.  
  126.           ISet& intersectionWith(const ISet& iset);
  127.           ISet&  operator&=(const ISet& iset);
  128.  
  129.       /************************************************************/
  130.       /*  "Non-Destructive" Set Union and Intersection            */
  131.       /*   - receiver set unchanged, result put in new set        */
  132.       /************************************************************/
  133.           ISet& unionOf(const ISet& iset);
  134.           ISet&  operator|(const ISet& iset);
  135.  
  136.           ISet& intersectOf(const ISet& iset);
  137.           ISet&  operator&(const ISet& iset);
  138.  
  139. protected:
  140.           virtual void  deleteObj(void* pobj){ delete pobj; };
  141.           virtual Boolean  isEqual(void* pobj1, void* pobj2){return true;};
  142.           INode* search(void *pObj, Boolean fIsForward,
  143.                         INode* pnodeStart,PFNCOMPARE pfnCompare);
  144.  
  145. };
  146.  
  147.  
  148.       /*********************************************************/
  149.       /* Generated set class macro definition                  */
  150.       /*   - name2 macro is  defined in ILINKLST.HPP           */
  151.       /*********************************************************/
  152. #define IGSet1(type)       name2(IGSet1,type)
  153.  
  154. #define IGSet1declare(type) \
  155. class IGSet1(type) : public ISet {\
  156. public: \
  157.    class Cursor : public ISet::Cursor {\
  158.    public:\
  159.        Cursor(IGSet1(type)& iset) \
  160.            : ISet::Cursor(iset) {}\
  161.     Boolean setToFirst() { return ISet::Cursor::setToFirst(); }\
  162.     Boolean setToNext()  { return ISet::Cursor::setToNext(); }\
  163.     Boolean isValid() const   { return ISet::Cursor::isValid(); }\
  164.        void invalidate() { ISet::Cursor::invalidate(); }\
  165.    }; \
  166.     IGSet1(type)() : ISet(false,0) { } \
  167.     void  removeAt(const Cursor& cursor)     { ILinkList::remove(cursor.current()); }\
  168.     type* elementAt(const Cursor& cursor)    { return (type*)cursor.current(); }\
  169.     type* elementAtPosition(unsigned long pos)  { return (type*)atIndex(pos-1); }\
  170.     unsigned long numberOfElements()            { return count(); }\
  171.     Boolean  add(type* pobj)      { return ISet::addLast(pobj); }\
  172.     void unionWith(const IGSet1(type) & iset) \
  173.                      { ISet::unionWith(iset); }\
  174.     IGSet1(type) & operator=(const IGSet1(type) & iset) \
  175.                      { return ( IGSet1(type) &)ISet::operator=(iset); }\
  176. protected:\
  177.     void   deleteObj(void* pobj){ delete (type*)pobj; }\
  178.     INode* search(type* pobj,Boolean fForward,INode* pnStart,PFNCOMPARE pfnC) \
  179.               { return ISet::search(pobj,fForward,pnStart,pfnC); }\
  180. private: \
  181.     Boolean  includes(void* pobj) { return ISet::includes(pobj); }\
  182.     Boolean  isEqual(void* pobj1, void* pobj2) \
  183.               { return ((type*)pobj1 == (type*)pobj2); }\
  184. };
  185.  
  186. #endif
  187.