home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Power Pack / Visual_Basic4_Power_Pack.bin / vb4files / bitvctr / bitvectr.cls < prev    next >
Encoding:
Text File  |  1996-11-20  |  6.1 KB  |  276 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "BitVector"
  6. Attribute VB_Creatable = True
  7. Attribute VB_Exposed = True
  8. '
  9. ' BitVector Class
  10. ' Copyright ⌐ 1995-1996 by Gregg S. Irwin. All Rights Reserved.
  11. '
  12.  
  13. Option Explicit
  14. DefInt A-Z
  15.  
  16. Const CLASS_NAME = "BitVector"
  17. Const CLASS_VERSION = "110"
  18.  
  19. '-- Version 100     12/04/95
  20. ' ╖ Initial release
  21. '-- Version 110     01/24/96
  22. ' ╖ Added Bit Property (read-write)
  23. ' ╖ Added validation to NumElements Let procedure
  24.  
  25.  
  26. ' * PROPERTIES *
  27. ' .NumElements
  28. ' .Bit(BitIndex)        ; Version 110
  29.  
  30. ' * METHODS    *
  31. ' .ClearAll
  32. ' .ClearBit (BitIndex)
  33. ' .GetBit   (BitIndex)
  34. ' .IsBitSet (BitIndex)
  35. ' .SetAll
  36. ' .SetBit   (BitIndex)
  37. ' .Toggle   (BitIndex)
  38.  
  39. ' * ERRORS     *
  40. ' Subscript out of range
  41.  
  42. Const vbErrSubscriptOutOfRange = 9
  43.  
  44.  
  45. Const BITS_PER_ELEMENT = 8
  46.  
  47.  
  48. Private mBits()      As Byte
  49. Private mNumElements As Long
  50.  
  51.  
  52. '-- The following code:
  53. '
  54. '      ArrayIdx = Index \ BITS_PER_ELEMENT
  55. '      Bit = Index Mod BITS_PER_ELEMENT
  56. '
  57. '   appears in a few procedures and could/should be
  58. '   broken out into one or two procedures itself. I
  59. '   just haven't been able to come up with a good,
  60. '   clean syntax that I like yet.
  61.  
  62.  
  63. '------------------------------------------------------
  64. '-- CLASS EVENTS
  65. '------------------------------------------------------
  66.  
  67. ' _Initialize
  68. Private Sub Class_Initialize()
  69.  
  70. End Sub
  71.  
  72. ' _Terminate
  73. Private Sub Class_Terminate()
  74.     Erase mBits
  75. End Sub
  76.  
  77.  
  78. '------------------------------------------------------
  79. '-- PROPERTIES
  80. '------------------------------------------------------
  81.  
  82. ' .NumElements
  83. Public Property Let NumElements(NewValue As Long)
  84.     '-- Trap for bad values
  85.     Select Case NewValue
  86.         Case Is > 0
  87.             mNumElements = NewValue
  88.             ReDim Preserve mBits(mNumElements \ BITS_PER_ELEMENT)
  89.             'Debug.Print UBound(mBits)
  90.         Case 0
  91.             mNumElements = 0
  92.             Erase mBits
  93.         Case Else
  94.             Call RaiseError(vbErrSubscriptOutOfRange)
  95.     End Select
  96. End Property
  97.  
  98. Public Property Get NumElements() As Long
  99.     NumElements = mNumElements
  100. End Property
  101.  
  102.  
  103. ' .Bit
  104. Public Property Let Bit(Index As Long, NewBitValue As Integer)
  105.     If NewBitValue <> 0 Then
  106.         Call SetBit(Index)
  107.     Else
  108.         Call ClearBit(Index)
  109.     End If
  110. End Property
  111.  
  112. Public Property Get Bit(Index As Long) As Integer
  113.     Bit = GetBit(Index)
  114. End Property
  115.  
  116.  
  117. '------------------------------------------------------
  118. '-- METHODS
  119. '------------------------------------------------------
  120.  
  121. ' .ClearAll
  122. Public Sub ClearAll()
  123.     Dim i As Long
  124.     
  125.     '-- Set bit values in BITS_PER_ELEMENT chunks for speed
  126.     For i = LBound(mBits) To UBound(mBits)
  127.         mBits(i) = &H0
  128.     Next i
  129.     
  130. End Sub
  131.  
  132. ' .ClearBit
  133. Public Sub ClearBit(Index As Long)
  134. '-- Set Bit(Index) value to 0
  135.     Dim ArrayIdx As Long
  136.     Dim Bit      As Long
  137.     
  138.     Call ValidateIndex(Index)
  139.     
  140.     ArrayIdx = Index \ BITS_PER_ELEMENT
  141.     Bit = Index Mod BITS_PER_ELEMENT
  142.     'Debug.Print "Clearing ArrayIdx:"; ArrayIdx, " Bit:"; Bit
  143.     mBits(ArrayIdx) = mBits(ArrayIdx) And (Not (2 ^ Bit))
  144.     
  145. End Sub
  146.  
  147. ' .GetBit
  148. Public Function GetBit(Index As Long) As Integer
  149. '-- Returns 0 or 1
  150.     
  151.     Call ValidateIndex(Index)
  152.     
  153.     If IsBitSet(Index) Then
  154.         GetBit = 1
  155.     Else
  156.         GetBit = 0
  157.     End If
  158.     
  159. End Function
  160.  
  161. ' .IsBitSet
  162. Public Function IsBitSet(Index As Long) As Boolean
  163.     Dim ArrayIdx As Long
  164.     Dim Bit      As Long
  165.     
  166.     Call ValidateIndex(Index)
  167.     
  168.     ArrayIdx = Index \ BITS_PER_ELEMENT
  169.     Bit = Index Mod BITS_PER_ELEMENT
  170.     'Debug.Print "Testing ArrayIdx:"; ArrayIdx, " Bit:"; Bit
  171.     If mBits(ArrayIdx) And 2 ^ Bit Then
  172.         IsBitSet = True
  173.     Else
  174.         IsBitSet = False
  175.     End If
  176.  
  177. End Function
  178.  
  179. ' .SetAll
  180. Public Sub SetAll()
  181.     Dim i As Long
  182.     
  183.     '-- Set bit values in BITS_PER_ELEMENT chunks for speed
  184.     For i = LBound(mBits) To UBound(mBits)
  185.         mBits(i) = &HFF
  186.     Next i
  187.     
  188. End Sub
  189.  
  190. ' .SetBit
  191. Public Sub SetBit(Index As Long)
  192. '-- Set Bit(Index) value to 1
  193.     Dim ArrayIdx As Long
  194.     Dim Bit      As Long
  195.     
  196.     Call ValidateIndex(Index)
  197.     
  198.     ArrayIdx = Index \ BITS_PER_ELEMENT
  199.     Bit = Index Mod BITS_PER_ELEMENT
  200.     'Debug.Print "Setting ArrayIdx:"; ArrayIdx, " Bit:"; Bit
  201.     mBits(ArrayIdx) = mBits(ArrayIdx) Or 2 ^ Bit
  202.  
  203. End Sub
  204.  
  205. ' .ToggleBit
  206. Public Sub ToggleBit(Index As Long)
  207. '-- Toggle the value of Bit(Index)
  208.     
  209.     Call ValidateIndex(Index)
  210.     
  211.     If IsBitSet(Index) Then
  212.         Call ClearBit(Index)
  213.     Else
  214.         Call SetBit(Index)
  215.     End If
  216.     
  217. End Sub
  218.  
  219.  
  220. '------------------------------------------------------
  221. '-- INTERNAL SUPPORT
  222. '------------------------------------------------------
  223.  
  224. ''!! This is an unused (and untested) procedure. It's just
  225. ''   here to remind me that we can get the exponentiation
  226. ''   out of the inline code and do table lookups instead.
  227. 'Private Sub InitBitValueTable(BitValueTable() As Long)
  228. '    Dim i As Integer
  229. '
  230. '    For i = 1 To BITS_PER_ELEMENT
  231. '        BitValueTable(i) = 2 ^ i
  232. '    Next i
  233. '
  234. 'End Sub
  235.  
  236. ' .ValidateIndex
  237. Private Sub ValidateIndex(Index As Long)
  238.     
  239.     '-- Our bounds checking code is aware that this is
  240.     '   a 0 based array of bits.
  241.     If (Index < 0) Or (Index > (mNumElements - 1)) Then
  242.         Call RaiseError(vbErrSubscriptOutOfRange)
  243.     End If
  244.  
  245. End Sub
  246.  
  247.  
  248. '------------------------------------------------------
  249. '-- ERRORS
  250. '------------------------------------------------------
  251.  
  252. ' .GetErrorDesc
  253. Private Function GetErrorDesc(ErrCode As Long) As String
  254.     Dim Desc As String
  255.     
  256.     Select Case ErrCode
  257.         Case vbErrSubscriptOutOfRange
  258.             Desc = "Subscript out of Range"
  259.         Case Else
  260.             Desc = "Unknown error (" & CStr(ErrCode) & ")"
  261.     End Select
  262.     
  263.     GetErrorDesc = Desc
  264.     
  265. End Function
  266.  
  267.  
  268. ' .RaiseError
  269. Private Sub RaiseError(ErrCode As Long)
  270.         
  271.     Err.Raise Number:=vbObjectError + ErrCode, _
  272.               Source:=CLASS_NAME & " " & CLASS_VERSION, _
  273.               Description:=GetErrorDesc(ErrCode)
  274.  
  275. End Sub
  276.