多维数组的笛卡尔积

时间:2015-02-18 23:09:44

标签: javascript arrays multidimensional-array cartesian-product

我使用js-combinatorics代码生成了这个:

(function(global) {
    'use strict';
    if (global.Combinatorics) return;
    /* common methods */
    var addProperties = function(dst, src) {
        Object.keys(src).forEach(function(p) {
            Object.defineProperty(dst, p, {
                value: src[p]
            });
        });
    };
    var hideProperty = function(o, p) {
        Object.defineProperty(o, p, {
            writable: true
        });
    };
    var toArray = function(f) {
        var e, result = [];
        this.init();
        while (e = this.next()) result.push(f ? f(e) : e);
        this.init();
        return result;
    };
    var common = {
        toArray: toArray,
        map: toArray,
        forEach: function(f) {
            var e;
            this.init();
            while (e = this.next()) f(e);
            this.init();
        },
        filter: function(f) {
            var e, result = [];
            this.init();
            while (e = this.next()) if (f(e)) result.push(e);
            this.init();
            return result;
        }

    };
    /* Cartesian Product */
    var arraySlice = Array.prototype.slice;
    var cartesianProduct = function() {
        if (!arguments.length) throw new RangeError;
        var args = arraySlice.call(arguments);
        args = args[0];
        console.log(args);
        var
            size = args.reduce(function(p, a) {
                return p * a.length;
            }, 1),
            sizeOf = function() {
                return size;
            },
            dim = args.length,
            that = Object.create(args, {
                length: {
                    get: sizeOf
                }
            });
        if (!size) throw new RangeError;
        hideProperty(that, 'index');
        addProperties(that, {
            valueOf: sizeOf,
            dim: dim,
            init: function() {
                this.index = 0;
            },
            get: function() {
                if (arguments.length !== this.length) return;
                var result = [];
                arguments.forEach(function(element,index,array) {
                    var i = arguments[index];
                    if(i >= this[index].length) return;
                    result.push(this[index][i]);
                });
                return result;
            },
            nth: function(n) {
                var result = [];
                arguments.forEach(function(element,index,array) {
                    var l = this[index].length,
                        i = n % l;
                    result.push(this[index][i]);
                    n -= i;
                    n /= l;
                });
                return result;
            },
            next: function() {
                if (this.index >= size) return;
                var result = this.nth(this.index);
                this.index++;
                return result;
            }
        });
        addProperties(that, common);
        that.init();
        return that;
    };

    /* export */
    addProperties(global.Combinatorics = Object.create(null), {
        cartesianProduct: cartesianProduct
    });
})(this);

var _ = [];
_[1] = [1,4];
_[7] = [2,9];

cp = Combinatorics.cartesianProduct(_);
console.log(cp.toArray());

我希望最终得到这个结果:

[[1,2],[1,9],[4,2],[4,9]]

但每次在这部分代码中使用forEach时,Chrome中的Uncaught TypeError: undefined is not a function和Firefox中的TypeError: arguments.forEach is not a function都会继续:

nth: function(n) {
    var result = [];
    arguments.forEach(function(element,index,array) {
        var l = this[index].length,
            i = n % l;
        result.push(this[index][i]);
        n -= i;
        n /= l;
    });
    return result;
}

保持_数组的索引是必须的。

2 个答案:

答案 0 :(得分:0)

arguments不是数组,因此它没有forEach方法。

您可以像在var args = arraySlice.call(arguments);中一样将其转换为数组,或者使用for循环来迭代其元素。

答案 1 :(得分:0)

我需要发布带有非严格索引的_数组:

var _ = [];
_[1] = [1,4];
_[7] = [2,9];

默认解决方案是禁止的,因为它们不处理此类数组。所以我不得不调整Bergi的想法here

function cartesian(arg) {
    var r = [], max = arg.length-1;
    function helper(arr, i) {
        while(typeof arg[i] === "undefined") {
            i += 1;
        }
        for (var j=0, l=arg[i].length; j<l; j++) {
            var a = arr.slice(0); // clone arr
            a.push(arg[i][j]);
            if (i==max) {
                r.push(a);
            } else
                helper(a, i+1);
        }
    }
    helper([], 0);
    return r;
}
相关问题