@@ -24,7 +24,6 @@ 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.
2827 function get(i, array) {
2928 if (i < 0 || i >= length(array)) {
3029 throw new Error("Index " + i + " is out of range. Check the length of " +
@@ -34,31 +33,39 @@ Elm.Native.Array.make = function(elm) {
3433 }
3534
3635 function unsafeGet(i, array) {
37− if (array.height == 0) {
38− return array.table[i];
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];
3941 }
40− var slot = getSlot(i, array);
41− var offset = slot > 0 ? array.lengths[slot-1] : 0;
42− return unsafeGet(i - offset, array.table[slot]);
42+ array = array.table[slot];
43+ }
44+ return array.table[i];
4345 }
4446
4547 // Sets the value at the index i. Only the nodes leading to i will get
4648 // copied and updated.
47− function set(i, item, a) {
48− if (length(a) <= i) {
49− return a;
49+ function set(i, item, array) {
50+ if (i < 0 || length(array) <= i) {
51+ return array;
5052 }
51− var newA = nodeCopy(a);
52− newA.table = a.table.slice();
53+ return unsafeSet(i, item, array);
54+ }
5355
54− if (a.height == 0) {
55− 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;
5661 } else {
57− var slot = getSlot(i, a);
58− var sub = slot > 0 ? a.lengths[slot-1] : 0;
59− 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]);
6067 }
61− return newA;
68+ return array;
6269 }
6370
6471 function initialize(len, f) {
@@ -533,10 +540,8 @@ Elm.Native.Array.make = function(elm) {
533540 // Calculates in which slot of "table" the item probably is, then
534541 // find the exact slot via forward searching in "lengths". Returns the index.
535542 function getSlot(i, a) {
536− var slot = Math.floor(i / (Math.pow(M, a.height)));
537− while (a.lengths[slot] <= i) {
538− slot++
539− }
543+ var slot = i >> (5 * a.height);
544+ while (a.lengths[slot - 1] > i) { slot--; }
540545 return slot;
541546 }
542547