home *** CD-ROM | disk | FTP | other *** search
- Here's a simple class that fills a common need: A BitVector.
-
- This software, and the included source code, is FREEWARE.
-
- You may use it as you see fit in your own projects but you may
- not re-sell the original or a derivative. If you redistribute
- it you must include this disclaimer and all original copyright
- notices.
-
- No warranty is inferred or implied, as always seems to be the
- case with software these days. Use at your own risk. Keep away
- from small children. Don't exceed recommended dosage. I'm not
- liable, I'm not liable, I'm not liable.
-
- It's not complete, or *extremely* well thought out I suppose,
- but it's a good starting point. You'll see some "TBD"s in the
- code which indicates that it's a work in progress.
-
- There's not much code, and I haven't done any real docs on it
- yet, but it should be useful none-the-less and you can always
- e-mail questions to me.
-
- I'd love to see people improve upon this work. Please keep me
- posted if you do. If you should happen to change the behavior
- in a derivative version please change the class name so as not
- to cause problems with existing code.
-
- Enjoy!
-
- --Gregg Irwin [72450,676]
-
- --------------------------------------------------------------
-
- Q. What's new in Version 1.1?
-
- A. ╖ Bit property access. This lets you use normal array
- syntax to set and get bit values.
-
- BitVal = BV.Bit(1)
- BV.Bit(1) = 1
-
- ╖ Error handler in NumElements Let procedure
-
- --------------------------------------------------------------
-
- Q. What's a BitVector?
-
- A. It's just an array designed to store True/False values.
-
- --------------------------------------------------------------
-
- Q. Can't I just define an array of Boolean variables to do
- that?
-
- A. Yes you can.
-
- --------------------------------------------------------------
-
- Q. Why would I want to use a BitVector instead of an
- array of Boolean variables?
-
- A. Size. Boolean variables are stored as 16-bit
- (2-byte) integers. If you have a small number of
- items to track then memory consumption may not be
- a concern to you. If, however, you had 1,000,000
- items to track then you might flinch a bit at using
- 2 meg of memory to do so. A BitVector uses only 1
- bit per item, instead of 16 bits like a Boolean does.
- To track 1,000,000 items you would only use 125K of
- memory, rather than 2 meg.
-
- Under VB4/32 they may actually be stored as 32 bit
- (i.e. long) integers if I recall correctly. The current
- docs don't say so but it would make sense to make them
- as fast as possible on 32 bit systems. Keeping things
- aligned, etc.
-
- --------------------------------------------------------------
-
- Q. What's the downside?
-
- A. Speed. Rather than referencing array variables directly
- the BitVector has to do a bit of processing to identify
- individual bits. Jim Mack has written routines in assembly
- language (which are part of Microhelp Muscle) that should
- be fast enough for *anybody*. Using a table to look up
- values could also speed things up a bit. There's a note
- in the code about this idea.
-
- --------------------------------------------------------------
-
- Q. How do I use it?
-
- A. It's easy.
-
- 1. Dim a variable to hold the bit vector.
-
- Example: Dim BV As New BitVector
-
- 2. Set the number of elements you want the BitVector to
- hold using the NumElements property.
-
- Example: BV.NumElements = 25000
-
- 3. Set, Get, Clear, Toggle, and Test bits by referencing
- their index.
-
- BV.SetBit 12345
- BitVal = BV.GetBit 12345
- BV.ClearBit 12345
- BV.ToggleBit 12345
- On = BV.IsBitSet 12345
-
- --------------------------------------------------------------
-
- Q. What's the difference between GetBit and IsBitSet?
-
- A. GetBit returns the actual value of the bit (1 or 0) while
- IsBitSet returns a boolean value of True or False.
-
- --------------------------------------------------------------
-
- Q. What if I want to set, or clear, all of the bits at once?
-
- A. Simple. Use the SetAll or ClearAll methods.
-
- --------------------------------------------------------------
-
- Q. Why a class module?
-
- A. Why not? It has a defined interface. It provides
- encapsulation. It allows you to create multiple instances
- easily. It can be used as an OLE server. It can raise
- errors effectively. 'nuff said?
-
- --------------------------------------------------------------
-
- Q. What if I forget how many elements are in a BitVector?
-
- A. Use the NumElements method to find out.
-
- Example: ElCount = BV.NumElements
-
- --------------------------------------------------------------
-
- Q. What if I "grow" a BitVector (i.e. increase NumElements)?
-
- A. It remembers the current bit states so you'll need to
- clear them manually if that's what you want. New bits will
- be "off" initially.
-
- --------------------------------------------------------------
-
- Q. What if I have suggestions, requests, or improvements?
-
- A. Send them to me and I'll get back to you as quickly as I
- can. Hopefully we can make it a community effort and build
- up a good library of tools that we can all draw upon.
-
- --------------------------------------------------------------
-
- Q. What if it has a bug in it that costs me millions of
- dollars?
-
- A. Re-read the disclaimer and let me know about the bug
- please.<g>
-
- --------------------------------------------------------------
-