on master help sign in
Merge pull request #735 from Xashili/fixSlice

Fix slice and #725

commite13195c2ab9d5244823b7f512fa8b2fcf867cddf authorEvan Czaplicki <info@elm-lang.org> dateSat 9 Aug 2014, 01:56 (12 years ago) parentaebe55ebdfaef0
2 files changed+14−7
libraries/Native/Array.js+13−7 tests/libraries/Test/Array.elm+1
libraries/Native/Array.js+13−7
@@ -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
tests/libraries/Test/Array.elm+1
@@ -45,6 +45,7 @@ tests =
4545 , test "slice 2" <| assertEqual (Array.fromList [1,2,3]) (Array.slice 1 4 (Array.fromList [0,1,2,3,4]))
4646 , test "slice 3" <| assertEqual (Array.fromList [1,2,3]) (Array.slice 1 -1 (Array.fromList [0,1,2,3,4]))
4747 , test "slice 4" <| assertEqual (Array.fromList [2]) (Array.slice -3 -2 (Array.fromList [0,1,2,3,4]))
48+ , test "slice 5" <| assertEqual 63 (Array.length <| Array.slice 65 (65 + 63) <| Array.fromList [1..200])
4849 ]
4950 mappingAndFoldingTests = suite "Mapping and Folding"
5051 [ test "map" <| assertEqual (Array.fromList [1,2,3]) (Array.map sqrt (Array.fromList [1,4,9]))