mirror of
https://github.com/photonstorm/phaser
synced 2025-01-11 12:48:50 +00:00
108 lines
1.7 KiB
JavaScript
108 lines
1.7 KiB
JavaScript
var Set = function ()
|
|
{
|
|
this.values = [];
|
|
};
|
|
|
|
Set.prototype.constructor = Set;
|
|
|
|
Set.prototype = {
|
|
|
|
add: function (value)
|
|
{
|
|
if (this.values.indexOf(value) === -1)
|
|
{
|
|
this.values.push(value);
|
|
}
|
|
},
|
|
|
|
delete: function (value)
|
|
{
|
|
var index = this.values.indexOf(value);
|
|
|
|
if (index > -1)
|
|
{
|
|
this.values.splice(index, 1);
|
|
}
|
|
},
|
|
|
|
clear: function ()
|
|
{
|
|
this.values.length = 0;
|
|
},
|
|
|
|
contains: function (value)
|
|
{
|
|
return (this.values.indexOf(value) > -1);
|
|
},
|
|
|
|
union: function (set)
|
|
{
|
|
var newSet = new Set();
|
|
|
|
set.values.forEach(function (value)
|
|
{
|
|
newSet.add(value);
|
|
});
|
|
|
|
this.values.forEach(function (value)
|
|
{
|
|
newSet.add(value);
|
|
});
|
|
|
|
return newSet;
|
|
},
|
|
|
|
intersect: function (set)
|
|
{
|
|
var newSet = new Set();
|
|
|
|
this.values.forEach(function (value)
|
|
{
|
|
if (set.contains(value))
|
|
{
|
|
newSet.add(value);
|
|
}
|
|
});
|
|
|
|
return newSet;
|
|
},
|
|
|
|
difference: function (set)
|
|
{
|
|
var newSet = new Set();
|
|
|
|
this.values.forEach(function (value)
|
|
{
|
|
if (!set.contains(value))
|
|
{
|
|
newSet.add(value);
|
|
}
|
|
});
|
|
|
|
return newSet;
|
|
}
|
|
|
|
};
|
|
|
|
Object.defineProperties(Set.prototype, {
|
|
|
|
length: {
|
|
|
|
writable: true,
|
|
enumerable: false,
|
|
|
|
get: function ()
|
|
{
|
|
return this.values.length;
|
|
},
|
|
|
|
set: function (value)
|
|
{
|
|
return this.values.length = value;
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
module.exports = Set;
|