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 // Gets the value at index i recursively.
28 function get(i, array) {
29 if (i < 0 || i >= length(array)) {
30 throw new Error("Index " + i + " is out of range. Check the length of " +
31 "your array first or use getMaybe or getWithDefault.");
33 return unsafeGet(i, array);
36 function unsafeGet(i, array) {
37 if (array.height == 0) {
38 return array.table[i];
40 var slot = getSlot(i, array);
41 var offset = slot > 0 ? array.lengths[slot-1] : 0;
42 return unsafeGet(i - offset, array.table[slot]);
45 // Sets the value at the index i. Only the nodes leading to i will get
46 // copied and updated.
47 function set(i, item, a) {
51 var newA = nodeCopy(a);
56 var slot = getSlot(i, a);
57 var sub = slot > 0 ? a.lengths[slot-1] : 0;
58 newA.table[slot] = set(i - sub, item, a.table[slot]);
63 function initialize(len, f) {
64 if (len == 0) { return empty; }
65 var h = Math.floor(Math.log(len)/Math.log(M));
66 return initialize_(f, h, 0, len);
69 function initialize_(f, h, from, to) {
71 var table = new Array((to - from) % (M + 1));
72 for (var i = 0; i < table.length; i++) {
73 table[i] = f(from + i);
75 return { ctor:"_Array", height:0, table:table };
78 var step = Math.pow(M, h);
79 var table = new Array(Math.ceil((to - from) / step));
80 var lengths = new Array(table.length);
81 for (var i = 0; i < table.length; i++) {
82 table[i] = initialize_( f, h - 1, from + (i * step)
83 , Math.min(from + ((i + 1) * step), to));
84 lengths[i] = length(table[i]) + (i > 0 ? lengths[i-1] : 0);
86 return { ctor:"_Array", height:h, table:table, lengths:lengths };
89 function fromList(list) {
90 if (list == List.Nil) { return empty; }
92 // Allocate M sized blocks (table) and write list elements to it.
93 var table = new Array(M);
94 var nodes = new Array();
97 while (list.ctor !== '[]') {
102 // table is full, so we can push a leaf containing it into the
105 fromListPush({ ctor:"_Array", height:0, table:table }
107 table = new Array(M);
112 // Maybe there is something left on the table.
114 fromListPush({ ctor:"_Array", height:0, table:table.splice(0,i) }
118 // Go through all of the nodes and eventually push them into higher nodes.
119 for (var h = 0; h < nodes.length - 1; h++) {
120 if (nodes[h].table.length > 0) {
121 fromListPush(nodes[h], nodes);
125 var head = nodes[nodes.length - 1];
126 if (head.height > 0 && head.table.length == 1) {
127 return head.table[0];
133 // Push a node into a higher node as a child.
134 function fromListPush(toPush, nodes) {
135 var h = toPush.height;
137 // Maybe the node on this height does not exist.
138 if (nodes.length == h) {
139 nodes.push({ ctor:"_Array", height:h + 1
141 , lengths:new Array() });
144 nodes[h].table.push(toPush);
145 var len = length(toPush);
146 if (nodes[h].lengths.length > 0) {
147 len += nodes[h].lengths[nodes[h].lengths.length - 1];
149 nodes[h].lengths.push(len);
151 if (nodes[h].table.length == M) {
152 fromListPush(nodes[h], nodes);
153 nodes[h] = { ctor:"_Array", height:h + 1
155 , lengths:new Array() };
159 // Pushes an item via push_ to the bottom right of a tree.
160 function push(item, a) {
161 var pushed = push_(item, a);
162 if (pushed !== null) {
166 var newTree = create(item, a.height);
167 return siblise(a, newTree);
170 // Recursively tries to push an item to the bottom-right most
171 // tree possible. If there is no space left for the item,
172 // null will be returned.
173 function push_(item, a) {
174 // Handle resursion stop at leaf level.
176 if (a.table.length < M) {
177 var newA = { ctor:"_Array", height:0, table:a.table.slice() };
178 newA.table.push(item);
186 var pushed = push_(item, botRight(a));
188 // There was space in the bottom right tree, so the slot will
190 if (pushed != null) {
191 var newA = nodeCopy(a);
192 newA.table[newA.table.length - 1] = pushed;
193 newA.lengths[newA.lengths.length - 1]++;
197 // When there was no space left, check if there is space left
198 // for a new slot with a tree which contains only the item
200 if (a.table.length < M) {
201 var newSlot = create(item, a.height - 1);
202 var newA = nodeCopy(a);
203 newA.table.push(newSlot);
204 newA.lengths.push(newA.lengths[newA.lengths.length - 1] + length(newSlot));
211 // Converts an array into a list of elements.
213 return toList_(List.Nil, a);
216 function toList_(list, a) {
217 for (var i = a.table.length - 1; i >= 0; i--) {
218 list = a.height == 0 ? List.Cons(a.table[i], list) : toList_(list, a.table[i]);
223 // Maps a function over the elements of an array.
225 var newA = { ctor:"_Array", height:a.height, table:new Array(a.table) };
226 if (a.height > 0) { newA.lengths = a.lengths; }
227 for (var i = 0; i < a.table.length; i++) {
228 newA.table[i] = a.height == 0 ? f(a.table[i]) : map(f, a.table[i]);
233 // Maps a function over the elements with their index as first argument.
234 function indexedMap(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 ? A2(f, i, a.table[i]) : indexedMap(f, a.table[i]);
243 function foldl(f, b, a) {
244 for (var i = a.table.length - 1; i >= 0; i--) {
245 b = A2(f, a.height == 0 ? a.table[i] : foldl(f, b, a.table[i]), b);
250 function foldr(f, b, a) {
251 for (var i = 0; i < a.table.length; i++) {
252 b = A2(f, a.height == 0 ? a.table[i] : foldr(f, b, a.table[i]), b);
257 // Returns a sliced tree. "to" is inclusive, but this may change,
258 // when I understand, why e.g. JS does not handle it this way. :-)
259 // If "from" or "to" is negative, they will select from the end on.
260 // TODO: currently, it slices the right, then the left. This can be
262 function slice(from, to, a) {
263 if (from < 0) { from += length(a); }
264 if (to < 0) { to += length(a); }
265 return sliceLeft(from, sliceRight(to, a));
268 function sliceRight(to, a) {
269 if (to == length(a)) {
273 // Handle leaf level.
275 var newA = { ctor:"_Array", height:0 };
276 newA.table = a.table.slice(0, to + 1);
280 // Slice the right recursively.
281 var right = getSlot(to, a);
282 var sliced = sliceRight(to - (right > 0 ? a.lengths[right - 1] : 0), a.table[right]);
284 // Maybe the a node is not even needed, as sliced contains the whole slice.
290 var newA = { ctor:"_Array", height:a.height
291 , table:a.table.slice(0, right + 1)
292 , lengths:a.lengths.slice(0, right + 1) };
293 newA.table[right] = sliced;
294 newA.lengths[right] = length(sliced) + (right > 0 ? newA.lengths[right - 1] : 0);
298 function sliceLeft(from, a) {
303 // Handle leaf level.
305 var newA = { ctor:"_Array", height:0 };
306 newA.table = a.table.slice(from, a.table.length + 1);
310 // Slice the left recursively.
311 var left = getSlot(from, a);
312 var sliced = sliceLeft(from - (left > 0 ? a.lengths[left - 1] : 0), a.table[left]);
314 // Maybe the a node is not even needed, as sliced contains the whole slice.
315 if (left == a.table.length - 1) {
320 var newA = { ctor:"_Array", height:a.height
321 , table:a.table.slice(left, a.table.length + 1)
322 , lengths:new Array(a.table.length - left) };
323 newA.table[left] = sliced;
325 for (var i = 0; i < newA.table.length; i++) {
326 len += length(newA.table[i]);
327 newA.lengths[i] = len;
333 // Appends two trees.
334 // TODO: Add support for appending trees of different sizes. Current
335 // behavior will just rise the lower tree and then append them.
336 function append(a,b) {
337 if (b.height > a.height) { return append(parentise(a, b.height), b); }
338 if (a.height > b.height) { return append(a, parentise(b, a.height)); }
339 if (a.height == 0) { return append(parentise(a, 1), parentise(b, 1)); }
341 var c = append_(a, b);
342 if (c[1].table.length > 0) {
343 return siblise(c[0], c[1]);
349 // Returns an array of two nodes. The second node _may_ be empty. This case
350 // needs to be handled by the function, that called append_. May be only
351 // called for trees with an minimal height of 1.
352 function append_(a, b) {
354 // Check if balancing is needed and return based on that.
355 var toRemove = calcToRemove(a, b);
360 return shuffle(a, b, toRemove);
363 var appended = append_(botRight(a), botLeft(b));
364 a = nodeCopy(a), b = nodeCopy(b);
366 // Adjust the bottom right side of the new tree.
367 a.table[a.table.length - 1] = appended[0];
368 a.lengths[a.lengths.length - 1] = length(appended[0])
369 a.lengths[a.lengths.length - 1] += a.lengths.length > 1 ? a.lengths[a.lengths.length - 2] : 0;
371 // Adjust the bottom left side of the new tree.
372 if (appended[1].table.length > 0) {
373 b.table[0] = appended[1];
374 b.lengths[0] = length(appended[1]);
375 for (var i = 1, len = length(b.table[0]); i < b.lengths.length; i++) {
376 len += length(b.table[i]);
381 for (var i = 1; i < b.lengths.length; i++) {
382 b.lengths[i] = b.lengths[i] - b.lengths[0];
387 // Check if balancing is needed and return based on that.
388 var toRemove = calcToRemove(a, b);
389 if (toRemove <= E || b.table.length == 0) {
393 return shuffle(a, b, toRemove);
396 // Returns the extra search steps for E. Refer to the paper.
397 function calcToRemove(a, b) {
399 for (var i = 0; i < a.table.length; i++) {
400 subLengths += a.table[i].table.length;
402 for (var i = 0; i < b.table.length; i++) {
403 subLengths += b.table[i].table.length;
406 var toRemove = a.table.length + b.table.length
407 return toRemove - (Math.floor((subLengths - 1) / M) + 1);
410 // get2, set2 and saveSlot are helpers for accessing elements over two arrays.
411 function get2(a, b, index) {
412 return index < a.length ? a[index] : b[index - a.length];
415 function set2(a, b, index, value) {
416 if (index < a.length) {
419 b[index - a.length] = value;
423 function saveSlot(a, b, index, slot) {
424 set2(a.table, b.table, index, slot);
426 var l = (index == 0 || index == a.lengths.length) ?
427 0 : get2(a.lengths, a.lengths, index - 1);
428 set2(a.lengths, b.lengths, index, l + length(slot));
431 // Creates a node or leaf with a given length at their arrays for perfomance.
432 // Is only used by shuffle.
433 function createNode(h, length) {
434 if (length < 0) { length = 0; }
435 var a = { ctor:"_Array", height:h, table:new Array(length) };
437 a.lengths = new Array(length);
442 // Returns an array of two balanced nodes.
443 function shuffle(a, b, toRemove) {
444 var newA = createNode(a.height, Math.min(M, a.table.length + b.table.length - toRemove));
445 var newB = createNode(a.height, newA.table.length - (a.table.length + b.table.length - toRemove));
447 // Skip the slots with size M. More precise: copy the slot references
450 while (get2(a.table, b.table, read).table.length % M == 0) {
451 set2(newA.table, newB.table, read, get2(a.table, b.table, read));
452 set2(newA.lengths, newB.lengths, read, get2(a.lengths, b.lengths, read));
456 // Pulling items from left to right, caching in a slot before writing
457 // it into the new nodes.
459 var slot = new createNode(a.height - 1, 0);
462 // If the current slot is still containing data, then there will be at
463 // least one more write, so we do not break this loop yet.
464 while (read - write - (slot.table.length > 0 ? 1 : 0) < toRemove) {
465 // Find out the max possible items for copying.
466 var source = get2(a.table, b.table, read);
467 var to = Math.min(M - slot.table.length, source.table.length)
469 // Copy and adjust size table.
470 slot.table = slot.table.concat(source.table.slice(from, to));
471 if (slot.height > 0) {
472 var len = slot.lengths.length;
473 for (var i = len; i < len + to - from; i++) {
474 slot.lengths[i] = length(slot.table[i]);
475 slot.lengths[i] += (i > 0 ? slot.lengths[i - 1] : 0);
481 // Only proceed to next slots[i] if the current one was
483 if (source.table.length <= to) {
487 // Only create a new slot if the current one is filled up.
488 if (slot.table.length == M) {
489 saveSlot(newA, newB, write, slot);
490 slot = createNode(a.height - 1,0);
495 // Cleanup after the loop. Copy the last slot into the new nodes.
496 if (slot.table.length > 0) {
497 saveSlot(newA, newB, write, slot);
501 // Shift the untouched slots to the left
502 while (read < a.table.length + b.table.length ) {
503 saveSlot(newA, newB, write, get2(a.table, b.table, read));
510 // Navigation functions
511 function botRight(a) { return a.table[a.table.length - 1]; }
512 function botLeft(a) { return a.table[0]; }
514 // Copies a node for updating. Note that you should not use this if
515 // only updating only one of "table" or "lengths" for performance reasons.
516 function nodeCopy(a) {
517 var newA = { ctor:"_Array", height:a.height
518 , table:a.table.slice() };
519 if (a.height > 0) { newA.lengths = a.lengths.slice(); }
523 // Returns how many items are in the tree.
524 function length(array) {
525 if (array.height == 0) {
526 return array.table.length;
528 return array.lengths[array.lengths.length - 1];
532 // Calculates in which slot of "table" the item probably is, then
533 // find the exact slot via forward searching in "lengths". Returns the index.
534 function getSlot(i, a) {
535 var slot = Math.floor(i / (Math.pow(M, a.height)));
536 while (a.lengths[slot] <= i) {
542 // Recursively creates a tree with a given height containing
543 // only the given item.
544 function create(item, h) {
546 return { ctor:"_Array", height:0
549 return { ctor:"_Array", height:h
550 , table:[create(item, h - 1)]
555 // Recursively creates a tree that contains the given tree.
556 function parentise(tree, h) {
557 if (h == tree.height) {
560 return { ctor:"_Array", height:h
561 , table:[parentise(tree, h - 1)]
562 , lengths:[length(tree)] };
566 // Emphasizes blood brotherhood beneath two trees.
567 function siblise(a, b) {
568 return { ctor:"_Array", height:a.height + 1
570 , lengths:[length(a), length(a) + length(b)] };
573 Elm.Native.Array.values = {
577 initialize:F2(initialize),
584 indexedMap:F2(indexedMap),
590 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