@@ -0,0 +1,597 @@
1+Elm.Native.Array = {};
2+Elm.Native.Array.make = function(elm) {
3+ elm.Native = elm.Native || {};
4+ elm.Native.Array = elm.Native.Array || {};
5+ if (elm.Native.Array.values) return elm.Native.Array.values;
6+ if ('values' in Elm.Native.Array)
7+ return elm.Native.Array.values = Elm.Native.Array.values;
9+ var List = Elm.Native.List.make(elm);
11+ // A RRB-Tree has two distinct data types.
12+ // Leaf -> "height" is always 0
13+ // "table" is an array of elements
14+ // Node -> "height" is always greater than 0
15+ // "table" is an array of child nodes
16+ // "lengths" is an array of accumulated lengths of the child nodes
18+ // M is the maximal table size. 32 seems fast. E is the allowed increase
19+ // of search steps when concatting to find an index. Lower values will
20+ // decrease balancing, but will increase search steps.
25+ var empty = { ctor:"_Array", height:0, table:new Array() };
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.");
32+ return unsafeGet(i, array);
35+ function unsafeGet(i, array) {
36+ for (var x = array.height; x > 0; x--) {
37+ var slot = i >> (x * 5);
39+ while (array.lengths[slot - 1] > i) { slot--; }
40+ i -= array.lengths[slot - 1];
42+ array = array.table[slot];
44+ return array.table[i];
47+ // Sets the value at the index i. Only the nodes leading to i will get
48+ // copied and updated.
49+ function set(i, item, array) {
50+ if (i < 0 || length(array) <= i) {
53+ return unsafeSet(i, item, array);
56+ function unsafeSet(i, item, array) {
57+ array = nodeCopy(array);
59+ if (array.height == 0) {
60+ array.table[i] = item;
62+ var slot = getSlot(i, array);
64+ i -= array.lengths[slot - 1];
66+ array.table[slot] = unsafeSet(i, item, array.table[slot]);
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);
77+ function initialize_(f, h, from, to) {
79+ var table = new Array((to - from) % (M + 1));
80+ for (var i = 0; i < table.length; i++) {
81+ table[i] = f(from + i);
83+ return { ctor:"_Array", height:0, table:table };
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);
94+ return { ctor:"_Array", height:h, table:table, lengths:lengths };
97+ function fromList(list) {
98+ if (list == List.Nil) { return empty; }
100+ // Allocate M sized blocks (table) and write list elements to it.
101+ var table = new Array(M);
102+ var nodes = new Array();
105+ while (list.ctor !== '[]') {
110+ // table is full, so we can push a leaf containing it into the
113+ fromListPush({ ctor:"_Array", height:0, table:table }
115+ table = new Array(M);
120+ // Maybe there is something left on the table.
122+ fromListPush({ ctor:"_Array", height:0, table:table.splice(0,i) }
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);
133+ var head = nodes[nodes.length - 1];
134+ if (head.height > 0 && head.table.length == 1) {
135+ return head.table[0];
141+ // Push a node into a higher node as a child.
142+ function fromListPush(toPush, nodes) {
143+ var h = toPush.height;
145+ // Maybe the node on this height does not exist.
146+ if (nodes.length == h) {
147+ nodes.push({ ctor:"_Array", height:h + 1
149+ , lengths:new Array() });
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];
157+ nodes[h].lengths.push(len);
159+ if (nodes[h].table.length == M) {
160+ fromListPush(nodes[h], nodes);
161+ nodes[h] = { ctor:"_Array", height:h + 1
163+ , lengths:new Array() };
167+ // Pushes an item via push_ to the bottom right of a tree.
168+ function push(item, a) {
169+ var pushed = push_(item, a);
170+ if (pushed !== null) {
174+ var newTree = create(item, a.height);
175+ return siblise(a, newTree);
178+ // Recursively tries to push an item to the bottom-right most
179+ // tree possible. If there is no space left for the item,
180+ // null will be returned.
181+ function push_(item, a) {
182+ // Handle resursion stop at leaf level.
183+ if (a.height == 0) {
184+ if (a.table.length < M) {
185+ var newA = { ctor:"_Array", height:0, table:a.table.slice() };
186+ newA.table.push(item);
194+ var pushed = push_(item, botRight(a));
196+ // There was space in the bottom right tree, so the slot will
198+ if (pushed != null) {
199+ var newA = nodeCopy(a);
200+ newA.table[newA.table.length - 1] = pushed;
201+ newA.lengths[newA.lengths.length - 1]++;
205+ // When there was no space left, check if there is space left
206+ // for a new slot with a tree which contains only the item
208+ if (a.table.length < M) {
209+ var newSlot = create(item, a.height - 1);
210+ var newA = nodeCopy(a);
211+ newA.table.push(newSlot);
212+ newA.lengths.push(newA.lengths[newA.lengths.length - 1] + length(newSlot));
219+ // Converts an array into a list of elements.
220+ function toList(a) {
221+ return toList_(List.Nil, a);
224+ function toList_(list, a) {
225+ for (var i = a.table.length - 1; i >= 0; i--) {
226+ list = a.height == 0 ? List.Cons(a.table[i], list) : toList_(list, a.table[i]);
231+ // Maps a function over the elements of an array.
232+ function map(f, a) {
233+ var newA = { ctor:"_Array", height:a.height, table:new Array(a.table) };
234+ if (a.height > 0) { newA.lengths = a.lengths; }
235+ for (var i = 0; i < a.table.length; i++) {
236+ newA.table[i] = a.height == 0 ? f(a.table[i]) : map(f, a.table[i]);
241+ // Maps a function over the elements with their index as first argument.
242+ function indexedMap(f, a) {
243+ var newA = { ctor:"_Array", height:a.height, table:new Array(a.table) };
244+ if (a.height > 0) { newA.lengths = a.lengths; }
245+ for (var i = 0; i < a.table.length; i++) {
246+ newA.table[i] = a.height == 0 ? A2(f, i, a.table[i]) : indexedMap(f, a.table[i]);
251+ function foldl(f, b, a) {
252+ for (var i = 0; i < a.table.length; i++) {
253+ b = A2(f, a.height == 0 ? a.table[i] : foldl(f, b, a.table[i]), b);
258+ function foldr(f, b, a) {
259+ for (var i = a.table.length; i--; ) {
260+ b = A2(f, a.height == 0 ? a.table[i] : foldr(f, b, a.table[i]), b);
265+ // Returns a sliced tree. "to" is inclusive, but this may change,
266+ // when I understand, why e.g. JS does not handle it this way. :-)
267+ // If "from" or "to" is negative, they will select from the end on.
268+ // TODO: currently, it slices the right, then the left. This can be
270+ function slice(from, to, a) {
271+ if (from < 0) { from += length(a); }
272+ if (to < 0) { to += length(a); }
273+ return sliceLeft(from, sliceRight(to, a));
276+ function sliceRight(to, a) {
277+ if (to == length(a)) {
281+ // Handle leaf level.
282+ if (a.height == 0) {
283+ var newA = { ctor:"_Array", height:0 };
284+ newA.table = a.table.slice(0, to + 1);
288+ // Slice the right recursively.
289+ var right = getSlot(to, a);
290+ var sliced = sliceRight(to - (right > 0 ? a.lengths[right - 1] : 0), a.table[right]);
292+ // Maybe the a node is not even needed, as sliced contains the whole slice.
298+ var newA = { ctor:"_Array", height:a.height
299+ , table:a.table.slice(0, right + 1)
300+ , lengths:a.lengths.slice(0, right + 1) };
301+ newA.table[right] = sliced;
302+ newA.lengths[right] = length(sliced) + (right > 0 ? newA.lengths[right - 1] : 0);
306+ function sliceLeft(from, a) {
311+ // Handle leaf level.
312+ if (a.height == 0) {
313+ var newA = { ctor:"_Array", height:0 };
314+ newA.table = a.table.slice(from, a.table.length + 1);
318+ // Slice the left recursively.
319+ var left = getSlot(from, a);
320+ var sliced = sliceLeft(from - (left > 0 ? a.lengths[left - 1] : 0), a.table[left]);
322+ // Maybe the a node is not even needed, as sliced contains the whole slice.
323+ if (left == a.table.length - 1) {
328+ var newA = { ctor:"_Array", height:a.height
329+ , table:a.table.slice(left, a.table.length + 1)
330+ , lengths:new Array(a.table.length - left) };
331+ newA.table[left] = sliced;
333+ for (var i = 0; i < newA.table.length; i++) {
334+ len += length(newA.table[i]);
335+ newA.lengths[i] = len;
341+ // Appends two trees.
342+ // TODO: Add support for appending trees of different sizes. Current
343+ // behavior will just rise the lower tree and then append them.
344+ function append(a,b) {
345+ if (b.height > a.height) { return append(parentise(a, b.height), b); }
346+ if (a.height > b.height) { return append(a, parentise(b, a.height)); }
347+ if (a.height == 0) { return append(parentise(a, 1), parentise(b, 1)); }
349+ var c = append_(a, b);
350+ if (c[1].table.length > 0) {
351+ return siblise(c[0], c[1]);
357+ // Returns an array of two nodes. The second node _may_ be empty. This case
358+ // needs to be handled by the function, that called append_. May be only
359+ // called for trees with an minimal height of 1.
360+ function append_(a, b) {
361+ if (a.height == 1) {
362+ // Check if balancing is needed and return based on that.
363+ var toRemove = calcToRemove(a, b);
364+ if (toRemove <= E) {
368+ return shuffle(a, b, toRemove);
371+ var appended = append_(botRight(a), botLeft(b));
372+ a = nodeCopy(a), b = nodeCopy(b);
374+ // Adjust the bottom right side of the new tree.
375+ a.table[a.table.length - 1] = appended[0];
376+ a.lengths[a.lengths.length - 1] = length(appended[0])
377+ a.lengths[a.lengths.length - 1] += a.lengths.length > 1 ? a.lengths[a.lengths.length - 2] : 0;
379+ // Adjust the bottom left side of the new tree.
380+ if (appended[1].table.length > 0) {
381+ b.table[0] = appended[1];
382+ b.lengths[0] = length(appended[1]);
383+ for (var i = 1, len = length(b.table[0]); i < b.lengths.length; i++) {
384+ len += length(b.table[i]);
389+ for (var i = 1; i < b.lengths.length; i++) {
390+ b.lengths[i] = b.lengths[i] - b.lengths[0];
395+ // Check if balancing is needed and return based on that.
396+ var toRemove = calcToRemove(a, b);
397+ if (toRemove <= E || b.table.length == 0) {
401+ return shuffle(a, b, toRemove);
404+ // Returns the extra search steps for E. Refer to the paper.
405+ function calcToRemove(a, b) {
407+ for (var i = 0; i < a.table.length; i++) {
408+ subLengths += a.table[i].table.length;
410+ for (var i = 0; i < b.table.length; i++) {
411+ subLengths += b.table[i].table.length;
414+ var toRemove = a.table.length + b.table.length
415+ return toRemove - (Math.floor((subLengths - 1) / M) + 1);
418+ // get2, set2 and saveSlot are helpers for accessing elements over two arrays.
419+ function get2(a, b, index) {
420+ return index < a.length ? a[index] : b[index - a.length];
423+ function set2(a, b, index, value) {
424+ if (index < a.length) {
427+ b[index - a.length] = value;
431+ function saveSlot(a, b, index, slot) {
432+ set2(a.table, b.table, index, slot);
434+ var l = (index == 0 || index == a.lengths.length) ?
435+ 0 : get2(a.lengths, a.lengths, index - 1);
436+ set2(a.lengths, b.lengths, index, l + length(slot));
439+ // Creates a node or leaf with a given length at their arrays for perfomance.
440+ // Is only used by shuffle.
441+ function createNode(h, length) {
442+ if (length < 0) { length = 0; }
443+ var a = { ctor:"_Array", height:h, table:new Array(length) };
445+ a.lengths = new Array(length);
450+ // Returns an array of two balanced nodes.
451+ function shuffle(a, b, toRemove) {
452+ var newA = createNode(a.height, Math.min(M, a.table.length + b.table.length - toRemove));
453+ var newB = createNode(a.height, newA.table.length - (a.table.length + b.table.length - toRemove));
455+ // Skip the slots with size M. More precise: copy the slot references
458+ while (get2(a.table, b.table, read).table.length % M == 0) {
459+ set2(newA.table, newB.table, read, get2(a.table, b.table, read));
460+ set2(newA.lengths, newB.lengths, read, get2(a.lengths, b.lengths, read));
464+ // Pulling items from left to right, caching in a slot before writing
465+ // it into the new nodes.
467+ var slot = new createNode(a.height - 1, 0);
470+ // If the current slot is still containing data, then there will be at
471+ // least one more write, so we do not break this loop yet.
472+ while (read - write - (slot.table.length > 0 ? 1 : 0) < toRemove) {
473+ // Find out the max possible items for copying.
474+ var source = get2(a.table, b.table, read);
475+ var to = Math.min(M - slot.table.length, source.table.length)
477+ // Copy and adjust size table.
478+ slot.table = slot.table.concat(source.table.slice(from, to));
479+ if (slot.height > 0) {
480+ var len = slot.lengths.length;
481+ for (var i = len; i < len + to - from; i++) {
482+ slot.lengths[i] = length(slot.table[i]);
483+ slot.lengths[i] += (i > 0 ? slot.lengths[i - 1] : 0);
489+ // Only proceed to next slots[i] if the current one was
491+ if (source.table.length <= to) {
495+ // Only create a new slot if the current one is filled up.
496+ if (slot.table.length == M) {
497+ saveSlot(newA, newB, write, slot);
498+ slot = createNode(a.height - 1,0);
503+ // Cleanup after the loop. Copy the last slot into the new nodes.
504+ if (slot.table.length > 0) {
505+ saveSlot(newA, newB, write, slot);
509+ // Shift the untouched slots to the left
510+ while (read < a.table.length + b.table.length ) {
511+ saveSlot(newA, newB, write, get2(a.table, b.table, read));
515+ return [newA, newB];
518+ // Navigation functions
519+ function botRight(a) { return a.table[a.table.length - 1]; }
520+ function botLeft(a) { return a.table[0]; }
522+ // Copies a node for updating. Note that you should not use this if
523+ // only updating only one of "table" or "lengths" for performance reasons.
524+ function nodeCopy(a) {
525+ var newA = { ctor:"_Array", height:a.height
526+ , table:a.table.slice() };
527+ if (a.height > 0) { newA.lengths = a.lengths.slice(); }
531+ // Returns how many items are in the tree.
532+ function length(array) {
533+ if (array.height == 0) {
534+ return array.table.length;
536+ return array.lengths[array.lengths.length - 1];
540+ // Calculates in which slot of "table" the item probably is, then
541+ // find the exact slot via forward searching in "lengths". Returns the index.
542+ function getSlot(i, a) {
543+ var slot = i >> (5 * a.height);
544+ while (a.lengths[slot - 1] > i) { slot--; }
548+ // Recursively creates a tree with a given height containing
549+ // only the given item.
550+ function create(item, h) {
552+ return { ctor:"_Array", height:0
555+ return { ctor:"_Array", height:h
556+ , table:[create(item, h - 1)]
561+ // Recursively creates a tree that contains the given tree.
562+ function parentise(tree, h) {
563+ if (h == tree.height) {
566+ return { ctor:"_Array", height:h
567+ , table:[parentise(tree, h - 1)]
568+ , lengths:[length(tree)] };
572+ // Emphasizes blood brotherhood beneath two trees.
573+ function siblise(a, b) {
574+ return { ctor:"_Array", height:a.height + 1
576+ , lengths:[length(a), length(a) + length(b)] };
579+ Elm.Native.Array.values = {
583+ initialize:F2(initialize),
590+ indexedMap:F2(indexedMap),
596+ return elm.Native.Array.values = Elm.Native.Array.values;