@@ -61,6 +61,102 @@ Elm.Native.Array.make = function(elm) {
61 return newA;
61 return newA;
64+ function initialize(len, f) {
65+ if (len == 0) { return empty; }
66+ var h = Math.floor(Math.log(len)/Math.log(M));
67+ return initialize_(f, h, 0, len);
70+ function initialize_(f, h, from, to) {
72+ var table = new Array((to - from) % (M + 1));
73+ for (var i = 0; i < table.length; i++) {
74+ table[i] = f(from + i);
76+ return { ctor:"_Array", height:0, table:table };
79+ var step = Math.pow(M, h);
80+ var table = new Array(Math.ceil((to - from) / step));
81+ var lengths = new Array(table.length);
82+ for (var i = 0; i < table.length; i++) {
83+ table[i] = initialize_( f, h - 1, from + (i * step)
84+ , Math.min(from + ((i + 1) * step), to));
85+ if (i > 0) { lengths[i] = lengths[i-1] + length(table[i]); }
87+ return { ctor:"_Array", height:h, table:table, lengths:lengths };
90+ function fromList(list) {
91+ if (list == List.Nil) { return empty; }
93+ // Allocate M sized blocks (table) and write list elements to it.
94+ var table = new Array(M);
95+ var nodes = new Array();
98+ while (list.ctor !== '[]') {
103+ // table is full, so we can push a leaf containing it into the
106+ fromListPush({ ctor:"_Array", height:0, table:table }
108+ table = new Array(M);
113+ // Maybe there is something left on the table.
115+ fromListPush({ ctor:"_Array", height:0, table:table.splice(0,i) }
119+ // Go through all of the nodes and eventually push them into higher nodes.
120+ for (var h = 0; h < nodes.length - 1; h++) {
121+ if (nodes[h].table.length > 0) {
122+ fromListPush(nodes[h], nodes);
126+ var head = nodes[nodes.length - 1];
127+ if (head.height > 0 && head.table.length == 1) {
128+ return head.table[0];
134+ // Push a node into a higher node as a child.
135+ function fromListPush(toPush, nodes) {
136+ var h = toPush.height;
138+ // Maybe the node on this height does not exist.
139+ if (nodes.length == h) {
140+ nodes.push({ ctor:"_Array", height:h + 1
142+ , lengths:new Array() });
145+ nodes[h].table.push(toPush);
146+ var len = length(toPush);
147+ if (nodes[h].lengths.length > 0) {
148+ len += nodes[h].lengths[nodes[h].lengths.length - 1];
150+ nodes[h].lengths.push(len);
152+ if (nodes[h].table.length == M) {
153+ fromListPush(nodes[h], nodes);
154+ nodes[h] = { ctor:"_Array", height:h + 1
156+ , lengths:new Array() };
64 // Pushes an item via push_ to the bottom right of a tree.
160 // Pushes an item via push_ to the bottom right of a tree.
65 function push(item, a) {
161 function push(item, a) {
66 var pushed = push_(item, a);
162 var pushed = push_(item, a);
@@ -477,7 +573,9 @@ Elm.Native.Array.make = function(elm) {
478 Elm.Native.Array.values = {
574 Elm.Native.Array.values = {
479 empty:empty,
575 empty:empty,
480 toList:toList,
577 toList:toList,
578+ initialize:F2(initialize),
481 append:F2(append),
579 append:F2(append),
482 push:F2(push),
580 push:F2(push),
483 slice:F3(slice),
581 slice:F3(slice),