集合
一种包含不同元素的数据结构 数据是无序的,数据之间不可重复
集合的定义
- 空集合
- 集合相等
- 子集
- 交集
- 并集
- 补集
Set的类型实现
function mySet() {
this.dataStore = []
}
mySet.prototype = {
add: function(data) {
if(this.dataStore.indexOf(data)>-1) {
return false
} else {
this.dataStore.push(data)
}
},
remove: function(data) {
var index = this.dataStore.indexOf(data)
this.dataStore.splice(index, 1)
},
clear: function() {
this.dataStore = []
},
show: function() {
console.log(this.dataStore)
}
}
var eg = new mySet()
eg.add('KKKK')
eg.add('ttttt')
其他的集合操作
- 判断是否属于这个set
contains: function(data) {
if(this.dataStore.indexOf(data)>-1) {
return true
} else {
return false
}
},
- 并集
union = function(set) {
console.log(Object.getPrototypeOf(set).constructor )
if(Object.getPrototypeOf(set).constructor == 'mySet') {
set.dataStore.forEach(item => {
this.add(item)
});
} else {
return false
}
}
- 交集
intersect: function(set) {
console.log(Object.getPrototypeOf(set).constructor )
if(Object.getPrototypeOf(set).constructor == 'mySet') {
var tempSet = new mySet()
for(let i= 0; i< set.dataStore.length; i++) {
var item = set.dataStore[i]
if(this.contains(item)) {
tempSet.add(item)
}
}
}
return tempSet
}
- 子集
subSet: function(set) {
// console.log(Object.getPrototypeOf(set).constructor )
let result = true
if(Object.getPrototypeOf(set).constructor == 'mySet') {
if(set.dataStore.length < this.dataStore.length) {
set.dataStore.forEach(item => {
console.log(item)
if(!this.contains(item)) {
result = false
}
})
} else {
result = false
}
}
return result
}
完整代码
function mySet() {
this.dataStore = []
}
mySet.prototype = {
constructor: 'mySet',
add: function(data) {
if(this.dataStore.indexOf(data)>-1) {
return false
} else {
this.dataStore.push(data)
}
},
remove: function(data) {
var index = this.dataStore.indexOf(data)
this.dataStore.splice(index, 1)
},
clear: function() {
this.dataStore = []
},
show: function() {
console.log(this.dataStore)
},
contains: function(data) {
if(this.dataStore.indexOf(data)>-1) {
return true
} else {
return false
}
},
union: function(set) {
console.log(Object.getPrototypeOf(set).constructor )
if(Object.getPrototypeOf(set).constructor == 'mySet') {
set.dataStore.forEach(item => {
this.add(item)
});
} else {
return false
}
},
intersect: function(set) {
console.log(Object.getPrototypeOf(set).constructor )
if(Object.getPrototypeOf(set).constructor == 'mySet') {
var tempSet = new mySet()
for(let i= 0; i< set.dataStore.length; i++) {
var item = set.dataStore[i]
if(this.contains(item)) {
tempSet.add(item)
}
}
}
return tempSet
},
subSet: function(set) {
// console.log(Object.getPrototypeOf(set).constructor )
let result = true
if(Object.getPrototypeOf(set).constructor == 'mySet') {
if(set.dataStore.length < this.dataStore.length) {
set.dataStore.forEach(item => {
console.log(item)
if(!this.contains(item)) {
result = false
}
})
} else {
result = false
}
}
return result
}
}
var eg = new mySet()
eg.add('KKKK')
eg.add('ttttt')
var othereg = new mySet()
othereg.add('other')
othereg.add('KKKK')
var subset = new mySet()
subset.add('KKKK')