1.set define the set of
Collection members are unordered, is not a repeat of the group members.
Development can be used to remove duplicate data
set collections and map are not the same. Here only implements the methods set collection.
a hash map structure is defined to achieve, but also on the nature of the array and binding chain.
The method is not introduced, the mathematical definition of the set are learned.
2. Package Object
Here's a way to achieve the set target
function Set(){
this.items={}
}
3. New value-added
The default set of health and health value of its name
Set.prototype.add=function(value){
if(this.has(value)){
return false
}
this.items[value]=value
return true
}
3. Delete value
Set.prototype.has=function(value){
return this.items.hasOwnProperty(value)
}
Set.prototype.remove=function(value){
if(!this.has(value)){
return false
}
delete this.items[value]
return true
}
4. General procedure
Set.prototype.clear=function(){
this.items={}
}
Set.prototype.size=function(){
return Object.keys(this.items).length
}
Set.prototype.values=function(){
return Object.keys(this.items)
}
5. Set and
Set.prototype.union=function(otherSet){
var unionSet=new Set()
var values=this.values()
for(var i=0;i<values.length;i++){
unionSet.add(values[i])
}
values=otherSet.values()
for(var i=0;i<values.length;o++){
unionSet.add(values[i])
}
return unionSet
}
6. Intersection
Set.prototype.intersection=function(otherSet){
var intersectionSet=new Set()
var values=this.values()
for(var i=0;i<values.length;i++){
var item=values[i]
if(otherSet.has(item)){
intersectionSet.add(item)
}
}
return intersectionSet
}
7. complement
Set.prototype.difference=function(otherSet){
var differenceSet=new Set()
var values=this.values()
for(var i=0;i<values.length;i++){
var item=values[i]
if(!otherSet.has(item)){
differenceSet.add(item)
}
}
return differenceSet
}
8. child Collection
Set.prototype.subset=function(otherSet){
var values=this.values()
for(var i=0;i<values.length;i++){
var item=values[i]
if(!otherSet.has(item)){
return false
}
}
return true
}