Packages
mmath
0.2.0-alpha8
0.2.26
0.2.25
0.2.24
0.2.23
0.2.22
0.2.21
0.2.20
0.2.19
0.2.18
0.2.17
0.2.16
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.2.0-alpha9
0.2.0-alpha8
0.2.0-alpha7
0.2.0-alpha6
0.2.0-alpha5
0.2.0-alpha4
0.2.0-alpha3
0.2.0-alpha2
0.2.0-alpha10
0.2.0-alpha1
0.2.0-alpha
0.1.17-alpha
0.1.16
0.1.15
0.1.14
0.1.13
0.1.11
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
math library for metric sequences and binary arrays.
Current section
Files
Jump to
Current section
Files
src/mmath_bin.erl
%%%-------------------------------------------------------------------
%%% @author Heinz Nikolaus Gies <heinz@licenser.net>
%%% @copyright (C) 2014, Heinz Nikolaus Gies
%%% @doc
%%% Binary manipulation functions.
%%% @end
%%% Created : 8 Jun 2014 by Heinz Nikolaus Gies <heinz@licenser.net>
%%%-------------------------------------------------------------------
-module(mmath_bin).
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
-endif.
-include("mmath.hrl").
-export([from_list/1, to_list/1, empty/1, length/1, length_r/1,
realize/1, derealize/1, rdatasize/0, merge/2]).
-define(APPNAME, mmath).
-define(LIBNAME, bin_nif).
-on_load(load_nif/0).
load_nif() ->
SoName = case code:priv_dir(?APPNAME) of
{error, bad_name} ->
case filelib:is_dir(filename:join(["..", priv])) of
true ->
filename:join(["..", priv, ?LIBNAME]);
_ ->
filename:join([priv, ?LIBNAME])
end;
Dir ->
filename:join(Dir, ?LIBNAME)
end,
erlang:load_nif(SoName, 0).
%%--------------------------------------------------------------------
%% @doc
%% Converts a list of values to it's binary representation.
%% @end
%%--------------------------------------------------------------------
-spec from_list([number()]) -> binary().
from_list(_) ->
erlang:nif_error(nif_library_not_loaded).
%%--------------------------------------------------------------------
%% @doc
%% Converts a the binary repesentation back to a list of values.
%% @end
%%--------------------------------------------------------------------
-spec to_list([number()]) -> binary().
to_list(_) ->
erlang:nif_error(nif_library_not_loaded).
%%--------------------------------------------------------------------
%% @doc
%% Calculates the number of values in a binary representation.
%% @end
%%--------------------------------------------------------------------
length(B) ->
trunc(byte_size(B)/?DATA_SIZE).
%%--------------------------------------------------------------------
%% @doc
%% Calculates the number of values in a binary realized
%% representation.
%% @end
%%--------------------------------------------------------------------
length_r(B) ->
trunc(byte_size(B)/?RDATA_SIZE).
%%--------------------------------------------------------------------
%% @doc
%% Creates an empty binary representaton if a given list
%% representation.
%% @end
%%--------------------------------------------------------------------
empty(Length) ->
<<0:((?TYPE_SIZE + ?BITS)*Length)/?INT_TYPE>>.
%%--------------------------------------------------------------------
%% @doc
%% Converts a none realized to the realized data representation.
%% @end
%%--------------------------------------------------------------------
realize(_Data) ->
erlang:nif_error(nif_library_not_loaded).
%%--------------------------------------------------------------------
%% @doc
%% Converts a none realized to the realized data representation.
%% @end
%%--------------------------------------------------------------------
derealize(_Data) ->
erlang:nif_error(nif_library_not_loaded).
%%--------------------------------------------------------------------
%% @doc
%% Utility function to ensure that the ?RDATA_SIZE macro is set
%% correctly.
%% @end
%%--------------------------------------------------------------------
rdatasize() ->
erlang:nif_error(nif_library_not_loaded).
%%--------------------------------------------------------------------
%% @doc
%% Merges two metric lists, filling holes in one with the data of the
%% other.
%% @end
%%--------------------------------------------------------------------
merge(A, B) ->
merge(A, B, <<>>).
merge(<<?NONE:?TYPE_SIZE, _:?BITS/?INT_TYPE, R1/binary>>,
<<D:?DATA_SIZE/binary, R2/binary>>,
Acc) ->
merge(R1, R2, <<Acc/binary, D/binary>>);
merge(<<D:?DATA_SIZE/binary, R1/binary>>,
<<_:?DATA_SIZE/binary, R2/binary>>,
Acc) ->
merge(R1, R2, <<Acc/binary, D/binary>>);
merge(<<>>, <<>>, Acc) ->
Acc;
merge(<<>>, D, Acc) ->
<<Acc/binary, D/binary>>;
merge(D, <<>>, Acc) ->
<<Acc/binary, D/binary>>.
-ifdef(TEST).
size_test() ->
?assertEqual(?RDATA_SIZE, rdatasize()).
-endif.