Packed vectors

SmallCollections.PackedVectorType
PackedVector{U<:Unsigned,M,T<:Union{Base.BitInteger,Bool}} <: AbstractVector{T}

PackedVector{U,M,T}()
PackedVector{U,M,T}(iter)
PackedVector{U,M}(v::AbstractVector{T})
PackedVector{U,M}(t::Tuple)
PackedVector(v::AbstractSmallVector{M,T})

This type of immutable vector stores the elements in a common bit mask of type U with M bits for each entry. The range of allowed values is -2^(M-1):2^(M-1)-1 if T <: Signed, 0:2^M-1 if T <: Unsigned and false:true if T == Bool. Apart from that, the official element type T is only used when retrieving an entry. The capacity, that is, the number of elements that can be stored, is given by bitsize(U)÷M.

The element type T can be omitted when creating the PackedVector from an AbstractVector or from a tuple. In the latter case, T is determined by promoting the element types of the tuple. If no argument is given, then an empty vector is returned. If the PackedVector is created from a AbstractSmallVector v and the parameters U and M are omitted, then M is set to bitsize(T) and U is chosen such that the capacity of the resulting vector is at least the capacity of v.

Overflow or underflow during addition or subtraction of vectors do not throw an error. The same applies to multiplication by a scalar of type T. Scalar multiplication by other types returns a Vector.

Compared to a SmallVector, a PackedVector may have faster insert and delete operations. Arithmetic operations are usually slower unless M is the size of a hardware integer.

See also capacity, SmallCollections.bitsize.

Examples

julia> v = PackedVector{UInt64,5,Int8}(-5:5:10)
4-element PackedVector{UInt64, 5, Int8}:
 -5
  0
  5
 10

julia> capacity(v)
12

julia> w = PackedVector{UInt64,5,Int8}([1, 2, 3, 4])
4-element PackedVector{UInt64, 5, Int8}:
 1
 2
 3
 4

julia> v+w
4-element PackedVector{UInt64, 5, Int8}:
 -4
  2
  8
 14

julia> Int8(2)*v
4-element PackedVector{UInt64, 5, Int8}:
 -10
   0
  10
 -12
source
SmallCollections.capacityMethod
capacity(::Type{<:PackedVector}) -> Int
capacity(v::PackedVector) -> Int

Return the largest number of elements this vector type can hold.

source
SmallCollections.bitsMethod
bits(v::PackedVector{U}) where U -> U

Return the bit mask used internally to store the elements of the vector v.

source
SmallCollections.fasthashMethod
fasthash(v::PackedVector [, h0::UInt]) -> UInt

Return a hash for v that may be computed faster than the standard hash for vectors. This new hash is consistent across all PackedVector{U,M,T} of the same internal bit size M, but it may not be compatible with hash or with fasthash for a PackedVector having a different bit size. However, using fasthash for two PackedVectors with the same M, but both signed and unsigned element types T may lead to hash collisions.

See also Base.hash.

Examples

julia> v = PackedVector{UInt64,5,Int8}([1, 5, 6]);

julia> fasthash(v)
0x11e89b9d8f3daac6

julia> fasthash(v) == hash(v)
false

julia> w = PackedVector{UInt128,5,Int8}(v); fasthash(v) == fasthash(w)
true

julia> w = PackedVector{UInt64,4,Int8}(v); fasthash(v) == fasthash(w)
false

julia> w = PackedVector{UInt64,5,UInt8}(v); fasthash(v) == fasthash(w)
true
source
Base.emptyMethod
empty(v::V) where V <: PackedVector -> V
empty(v::PackedVector{U,M}, T::Type) where {U,M,T} -> PackedVector{U,M,T}

In the first form, return an empty PackedVector of the same type as v. In the second form, the bit mask type and the bit size are the same as for v, but the element type is T.

source
Base.getindexMethod
getindex(v::V, s::SmallBitSet) where V <: PackedVector -> V

Returns the vector with elements v[i] where i runs through the elements of s in increasing order. This operation is analogous to v[collect(s)], but faster.

Example

julia> v = PackedVector{UInt64,6,Int8}(-2:2)
5-element PackedVector{UInt64, 6, Int8}:
 -2
 -1
  0
  1
  2

julia> s = SmallBitSet((1, 3, 4))
SmallBitSet{UInt64} with 3 elements:
  1
  3
  4

julia> v[s]
3-element PackedVector{UInt64, 6, Int8}:
 -2
  0
  1
source
Base.setindexMethod
setindex(v::V, x, i::Integer) where V <: PackedVector -> V

