Repository navigation
Category BitSet
Home › All Categories › BitSet
A BitSet is a set of small non-negative whole numbers, stored as one bit each. It answers "is 70 in the set?" in O(1) and holds a million flags in 125 KB.
#import "std/containers"
using containers
dim b as BitSet
b.Set( 3 )
b.Set( 70 )
print b.Test( 70 ), b.Test( 4 ) ' true false
print b.CountOnes( ) ' 2
for each i as long in b
print i ' 3, then 70
nextUse it for "seen" flags in a graph search, free-slot maps, sieves, or any set of indices.
Indices start at 0. Set( i ) grows the set as needed. Length( ) is the number of addressable bits — one more than the highest bit ever touched — not the number of bits set; CountOnes( ) is that. Test beyond Length( ) is false.
BitSet( n ) starts with n clear bits; Resize( n ) grows or shrinks, and shrinking clears the bits that fall off.
The set operations change the left-hand set in place. A shorter right-hand set counts as zeros beyond its length.
#import "std/containers"
using containers
dim x as BitSet
dim y as BitSet
x.Set( 1 ) : x.Set( 2 )
y.Set( 2 ) : y.Set( 3 )
dim u as BitSet = x
u.UnionWith( y )
print u.CountOnes( ) ' 3 -- {1, 2, 3}
dim n as BitSet = x
n.IntersectWith( y )
print n.FirstSet( ) ' 2
print n.IsSubsetOf( x ), x.Equals( y ) ' true falseEquals compares the bits that are set, so two sets of different Length( ) can be equal.
Set( byval i as long, byval v as boolean = true )
Reset( byval i as long )
Flip( byval i as long )
Test( byval i as long ) as boolean
Length( ) as long
Resize( byval nbits as long )
CountOnes( ) as long
FirstSet( byval from as long = 0 ) as long -1 when none
NextClear( byval from as long = 0 ) as long
SetAll( ) every bit below Length( )
ClearAll( )
IsEmpty( ) as boolean no bit set
UnionWith( byref o as BitSet )
IntersectWith( byref o as BitSet )
DifferenceWith( byref o as BitSet )
SymmetricDifferenceWith( byref o as BitSet )
IsSubsetOf( byref o as BitSet ) as boolean
Equals( byref o as BitSet ) as boolean
GetIterator( ) as BitSetIterator set bits, ascendingSet · Bit Manipulation · Array
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing