| 1 | 
         48 | 
         robfinch | 
         #ifndef SET_H
  | 
      
      
         | 2 | 
          | 
          | 
         #define SET_H
  | 
      
      
         | 3 | 
          | 
          | 
         #ifndef STRING_H
  | 
      
      
         | 4 | 
          | 
          | 
         #include <string.h>
  | 
      
      
         | 5 | 
          | 
          | 
         #endif
  | 
      
      
         | 6 | 
          | 
          | 
         /*      Header file for set routines.
  | 
      
      
         | 7 | 
          | 
          | 
         */
  | 
      
      
         | 8 | 
          | 
          | 
          
  | 
      
      
         | 9 | 
          | 
          | 
         #define SET_DEFAULT_SIZE     (128 / (sizeof(int) << 3))
  | 
      
      
         | 10 | 
          | 
          | 
         #define SET_DIV_WSIZE(bit)   (bit) / (sizeof(int) << 3)
  | 
      
      
         | 11 | 
          | 
          | 
         #define SET_ROUND(bit)       (((SET_DIV_WSIZE(bit) + 8) >> 3) << 3)
  | 
      
      
         | 12 | 
          | 
          | 
         #define SET_GBIT(s,bit,op) \
  | 
      
      
         | 13 | 
          | 
          | 
             (((s).map)[SET_DIV_WSIZE(bit)] (op) (1 << (bit) & 15))
  | 
      
      
         | 14 | 
          | 
          | 
         #define SET_BPW              (sizeof(int) << 3)
  | 
      
      
         | 15 | 
          | 
          | 
         #define SET_BMASK            (SET_BPW - 1)
  | 
      
      
         | 16 | 
          | 
          | 
         #define SET_NBIT             ((SET_BPW & 32) ? 5 : 4)
  | 
      
      
         | 17 | 
          | 
          | 
          
  | 
      
      
         | 18 | 
          | 
          | 
         #define SET_EQUIV   0
  | 
      
      
         | 19 | 
          | 
          | 
         #define SET_DISJ    1
  | 
      
      
         | 20 | 
          | 
          | 
         #define SET_INTER   2
  | 
      
      
         | 21 | 
          | 
          | 
         #define SET_UNKNOWN     3
  | 
      
      
         | 22 | 
          | 
          | 
          
  | 
      
      
         | 23 | 
          | 
          | 
         extern void *allocx(int);
  | 
      
      
         | 24 | 
          | 
          | 
          
  | 
      
      
         | 25 | 
          | 
          | 
         class CSet //: public CObject
  | 
      
      
         | 26 | 
          | 
          | 
         {
  | 
      
      
         | 27 | 
          | 
          | 
            unsigned int dmap[SET_DEFAULT_SIZE];  // Default bitmap - 128 bits
  | 
      
      
         | 28 | 
          | 
          | 
            unsigned int *map;                           // Pointer to bit map of elements
  | 
      
      
         | 29 | 
          | 
          | 
            unsigned __int16 MemberPtr;    // for NextMember
  | 
      
      
         | 30 | 
          | 
          | 
            unsigned __int16 nbits;        // Number of bits in map.
  | 
      
      
         | 31 | 
          | 
          | 
            unsigned __int8 size;          // number of int's of bitmap.
  | 
      
      
         | 32 | 
          | 
          | 
            unsigned __int8 compl;         // Negative true set if compl is true
  | 
      
      
         | 33 | 
          | 
          | 
          
  | 
      
      
         | 34 | 
          | 
          | 
            void enlarge(int);            // increase size of set
  | 
      
      
         | 35 | 
          | 
          | 
            int cmp(CSet&) const;
  | 
      
      
         | 36 | 
          | 
          | 
            int SetTest(CSet &);
  | 
      
      
         | 37 | 
          | 
          | 
            void allocBitStorage();
  | 
      
      
         | 38 | 
          | 
          | 
         public:
  | 
      
      
         | 39 | 
          | 
          | 
                 void Create() {
  | 
      
      
         | 40 | 
          | 
          | 
                         map = dmap;
  | 
      
      
         | 41 | 
          | 
          | 
                         size = SET_DEFAULT_SIZE;
  | 
      
      
         | 42 | 
          | 
          | 
                         nbits = SET_DEFAULT_SIZE * sizeof(int) << 3;
  | 
      
      
         | 43 | 
          | 
          | 
                         MemberPtr = 0;
  | 
      
      
         | 44 | 
          | 
          | 
                         compl = 0;
  | 
      
      
         | 45 | 
          | 
          | 
                         clear();
  | 
      
      
         | 46 | 
          | 
          | 
                 };
  | 
      
      
         | 47 | 
          | 
          | 
                 static CSet *MakeNew() {
  | 
      
      
         | 48 | 
          | 
          | 
                         CSet *p;
  | 
      
      
         | 49 | 
          | 
          | 
          
  | 
      
      
         | 50 | 
          | 
          | 
                         p = (CSet *)allocx(sizeof(CSet));
  | 
      
      
         | 51 | 
          | 
          | 
                         p->Create();
  | 
      
      
         | 52 | 
          | 
          | 
                         return (p);
  | 
      
      
         | 53 | 
          | 
          | 
                 };
  | 
      
      
         | 54 | 
          | 
          | 
                 CSet() {
  | 
      
      
         | 55 | 
          | 
          | 
                         Create();
  | 
      
      
         | 56 | 
          | 
          | 
                 };
  | 
      
      
         | 57 | 
          | 
          | 
                 CSet(const CSet &s) { copy(s); };
  | 
      
      
         | 58 | 
          | 
          | 
                 ~CSet() {
  | 
      
      
         | 59 | 
          | 
          | 
                         //if (map != dmap)
  | 
      
      
         | 60 | 
          | 
          | 
                         //      delete[] map;
  | 
      
      
         | 61 | 
          | 
          | 
                 };
  | 
      
      
         | 62 | 
          | 
          | 
          
  | 
      
      
         | 63 | 
          | 
          | 
            // Assignment other operators
  | 
      
      
         | 64 | 
          | 
          | 
            //---------------------------
  | 
      
      
         | 65 | 
          | 
          | 
                 void copy(const CSet &s);
  | 
      
      
         | 66 | 
          | 
          | 
          
  | 
      
      
         | 67 | 
          | 
          | 
             CSet& operator=(CSet&);                             // Assignment (copy)
  | 
      
      
         | 68 | 
          | 
          | 
                 CSet operator!();                               // s = not s1
  | 
      
      
         | 69 | 
          | 
          | 
                 CSet operator|(CSet);                           // s = s1 union s2
  | 
      
      
         | 70 | 
          | 
          | 
                 CSet& operator|=(CSet);                         // s1 = s1 union s2
  | 
      
      
         | 71 | 
          | 
          | 
                 CSet operator&(CSet);                           // s = s1 intersection s2
  | 
      
      
         | 72 | 
          | 
          | 
                 CSet& operator&=(CSet);                         // s1 = s1 intersection s2
  | 
      
      
         | 73 | 
          | 
          | 
                 int operator&(int bit)
  | 
      
      
         | 74 | 
          | 
          | 
                 { return isMember(bit); };  // is n a member of s ?
  | 
      
      
         | 75 | 
          | 
          | 
                 CSet operator+(int);        // s = s1 plus element
  | 
      
      
         | 76 | 
          | 
          | 
                 CSet operator-(int);        // s = s1 minus element
  | 
      
      
         | 77 | 
          | 
          | 
                 CSet operator-(CSet);     // s = (symmetric) difference bewteen s1, s2
  | 
      
      
         | 78 | 
          | 
          | 
          
  | 
      
      
         | 79 | 
          | 
          | 
                 // Relational operators
  | 
      
      
         | 80 | 
          | 
          | 
             //---------------------
  | 
      
      
         | 81 | 
          | 
          | 
                 int operator==(CSet &s) const { return cmp(s) ? 0 : 1; };
  | 
      
      
         | 82 | 
          | 
          | 
                 int operator!=(CSet &s) const { return cmp(s) ? 1 : 0; };
  | 
      
      
         | 83 | 
          | 
          | 
                 int operator>(CSet &s) const { return (cmp(s) > 0) ? 1 : 0; };
  | 
      
      
         | 84 | 
          | 
          | 
                 int operator<(CSet &s) const { return (cmp(s) < 0) ? 1 : 0; };
  | 
      
      
         | 85 | 
          | 
          | 
                 int operator>=(CSet &s) const { return (cmp(s) >= 0) ? 1 : 0; };
  | 
      
      
         | 86 | 
          | 
          | 
                 int operator<=(CSet &s) const { return (cmp(s) <= 0) ? 1 : 0; };
  | 
      
      
         | 87 | 
          | 
          | 
          
  | 
      
      
         | 88 | 
          | 
          | 
            // Functions
  | 
      
      
         | 89 | 
          | 
          | 
            //----------
  | 
      
      
         | 90 | 
          | 
          | 
                 // Add a new element to the set.
  | 
      
      
         | 91 | 
          | 
          | 
                 inline void add(int bit)
  | 
      
      
         | 92 | 
          | 
          | 
                 {
  | 
      
      
         | 93 | 
          | 
          | 
                         if (bit > nbits)
  | 
      
      
         | 94 | 
          | 
          | 
                                 enlarge(bit >> SET_NBIT);
  | 
      
      
         | 95 | 
          | 
          | 
                         map[bit >> SET_NBIT] |= (1 << (bit & SET_BMASK));
  | 
      
      
         | 96 | 
          | 
          | 
                 };
  | 
      
      
         | 97 | 
          | 
          | 
                 void add(CSet &s);
  | 
      
      
         | 98 | 
          | 
          | 
                 void add(CSet *s);
  | 
      
      
         | 99 | 
          | 
          | 
         //      inline void add(int);       // add member to set
  | 
      
      
         | 100 | 
          | 
          | 
                 void remove(int);    // Remove member - clears bit in map
  | 
      
      
         | 101 | 
          | 
          | 
                 inline void remove(CSet &s)
  | 
      
      
         | 102 | 
          | 
          | 
                 {
  | 
      
      
         | 103 | 
          | 
          | 
                         int ii;
  | 
      
      
         | 104 | 
          | 
          | 
                         ii = min(size, s.size);
  | 
      
      
         | 105 | 
          | 
          | 
                         while (--ii >= 0)
  | 
      
      
         | 106 | 
          | 
          | 
                                 map[ii] &= ~s.map[ii];
  | 
      
      
         | 107 | 
          | 
          | 
                 }
  | 
      
      
         | 108 | 
          | 
          | 
                 inline void remove(CSet *s)
  | 
      
      
         | 109 | 
          | 
          | 
                 {
  | 
      
      
         | 110 | 
          | 
          | 
                         int ii;
  | 
      
      
         | 111 | 
          | 
          | 
                         ii = min(size, s->size);
  | 
      
      
         | 112 | 
          | 
          | 
                         while (--ii >= 0)
  | 
      
      
         | 113 | 
          | 
          | 
                                 map[ii] &= ~s->map[ii];
  | 
      
      
         | 114 | 
          | 
          | 
                 }
  | 
      
      
         | 115 | 
          | 
          | 
                 int resetPtr() {
  | 
      
      
         | 116 | 
          | 
          | 
               int i = MemberPtr;
  | 
      
      
         | 117 | 
          | 
          | 
               MemberPtr = 0;
  | 
      
      
         | 118 | 
          | 
          | 
               return i; };
  | 
      
      
         | 119 | 
          | 
          | 
                 int SetPtr(int n) {
  | 
      
      
         | 120 | 
          | 
          | 
               int i = MemberPtr;
  | 
      
      
         | 121 | 
          | 
          | 
               MemberPtr = (unsigned __int16)n;
  | 
      
      
         | 122 | 
          | 
          | 
               return i; };
  | 
      
      
         | 123 | 
          | 
          | 
                   int Member(int n);
  | 
      
      
         | 124 | 
          | 
          | 
                 int nextMember();
  | 
      
      
         | 125 | 
          | 
          | 
                 int prevMember();
  | 
      
      
         | 126 | 
          | 
          | 
                 int lastMember();
  | 
      
      
         | 127 | 
          | 
          | 
                 int length() const { return sizeof(int) * size << 3; };
  | 
      
      
         | 128 | 
          | 
          | 
                 int NumMember() const;  // Number of 'ON' elements in set
  | 
      
      
         | 129 | 
          | 
          | 
                 int print(int (*)(void *,...), void*);
  | 
      
      
         | 130 | 
          | 
          | 
                 int sprint(char *, int);
  | 
      
      
         | 131 | 
          | 
          | 
                 unsigned hash() const;
  | 
      
      
         | 132 | 
          | 
          | 
                 int subset(CSet &) const;
  | 
      
      
         | 133 | 
          | 
          | 
                 void truncate();
  | 
      
      
         | 134 | 
          | 
          | 
                 void clear();                                   // Zero all bits
  | 
      
      
         | 135 | 
          | 
          | 
                 void fill();                                    // Set all bits
  | 
      
      
         | 136 | 
          | 
          | 
                 void complement();
  | 
      
      
         | 137 | 
          | 
          | 
                 int isDisjoint(CSet &s) { return (SetTest(s) == SET_DISJ); };
  | 
      
      
         | 138 | 
          | 
          | 
                 int isIntersecting(CSet &s) { return (SetTest(s) == SET_INTER); };
  | 
      
      
         | 139 | 
          | 
          | 
                 int isEmpty() { return NumMember() == 0; };
  | 
      
      
         | 140 | 
          | 
          | 
                 int isMember(int bit) { return ((bit >= nbits)||bit < 0 ? 0 : (map[bit >> SET_NBIT] & (1 << (bit & SET_BMASK)))) != 0; }; // is n a member of s ?
  | 
      
      
         | 141 | 
          | 
          | 
                 int test(int x) { return (isMember(x)) ? !compl : compl; };
  | 
      
      
         | 142 | 
          | 
          | 
                 int isSubset(CSet &);
  | 
      
      
         | 143 | 
          | 
          | 
          
  | 
      
      
         | 144 | 
          | 
          | 
         //      void Serialize(CArchive& ar);
  | 
      
      
         | 145 | 
          | 
          | 
         };
  | 
      
      
         | 146 | 
          | 
          | 
         #endif
  |