集合 -- es5實現

weixin_33807284發表於2018-06-21

集合是由一組無序且唯一(不能重複的)的項組成,這個資料結構與有限集合相同的數學概念,但應用在電腦科學的資料結構中。
es6中set類:ES6 提供了新的資料結構 Set。它類似於陣列,但是成員的值都是唯一的,沒有重複的值.

1,es5的方法實現es6的方法

es6-set 阮一峰

function mySet(){
  let items = {};
  this.size = function(){
    return Object.keys(items).length
  }
  this.has = function(value){
    items.hasOwnProperty(value)
  }
  this.add = function (value){
    if(!this.has[value]){
      items[value] = value
      return true 
    } 
      return false
  }
  this.keys = function(){
    return Object.keys(items)
  }
  this.values = function(){
    return Object.values(items)
  }
  this.clear = function(){
    items = {}
  }
  this.delete = function(value){
    if(this.has(value)){
      delete items[value]
      return true
    }
      return false
  }
}

2, 集合的操作

6036420-b2c0681ee385d8ee.png
集合操作圖解.png

2.1 並集
mySet增加一個方法

this.union = function(B){
    let unionSet = new mySet()
    let values = this.values()
    for(let i=0;i<values.length;i++){
      unionSet.add(values[i])
    }
    values = B.values()
    for(let i=0;i<values.length;i++){
      unionSet.add(values[i])
    }
    return unionSet  
  }

2.2交集

 this.overlap=function(B){
  let overlapSet = new mySet()
  let values = this.values() 
  let overlapArr = values.filter(v=>B.has(v))
  
  for(let i=0;i<overlapArr.length;i++){
    overlapSet.add(overlapArr[i])
  }
    return overlapSet
}

2.3差集

 this.diffSet=function(B){
  let diffSet = new mySet()
  let values = this.values()
  for(let i =0;i<values.length;i++){
    if(!B.has(values[i])){
       diffSet.add(values[i])   
    }
  }
   return diffSet
  }

結果如圖

6036420-021d0aa18e140ac0.png
image.png

2.4子集

 this.subset=function(B){
    if(this.size()>B.size()){
      return false
    }
    let values = this.values
    for(let i=0;i<values.length;i++){
      if(!B.has(values[i])){
        return false
      }
    }
    return true
  }

3 所有程式碼

function mySet(){
  let items = {};
  this.size = function(){
    return Object.keys(items).length
  }
  this.has = function(value){
    return items.hasOwnProperty(value)
  }
  this.add = function (value){
    if(!this.has[value]){
      items[value] = value
      return true 
    } 
      return false
  }
  this.keys = function(){
    return Object.keys(items)
  }
  this.values = function(){
    return Object.values(items)
  }
  this.clear = function(){
    items = {}
  }
  this.delete = function(value){
    if(this.has(value)){
      delete items[value]
      return true
    }
      return false
  }
  this.union = function(B){
    let unionSet = new mySet()
    let values = this.values()
    for(let i=0;i<values.length;i++){
      unionSet.add(values[i])
    }
    values = B.values()
    for(let i=0;i<values.length;i++){
      unionSet.add(values[i])
    }
    return unionSet  
  }
  this.overlap=function(B){
  let overlapSet = new mySet()
  let values = this.values() 
  let overlapArr = values.filter(v=>B.has(v))
  
  for(let i=0;i<overlapArr.length;i++){
    overlapSet.add(overlapArr[i])
  }
    return overlapSet
}
  this.diffSet=function(B){
  let diffSet = new mySet()
  let values = this.values()
  for(let i =0;i<values.length;i++){
    if(!B.has(values[i])){
       diffSet.add(values[i])   
    }
  }
   return diffSet
  }
  this.subset=function(B){
    if(this.size()>B.size()){
      return false
    }
    let values = this.values
    for(let i=0;i<values.length;i++){
      if(!B.has(values[i])){
        return false
      }
    }
    return true
  }
}

相關文章