@@ -1,711 +0,0 @@
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);
38− while (array.lengths[slot] <= i) {
42− i -= array.lengths[slot - 1];
44− array = array.table[slot];
46− return array.table[i];
49− // Sets the value at the index i. Only the nodes leading to i will get
50− // copied and updated.
51− function set(i, item, array) {
52− if (i < 0 || length(array) <= i) {
55− return unsafeSet(i, item, array);
58− function unsafeSet(i, item, array) {
59− array = nodeCopy(array);
61− if (array.height == 0) {
62− array.table[i] = item;
64− var slot = getSlot(i, array);
66− i -= array.lengths[slot - 1];
68− array.table[slot] = unsafeSet(i, item, array.table[slot]);
73− function initialize(len, f) {
74− if (len == 0) { return empty; }
75− var h = Math.floor(Math.log(len)/Math.log(M));
76− return initialize_(f, h, 0, len);
79− function initialize_(f, h, from, to) {
81− var table = new Array((to - from) % (M + 1));
82− for (var i = 0; i < table.length; i++) {
83− table[i] = f(from + i);
85− return { ctor:"_Array", height:0, table:table };
88− var step = Math.pow(M, h);
89− var table = new Array(Math.ceil((to - from) / step));
90− var lengths = new Array(table.length);
91− for (var i = 0; i < table.length; i++) {
92− table[i] = initialize_( f, h - 1, from + (i * step)
93− , Math.min(from + ((i + 1) * step), to));
94− lengths[i] = length(table[i]) + (i > 0 ? lengths[i-1] : 0);
96− return { ctor:"_Array", height:h, table:table, lengths:lengths };
99− function fromList(list) {
100− if (list == List.Nil) { return empty; }
102− // Allocate M sized blocks (table) and write list elements to it.
103− var table = new Array(M);
104− var nodes = new Array();
107− while (list.ctor !== '[]') {
112− // table is full, so we can push a leaf containing it into the
115− fromListPush({ ctor:"_Array", height:0, table:table }
117− table = new Array(M);
122− // Maybe there is something left on the table.
124− fromListPush({ ctor:"_Array", height:0, table:table.splice(0,i) }
128− // Go through all of the nodes and eventually push them into higher nodes.
129− for (var h = 0; h < nodes.length - 1; h++) {
130− if (nodes[h].table.length > 0) {
131− fromListPush(nodes[h], nodes);
135− var head = nodes[nodes.length - 1];
136− if (head.height > 0 && head.table.length == 1) {
137− return head.table[0];
143− // Push a node into a higher node as a child.
144− function fromListPush(toPush, nodes) {
145− var h = toPush.height;
147− // Maybe the node on this height does not exist.
148− if (nodes.length == h) {
149− nodes.push({ ctor:"_Array", height:h + 1
151− , lengths:new Array() });
154− nodes[h].table.push(toPush);
155− var len = length(toPush);
156− if (nodes[h].lengths.length > 0) {
157− len += nodes[h].lengths[nodes[h].lengths.length - 1];
159− nodes[h].lengths.push(len);
161− if (nodes[h].table.length == M) {
162− fromListPush(nodes[h], nodes);
163− nodes[h] = { ctor:"_Array", height:h + 1
165− , lengths:new Array() };
169− // Pushes an item via push_ to the bottom right of a tree.
170− function push(item, a) {
171− var pushed = push_(item, a);
172− if (pushed !== null) {
176− var newTree = create(item, a.height);
177− return siblise(a, newTree);
180− // Recursively tries to push an item to the bottom-right most
181− // tree possible. If there is no space left for the item,
182− // null will be returned.
183− function push_(item, a) {
184− // Handle resursion stop at leaf level.
185− if (a.height == 0) {
186− if (a.table.length < M) {
187− var newA = { ctor:"_Array", height:0, table:a.table.slice() };
188− newA.table.push(item);
196− var pushed = push_(item, botRight(a));
198− // There was space in the bottom right tree, so the slot will
200− if (pushed != null) {
201− var newA = nodeCopy(a);
202− newA.table[newA.table.length - 1] = pushed;
203− newA.lengths[newA.lengths.length - 1]++;
207− // When there was no space left, check if there is space left
208− // for a new slot with a tree which contains only the item
210− if (a.table.length < M) {
211− var newSlot = create(item, a.height - 1);
212− var newA = nodeCopy(a);
213− newA.table.push(newSlot);
214− newA.lengths.push(newA.lengths[newA.lengths.length - 1] + length(newSlot));
221− // Converts an array into a list of elements.
222− function toList(a) {
223− return toList_(List.Nil, a);
226− function toList_(list, a) {
227− for (var i = a.table.length - 1; i >= 0; i--) {
228− list = a.height == 0 ? List.Cons(a.table[i], list) : toList_(list, a.table[i]);
233− // Maps a function over the elements of an array.
234− function map(f, a) {
235− var newA = { ctor:"_Array", height:a.height, table:new Array(a.table) };
236− if (a.height > 0) { newA.lengths = a.lengths; }
237− for (var i = 0; i < a.table.length; i++) {
238− newA.table[i] = a.height == 0 ? f(a.table[i]) : map(f, a.table[i]);
243− // Maps a function over the elements with their index as first argument.
244− function indexedMap(f, a) {
245− return indexedMap_(f, a, 0);
248− function indexedMap_(f, a, from) {
249− var newA = { ctor:"_Array", height:a.height, table:new Array(a.table) };
250− if (a.height > 0) { newA.lengths = a.lengths; }
251− for (var i = 0; i < a.table.length; i++) {
252− newA.table[i] = a.height == 0 ? A2(f, from + i, a.table[i])
253− : indexedMap_( f, a.table[i]
254− , i == 0 ? 0 : a.lengths[i - 1]);
259− function foldl(f, b, a) {
260− if (a.height == 0) {
261− for (var i = 0; i < a.table.length; i++) {
262− b = A2(f, a.table[i], b);
265− for (var i = 0; i < a.table.length; i++) {
266− b = foldl(f, b, a.table[i]);
272− function foldr(f, b, a) {
273− if (a.height == 0) {
274− for (var i = a.table.length; i--; ) {
275− b = A2(f, a.table[i], b);
278− for (var i = a.table.length; i--; ) {
279− b = foldl(f, b, a.table[i]);
285− // TODO: currently, it slices the right, then the left. This can be
287− function slice(from, to, a) {
288− if (from < 0) { from += length(a); }
289− if (to < 0) { to += length(a); }
290− return sliceLeft(from, sliceRight(to, a));
293− function sliceRight(to, a) {
294− if (to == length(a)) {
298− // Handle leaf level.
299− if (a.height == 0) {
300− var newA = { ctor:"_Array", height:0 };
301− newA.table = a.table.slice(0, to);
305− // Slice the right recursively.
306− var right = getSlot(to, a);
307− var sliced = sliceRight(to - (right > 0 ? a.lengths[right - 1] : 0), a.table[right]);
309− // Maybe the a node is not even needed, as sliced contains the whole slice.
315− var newA = { ctor:"_Array", height:a.height
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);
325− function sliceLeft(from, a) {
330− // Handle leaf level.
331− if (a.height == 0) {
332− var newA = { ctor:"_Array", height:0 };
333− newA.table = a.table.slice(from, a.table.length + 1);
337− // Slice the left recursively.
338− var left = getSlot(from, a);
339− var sliced = sliceLeft(from - (left > 0 ? a.lengths[left - 1] : 0), a.table[left]);
341− // Maybe the a node is not even needed, as sliced contains the whole slice.
342− if (left == a.table.length - 1) {
347− var newA = { ctor:"_Array", height:a.height
348− , table:a.table.slice(left, a.table.length + 1)
349− , lengths:new Array(a.table.length - left) };
350− newA.table[0] = sliced;
352− for (var i = 0; i < newA.table.length; i++) {
353− len += length(newA.table[i]);
354− newA.lengths[i] = len;
360− // Appends two trees.
361− function append(a,b) {
362− if (a.table.length === 0) {
365− if (b.table.length === 0) {
369− var c = append_(a, b);
371− // Check if both nodes can be crunshed together.
372− if (c[0].table.length + c[1].table.length <= M) {
373− if (c[0].table.length === 0) {
376− if (c[1].table.length === 0) {
380− // Adjust .table and .lengths
381− c[0].table = c[0].table.concat(c[1].table);
382− if (c[0].height > 0) {
383− var len = length(c[0]);
384− for (var i = 0; i < c[1].lengths.length; i++) {
385− c[1].lengths[i] += len;
387− c[0].lengths = c[0].lengths.concat(c[1].lengths);
393− if (c[0].height > 0) {
394− var toRemove = calcToRemove(a, b);
396− c = shuffle(c[0], c[1], toRemove);
400− return siblise(c[0], c[1]);
403− // Returns an array of two nodes; right and left. One node _may_ be empty.
404− function append_(a, b) {
405− if (a.height === 0 && b.height === 0) {
409− if (a.height !== 1 || b.height !== 1) {
410− if (a.height === b.height) {
413− var appended = append_(botRight(a), botLeft(b));
415− insertRight(a, appended[1]);
416− insertLeft(b, appended[0]);
417− } else if (a.height > b.height) {
419− var appended = append_(botRight(a), b);
421− insertRight(a, appended[0]);
422− b = parentise(appended[1], appended[1].height + 1);
425− var appended = append_(a, botLeft(b));
427− var left = appended[0].table.length === 0 ? 0 : 1;
428− var right = left === 0 ? 1 : 0;
429− insertLeft(b, appended[left]);
430− a = parentise(appended[right], appended[right].height + 1);
434− // Check if balancing is needed and return based on that.
435− if (a.table.length === 0 || b.table.length === 0) {
439− var toRemove = calcToRemove(a, b);
440− if (toRemove <= E) {
443− return shuffle(a, b, toRemove);
446− // Helperfunctions for append_. Replaces a child node at the side of the parent.
447− function insertRight(parent, node) {
448− var index = parent.table.length - 1;
449− parent.table[index] = node;
450− parent.lengths[index] = length(node)
451− parent.lengths[index] += index > 0 ? parent.lengths[index - 1] : 0;
454− function insertLeft(parent, node) {
455− if (node.table.length > 0) {
456− parent.table[0] = node;
457− parent.lengths[0] = length(node);
459− var len = length(parent.table[0]);
460− for (var i = 1; i < parent.lengths.length; i++) {
461− len += length(parent.table[i]);
462− parent.lengths[i] = len;
465− parent.table.shift();
466− for (var i = 1; i < parent.lengths.length; i++) {
467− parent.lengths[i] = parent.lengths[i] - parent.lengths[0];
469− parent.lengths.shift();
473− // Returns the extra search steps for E. Refer to the paper.
474− function calcToRemove(a, b) {
476− for (var i = 0; i < a.table.length; i++) {
477− subLengths += a.table[i].table.length;
479− for (var i = 0; i < b.table.length; i++) {
480− subLengths += b.table[i].table.length;
483− var toRemove = a.table.length + b.table.length
484− return toRemove - (Math.floor((subLengths - 1) / M) + 1);
487− // get2, set2 and saveSlot are helpers for accessing elements over two arrays.
488− function get2(a, b, index) {
489− return index < a.length ? a[index] : b[index - a.length];
492− function set2(a, b, index, value) {
493− if (index < a.length) {
496− b[index - a.length] = value;
500− function saveSlot(a, b, index, slot) {
501− set2(a.table, b.table, index, slot);
503− var l = (index == 0 || index == a.lengths.length) ?
504− 0 : get2(a.lengths, a.lengths, index - 1);
505− set2(a.lengths, b.lengths, index, l + length(slot));
508− // Creates a node or leaf with a given length at their arrays for perfomance.
509− // Is only used by shuffle.
510− function createNode(h, length) {
511− if (length < 0) { length = 0; }
512− var a = { ctor:"_Array", height:h, table:new Array(length) };
514− a.lengths = new Array(length);
519− // Returns an array of two balanced nodes.
520− function shuffle(a, b, toRemove) {
521− var newA = createNode(a.height, Math.min(M, a.table.length + b.table.length - toRemove));
522− var newB = createNode(a.height, newA.table.length - (a.table.length + b.table.length - toRemove));
524− // Skip the slots with size M. More precise: copy the slot references
527− while (get2(a.table, b.table, read).table.length % M == 0) {
528− set2(newA.table, newB.table, read, get2(a.table, b.table, read));
529− set2(newA.lengths, newB.lengths, read, get2(a.lengths, b.lengths, read));
533− // Pulling items from left to right, caching in a slot before writing
534− // it into the new nodes.
536− var slot = new createNode(a.height - 1, 0);
539− // If the current slot is still containing data, then there will be at
540− // least one more write, so we do not break this loop yet.
541− while (read - write - (slot.table.length > 0 ? 1 : 0) < toRemove) {
542− // Find out the max possible items for copying.
543− var source = get2(a.table, b.table, read);
544− var to = Math.min(M - slot.table.length, source.table.length)
546− // Copy and adjust size table.
547− slot.table = slot.table.concat(source.table.slice(from, to));
548− if (slot.height > 0) {
549− var len = slot.lengths.length;
550− for (var i = len; i < len + to - from; i++) {
551− slot.lengths[i] = length(slot.table[i]);
552− slot.lengths[i] += (i > 0 ? slot.lengths[i - 1] : 0);
558− // Only proceed to next slots[i] if the current one was
560− if (source.table.length <= to) {
564− // Only create a new slot if the current one is filled up.
565− if (slot.table.length == M) {
566− saveSlot(newA, newB, write, slot);
567− slot = createNode(a.height - 1,0);
572− // Cleanup after the loop. Copy the last slot into the new nodes.
573− if (slot.table.length > 0) {
574− saveSlot(newA, newB, write, slot);
578− // Shift the untouched slots to the left
579− while (read < a.table.length + b.table.length ) {
580− saveSlot(newA, newB, write, get2(a.table, b.table, read));
584− return [newA, newB];
587− // Navigation functions
588− function botRight(a) { return a.table[a.table.length - 1]; }
589− function botLeft(a) { return a.table[0]; }
591− // Copies a node for updating. Note that you should not use this if
592− // only updating only one of "table" or "lengths" for performance reasons.
593− function nodeCopy(a) {
594− var newA = { ctor:"_Array", height:a.height
595− , table:a.table.slice() };
596− if (a.height > 0) { newA.lengths = a.lengths.slice(); }
600− // Returns how many items are in the tree.
601− function length(array) {
602− if (array.height == 0) {
603− return array.table.length;
605− return array.lengths[array.lengths.length - 1];
609− // Calculates in which slot of "table" the item probably is, then
610− // find the exact slot via forward searching in "lengths". Returns the index.
611− function getSlot(i, a) {
612− var slot = i >> (5 * a.height);
613− while (a.lengths[slot] <= i) {
619− // Recursively creates a tree with a given height containing
620− // only the given item.
621− function create(item, h) {
623− return { ctor:"_Array", height:0
626− return { ctor:"_Array", height:h
627− , table:[create(item, h - 1)]
632− // Recursively creates a tree that contains the given tree.
633− function parentise(tree, h) {
634− if (h == tree.height) {
637− return { ctor:"_Array", height:h
638− , table:[parentise(tree, h - 1)]
639− , lengths:[length(tree)] };
643− // Emphasizes blood brotherhood beneath two trees.
644− function siblise(a, b) {
645− return { ctor:"_Array", height:a.height + 1
647− , lengths:[length(a), length(a) + length(b)] };
650− function toJSArray(a) {
651− var jsArray = new Array(length(a));
652− toJSArray_(jsArray, 0, a);
656− function toJSArray_(jsArray, i, a) {
657− for (var t = 0; t < a.table.length; t++) {
658− if (a.height == 0) {
659− jsArray[i + t] = a.table[t];
661− var inc = t == 0 ? 0 : a.lengths[t - 1];
662− toJSArray_(jsArray, i + inc, a.table[t]);
667− function fromJSArray(jsArray) {
668− if (jsArray.length == 0) { return empty; }
669− var h = Math.floor(Math.log(jsArray.length) / Math.log(M));
670− return fromJSArray_(jsArray, h, 0, jsArray.length);
673− function fromJSArray_(jsArray, h, from, to) {
675− return { ctor:"_Array", height:0
676− , table:jsArray.slice(from, to) };
679− var step = Math.pow(M, h);
680− var table = new Array(Math.ceil((to - from) / step));
681− var lengths = new Array(table.length);
682− for (var i = 0; i < table.length; i++) {
683− table[i] = fromJSArray_( jsArray, h - 1, from + (i * step)
684− , Math.min(from + ((i + 1) * step), to));
685− lengths[i] = length(table[i]) + (i > 0 ? lengths[i-1] : 0);
687− return { ctor:"_Array", height:h, table:table, lengths:lengths };
690− Elm.Native.Array.values = {
694− initialize:F2(initialize),
701− indexedMap:F2(indexedMap),
706− toJSArray:toJSArray,
707− fromJSArray:fromJSArray
710− return elm.Native.Array.values = Elm.Native.Array.values;