2Elm.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.
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.
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.
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) {
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) {
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.
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.
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 var toRemove = calcToRemove(a, b);
395 c = shuffle(c[0], c[1], toRemove);
398 return siblise(c[0], c[1]);
401 // Returns an array of two nodes; right and left. One node _may_ be empty.
402 function append_(a, b) {
403 if (a.height === 0 && b.height === 0) {
407 if (a.height !== 1 || b.height !== 1) {
408 if (a.height === b.height) {
411 var appended = append_(botRight(a), botLeft(b));
413 insertRight(a, appended[1]);
414 insertLeft(b, appended[0]);
415 } else if (a.height > b.height) {
417 var appended = append_(botRight(a), b);
419 insertRight(a, appended[0]);
420 b = parentise(appended[1], appended[1].height + 1);
423 var appended = append_(a, botLeft(b));
425 var left = appended[0].table.length === 0 ? 0 : 1;
426 var right = left === 0 ? 1 : 0;
427 insertLeft(b, appended[left]);
428 a = parentise(appended[right], appended[right].height + 1);
432 // Check if balancing is needed and return based on that.
433 if (a.table.length === 0 || b.table.length === 0) {
437 var toRemove = calcToRemove(a, b);
441 return shuffle(a, b, toRemove);
444 // Helperfunctions for append_. Replaces a child node at the side of the parent.
445 function insertRight(parent, node) {
446 var index = parent.table.length - 1;
447 parent.table[index] = node;
448 parent.lengths[index] = length(node)
449 parent.lengths[index] += index > 0 ? parent.lengths[index - 1] : 0;
452 function insertLeft(parent, node) {
453 if (node.table.length > 0) {
454 parent.table[0] = node;
455 parent.lengths[0] = length(node);
457 var len = length(parent.table[0]);
458 for (var i = 1; i < parent.lengths.length; i++) {
459 len += length(parent.table[i]);
460 parent.lengths[i] = len;
463 parent.table.shift();
464 for (var i = 1; i < parent.lengths.length; i++) {
465 parent.lengths[i] = parent.lengths[i] - parent.lengths[0];
467 parent.lengths.shift();
471 // Returns the extra search steps for E. Refer to the paper.
472 function calcToRemove(a, b) {
474 for (var i = 0; i < a.table.length; i++) {
475 subLengths += a.table[i].table.length;
477 for (var i = 0; i < b.table.length; i++) {
478 subLengths += b.table[i].table.length;
481 var toRemove = a.table.length + b.table.length
482 return toRemove - (Math.floor((subLengths - 1) / M) + 1);
485 // get2, set2 and saveSlot are helpers for accessing elements over two arrays.
486 function get2(a, b, index) {
487 return index < a.length ? a[index] : b[index - a.length];
490 function set2(a, b, index, value) {
491 if (index < a.length) {
494 b[index - a.length] = value;
498 function saveSlot(a, b, index, slot) {
499 set2(a.table, b.table, index, slot);
501 var l = (index == 0 || index == a.lengths.length) ?
502 0 : get2(a.lengths, a.lengths, index - 1);
503 set2(a.lengths, b.lengths, index, l + length(slot));
506 // Creates a node or leaf with a given length at their arrays for perfomance.
507 // Is only used by shuffle.
508 function createNode(h, length) {
509 if (length < 0) { length = 0; }
510 var a = { ctor:"_Array", height:h, table:new Array(length) };
512 a.lengths = new Array(length);
517 // Returns an array of two balanced nodes.
518 function shuffle(a, b, toRemove) {
519 var newA = createNode(a.height, Math.min(M, a.table.length + b.table.length - toRemove));
520 var newB = createNode(a.height, newA.table.length - (a.table.length + b.table.length - toRemove));
522 // Skip the slots with size M. More precise: copy the slot references
525 while (get2(a.table, b.table, read).table.length % M == 0) {
526 set2(newA.table, newB.table, read, get2(a.table, b.table, read));
527 set2(newA.lengths, newB.lengths, read, get2(a.lengths, b.lengths, read));
531 // Pulling items from left to right, caching in a slot before writing
532 // it into the new nodes.
534 var slot = new createNode(a.height - 1, 0);
537 // If the current slot is still containing data, then there will be at
538 // least one more write, so we do not break this loop yet.
539 while (read - write - (slot.table.length > 0 ? 1 : 0) < toRemove) {
540 // Find out the max possible items for copying.
541 var source = get2(a.table, b.table, read);
542 var to = Math.min(M - slot.table.length, source.table.length)
544 // Copy and adjust size table.
545 slot.table = slot.table.concat(source.table.slice(from, to));
546 if (slot.height > 0) {
547 var len = slot.lengths.length;
548 for (var i = len; i < len + to - from; i++) {
549 slot.lengths[i] = length(slot.table[i]);
550 slot.lengths[i] += (i > 0 ? slot.lengths[i - 1] : 0);
556 // Only proceed to next slots[i] if the current one was
558 if (source.table.length <= to) {
562 // Only create a new slot if the current one is filled up.
563 if (slot.table.length == M) {
564 saveSlot(newA, newB, write, slot);
565 slot = createNode(a.height - 1,0);
570 // Cleanup after the loop. Copy the last slot into the new nodes.
571 if (slot.table.length > 0) {
572 saveSlot(newA, newB, write, slot);
576 // Shift the untouched slots to the left
577 while (read < a.table.length + b.table.length ) {
578 saveSlot(newA, newB, write, get2(a.table, b.table, read));
585 // Navigation functions
586 function botRight(a) { return a.table[a.table.length - 1]; }
587 function botLeft(a) { return a.table[0]; }
589 // Copies a node for updating. Note that you should not use this if
590 // only updating only one of "table" or "lengths" for performance reasons.
591 function nodeCopy(a) {
592 var newA = { ctor:"_Array", height:a.height
593 , table:a.table.slice() };
594 if (a.height > 0) { newA.lengths = a.lengths.slice(); }
598 // Returns how many items are in the tree.
599 function length(array) {
600 if (array.height == 0) {
601 return array.table.length;
603 return array.lengths[array.lengths.length - 1];
607 // Calculates in which slot of "table" the item probably is, then
608 // find the exact slot via forward searching in "lengths". Returns the index.
609 function getSlot(i, a) {
610 var slot = i >> (5 * a.height);
611 while (a.lengths[slot] <= i) {
617 // Recursively creates a tree with a given height containing
618 // only the given item.
619 function create(item, h) {
621 return { ctor:"_Array", height:0
624 return { ctor:"_Array", height:h
625 , table:[create(item, h - 1)]
630 // Recursively creates a tree that contains the given tree.
631 function parentise(tree, h) {
632 if (h == tree.height) {
635 return { ctor:"_Array", height:h
636 , table:[parentise(tree, h - 1)]
637 , lengths:[length(tree)] };
641 // Emphasizes blood brotherhood beneath two trees.
642 function siblise(a, b) {
643 return { ctor:"_Array", height:a.height + 1
645 , lengths:[length(a), length(a) + length(b)] };
648 function toJSArray(a) {
649 var jsArray = new Array(length(a));
650 toJSArray_(jsArray, 0, a);
654 function toJSArray_(jsArray, i, a) {
655 for (var t = 0; t < a.table.length; t++) {
657 jsArray[i + t] = a.table[t];
659 var inc = t == 0 ? 0 : a.lengths[t - 1];
660 toJSArray_(jsArray, i + inc, a.table[t]);
665 function fromJSArray(jsArray) {
666 if (jsArray.length == 0) { return empty; }
667 var h = Math.floor(Math.log(jsArray.length) / Math.log(M));
668 return fromJSArray_(jsArray, h, 0, jsArray.length);
671 function fromJSArray_(jsArray, h, from, to) {
673 return { ctor:"_Array", height:0
674 , table:jsArray.slice(from, to) };
677 var step = Math.pow(M, h);
678 var table = new Array(Math.ceil((to - from) / step));
679 var lengths = new Array(table.length);
680 for (var i = 0; i < table.length; i++) {
681 table[i] = fromJSArray_( jsArray, h - 1, from + (i * step)
682 , Math.min(from + ((i + 1) * step), to));
683 lengths[i] = length(table[i]) + (i > 0 ? lengths[i-1] : 0);
685 return { ctor:"_Array", height:h, table:table, lengths:lengths };
688 Elm.Native.Array.values = {
692 initialize:F2(initialize),
699 indexedMap:F2(indexedMap),
705 fromJSArray:fromJSArray
708 return elm.Native.Array.values = Elm.Native.Array.values;
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list