bit_field_set v0.1.1 BitFieldSet
A bit field implementation using MapSets.
Its main usecase is for BitTorrent implementations.
Summary
Functions
Takes two piece sets, a and b, who both has the same info_hash
, and return a MapSet
containing the pieces in a without the pieces contained in b
Takes two piece sets and return true
if the two sets does not share any members,
otherwise false
will get returned
Takes two piece sets with the same info_hash
, and return true
if both sets
contain exactly the same pieces; and false
otherwise
Take a piece set and return the number of its available pieces
Take a piece set and return true
if the set contains all the pieces,
and false
otherwise
Takes two piece sets with the same info_hash
and return a set containing the pieces
that belong to both sets
Takes a piece set and a piece number and return true
if the given piece number
is present in the set; false
otherwise
Create a new piece set given either a size
(an integer denoting bit size)
or some content
(a binary, the size will be set from the bit size of this
binary) and a size
Take a piece set and an index. The given index will get removed from the piece set and the updated piece set will get returned
Take a piece set and an index. The given index will get added to the piece set and the updated piece set will get returned
Set all the bits to true in the set
Takes two piece sets, a and b, who has the same info_hash
, and return true
if
all the members of set a are also members of set b; false
otherwise
Take a piece set and return the bit field representation of the set
Take a piece set and return the available pieces as a list
Takes two piece sets with the same info_hash
and return a set containing all
members of both sets
Types
Functions
Takes two piece sets, a and b, who both has the same info_hash
, and return a MapSet
containing the pieces in a without the pieces contained in b.
iex> {:ok, a} = BitFieldSet.new(<<170>>, 8)
iex> {:ok, b} = BitFieldSet.new(<<85>>, 8)
iex> BitFieldSet.difference(a, b)
#MapSet<[0, 2, 4, 6]>
iex> BitFieldSet.difference(b, a)
#MapSet<[1, 3, 5, 7]>
Takes two piece sets and return true
if the two sets does not share any members,
otherwise false
will get returned.
iex> {:ok, a} = BitFieldSet.new(<<0b00101110>>, 8)
iex> {:ok, b} = BitFieldSet.new(<<0b11010001>>, 8)
iex> {:ok, c} = BitFieldSet.new(<<0b11101000>>, 8)
iex> BitFieldSet.disjoint?(a, b)
true
iex> BitFieldSet.disjoint?(a, c)
false
Takes two piece sets with the same info_hash
, and return true
if both sets
contain exactly the same pieces; and false
otherwise.
iex> {:ok, a} = BitFieldSet.new(<<0b10100110>>, 8)
iex> {:ok, b} = BitFieldSet.new(<<0b10100110>>, 8)
iex> BitFieldSet.equal?(a, b)
true
iex> {:ok, c} = BitFieldSet.new(<<0b11011011>>, 8)
iex> BitFieldSet.equal?(a, c)
false
Specs
has(t) :: non_neg_integer
Take a piece set and return the number of its available pieces.
iex> BitFieldSet.new!(<<0b10101010>>, 8) |> BitFieldSet.has
4
Specs
has_all?(t) :: boolean
Take a piece set and return true
if the set contains all the pieces,
and false
otherwise.
iex> BitFieldSet.new!(<<0b10011010>>, 8) |> BitFieldSet.has_all?
false
iex> BitFieldSet.new!(<<0b11111111>>, 8) |> BitFieldSet.has_all?
true
Takes two piece sets with the same info_hash
and return a set containing the pieces
that belong to both sets.
iex> {:ok, a} = BitFieldSet.new(<<0b00101010>>, 8)
iex> {:ok, b} = BitFieldSet.new(<<0b10110011>>, 8)
iex> BitFieldSet.intersection(a, b)
#MapSet<[2, 6]>
Specs
member?(t, non_neg_integer) :: boolean
Takes a piece set and a piece number and return true
if the given piece number
is present in the set; false
otherwise.
iex> {:ok, set} = BitFieldSet.new(<<0b10000001>>, 8)
iex> BitFieldSet.member?(set, 7)
true
iex> BitFieldSet.member?(set, 2)
false
Specs
new(binary, pos_integer, any) :: t
Create a new piece set given either a size
(an integer denoting bit size)
or some content
(a binary, the size will be set from the bit size of this
binary) and a size
.
iex> BitFieldSet.new!(16) |> BitFieldSet.to_binary
<<0, 0>>
The size will be taken from the input when a piece set is created with data:
iex> BitFieldSet.new!(<<128, 1>>, 16) |> BitFieldSet.to_list
[0, 15]
An optional info_hash
can be given as the third argument, which will ensure
only compatible bit-fields are compared.
Take a piece set and an index. The given index will get removed from the piece set and the updated piece set will get returned:
iex> {:ok, set} = BitFieldSet.new(<<0b10101000>>, 8)
iex> BitFieldSet.remove(set, 2) |> BitFieldSet.to_list
[0, 4]
Take a piece set and an index. The given index will get added to the piece set and the updated piece set will get returned:
iex> {:ok, set} = BitFieldSet.new(<<0b10101000>>, 8)
iex> BitFieldSet.set(set, 6) |> BitFieldSet.to_list
[0, 2, 4, 6]
Set all the bits to true in the set.
iex> {:ok, set} = BitFieldSet.new(<<0b10100110>>, 8)
iex> BitFieldSet.set_all(set) |> BitFieldSet.has_all?
true
Takes two piece sets, a and b, who has the same info_hash
, and return true
if
all the members of set a are also members of set b; false
otherwise.
iex> {:ok, a} = BitFieldSet.new(<<0b00000110>>, 8)
iex> {:ok, b} = BitFieldSet.new(<<0b00101110>>, 8)
iex> BitFieldSet.subset?(a, b)
true
iex> BitFieldSet.subset?(b, a)
false
Specs
to_binary(t) :: binary
Take a piece set and return the bit field representation of the set.
iex> BitFieldSet.new!(<<0b10011010, 0b10000000>>, 16) |> BitFieldSet.to_binary
<<154, 128>>
Specs
to_list(t) :: [non_neg_integer]
Take a piece set and return the available pieces as a list.
iex> BitFieldSet.new!(<<0b10011010>>, 8) |> BitFieldSet.to_list
[0, 3, 4, 6]
Takes two piece sets with the same info_hash
and return a set containing all
members of both sets.
iex> {:ok, a} = BitFieldSet.new(<<0b00101010>>, 8)
iex> {:ok, b} = BitFieldSet.new(<<0b10000000>>, 8)
iex> BitFieldSet.union(a, b)
#MapSet<[0, 2, 4, 6]>