Interesting. You could use bitstrings for this, but matrix operations are absolutely not in my area of expertise, so I can't judge if it's actually a good fit.
The bitstring syntax can indeed be a bit confusing at first, but once you get the hang of it, it's actually pretty easy to use. If you want to create a 8bit bitstring out of single bits:
iex(1)> bits = <<0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1), 1::size(1), 1::size(1)>>
<<3>>
If you want to unpack a 8bit bitstring into its individual bits, you could do it like this:
iex(2)> <<b8::size(1), b7::size(1), b6::size(1), b5::size(1), b4::size(1), b3::size(1), b2::size(1), b1::size(1)>> = bits
<<3>>
iex(3)> b1
1
iex(4)> b2
1
iex(5)> b3
0
etc.
You can use bitstring and list comprehensions to operate on single bits too.
Flip the bits:
iex(6)> flip_bit = fn 0 -> 1; 1 -> 0 end
#Function<6.80484245 in :erl_eval.expr/5>
iex(6)> bc <<b::size(1)>> inbits <<3>>, do: <<flip_bit.(b)::size(1)>>
<<252>>
unpack a 16bit bitstring to a list of bits:
iex(7)> lc <<b::size(1)>> inbits <<1023::size(16)>>, do: b
[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
create an 8bit bitstring from a list of bits:
iex(8)> bc b inlist [0,0,0,0,0,0,1,1], do: <<b::size(1)>>
<<3>>
You can use ETS tables in Elixir when you need mutable data in RAM. It provides quite fast read and write operations. But as far as I know, the way ETS works doesn't allow you to update a bitstring, without reading the entire bitstring from the table, update it, and write it back to the table, so I doubt ETS tables will give you good performance for your use case.
Have fun experimenting :)
Op woensdag 4 december 2013 23:23:14 UTC+1 schreef mraptor: