SparseArray¶
from acron.core import SparseArray
- class SparseArray¶
Bases:
This class records a set of integers, where each integer is either present or not present in the set.
It is similar in principle and in interface to a
BitArray(which can be thought of as a set of integers, one integer corresponding to each different bit position), but the SparseArray is implemented as a list of min/max subrange lists, rather than as a bitmask.This makes it particularly efficient for storing sets which consist of large sections of consecutively included or consecutively excluded elements, with arbitrarily large integers, but particularly inefficient for doing boolean operations such as & or |.
Also, unlike
BitArray, the SparseArray can store negative integers.Inheritance diagram
digraph inheritance0c7a38f2f7 { bgcolor=transparent; rankdir=BT; size="8.0, 12.0"; "DTOOL_SUPER_BASE" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=11,height=0.25,shape=box,style=""]; "SparseArray" [URL="acron.core.SparseArray.html#acron.core.SparseArray",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=11,height=0.25,shape=box,style="",target="_top",tooltip="/**"]; "DTOOL_SUPER_BASE" -> "SparseArray" [arrowsize=0.75,style=""]; }- __and__()¶
- __bool__()¶
- __eq__()¶
- __getstate__()¶
- __iand__()¶
- __ilshift__()¶
- __init__()¶
- __ior__()¶
- __irshift__()¶
- __ixor__()¶
- __lt__()¶
- __ne__()¶
- __or__()¶
- __rshift__()¶
- __setstate__()¶
- __xor__()¶
- static all_off()¶
Returns a
SparseArraywhose bits are all off.
- static all_on()¶
Returns a
SparseArraywith an infinite array of bits, all on.
- static bit()¶
Returns a
SparseArraywith only the indicated bit on.
- clear()¶
Sets all the bits in the
SparseArrayoff.
- clear_bit()¶
Sets the nth bit off. If n >=
get_num_bits(), this automatically extends the array.
- clear_range()¶
Sets the indicated range of bits off.
- compare_to()¶
Returns a number less than zero if this
SparseArraysorts before the indicated otherSparseArray, greater than zero if it sorts after, or 0 if they are equivalent. This is based on the same ordering defined by operator <.
- get_bit()¶
Returns true if the nth bit is set, false if it is cleared. It is valid for n to increase beyond
get_num_bits(), but the return valueget_num_bits()will always be the same.
- static get_class_type()¶
- get_highest_bits()¶
Returns true if the infinite set of bits beyond
get_num_bits()are all on, or false of they are all off.
- get_highest_off_bit()¶
Returns the index of the highest 0 bit in the array. Returns -1 if there are no 0 bits or if there an infinite number of 1 bits.
- get_highest_on_bit()¶
Returns the index of the highest 1 bit in the array. Returns -1 if there are no 1 bits or if there an infinite number of 1 bits.
- get_lowest_off_bit()¶
Returns the index of the lowest 0 bit in the array. Returns -1 if there are no 0 bits or if there are an infinite number of 1 bits.
- get_lowest_on_bit()¶
Returns the index of the lowest 1 bit in the array. Returns -1 if there are no 1 bits and 0 if there are an infinite number of 1 bits.
- static get_max_num_bits()¶
If get_max_num_bits() returned true, this method may be called to return the maximum number of bits that may be stored in this structure. It is an error to call this if get_max_num_bits() return false.
It is always an error to call this method. The
SparseArrayhas no maximum number of bits. This method is defined so generic programming algorithms can use BitMask orSparseArrayinterchangeably.
- get_next_higher_different_bit()¶
Returns the index of the next bit in the array, above low_bit, whose value is different that the value of low_bit. Returns low_bit again if all bits higher than low_bit have the same value.
This can be used to quickly iterate through all of the bits in the array.
- get_num_bits()¶
Returns the current number of possibly different bits in this array. There are actually an infinite number of bits, but every bit higher than this bit will have the same value, either 0 or 1 (see
get_highest_bits()).This number may grow and/or shrink automatically as needed.
- get_num_off_bits()¶
Returns the number of bits that are set to 0 in the array. Returns -1 if there are an infinite number of 0 bits.
- get_num_on_bits()¶
Returns the number of bits that are set to 1 in the array. Returns -1 if there are an infinite number of 1 bits.
- get_num_subranges()¶
Returns the number of separate subranges stored in the
SparseArray. You can use this limit to iterate through the subranges, callingget_subrange_begin()andget_subrange_end()for each one.Also see
is_inverse().
- get_subrange_begin()¶
Returns the first numeric element in the nth subrange.
Also see
is_inverse().
- get_subrange_end()¶
Returns the last numeric element, plus one, in the nth subrange.
Also see
is_inverse().
- has_all_of()¶
Returns true if all bits in the indicated range are set, false otherwise.
- has_any_of()¶
Returns true if any bit in the indicated range is set, false otherwise.
- has_bits_in_common()¶
Returns true if this
SparseArrayhas any “one” bits in common with the other one, false otherwise.This is equivalent to (array & other) != 0, but may be faster.
- static has_max_num_bits()¶
Returns true if there is a maximum number of bits that may be stored in this structure, false otherwise. If this returns true, the number may be queried in
get_max_num_bits().This method always returns false. The
SparseArrayhas no maximum number of bits. This method is defined so generic programming algorithms can use BitMask orSparseArrayinterchangeably.
- invert_in_place()¶
Inverts all the bits in the
SparseArray. This is equivalent to array = ~array.
- is_all_on()¶
Returns true if the entire bitmask is one, false otherwise.
- is_inverse()¶
If this is true, the
SparseArrayis actually defined as a list of subranges of integers that are not in the set. If this is false (the default), then the subranges define the integers that are in the set. This affects the interpretation of the values returned by iterating throughget_num_subranges().
- is_zero()¶
Returns true if the entire bitmask is zero, false otherwise.
- static lower_on()¶
Returns a
SparseArraywhose lower on_bits bits are on.
- operator__()¶
- operator___()¶
- output()¶
- static range()¶
Returns a
SparseArraywhose size bits, beginning at low_bit, are on.
- set_bit()¶
Sets the nth bit on. If n >=
get_num_bits(), this automatically extends the array.
- set_bit_to()¶
Sets the nth bit either on or off, according to the indicated bool value.
- set_range()¶
Sets the indicated range of bits on.
- set_range_to()¶
Sets the indicated range of bits to either on or off.