BitArray is an array data structure that compactly stores bits.
Bits externally represented as Bools are stored internally as UInt32s. The total number of bits stored is set at creation and is immutable.
require "bit_array"
ba = BitArray.new(12) # => "BitArray[000000000000]"
ba[2] # => false
0.upto(5) { |i| ba[i * 2] = true }
ba # => "BitArray[101010101010]"
ba[2] # => true Creates a new BitArray of size bits.
Returns true if this struct is equal to other.
Returns count or less (if there aren't enough) elements starting at the given start index.
Returns all elements that are within the given range.
Sets the bit at the given index.
Creates a string representation of self.
Inverts all bits in the array.
The number of bits the BitArray stores
Creates a string representation of self.
Returns a Bytes able to read and write bytes from a buffer.
Toggles the bit at the given index.
Returns the element at the given index, without doing any bounds check.
Indexable(Bool)
Enumerable(Bool)
Iterable(Bool)
Struct
Value
Object
Object
Creates a new BitArray of size bits.
initial optionally sets the starting value, true or false, for all bits in the array.
Returns true if this struct is equal to other.
Both structs's instance vars are compared to each other. Thus, two structs are considered equal if each of their instance variables are equal. Subclasses should override this method to provide specific equality semantics.
struct Point def initialize(@x : Int32, @y : Int32) end end p1 = Point.new 1, 2 p2 = Point.new 1, 2 p3 = Point.new 3, 4 p1 == p2 # => true p1 == p3 # => false
Returns count or less (if there aren't enough) elements starting at the given start index.
Negative indices count backward from the end of the array (-1 is the last element). Additionally, an empty array is returned when the starting index for an element range is at the end of the array.
Raises IndexError if the starting index is out of range.
require "bit_array" ba = BitArray.new(5) ba[0] = true; ba[2] = true; ba[4] = true ba # => BitArray[10101] ba[-3, 3] # => BitArray[101] ba[6, 1] # raise indexError ba[1, 2] # => BitArray[01] ba[5, 1] # => BitArray[]
Returns all elements that are within the given range.
Negative indices count backward from the end of the array (-1 is the last element). Additionally, an empty array is returned when the starting index for an element range is at the end of the array.
Raises IndexError if the starting index is out of range.
require "bit_array" ba = BitArray.new(5) ba[0] = true; ba[2] = true; ba[4] = true ba # => BitArray[10101] ba[1..3] # => BitArray[010] ba[4..7] # => BitArray[1] ba[6..10] # raise IndexError ba[5..10] # => BitArray[] ba[-2...-1] # => BitArray[0]
Sets the bit at the given index. Negative indices can be used to start counting from the end of the array. Raises IndexError if trying to access a bit outside the array's range.
require "bit_array" ba = BitArray.new(5) ba[3] = true
Creates a string representation of self.
require "bit_array" ba = BitArray.new(5) ba.to_s # => "BitArray[00000]"
Inverts all bits in the array. Falses become true and vice versa.
require "bit_array" ba = BitArray.new(5) ba[2] = true; ba[3] = true ba # => BitArray[00110] ba.invert ba # => BitArray[11001]
Creates a string representation of self.
require "bit_array" ba = BitArray.new(5) ba.to_s # => "BitArray[00000]"
Toggles the bit at the given index. A false bit becomes a true bit, and vice versa. Negative indices can be used to start counting from the end of the array. Raises IndexError if trying to access a bit outside the array's range.
require "bit_array" ba = BitArray.new(5) ba[3] # => false ba.toggle(3) ba[3] # => true
Returns the element at the given index, without doing any bounds check.
Indexable makes sure to invoke this method with index in 0...size, so converting negative indices to positive ones is not needed here.
Clients never invoke this method directly. Instead, they access elements with #[](index) and #[]?(index).
This method should only be directly invoked if you are absolutely sure the index is in bounds, to avoid a bounds check for a small boost of performance.
© 2012–2020 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.35.1/BitArray.html