home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / todb101 / todb / lib / keyset.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-04-26  |  3.8 KB  |  191 lines

  1. // ========================================================================
  2. //  TODB LIBRARY
  3. //    keyset.h
  4. //
  5. //    KSet class, KeySet class
  6. //
  7. //    Version: see TODB.H file
  8. //
  9. //    Copyright 1993 Christian Thérien
  10. //    All rights reserved
  11. // ========================================================================
  12.  
  13. #ifndef _KEYSET_H
  14. #define _KEYSET_H
  15.  
  16. #include "kspersis.h"
  17. #include "ptodb.h"
  18.  
  19. class ErrReporter;
  20.  
  21. //-------------------------------------------------------------------------
  22. // KSet class
  23. //    Keeps an ordered set of KeyType
  24. //-------------------------------------------------------------------------
  25.  
  26. enum KSetError
  27. {
  28.     KS_ALLOC
  29. };
  30.  
  31. class KSet
  32. {
  33. public:
  34.     KSet();
  35.  
  36.     ~KSet();
  37.  
  38.     KSet & operator = ( const KSet & os );
  39.     operator KeyType * ();
  40.     operator unsigned char * ();
  41.  
  42.     // -- inserts a key
  43.     Boolean Insert( KeyType k );
  44.     // -- deletes a key
  45.     Boolean Remove( KeyType k );
  46.     // -- reads a key
  47.     KeyType Get( KeyNbr n ) const;
  48.     // -- cardinal
  49.     KeyNbr Card() const;
  50.     // -- creates an empty set of length l
  51.     void Create( KeyNbr l );
  52.     // -- loads the set
  53.     void Put( void * buf, KeyNbr length );
  54.     // -- updates card_ value
  55.     void SetCard( KeyNbr n );
  56.  
  57.     // -- navigation, a returned value of 0 marks the end
  58.     KeyType First();
  59.     KeyType Next();
  60.  
  61.     // error control
  62.     static void SetErrOut( ErrReporter * er );
  63.  
  64. private:
  65.     KeyType * set_;
  66.     KeyNbr card_;
  67.     KeyNbr curr_index_;
  68.  
  69.     KSet( const KSet & os );
  70.  
  71.     // error control
  72.     static ErrReporter * ErrOut;
  73.     void ErrorHandler( KSetError err ) const;
  74. };
  75.  
  76. inline KSet::~KSet()
  77. {
  78.     delete [] set_;
  79. }
  80.  
  81. inline KSet::operator KeyType * ()
  82. {
  83.     return set_;
  84. }
  85.  
  86. inline KSet::operator unsigned char * ()
  87. {
  88.     return (unsigned char *)set_;
  89. }
  90.  
  91. inline KeyNbr KSet::Card() const
  92. {
  93.     return card_;
  94. }
  95.  
  96. inline void KSet::Put( void * buf, KeyNbr length )
  97. {
  98.     memcpy( set_, buf, length * sizeof( KeyType ) );
  99.     card_ = length;
  100. }
  101.  
  102. inline void KSet::SetCard( KeyNbr n )
  103. {
  104.     card_ = n;
  105. }
  106.  
  107. inline KeyType KSet::Get( KeyNbr n ) const
  108. {
  109.     return set_[ n ];
  110. }
  111.  
  112. // returns first key of the set
  113. // if empty, returns 0
  114. inline KeyType KSet::First()
  115. {
  116.     curr_index_ = 0;
  117.     return Next();
  118. }
  119.  
  120. // returns next key
  121. // a returned value of 0 marks the end
  122. inline KeyType KSet::Next()
  123. {
  124.     if( curr_index_ < Card() )
  125.       return set_[ curr_index_++ ];
  126.     else
  127.       return 0;
  128. }
  129.  
  130. //-------------------------------------------------------------------------
  131. // KeySet class
  132. //    Persistent key set (saved in .KEY file, indexed in .IDX file)
  133. //-------------------------------------------------------------------------
  134.  
  135. // The class id value 1 is used and values between 2 and 10 are reserved
  136. // for upcoming compressed key set and others specialized data structure.
  137. // If you want to save your own objects in the keyset data base,
  138. // use a class id value greater than 10
  139.  
  140. enum SetId
  141. {
  142.     KEYSET = 1    // uncompressed persistent key set
  143. };
  144.  
  145. class KeySet : public KSPersistent
  146. {
  147. public:
  148.     KeySet( ObjAdr oa, TODB & db );
  149.     KeySet( ObjAdr oa, TODB & db, Boolean append );
  150.  
  151.     ~KeySet();
  152.  
  153.     KSet & set();
  154.     void AddKey( KeyType k );
  155.     void RemoveKey( KeyType k );
  156.     KeyNbr Card() const;
  157.  
  158. private:
  159.     KSet set_;
  160.     Boolean append_;
  161.     KeyNbr card_;
  162.  
  163.     void Read();
  164.     void Write();
  165.  
  166.     KeySet( const KeySet & ks );
  167. };
  168.  
  169. inline KeySet::~KeySet()
  170. {
  171.     if ( append_ == False ) SaveObject();
  172. }
  173.  
  174. inline KSet & KeySet::set()
  175. {
  176.     return set_;
  177. }
  178.  
  179. inline void KeySet::RemoveKey( KeyType k )
  180. {
  181.     if ( set_.Remove( k ) == True )
  182.     ChangeObject();
  183. }
  184.  
  185. inline KeyNbr KeySet::Card() const
  186. {
  187.     return set_.Card();
  188. }
  189.  
  190. #endif // _KEYSET_H
  191.