on master help sign in
Remove trailing whitespaces
fecaad3Christian Widera
libraries/Array.elm+14−14
@@ -4,7 +4,7 @@ module Array where
4array must have the same type. The arrays are implemented in Relaxed
4array must have the same type. The arrays are implemented in Relaxed
5Radix Balanced-Trees for fast updating and concating.
5Radix Balanced-Trees for fast updating and concating.
6
6
7−If you use more then one map or zip function on an array, consider turning it
7+If you use more then one map or zip function on an array, consider turning it
8first into a list, operating on it, and then turning it back into an array.
8first into a list, operating on it, and then turning it back into an array.
9
9
10# Basics
10# Basics
@@ -27,7 +27,7 @@ import List
27
27
28data Array a = Array
28data Array a = Array
29
29
30−{-| Fills an array with a given length and a default element.
30+{-| Fills an array with a given length and a default element.
31
31
32 fill 3 5 == fromList [5,5,5]
32 fill 3 5 == fromList [5,5,5]
33-}
33-}
@@ -59,7 +59,7 @@ indices a = [0..Native.Array.length a - 1]
59assocs : Array a -> [(Int, a)]
59assocs : Array a -> [(Int, a)]
60assocs a = List.zip (indices a) (Native.Array.elements a)
60assocs a = List.zip (indices a) (Native.Array.elements a)
61
61
62−{-| Apply a function on every element in an array.
62+{-| Apply a function on every element in an array.
63
63
64 map (\x -> x*x) (fromList [1,2,3]) == fromList [1,4,9]
64 map (\x -> x*x) (fromList [1,2,3]) == fromList [1,4,9]
65-}
65-}
@@ -73,7 +73,7 @@ map = Native.Array.map
73assocMap : (Int -> a -> b) -> Array a -> Array b
73assocMap : (Int -> a -> b) -> Array a -> Array b
74assocMap = Native.Array.assocMap
74assocMap = Native.Array.assocMap
75
75
76−{-| Reduce an array from the left.
76+{-| Reduce an array from the left.
77
77
78 foldl (+) 0 (fill 3 5) == 15
78 foldl (+) 0 (fill 3 5) == 15
79-}
79-}
@@ -87,7 +87,7 @@ foldl = Native.Array.foldl
87foldr : (a -> b -> b) -> b -> Array a -> b
87foldr : (a -> b -> b) -> b -> Array a -> b
88foldr = Native.Array.foldr
88foldr = Native.Array.foldr
89
89
90−{-| Zip the elements of two arrays via a tuple into a new array. If one array is
90+{-| Zip the elements of two arrays via a tuple into a new array. If one array is
91longer, the extra elements are dropped.
91longer, the extra elements are dropped.
92
92
93 zip (fromList [1,2,3,4]) (fromList [0,0,0]) = fromList [(1,0), (2,0), (3,0)]
93 zip (fromList [1,2,3,4]) (fromList [0,0,0]) = fromList [(1,0), (2,0), (3,0)]
@@ -95,16 +95,16 @@ longer, the extra elements are dropped.
95zip : Array a -> Array b -> Array (a,b)
95zip : Array a -> Array b -> Array (a,b)
96zip = zipWith (,)
96zip = zipWith (,)
97
97
98−{-| Zips the elements of two arrays via a function into a new array. If one array
98+{-| Zips the elements of two arrays via a function into a new array. If one array
99is longer, the extra elements are dropped.
99is longer, the extra elements are dropped.
100
100
101 zipWith (+) (fill 3 5) (fill 5 8) == fill 3 13
101 zipWith (+) (fill 3 5) (fill 5 8) == fill 3 13
102-}
102-}
103zipWith : (a -> b -> c) -> Array a -> Array b -> Array c
103zipWith : (a -> b -> c) -> Array a -> Array b -> Array c
104−zipWith f a b =
104+zipWith f a b =
105 fromList <| List.zipWith f (Native.Array.elements a) (Native.Array.elements b)
105 fromList <| List.zipWith f (Native.Array.elements a) (Native.Array.elements b)
106
106
107−{-| Return an empty array.
107+{-| Return an empty array.
108
108
109 length empty == 0
109 length empty == 0
110-}
110-}
@@ -118,8 +118,8 @@ empty = Native.Array.empty
118push : a -> Array a -> Array a
118push : a -> Array a -> Array a
119push = Native.Array.push
119push = Native.Array.push
120
120
121−{-| Return the value at the index. Breaks, if index is out of range. If the
121+{-| Return the value at the index. Breaks, if index is out of range. If the
122−array length is unkown, use safeGet oder getWithDefault.
122+array length is unkown, use safeGet oder getWithDefault.
123
123
124 get 2 (A.fromList [3,2,1]) == 1
124 get 2 (A.fromList [3,2,1]) == 1
125-}
125-}
@@ -136,7 +136,7 @@ safeGet i array = if Native.Array.length array > i
136 then Just (Native.Array.get i array)
136 then Just (Native.Array.get i array)
137 else Nothing
137 else Nothing
138
138
139−{-| Get the element at the index. If the index is out of range, the given default
139+{-| Get the element at the index. If the index is out of range, the given default
140elementis returned.
140elementis returned.
141
141
142 getWithDefault 0 2 (A.fromList [3,2,1]) == 1
142 getWithDefault 0 2 (A.fromList [3,2,1]) == 1
@@ -146,7 +146,7 @@ getWithDefault : a -> Int -> Array a -> a
146getWithDefault default i array = if Native.Array.length array > i
146getWithDefault default i array = if Native.Array.length array > i
147 then Native.Array.get i array else default
147 then Native.Array.get i array else default
148
148
149−{-| Set the element at the index. Returns the updated array, or if the index is
149+{-| Set the element at the index. Returns the updated array, or if the index is
150out of range, the unaltered array.
150out of range, the unaltered array.
151
151
152 set 1 7 (fromList [1,2,3]) == fromList [1,7,3]
152 set 1 7 (fromList [1,2,3]) == fromList [1,7,3]
@@ -162,7 +162,7 @@ updates : [(Int, a)] -> Array a -> Array a
162updates assocs array = List.foldl (uncurry Native.Array.set) array assocs
162updates assocs array = List.foldl (uncurry Native.Array.set) array assocs
163
163
164{-| Slice an array given a range. The selection is inclusive, so the last
164{-| Slice an array given a range. The selection is inclusive, so the last
165−element in the selection will also be in the new array. This may change in the
165+element in the selection will also be in the new array. This may change in the
166future.
166future.
167You can select from the end by giving a negative Int.
167You can select from the end by giving a negative Int.
168
168
@@ -180,7 +180,7 @@ slice = Native.Array.slice
180length : Array a -> Int
180length : Array a -> Int
181length = Native.Array.length
181length = Native.Array.length
182
182
183−{-| Concat two arrays to a new one.
183+{-| Concat two arrays to a new one.
184
184
185 concat (fill 3 1) (fill 4 2) == fromList [1,1,1,2,2,2,2]
185 concat (fill 3 1) (fill 4 2) == fromList [1,1,1,2,2,2,2]
186-}
186-}
libraries/Native/Array.js+17−19
@@ -16,9 +16,9 @@ Elm.Native.Array.make = function(elm) {
16 // M is the maximal node size. 32 seems fast. E is the allowed increase
16 // M is the maximal node size. 32 seems fast. E is the allowed increase
17 // of search steps when concatting to find an index. Lower values will
17 // of search steps when concatting to find an index. Lower values will
18 // decrease balancing, but will increase search steps.
18 // decrease balancing, but will increase search steps.
19− var M = 32;
19+ var M = 32;
20 var E = 2;
20 var E = 2;
21−
21+
22 // An empty array.
22 // An empty array.
23 var empty = { ctor:"_Array", _0:0, _1:new Array() };
23 var empty = { ctor:"_Array", _0:0, _1:new Array() };
24
24
@@ -31,12 +31,12 @@ Elm.Native.Array.make = function(elm) {
31 throw new Error("Index "+ i +" on the array is out of range. Check the length first or you safeGet.");
31 throw new Error("Index "+ i +" on the array is out of range. Check the length first or you safeGet.");
32 }
32 }
33 }
33 }
34−
34+
35 var slot = getSlot(i, a);
35 var slot = getSlot(i, a);
36 var sub = slot > 0 ? a._2[slot-1] : 0;
36 var sub = slot > 0 ? a._2[slot-1] : 0;
37 return get(i - sub, a._1[slot]);
37 return get(i - sub, a._1[slot]);
38− }
38+ }
39−
39+
40 // Sets the value at the index i. Only the nodes leading to i will get
40 // Sets the value at the index i. Only the nodes leading to i will get
41 // copied and updated.
41 // copied and updated.
42 function set(i, item, a) {
42 function set(i, item, a) {
@@ -81,7 +81,7 @@ Elm.Native.Array.make = function(elm) {
81 return null;
81 return null;
82 }
82 }
83 }
83 }
84−
84+
85 // Recursively push
85 // Recursively push
86 var pushed = push_(item, botRight(a));
86 var pushed = push_(item, botRight(a));
87
87
@@ -124,7 +124,7 @@ Elm.Native.Array.make = function(elm) {
124 function map(f, a) {
124 function map(f, a) {
125 var newA = { ctor:"_Array", _0:a._0, _1:new Array(a._1) };
125 var newA = { ctor:"_Array", _0:a._0, _1:new Array(a._1) };
126 if (a._0 > 0) { newA._2 = a._2; }
126 if (a._0 > 0) { newA._2 = a._2; }
127− for (var i = 0; i < a._1.length; i++) {
127+ for (var i = 0; i < a._1.length; i++) {
128 newA._1[i] = a._0 == 0 ? f(a._1[i]) : map(f, a._1[i]);
128 newA._1[i] = a._0 == 0 ? f(a._1[i]) : map(f, a._1[i]);
129 }
129 }
130 return newA;
130 return newA;
@@ -156,7 +156,7 @@ Elm.Native.Array.make = function(elm) {
156 // Returns a sliced tree. The to is inclusive, but this may change,
156 // Returns a sliced tree. The to is inclusive, but this may change,
157 // when I understand, why e.g. JS does not handle it this way. :-)
157 // when I understand, why e.g. JS does not handle it this way. :-)
158 // If from or to is negative, they will select from the end on.
158 // If from or to is negative, they will select from the end on.
159− // TODO: currently, it slices the right, then the left. This can be
159+ // TODO: currently, it slices the right, then the left. This can be
160 // optimized.
160 // optimized.
161 function slice(from, to, a) {
161 function slice(from, to, a) {
162 if (from < 0) { from += length(a); }
162 if (from < 0) { from += length(a); }
@@ -184,7 +184,7 @@ Elm.Native.Array.make = function(elm) {
184 if (right == 0) {
184 if (right == 0) {
185 return sliced;
185 return sliced;
186 }
186 }
187−
187+
188 // Create new node.
188 // Create new node.
189 var newA = { ctor:"_Array", _0:a._0
189 var newA = { ctor:"_Array", _0:a._0
190 , _1:a._1.slice(0, right + 1)
190 , _1:a._1.slice(0, right + 1)
@@ -219,7 +219,7 @@ Elm.Native.Array.make = function(elm) {
219 var newA = { ctor:"_Array", _0:a._0
219 var newA = { ctor:"_Array", _0:a._0
220 , _1:a._1.slice(left, a._1.length + 1)
220 , _1:a._1.slice(left, a._1.length + 1)
221 , _2:new Array(a._1.length - left) };
221 , _2:new Array(a._1.length - left) };
222− newA._1[left] = sliced;
222+ newA._1[left] = sliced;
223 var len = 0;
223 var len = 0;
224 for (var i = 0; i < newA._1.length; i++) {
224 for (var i = 0; i < newA._1.length; i++) {
225 len += length(newA._1[i]);
225 len += length(newA._1[i]);
@@ -258,7 +258,7 @@ Elm.Native.Array.make = function(elm) {
258
258
259 return shuffle(a, b, toRemove);
259 return shuffle(a, b, toRemove);
260 }
260 }
261−
261+
262 var concated = concat_(botRight(a), botLeft(b));
262 var concated = concat_(botRight(a), botLeft(b));
263 a = nodeCopy(a), b = nodeCopy(b);
263 a = nodeCopy(a), b = nodeCopy(b);
264
264
@@ -302,9 +302,9 @@ Elm.Native.Array.make = function(elm) {
302 subLengths += b._1[i]._1.length;
302 subLengths += b._1[i]._1.length;
303 }
303 }
304
304
305− var toRemove = a._1.length + b._1.length
305+ var toRemove = a._1.length + b._1.length
306 return toRemove - (Math.floor((subLengths - 1) / M) + 1);
306 return toRemove - (Math.floor((subLengths - 1) / M) + 1);
307− }
307+ }
308
308
309 // get2 and set2 are helpers for accessing over two arrays.
309 // get2 and set2 are helpers for accessing over two arrays.
310 function get2(a, b, index) {
310 function get2(a, b, index) {
@@ -333,7 +333,7 @@ Elm.Native.Array.make = function(elm) {
333 function saveSlot(a, b, index, slot) {
333 function saveSlot(a, b, index, slot) {
334 set2(a._1, b._1, index, slot);
334 set2(a._1, b._1, index, slot);
335
335
336− var l = (index == 0 || index == a._2.length) ?
336+ var l = (index == 0 || index == a._2.length) ?
337 0 : get2(a._2, a._2, index - 1);
337 0 : get2(a._2, a._2, index - 1);
338 set2(a._2, b._2, index, l + length(slot));
338 set2(a._2, b._2, index, l + length(slot));
339 }
339 }
@@ -384,12 +384,12 @@ Elm.Native.Array.make = function(elm) {
384 }
384 }
385
385
386 // Only create a new slot if the current one is filled up.
386 // Only create a new slot if the current one is filled up.
387− if (slot._1.length == M) {
387+ if (slot._1.length == M) {
388 saveSlot(newA, newB, write, slot);
388 saveSlot(newA, newB, write, slot);
389 slot = createNode(a._0 - 1,0);
389 slot = createNode(a._0 - 1,0);
390 write++;
390 write++;
391 }
391 }
392− }
392+ }
393
393
394 // Cleanup after the loop. Copy the last slot into the new nodes.
394 // Cleanup after the loop. Copy the last slot into the new nodes.
395 if (slot._1.length > 0) {
395 if (slot._1.length > 0) {
@@ -398,7 +398,7 @@ Elm.Native.Array.make = function(elm) {
398 }
398 }
399
399
400 // Shift the untouched slots to the left
400 // Shift the untouched slots to the left
401− while (read < a._1.length + b._1.length ) {
401+ while (read < a._1.length + b._1.length ) {
402 saveSlot(newA, newB, write, get2(a._1, b._1, read));
402 saveSlot(newA, newB, write, get2(a._1, b._1, read));
403 read++; write++;
403 read++; write++;
404 }
404 }
@@ -486,5 +486,3 @@ Elm.Native.Array.make = function(elm) {
486
486
487 return elm.Native.Array.values = Elm.Native.Array.values;
487 return elm.Native.Array.values = Elm.Native.Array.values;
488}
488}
489−
490−