@@ -1,4 +1,4 @@
1−Elm.Native.Array = {};
1+Elm.Native.Array = {};
22Elm.Native.Array.make = function(elm) {
33 elm.Native = elm.Native || {};
44 elm.Native.Array = elm.Native.Array || {};
@@ -24,38 +24,144 @@ Elm.Native.Array.make = function(elm) {
2424 // An empty array.
2525 var empty = { ctor:"_Array", height:0, table:new Array() };
2626
27− // Gets the value at index i recursively.
28− function get(i, a) {
29− if (a.height == 0) {
30− if (i < a.table.length) {
31− return a.table[i];
32− } else {
33− throw new Error("Index "+ i +" is out of range. Check the length of your array first or use getSafe or getMaybe.");
27+ function get(i, array) {
28+ if (i < 0 || i >= length(array)) {
29+ throw new Error("Index " + i + " is out of range. Check the length of " +
30+ "your array first or use getMaybe or getWithDefault.");
3431 }
35− }
32+ return unsafeGet(i, array);
33+ }
3634
37− var slot = getSlot(i, a);
38− var sub = slot > 0 ? a.lengths[slot-1] : 0;
39− return get(i - sub, a.table[slot]);
35+ function unsafeGet(i, array) {
36+ for (var x = array.height; x > 0; x--) {
37+ var slot = i >> (x * 5);
38+ if (slot > 0) {
39+ while (array.lengths[slot - 1] > i) { slot--; }
40+ i -= array.lengths[slot - 1];
41+ }
42+ array = array.table[slot];
43+ }
44+ return array.table[i];
4045 }
4146
4247 // Sets the value at the index i. Only the nodes leading to i will get
4348 // copied and updated.
44− function set(i, item, a) {
45− if (length(a) <= i) {
46− return a;
49+ function set(i, item, array) {
50+ if (i < 0 || length(array) <= i) {
51+ return array;
4752 }
48− var newA = nodeCopy(a);
49− newA.table = a.table.slice();
53+ return unsafeSet(i, item, array);
54+ }
5055
51− if (a.height == 0) {
52− newA.table[i] = item;
56+ function unsafeSet(i, item, array) {
57+ array = nodeCopy(array);
58+
59+ if (array.height == 0) {
60+ array.table[i] = item;
5361 } else {
54− var slot = getSlot(i, a);
55− var sub = slot > 0 ? a.lengths[slot-1] : 0;
56− newA.table[slot] = set(i - sub, item, a.table[slot]);
62+ var slot = getSlot(i, array);
63+ if (slot > 0) {
64+ i -= array.lengths[slot - 1];
65+ }
66+ array.table[slot] = unsafeSet(i, item, array.table[slot]);
67+ }
68+ return array;
69+ }
70+
71+ function initialize(len, f) {
72+ if (len == 0) { return empty; }
73+ var h = Math.floor(Math.log(len)/Math.log(M));
74+ return initialize_(f, h, 0, len);
75+ }
76+
77+ function initialize_(f, h, from, to) {
78+ if (h == 0) {
79+ var table = new Array((to - from) % (M + 1));
80+ for (var i = 0; i < table.length; i++) {
81+ table[i] = f(from + i);
82+ }
83+ return { ctor:"_Array", height:0, table:table };
84+ }
85+
86+ var step = Math.pow(M, h);
87+ var table = new Array(Math.ceil((to - from) / step));
88+ var lengths = new Array(table.length);
89+ for (var i = 0; i < table.length; i++) {
90+ table[i] = initialize_( f, h - 1, from + (i * step)
91+ , Math.min(from + ((i + 1) * step), to));
92+ lengths[i] = length(table[i]) + (i > 0 ? lengths[i-1] : 0);
93+ }
94+ return { ctor:"_Array", height:h, table:table, lengths:lengths };
95+ }
96+
97+ function fromList(list) {
98+ if (list == List.Nil) { return empty; }
99+
100+ // Allocate M sized blocks (table) and write list elements to it.
101+ var table = new Array(M);
102+ var nodes = new Array();
103+ var i = 0;
104+
105+ while (list.ctor !== '[]') {
106+ table[i] = list._0;
107+ list = list._1;
108+ i++;
109+
110+ // table is full, so we can push a leaf containing it into the
111+ // next node.
112+ if (i == M) {
113+ fromListPush({ ctor:"_Array", height:0, table:table }
114+ , nodes);
115+ table = new Array(M);
116+ i = 0;
117+ }
118+ }
119+
120+ // Maybe there is something left on the table.
121+ if (i > 0) {
122+ fromListPush({ ctor:"_Array", height:0, table:table.splice(0,i) }
123+ , nodes);
124+ }
125+
126+ // Go through all of the nodes and eventually push them into higher nodes.
127+ for (var h = 0; h < nodes.length - 1; h++) {
128+ if (nodes[h].table.length > 0) {
129+ fromListPush(nodes[h], nodes);
130+ }
131+ }
132+
133+ var head = nodes[nodes.length - 1];
134+ if (head.height > 0 && head.table.length == 1) {
135+ return head.table[0];
136+ } else {
137+ return head;
138+ }
139+ }
140+
141+ // Push a node into a higher node as a child.
142+ function fromListPush(toPush, nodes) {
143+ var h = toPush.height;
144+
145+ // Maybe the node on this height does not exist.
146+ if (nodes.length == h) {
147+ nodes.push({ ctor:"_Array", height:h + 1
148+ , table:new Array()
149+ , lengths:new Array() });
150+ }
151+
152+ nodes[h].table.push(toPush);
153+ var len = length(toPush);
154+ if (nodes[h].lengths.length > 0) {
155+ len += nodes[h].lengths[nodes[h].lengths.length - 1];
156+ }
157+ nodes[h].lengths.push(len);
158+
159+ if (nodes[h].table.length == M) {
160+ fromListPush(nodes[h], nodes);
161+ nodes[h] = { ctor:"_Array", height:h + 1
162+ , table:new Array()
163+ , lengths:new Array() };
57164 }
58− return newA;
59165 }
60166
61167 // Pushes an item via push_ to the bottom right of a tree.
@@ -65,7 +171,7 @@ Elm.Native.Array.make = function(elm) {
65171 return pushed;
66172 }
67173
68− newTree = create(item, a.height);
174+ var newTree = create(item, a.height);
69175 return siblise(a, newTree);
70176 }
71177
@@ -143,22 +249,19 @@ Elm.Native.Array.make = function(elm) {
143249 }
144250
145251 function foldl(f, b, a) {
146− for (var i = a.table.length - 1; i >= 0; i--) {
252+ for (var i = 0; i < a.table.length; i++) {
147253 b = A2(f, a.height == 0 ? a.table[i] : foldl(f, b, a.table[i]), b);
148254 }
149255 return b;
150256 }
151257
152258 function foldr(f, b, a) {
153− for (var i = 0; i < a.table.length; i++) {
259+ for (var i = a.table.length; i--; ) {
154260 b = A2(f, a.height == 0 ? a.table[i] : foldr(f, b, a.table[i]), b);
155261 }
156262 return b;
157263 }
158264
159− // Returns a sliced tree. "to" is inclusive, but this may change,
160− // when I understand, why e.g. JS does not handle it this way. :-)
161− // If "from" or "to" is negative, they will select from the end on.
162265 // TODO: currently, it slices the right, then the left. This can be
163266 // optimized.
164267 function slice(from, to, a) {
@@ -175,7 +278,7 @@ Elm.Native.Array.make = function(elm) {
175278 // Handle leaf level.
176279 if (a.height == 0) {
177280 var newA = { ctor:"_Array", height:0 };
178− newA.table = a.table.slice(0, to + 1);
281+ newA.table = a.table.slice(0, to);
179282 return newA;
180283 }
181284
@@ -232,15 +335,15 @@ Elm.Native.Array.make = function(elm) {
232335 return newA;
233336 }
234337
235− // Concats two trees.
236− // TODO: Add support for concatting trees of different sizes. Current
237− // behavior will just rise the lower tree and then concat them.
238− function concat(a, b) {
239− if (b.height > a.height) { return concat(parentise(a, b.height), b); }
240− if (a.height > b.height) { return concat(a, parentise(b, a.height)); }
241− if (a.height == 0) { return concat(parentise(a, 1), parentise(b, 1)); }
338+ // Appends two trees.
339+ // TODO: Add support for appending trees of different sizes. Current
340+ // behavior will just rise the lower tree and then append them.
341+ function append(a,b) {
342+ if (b.height > a.height) { return append(parentise(a, b.height), b); }
343+ if (a.height > b.height) { return append(a, parentise(b, a.height)); }
344+ if (a.height == 0) { return append(parentise(a, 1), parentise(b, 1)); }
242345
243− var c = concat_(a, b);
346+ var c = append_(a, b);
244347 if (c[1].table.length > 0) {
245348 return siblise(c[0], c[1]);
246349 } else {
@@ -249,9 +352,9 @@ Elm.Native.Array.make = function(elm) {
249352 }
250353
251354 // Returns an array of two nodes. The second node _may_ be empty. This case
252− // needs to be handled by the function, that called concat_. May be only
355+ // needs to be handled by the function, that called append_. May be only
253356 // called for trees with an minimal height of 1.
254− function concat_(a, b) {
357+ function append_(a, b) {
255358 if (a.height == 1) {
256359 // Check if balancing is needed and return based on that.
257360 var toRemove = calcToRemove(a, b);
@@ -262,18 +365,18 @@ Elm.Native.Array.make = function(elm) {
262365 return shuffle(a, b, toRemove);
263366 }
264367
265− var concated = concat_(botRight(a), botLeft(b));
368+ var appended = append_(botRight(a), botLeft(b));
266369 a = nodeCopy(a), b = nodeCopy(b);
267370
268371 // Adjust the bottom right side of the new tree.
269− a.table[a.table.length - 1] = concated[0];
270− a.lengths[a.lengths.length - 1] = length(concated[0])
372+ a.table[a.table.length - 1] = appended[0];
373+ a.lengths[a.lengths.length - 1] = length(appended[0])
271374 a.lengths[a.lengths.length - 1] += a.lengths.length > 1 ? a.lengths[a.lengths.length - 2] : 0;
272375
273376 // Adjust the bottom left side of the new tree.
274− if (concated[1].table.length > 0) {
275− b.table[0] = concated[1];
276− b.lengths[0] = length(concated[1]);
377+ if (appended[1].table.length > 0) {
378+ b.table[0] = appended[1];
379+ b.lengths[0] = length(appended[1]);
277380 for (var i = 1, len = length(b.table[0]); i < b.lengths.length; i++) {
278381 len += length(b.table[i]);
279382 b.lengths[i] = len;
@@ -423,21 +526,19 @@ Elm.Native.Array.make = function(elm) {
423526 }
424527
425528 // Returns how many items are in the tree.
426− function length(a) {
427− if (a.height == 0) {
428− return a.table.length;
529+ function length(array) {
530+ if (array.height == 0) {
531+ return array.table.length;
429532 } else {
430− return a.lengths[a.lengths.length - 1];
533+ return array.lengths[array.lengths.length - 1];
431534 }
432535 }
433536
434537 // Calculates in which slot of "table" the item probably is, then
435538 // find the exact slot via forward searching in "lengths". Returns the index.
436539 function getSlot(i, a) {
437− var slot = Math.floor(i / (Math.pow(M, a.height)));
438− while (a.lengths[slot] <= i) {
439− slot++
440− }
540+ var slot = i >> (5 * a.height);
541+ while (a.lengths[slot - 1] > i) { slot--; }
441542 return slot;
442543 }
443544
@@ -472,10 +573,52 @@ Elm.Native.Array.make = function(elm) {
472573 , lengths:[length(a), length(a) + length(b)] };
473574 }
474575
576+ function toJSArray(a) {
577+ var jsArray = new Array(length(a));
578+ toJSArray_(jsArray, 0, a);
579+ return jsArray;
580+ }
581+
582+ function toJSArray_(jsArray, i, a) {
583+ for (var t = 0; t < a.table.length; t++) {
584+ if (a.height == 0) {
585+ jsArray[i + t] = a.table[t];
586+ } else {
587+ var inc = t == 0 ? 0 : a.lengths[t - 1];
588+ toJSArray_(jsArray, i + inc, a.table[t]);
589+ }
590+ }
591+ }
592+
593+ function fromJSArray(jsArray) {
594+ if (jsArray.length == 0) { return empty; }
595+ var h = Math.floor(Math.log(jsArray.length) / Math.log(M));
596+ return fromJSArray_(jsArray, h, 0, jsArray.length);
597+ }
598+
599+ function fromJSArray_(jsArray, h, from, to) {
600+ if (h == 0) {
601+ return { ctor:"_Array", height:0
602+ , table:jsArray.slice(from, to) };
603+ }
604+
605+ var step = Math.pow(M, h);
606+ var table = new Array(Math.ceil((to - from) / step));
607+ var lengths = new Array(table.length);
608+ for (var i = 0; i < table.length; i++) {
609+ table[i] = fromJSArray_( jsArray, h - 1, from + (i * step)
610+ , Math.min(from + ((i + 1) * step), to));
611+ lengths[i] = length(table[i]) + (i > 0 ? lengths[i-1] : 0);
612+ }
613+ return { ctor:"_Array", height:h, table:table, lengths:lengths };
614+ }
615+
475616 Elm.Native.Array.values = {
476617 empty:empty,
618+ fromList:fromList,
477619 toList:toList,
478− concat:F2(concat),
620+ initialize:F2(initialize),
621+ append:F2(append),
479622 push:F2(push),
480623 slice:F3(slice),
481624 get:F2(get),
@@ -484,7 +627,10 @@ Elm.Native.Array.make = function(elm) {
484627 indexedMap:F2(indexedMap),
485628 foldl:F3(foldl),
486629 foldr:F3(foldr),
487− length:length
630+ length:length,
631+
632+ toJSArray:toJSArray,
633+ fromJSArray:fromJSArray
488634 };
489635
490636 return elm.Native.Array.values = Elm.Native.Array.values;