Substitute x for the i-th component of v and return the new vector.

See also Base.setindex, addindex.

source
SmallCollections.pushMethod
push(v::V, xs...) where V <: PackedVector -> V

Return the PackedVector obtained from v by appending the arguments xs.

See also Base.push!, BangBang.push!!.

source
SmallCollections.popMethod
pop(v::PackedVector{U,M,T}) where {U,M,T} -> Tuple{PackedVector{U,M,T}, T}

Return the tuple (w, x) where x is the last element of v and w obtained from v by dropping this element. The vector v must not be empty.

See also Base.pop!, BangBang.pop!!.

source
SmallCollections.pushfirstMethod
pushfirst((v::V, xs...) where V <: PackedVector -> V

Return the PackedVector obtained from v by prepending the arguments xs.

See also Base.pushfirst!, BangBang.pushfirst!!.

source
SmallCollections.popfirstMethod
popfirst(v::PackedVector{U,M,T}) where {U,M,T} -> Tuple{PackedVector{U,M,T}, T}

Return the tuple (w, x) where x is the first element of v and w obtained from v by dropping this element. The vector v must not be empty.

See also Base.popfirst!, BangBang.popfirst!!.

source
SmallCollections.insertMethod
insert(v::V, i::Integer, x) where V <: PackedVector -> V

Return the PackedVector obtained from v by inserting x at position i. The position i must be between 1 and length(v)+1.

This is the non-mutating analog of Base.insert!.

See also duplicate.

source
SmallCollections.duplicateMethod
duplicate(v::V, i::Integer) where V <: PackedVector -> V

Duplicate the i-th entry of v by inserting it at position i+1 and return the new vector.

See also insert.

Example

julia> v = PackedVector{UInt,5,Int8}(1:3)
3-element PackedVector{UInt64, 5, Int8}:
 1
 2
 3

julia> duplicate(v, 2)
4-element PackedVector{UInt64, 5, Int8}:
 1
 2
 2
 3
source
SmallCollections.deleteatMethod
deleteat(v::V, i::Integer) where V <: PackedVector -> V

Return the PackedVector obtained from v by deleting the element at position i. The latter must be between 1 and length(v).

See also Base.deleteat!, BangBang.deleteat!!.

source
SmallCollections.popatMethod
popat(v::PackedVector{U,M,T}, i::Integer) where {U,M,T} -> Tuple{PackedVector{U,M,T}, T}

Return the tuple (w, x) where w obtained from v by deleting the element x at position i. The latter must be between 1 and length(v).

See also Base.popat!, BangBang.popat!!.

source
SmallCollections.appendMethod
append(v::V, ws...) where V <: PackedVector -> V

Append all elements of the collections ws to v and return the new vector. Note that the resulting PackedVector has the same capacity as v.

See also Base.append!, BangBang.append!!.

source
SmallCollections.prependMethod
prepend(v::V, ws...) where V <: PackedVector -> V

Prepend all elements of the collections ws to v and return the new vector. Note that the resulting PackedVector has the same capacity as v.

See also Base.prepend!.

source
Base.zerosMethod
zeros(::Type{V}, n::Integer) where V <: PackedVector -> V

Return a PackedVector of type V containing n zeros.

See also ones.

source
Base.onesMethod
ones(::Type{V}, n::Integer) where V <: PackedVector -> V

Return a PackedVector of type V containing n ones.

See also zeros.

source
SmallCollections.unsafe_addFunction
SmallCollections.unsafe_add(v::V, w::V) where V <: PackedVector -> V

Add v and w and return the result. It is not checked that v and w have the same length. No overflow or underflow is allowed in any component, nor are sign changes in the case of signed integers.

This function is much faster than regular addition.

See also unsafe_sub.

source
SmallCollections.unsafe_subFunction
SmallCollections.unsafe_sub(v::V, w::V) where V <: PackedVector -> V

Subtract w from v and return the result. It is not checked that v and w have the same length. No overflow or underflow is allowed in any component, nor are sign changes in the case of signed integers.

This function is much faster than regular addition.

See also unsafe_add.

source
SmallCollections.supportMethod
support(v::PackedVector) -> SmallBitSet

Return the SmallBitSet with the indices of the non-zero elements of v. If v has Bool elements, then this means the elements that are true.

See also SmallBitSet.

Example

julia> v = PackedVector{UInt,5,Int8}([1, 0, 2, 0, 0, 3]);

julia> support(v)
SmallBitSet{UInt64} with 3 elements:
  1
  3
  6
source