7x7


Feiner Sand

Hello World

Written by Administrator | 27th Jul, 2015

Hello World!

This is the first post.

function MaxArea(array) {

    function setMetrics() {

        function getArea(a, i, o) {
            if (lastValue >= a) {
                lastValue = a;
                if (i + 1 < o.length && o[i + 1] === a) {
                    return '_';// '<span style="background-color: #ccccff;">>' + (a * (i + 1)) + '</span>';
                } else {
                    return a * (i + 1) || '_';
                }
            } else {
                return '_'; // '<span style="background-color: #ffff00;">' + (lastValue * (i + 1)) + '</span>'; // 'wrong order'; //
            }
        }

        var horizontal = [], vertical = [], left = [], top = [], adjacentTop = [], leftArea, topArea, lastValue;

        this.sort.forEach(function (r, j) {
            var adjacentLeft = true;
            horizontal[j] = 0;
            left[j] = 0;
            r.forEach(function (e, i) {
                if (!j) {
                    vertical[i] = 0;
                    top[i] = 0;
                    adjacentTop[i] = true;
                }
                if (e !== ' ') {
                    horizontal[j]++
                    vertical[i]++;
                    adjacentLeft && left[j]++;
                    adjacentTop[i] && top[i]++;
                } else {
                    adjacentLeft = false,
                    adjacentTop[i] = false;
                }
            });
        });
        lastValue = Number.MAX_VALUE;
        leftArea = left.map(getArea);
        lastValue = Number.MAX_VALUE;
        topArea = top.map(getArea);

        this.metrics.horizontal = horizontal;
        this.metrics.vertical = vertical;
        this.metrics.left = left;
        this.metrics.top = top;
        this.metrics.area = { left: leftArea, top: topArea };
    }

    function moveRow(from, to) {
        to = to || from - 1;
        this.sort.splice(to, 0, this.sort.splice(from, 1)[0]);
        this.keys.horizontal.splice(to, 0, this.keys.horizontal.splice(from, 1));
    }

    function moveCol(from, to) {
        to = to || from - 1;
        this.sort.forEach(function (r) {
            r.splice(to, 0, r.splice(from, 1)[0]);
        });
        this.keys.vertical.splice(to, 0, this.keys.vertical.splice(from, 1));
    }

    function optimize(target) {
        var i = 1,
            isHorizontal = !!~['horizontal', 'left'].indexOf(target),
            len = isHorizontal ? this.sort.length : this.sort[0].length;

        while (i < len) {
            if (this.metrics[target][i - 1] < this.metrics[target][i]) {
                this[isHorizontal ? 'moveRow' : 'moveCol'](i);
                i = 1;
            } else {
                i++;
            }
        }
        this.printSort('optimize ' + target);
    }

    function optimizeClip(target, clip) {
        var i = 1,
            direction = ~['horizontal', 'left'].indexOf(target) ? 'horizontal' : 'vertical',
            len;

        clip = clip || {};
        clip.horizontal = clip.horizontal || this.sort.length;
        clip.horizontal = Math.min(clip.horizontal, this.sort.length);
        clip.vertical = clip.vertical || this.sort[0].length;
        clip.vertical = Math.min(clip.vertical, this.sort[0].length);
        len = clip[direction];

        while (i < len) {
            if (this.metrics[target][i - 1] < this.metrics[target][i]) {
                this[direction === 'horizontal' ? 'moveRow' : 'moveCol'](i);
                this.setMetrics();
                i = 1;
            } else {
                i++;
            }
        }
        this.printSort('optimize ' + target + ' (' + direction + ' ' + len + ') ' + '[' + clip.horizontal + ', ' + clip.vertical + ']', target);
    }

    function printSort(s, target) {

        function pad(s) { while (s.length < 20) { s += ' '; } return s; }
        function frame(s) { return '+' + (Array(s.length + 3).join('-')) + '+\n| ' + s + ' |\n+' + (Array(s.length + 3).join('-')) + '+\n\n'; }

        var i, that = this;

        s = s ? frame(s) : '';
        s += this.sort.reduce(function (s, a, j) {
            //return s + a.join('') + '|\n';

            return s + a.reduce(function (t, b, i) {
                if (that.metrics.area.left[j] === (i + 1) * (j + 1) && that.metrics.area.top[i] === (i + 1) * (j + 1)) {
                    return t + '<span title="Area: ' + that.metrics.area.left[j] + '" style="background-color: #ffff00;">' + b + '</span>';
                } else {
                    return t + b;
                }
            }, '') + '|\n';

        }, '') + '\n';
        for (i in this.keys) {
            s += pad('keys.' + i + ':') + ' [' + this.keys[i].join(', ') + ']\n';
        }
        for (i in this.metrics) {
            if (i !== 'area') {
                s += pad('metrics.' + i + (target === i ? '*' : '') + ':') + ' [' + this.metrics[i].join(', ') + ']\n';
            }
        }
        s += pad('metrics.area.left:') + ' [' + this.metrics.area.left.join(', ') + ']\n';
        s += pad('metrics.area.top:') + ' [' + this.metrics.area.top.join(', ') + ']';
        //s += '\n<span title="blabla">x</span>';
        out(s, true);
    }

    this.setMetrics = setMetrics;
    this.moveRow = moveRow;
    this.moveCol = moveCol;
    this.optimize = optimize;
    this.optimizeClip = optimizeClip;
    this.printSort = printSort;

    this.array = array;
    this.sort = array.map(function (e) {
        return e.split('');
    });
    this.keys = {
        horizontal: this.sort.map(function (_, i) { return i; }),
        vertical: this.sort[0].map(function (_, i) { return i; })
    };
    this.metrics = {};
    this.setMetrics();
}

var i, j;
var try4 = new MaxArea([
        '   xxx    ',
        '     x    ',
        '          ',
        '  xxxxx   ',
        '  xxxxx   ',
        '  xxxxx   ',
        'x xxx   x ',
        '  xxx    x',
        '          ',
        'xxxx x xxx'
]);
try4.printSort();

try4.optimizeClip('vertical');
try4.optimizeClip('horizontal');
for (i = 1; i <= try4.sort.length; i++) {
    //try4.optimizeClip('top', { horizontal: i });
    try4.optimizeClip('left', { horizontal: i });
}

function out(s, pre) {
    var node = document.createElement('div');
    if (pre) {
        var preNode = document.createElement('pre');
        preNode.innerHTML = s + '<br>';
        node.appendChild(preNode);
    } else {
        node.innerHTML = s + '<br>';
    }
    document.getElementById('out').appendChild(node);
}

abc


Comments