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

t

Functions

difference(bit_field_set1, bit_field_set2)

Specs

difference(t, t) :: MapSet.t

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]>
disjoint?(bit_field_set1, bit_field_set2)

Specs

disjoint?(t, t) :: boolean

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
equal?(bit_field_set1, bit_field_set2)

Specs

equal?(t, t) :: boolean

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
has(bit_field_set)

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
has_all?(bit_field_set)

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
intersection(bit_field_set1, bit_field_set2)

Specs

intersection(t, t) :: MapSet.t

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]>
member?(bit_field_set, piece_number)

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
new(content \\ <<>>, size, info_hash \\ nil)

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.

new!(content \\ <<>>, content_size, info_hash \\ nil)

Specs

new!(binary, pos_integer, any) :: t
remove(state, piece)

Specs

remove(t, non_neg_integer) :: t

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]
set(state, piece)

Specs

set(t, non_neg_integer) :: t

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(state)

Specs

set_all(t) :: t

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
subset?(bit_field_set1, bit_field_set2)

Specs

subset?(t, t) :: boolean

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
to_binary(set)

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>>
to_list(bit_field_set)

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]
union(bit_field_set1, bit_field_set2)

Specs

union(t, t) :: MapSet.t

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]>