@@ -358,70 +358,115 @@ Elm.Native.Array.make = function(elm) {
358358 }
359359
360360 // Appends two trees.
361− // TODO: Add support for appending trees of different sizes. Current
362− // behavior will just rise the lower tree and then append them.
363361 function append(a,b) {
364− if (a.table.length == 0) return b;
365− if (b.table.length == 0) return a;
366− if (b.height > a.height) return append(parentise(a, b.height), b);
367− if (a.height > b.height) return append(a, parentise(b, a.height));
368− if (a.height == 0) return append(parentise(a, 1), parentise(b, 1));
362+ if (a.table.length == 0) {
363+ return b;
364+ }
365+ if (b.table.length == 0) {
366+ return a;
367+ }
369368
370369 var c = append_(a, b);
371− if (c[1].table.length > 0) {
372− return siblise(c[0], c[1]);
373− } else {
370+
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) {
374+ return c[1];
375+ }
376+ if (c[1].table.length == 0) {
377+ return c[0];
378+ }
379+
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;
386+ }
387+ c[0].lengths = c[0].lengths.concat(c[1].lengths);
388+ }
389+
374390 return c[0];
375391 }
392+
393+ var toRemove = calcToRemove(a, b);
394+ if (toRemove > E) {
395+ c = shuffle(c[0], c[1], toRemove);
396+ }
397+
398+ return siblise(c[0], c[1]);
376399 }
377400
378− // Returns an array of two nodes. The second node _may_ be empty. This case
379− // needs to be handled by the function, that called append_. May be only
380− // called for trees with an minimal height of 1.
401+ // Returns an array of two nodes; right and left. One node _may_ be empty.
381402 function append_(a, b) {
382− if (a.height == 1) {
383− // Check if balancing is needed and return based on that.
384− var toRemove = calcToRemove(a, b);
385− if (toRemove <= E) {
386− return [a,b];
387− }
388−
389− return shuffle(a, b, toRemove);
403+ if (a.height == 0 && b.height == 0) {
404+ return [a, b];
390405 }
391406
392− var appended = append_(botRight(a), botLeft(b));
393− a = nodeCopy(a), b = nodeCopy(b);
407+ if (a.height != 1 || b.height != 1) {
408+ if (a.height == b.height) {
409+ a = nodeCopy(a), b = nodeCopy(b);
410+ var appended = append_(botRight(a), botLeft(b));
394411
395− // Adjust the bottom right side of the new tree.
396− a.table[a.table.length - 1] = appended[0];
397− a.lengths[a.lengths.length - 1] = length(appended[0])
398− a.lengths[a.lengths.length - 1] += a.lengths.length > 1 ? a.lengths[a.lengths.length - 2] : 0;
412+ insertRight(a, appended[1]);
413+ insertLeft(b, appended[0]);
414+ } else if (a.height > b.height) {
415+ a = nodeCopy(a);
416+ var appended = append_(botRight(a), b);
399417
400− // Adjust the bottom left side of the new tree.
401− if (appended[1].table.length > 0) {
402− b.table[0] = appended[1];
403− b.lengths[0] = length(appended[1]);
404− for (var i = 1, len = length(b.table[0]); i < b.lengths.length; i++) {
405− len += length(b.table[i]);
406− b.lengths[i] = len;
407− }
408− } else {
409− b.table.shift();
410− for (var i = 1; i < b.lengths.length; i++) {
411− b.lengths[i] = b.lengths[i] - b.lengths[0];
418+ insertRight(a, appended[0]);
419+ b = parentise(appended[1], appended[1].height + 1);
420+ } else {
421+ b = nodeCopy(b);
422+ var appended = append_(a, botLeft(b));
423+
424+ var left = appended[0].table.length == 0 ? 0 : 1;
425+ var right = left == 0 ? 1 : 0;
426+ insertLeft(b, appended[left]);
427+ a = parentise(appended[right], appended[right].height + 1);
412428 }
413− b.lengths.shift();
414429 }
415430
416431 // Check if balancing is needed and return based on that.
417− var toRemove = calcToRemove(a, b);
418− if (toRemove <= E || b.table.length == 0) {
432+ if (a.table.length == 0 || b.table.length == 0) {
419433 return [a,b];
420434 }
421435
436+ var toRemove = calcToRemove(a, b);
437+ if (toRemove <= E) {
438+ return [a,b];
439+ }
422440 return shuffle(a, b, toRemove);
423441 }
424442
443+ // Helperfunctions for append_. Replaces a child node at the side of the parent.
444+ function insertRight(parent, node) {
445+ var index = parent.table.length - 1;
446+ parent.table[index] = node;
447+ parent.lengths[index] = length(node)
448+ parent.lengths[index] += index > 0 ? parent.lengths[index - 1] : 0;
449+ }
450+
451+ function insertLeft(parent, node) {
452+ if (node.table.length > 0) {
453+ parent.table[0] = node;
454+ parent.lengths[0] = length(node);
455+
456+ var len = length(parent.table[0]);
457+ for (var i = 1; i < parent.lengths.length; i++) {
458+ len += length(parent.table[i]);
459+ parent.lengths[i] = len;
460+ }
461+ } else {
462+ parent.table.shift();
463+ for (var i = 1; i < parent.lengths.length; i++) {
464+ parent.lengths[i] = parent.lengths[i] - parent.lengths[0];
465+ }
466+ parent.lengths.shift();
467+ }
468+ }
469+
425470 // Returns the extra search steps for E. Refer to the paper.
426471 function calcToRemove(a, b) {
427472 var subLengths = 0;