Peer Stritzinger avatar Peer Stritzinger committed 6de6655

Allow lists as value in new, will be replicated

Comments (0)

Files changed (1)

 	     type=Type, elem_size=Elem_size}.
 
 repl_value(Count, integer, Elem_size, Value) ->
-    << <<X:Elem_size/integer>> || X <- lists:duplicate(Count, Value) >>;
+    << <<X:Elem_size/integer>> || X <- fill_list(Count, Value) >>;
 repl_value(Count, float, Elem_size, Value) ->
-    << <<X:Elem_size/float>> || X <- lists:duplicate(Count, Value) >>.
+    << <<X:Elem_size/float>> || X <- fill_list(Count, Value) >>.
+
+fill_list(Count, Value) when not is_list(Value) ->
+    lists:duplicate(Count, Value);
+fill_list(Count, [_|_]=List) when Count >= 0 ->
+    Len = length(List),
+    Dup = Count div Len,
+    Res = lists:flatten(lists:duplicate(Dup, List)),
+    case Count rem Len of
+	0 ->
+	    Res;
+	Rem ->
+	    Res ++ lists:sublist(List, Rem)
+    end.
 
 to_list(#ndarray{strides=Strides, data=Data, 
 		  type=Type, elem_size=Elem_size}) ->
 new_test_() ->
     [?_assertEqual([[0,0],[0,0],[0,0]], to_list(new([3,2], integer, 32))),
      ?_assertEqual([[1,1,1],[1,1,1]], to_list(new([2,3], integer, 8, 1))),
+     ?_assertEqual([[1,1,1],[1,1,1]], to_list(new([2,3], integer, 8, [1]))),
+     ?_assertEqual([[1,2,3],[1,2,3]], to_list(new([2,3], integer, 8, [1,2,3]))),
+     ?_assertEqual([[1,2,3],[4,5,6]], to_list(new([2,3], integer, 8, 
+						  [1,2,3,4,5,6]))),
      ?_assertError(badarg, to_list(new([-1,3], integer, 8, 1))),
      ?_assertError(badarg, to_list(new([2,0], integer, 8, 1))),
      ?_assertError(function_clause, to_list(new([], integer, 8, 1))),
      ?_assertEqual([[3.5,3.5],[3.5,3.5]], 
 		   to_list(new([2,2], float, 32, 3.5))),
      ?_assertEqual([[3.14,3.14],[3.14,3.14]], 
-		   to_list(new([2,2], float, 64, 3.14)))].
+		   to_list(new([2,2], float, 64, 3.14))),
+     ?_assertEqual([1,1,1], fill_list(3,1)),
+     ?_assertEqual([1,1,1], fill_list(3,[1])),
+     ?_assertEqual([1,2,3], fill_list(3,[1,2,3])),
+     ?_assertEqual([1,2], fill_list(2,[1,2,3])),
+     ?_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,[]))].
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.