home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 25 / nopv25.iso / 040A / PBL30DEV.ZIP / BITVECT.H < prev    next >
Encoding:
C/C++ Source or Header  |  1997-03-28  |  2.5 KB  |  113 lines

  1. /*
  2.  * This file is part of PB-Lib v3.0 C++ Programming Library
  3.  *
  4.  * Copyright (c) 1995, 1997 by Branislav L. Slantchev
  5.  * A fine product of Silicon Creations, Inc. (gargoyle)
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the License which accompanies this
  9.  * software. This library is distributed in the hope that it will
  10.  * be useful, but without any warranty; without even the implied
  11.  * warranty of merchantability or fitness for a particular purpose.
  12.  *
  13.  * You should have received a copy of the License along with this
  14.  * library, in the file LICENSE.DOC; if not, write to the address
  15.  * below to receive a copy via electronic mail.
  16.  *
  17.  * You can reach Branislav L. Slantchev (Silicon Creations, Inc.)
  18.  * at bslantch@cs.angelo.edu. The file SUPPORT.DOC has the current
  19.  * telephone numbers and the postal address for contacts.
  20. */
  21.  
  22. #ifndef INCLUDED_BITVECT_H
  23. #define INCLUDED_BITVECT_H
  24. #include "typedef.h"
  25.  
  26. class zBitVector
  27. {
  28. public:
  29.     enum bitvect_errors
  30.     {
  31.         none  = 0,
  32.         range = 1,
  33.         nomem = 2,
  34.     };
  35.  
  36.     zBitVector(size_t aNumBits);
  37.     zBitVector(const zBitVector &aVector);
  38.     ~zBitVector();
  39.  
  40.     zBitVector& operator=(const zBitVector &aVector);
  41.  
  42.     Boolean operator[](size_t bitno) const;
  43.     Boolean has(size_t bitno) const;
  44.     void    set(size_t bitno);
  45.     void    reset(size_t bitno);
  46.     void    toggle(size_t bitno);
  47.     void    clearAll();
  48.     void    setAll();
  49.     int     error() const;
  50.     size_t  capacity() const;
  51.  
  52. private:
  53.     char   *bits;
  54.     size_t  nbits;
  55.     int     err;
  56. };
  57.  
  58. inline
  59. zBitVector::~zBitVector()
  60. {
  61.     if( bits ) delete[] bits;
  62. }
  63.  
  64. inline Boolean
  65. zBitVector::operator[](size_t bitno) const
  66. {
  67.     return (bits[bitno >> 3] & (1 << (bitno & 7))) ? True : False;
  68. }
  69.  
  70. inline Boolean
  71. zBitVector::has(size_t bitno) const
  72. {
  73.     return (bits[bitno >> 3] & (1 << (bitno & 7))) ? True : False;
  74. }
  75.  
  76. inline void
  77. zBitVector::reset(size_t bitno)
  78. {
  79.     err = none;
  80.     if( bitno >= nbits ) err = range;
  81.     else bits[bitno >> 3] &= ~(1 << (bitno & 7));
  82. }
  83.  
  84. inline void
  85. zBitVector::set(size_t bitno)
  86. {
  87.     err = none;
  88.     if( bitno >= nbits ) err = range;
  89.     else bits[bitno >> 3] |= 1 << (bitno & 7);
  90. }
  91.  
  92. inline void
  93. zBitVector::toggle(size_t bitno)
  94. {
  95.     err = none;
  96.     if( bitno >= nbits ) err = range;
  97.     else bits[bitno >> 3] ^= 1 << (bitno & 7);
  98. }
  99.  
  100. inline int
  101. zBitVector::error() const
  102. {
  103.     return err;
  104. }
  105.  
  106. inline size_t
  107. zBitVector::capacity() const
  108. {
  109.     return nbits;
  110. }
  111.  
  112. #endif /* INCLUDED_BITVECT_H */
  113.