Commits

Peer Stritzinger  committed 54c0084

Implement part without inside ranges. Fix to_list.

  • Participants
  • Parent commits 43a8909

Comments (0)

Files changed (1)

File src/ndarray.erl

 
 -include_lib("eunit/include/eunit.hrl").
 
--export([new_packed/3, new_packed/4, to_list/1]). 
+-export([new_packed/3, new_packed/4, to_list/1, part/2]). 
 
--record(ndarray, {dimensions, strides, type, elem_size, data}).
+-define(min_index, 1).		   % this might be variable sometimes 
+
+-record(ndarray, {dimensions, strides, type, elem_size, offset, data}).
 
 new_packed(Dims, Type, Elem_size) ->
     new_packed(Dims, Type, Elem_size, 0).
     {Strides, _Bc} = lists:mapfoldl(fun(X, Prod) -> {Prod, X*Prod} end,  
 				    Elem_size, lists:reverse(Dims)),
     Data = repl_value(Count, Type, Elem_size, Value),
-    #ndarray{dimensions=Dims, data=Data, strides=Strides, 
-	     type=Type, elem_size=Elem_size}.
+    #ndarray{dimensions=Dims, strides=lists:reverse(Strides), 
+	     type=Type, elem_size=Elem_size,
+	     offset=0, data=Data}.
 
 repl_value(Count, integer, Elem_size, Value) ->
     << <<X:Elem_size/integer>> || X <- fill_list(Count, Value) >>;
 	    Res ++ lists:sublist(List, Rem)
     end.
 
-to_list(#ndarray{strides=Strides, data=Data, 
-		  type=Type, elem_size=Elem_size}) ->
-    to_list(lists:reverse(Strides), Type, Elem_size, Data).
+to_list(#ndarray{dimensions=[D|_]}=Nda) ->
+    [ to_list(part(Nda, [X])) || X <- lists:seq(?min_index, D) ];
+to_list(Val) -> Val.
 
-to_list([Elem_size], integer, Elem_size, Data) ->
-    [ X || <<X:Elem_size/integer>> <= Data ];
-to_list([Elem_size], float, Elem_size, Data) ->
-    [ X || <<X:Elem_size/float>> <= Data ];
-to_list([S|Strides], Type, Elem_size, Data) ->
-    [ to_list(Strides, Type, Elem_size, X) 
-      || <<X:S/bitstring>> <= Data ].
-    
+part(#ndarray{dimensions=[D], strides=[S], 
+	      type=Type, elem_size=Elem_size,
+	      offset=Offs, data=Data}, [Index]) 
+  when is_integer(Index), ?min_index =< Index, Index =< D ->
+    get_val(Type, Elem_size, (Index-?min_index)*S + Offs, Data);
+
+part(Nda, []) -> 
+    Nda;
+
+part(#ndarray{dimensions=[D|Dims], strides=[S|Strides], offset=Offs}=Nda, 
+     [Index|Rest]) 
+  when is_integer(Index), ?min_index =< Index, Index =< D ->
+    part(Nda#ndarray{dimensions=Dims, strides=Strides, 
+		     offset=(Index-?min_index)*S + Offs}, Rest).
+
+get_val(integer, Elem_size, Skip, Data) ->
+    <<_:Skip/bitstring, X:Elem_size/integer, _/bitstring>> = Data,
+    X;
+get_val(float, Elem_size, Skip, Data) ->
+    <<_:Skip/bitstring, X:Elem_size/float, _/bitstring>> = Data,
+    X.
+
+
 %% Copyright 2011 Peer Stritzinger GmbH, Aumuellerstr. 14, 82216
 %% Ueberacker, Germany http://www.stritzinger.com. All rights reserved. 
 %% 
      ?_assertEqual([1,2,3,1,2], fill_list(5,[1,2,3])),
      ?_assertEqual([], fill_list(0,[1,2,3])),
      ?_assertError(function_clause, fill_list(-1,[1,2,3])),
-     ?_assertError(function_clause, fill_list(2,[]))].
+     ?_assertError(function_clause, fill_list(2,[])),
+     ?_assertEqual([1,2,3], 
+		   to_list(part(new_packed([2,3], integer, 8, 
+					   [[1,2,3],[4,5,6]]), [1]))),
+     ?_assertEqual(2, part(new_packed([2,3], integer, 8, [[1,2,3],[4,5,6]]), 
+			   [1,2]))].