Skip to content

Instantly share code, notes, and snippets.

@pragdave
Created January 15, 2014 17:46
Show Gist options
  • Save pragdave/8440865 to your computer and use it in GitHub Desktop.
Save pragdave/8440865 to your computer and use it in GitHub Desktop.
defimpl Enumerable, for: Bitmap do
import :math, only: [log: 1]
def reduce(bitmap, {:cont, acc}, fun) do
bit_count = Enum.count(bitmap)
_reduce({bitmap, bit_count}, { :cont, acc }, fun)
end
defp _reduce({_bitmap, -1}, { :cont, acc }, _fun), do: { :done, acc }
defp _reduce({bitmap, bit_number}, { :cont, acc }, fun) do
_reduce({bitmap, bit_number-1}, fun.(bitmap[bit_number], acc), fun)
end
def member?(value, bit_number) do
{ :ok, value[bit_number] }
end
def count(Bitmap[value: value]) do
{ :ok, trunc(log(abs(value))/log(2)) + 1 }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment