Code Monkey home page Code Monkey logo

bitset's Introduction

bitset

Hex.pm Version

elixir bitset module inspired by stl bitset, C# BitArray

Installation

add in deps of mix.exs

def deps do
  [
    {:bitset, "~> 0.2"}
  ]
end

Test

test "bitset - new" do
    assert new(0) == %Bitset{size: 0, data: <<>>}
    assert new(10) == %Bitset{size: 10, data: <<0, 0::2>>}
    assert new(17) == %Bitset{size: 17, data: <<0, 0, 0::1>>}
  end

  test "bitset - all?" do
    assert not (new(10) |> set(0) |> all?())
    assert not (new(10) |> set(1) |> all?())
    assert not (new(10) |> set(9) |> all?())
    assert new(3) |> set(0) |> set(1) |> set(2) |> all?()
  end

  test "bitset - any?" do
    assert new(10) |> set(0) |> any?()
    assert new(10) |> set(1) |> any?()
    assert new(10) |> set(9) |> any?()
    assert new(3) |> set(0) |> set(1) |> set(2) |> any?()
  end

  test "bitset - none?" do
    assert not (new(10) |> set(0) |> none?())
    assert not (new(10) |> set(1) |> none?())
    assert not (new(10) |> set(9) |> none?())
    assert not (new(3) |> set(0) |> set(1) |> set(2) |> none?())

    assert new(1) |> none?()
    assert new(5) |> none?()
    assert new(8) |> none?()
    assert new(10) |> none?()
  end

  # 10110000
  defp bit8() do
    new(8) |> set(0) |> set(2) |> set(3)
  end

  # 10110000 10
  defp bit10() do
    new(10) |> set(0) |> set(2) |> set(3) |> set(8)
  end

  # 10110000 11010100 00010001 00001010
  defp bit32() do
    new(32)
    |> set(0)
    |> set(2)
    |> set(3)
    |> set(8)
    |> set(9)
    |> set(11)
    |> set(13)
    |> set(19)
    |> set(23)
    |> set(28)
    |> set(30)
  end

  # 10110000 11010100 00010001 00001010 101
  defp bit35() do
    new(35)
    |> set(0)
    |> set(2)
    |> set(3)
    |> set(8)
    |> set(9)
    |> set(11)
    |> set(13)
    |> set(19)
    |> set(23)
    |> set(28)
    |> set(30)
    |> set(32)
    |> set(34)
  end

  test "bitset - flip" do
    # 10110000 10
    # flip> 01001111 01
    assert bit10() |> to_string() == "1011000010"
    assert bit10() |> flip() |> to_string() == "0100111101"

    # 10110000 10
    assert bit10() |> flip(2) |> to_string() == "1001000010"
    # 10110000 10
    assert bit10() |> flip(8) |> to_string() == "1011000000"
  end

  test "bitset - reverse" do
    # 10110000 10
    # reverse> 01001111 01
    assert bit10() |> to_string() == "1011000010"
    assert bit10() |> reverse() |> to_string() == "0100001101"
  end

  test "bitset - set all bits" do
    assert new(1) |> set() |> count() == 1
    assert new(8) |> set(1) |> set() |> count() == 8
    assert new(11) |> set() |> count() == 11
    assert new(19) |> set() |> count() == 19
  end

  test "bitset - reset" do
    assert bit10() |> reset() |> count() == 0
    assert bit32() |> reset() |> count() == 0
    assert bit35() |> reset() |> count() == 0
    assert new(8) |> set(1) |> reset(1) |> count() == 0
  end

  test "bitset - to_string" do
    assert new(10) |> set(0) |> to_string() == "1000000000"
    assert new(10) |> set(1) |> to_string() == "0100000000"
    # assert new(10) |> set(9) |> to_string() == "0000001000"
    assert new(3) |> set(0) |> set(1) |> set(2) |> to_string() == "111"

    # 10110000 10
    # 10110000 11010100 00010001 00001010
    # 10110000 11010100 00010001 00001010 101
    assert bit10() |> to_string() == "1011000010"

    assert bit32()
           |> to_string() ==
             "10110000110101000001000100001010"

    assert bit35()
           |> to_string() ==
             "10110000110101000001000100001010101"
  end

  test "bitset - to_bytes" do
    # 10110000
    # -> 00001101
    assert bit8() |> to_bytes() |> to_string() == "00001101"

    # 10110000 10
    # -> 00001101 00000001
    assert bit10() |> to_bytes() |> size() == 16
    assert bit10() |> to_bytes() |> to_string() == "0000110100000001"

    # 10110000 11010100 00010001 00001010
    # -> 00001101 00101011 10001000 01010000
    assert bit32()
           |> to_bytes()
           |> to_string() ==
             "00001101001010111000100001010000"

    # 10110000 11010100 00010001 00001010 101
    # -> 00001101 00101011 10001000 01010000 00000101
    assert bit35()
           |> to_bytes()
           |> to_string() ==
             "0000110100101011100010000101000000000101"
  end

bitset's People

Contributors

kminwoog avatar

Stargazers

Abhijit Kar ใƒ„ avatar Alexandre de Souza avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.