-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add configurable size filter. Add struct
- Loading branch information
1 parent
af9d56d
commit e11cf42
Showing
4 changed files
with
57 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
defmodule Petal.Bitfield do | ||
@moduledoc """ | ||
Bitfield is the core structure about the Bloom filter. | ||
""" | ||
|
||
@typedoc "Defines the core type" | ||
@type t :: %__MODULE__{ | ||
bitfield: binary(), | ||
size: pos_integer() | ||
} | ||
|
||
defstruct bitfield: <<>>, size: 64 | ||
end | ||
|
||
defimpl String.Chars, for: Petal.Bitfield do | ||
def to_string(item) do | ||
"Bitsize: #{item.size} Field data: #{do_pretty_print(item.bitfield)}" | ||
end | ||
|
||
defp do_pretty_print(bitfield, accm \\ []) | ||
|
||
defp do_pretty_print(<<bit::1>> = _bitfield, accm) do | ||
[bit_set(bit) | accm] | ||
|> Enum.reverse() | ||
|> Enum.join() | ||
end | ||
|
||
defp do_pretty_print(<<bit::1, rest::bitstring>> = _bitfield, accm) do | ||
do_pretty_print(rest, [bit_set(bit) | accm]) | ||
end | ||
|
||
defp bit_set(1), do: "1" | ||
defp bit_set(_not_set), do: "0" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters