javascript Set类 集合 交集+并集+差集+子集

* Set.js

/**
 * Created by Mch on 8/20/18.
 */
function Set() {
    this.items = {}
}

Set.prototype = {
    has: function(value) {
        return this.items.hasOwnProperty(value);
    },
    add: function(value) {
        if (!this.has(value)) {
            this.items[value] = value;
            return true;
        }
        return false;
    },
    remove: function(value) {
        if (this.has(value)) {
            delete this.items[value];
            return true;
        }
        return false;
    },
    clear: function() {
        this.items = {}
    },
    size: function() {
        var count = 0;
        for (var prop in this.items) {
            if (this.items.hasOwnProperty(prop)) {
                ++count;
            }
        }
        return count;
    },
    values: function() {
        var keys = [];
        for (var key in this.items) {
            if (this.items.hasOwnProperty(key)) {
                keys.push(key);
            }
        }
        return keys;
    },
    // A∪B = { x | x ∈ A∨x ∈ B }
    union: function(otherSet) {
        var unionSet = new Set(),
            values = this.values();
        for (var i = 0; i < values.length; i++) {
            unionSet.add(values[i]);
        }
        values = otherSet.values();
        for (i = 0; i < values.length; i++){
            unionSet.add(values[i]);
        }
        return unionSet;
    },
    // A∩B = { x | x ∈ A∧x ∈ B }
    intersection: function(otherSet) {
        var intersectionSet = new Set(),
            values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (otherSet.has(values[i])) {
                intersectionSet.add(values[i]);
            }
        }
        return intersectionSet;
    },
    // AB = { x | x ∈ A ∧ x  B }
    difference: function(otherSet) {
        var differenceSet = new Set(),
            values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (!otherSet.has(values[i])) {
                differenceSet.add(values[i]);
            }
        }
        return differenceSet;
    },
    // ∀x { x ∈ A → x ∈ B }
    subset: function(otherSet) {
        if (this.size() > otherSet.size()) {
            return false;
        }
        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (!otherSet.has(values[i])) {
                return false;
            }
        }
        return true;
    }
};

exports.Set = Set;

* TestSet.js

/**
 * Created by Mch on 8/20/18.
 */
var Set = require('./Set.js').Set;

function TestSet() {}

TestSet.main = function() {
    var set = new Set();
    set.add(1); console.log(set.values()); //输出["1"] console.log(set.has(1)); //输出true console.log(set.size()); //输出1
    set.add(2);
    console.log(set.values()); //输出["1", "2"] console.log(set.has(2)); //true console.log(set.size()); //2
    set.remove(1); console.log(set.values()); //输出["2"]
    set.remove(2); console.log(set.values()); //输出[]

    var setA = new Set();
    setA.add(1);
    setA.add(2);
    setA.add(3);

    var setB = new Set();
    setB.add(2);
    setB.add(3);
    setB.add(4);
    setB.add(5);
    setB.add(6);

    var unionAB = setA.union(setB);
    console.log(unionAB.values()); // [ '1', '2', '3', '4', '5', '6' ]

    var intersectionAB = setA.intersection(setB);
    console.log(intersectionAB.values()); // [ '2', '3' ]

    var differenceAB = setA.difference(setB);
    console.log(differenceAB.values());  // ['1']

    setA.clear();
    setA.add(1);
    setA.add(2);
    setB.clear();
    setB.add(1);
    setB.add(2);
    setB.add(3);
    var setC = new Set();
    setC.add(1);
    setC.add(3);
    setC.add(4);

    console.log(setA.subset(setB));  // true
    console.log(setA.subset(setC));  // false
};

TestSet.main();

猜你喜欢

转载自blog.csdn.net/fareast_mzh/article/details/81879409