@@ -35,8 +35,10 @@ Elm.Native.Array.make = function(elm) {
3535 function unsafeGet(i, array) {
3636 for (var x = array.height; x > 0; x--) {
3737 var slot = i >> (x * 5);
38+ while (array.lengths[slot] <= i) {
39+ slot++;
40+ }
3841 if (slot > 0) {
39− while (array.lengths[slot - 1] > i) { slot--; }
4042 i -= array.lengths[slot - 1];
4143 }
4244 array = array.table[slot];
@@ -311,10 +313,12 @@ Elm.Native.Array.make = function(elm) {
311313
312314 // Create new node.
313315 var newA = { ctor:"_Array", height:a.height
314− , table:a.table.slice(0, right + 1)
315− , lengths:a.lengths.slice(0, right + 1) };
316− newA.table[right] = sliced;
317− newA.lengths[right] = length(sliced) + (right > 0 ? newA.lengths[right - 1] : 0);
316+ , table:a.table.slice(0, right)
317+ , lengths:a.lengths.slice(0, right) };
318+ if (sliced.table.length > 0) {
319+ newA.table[right] = sliced;
320+ newA.lengths[right] = length(sliced) + (right > 0 ? newA.lengths[right - 1] : 0);
321+ }
318322 return newA;
319323 }
320324
@@ -343,7 +347,7 @@ Elm.Native.Array.make = function(elm) {
343347 var newA = { ctor:"_Array", height:a.height
344348 , table:a.table.slice(left, a.table.length + 1)
345349 , lengths:new Array(a.table.length - left) };
346− newA.table[left] = sliced;
350+ newA.table[0] = sliced;
347351 var len = 0;
348352 for (var i = 0; i < newA.table.length; i++) {
349353 len += length(newA.table[i]);
@@ -558,7 +562,9 @@ Elm.Native.Array.make = function(elm) {
558562 // find the exact slot via forward searching in "lengths". Returns the index.
559563 function getSlot(i, a) {
560564 var slot = i >> (5 * a.height);
561− while (a.lengths[slot - 1] > i) { slot--; }
565+ while (a.lengths[slot] <= i) {
566+ slot++;
567+ }
562568 return slot;
563569 }
564570