首页 > Web开发 > 详细

set.js

时间:2017-04-01 10:38:38      阅读:201      评论:0      收藏:0      [点我收藏+]
function Set() {
	this.dataStore = [];
	this.add = add;
	this.remove = remove;
	this.size = size;
	this.union = union;
	this.intersect = intersect;
	this.subset = subset;
	this.difference = difference;
	this.show = show;
}

function add(data) {
	if (this.dataStore.indexOf(data) < 0) {
		this.dataStore.push(data);
		return true;
	}
	else {
		return false;
	}
}

function remove(data) {
	var pos = this.dataStore.indexOf(data);
	if (pos > -1) {
		this.dataStore.splice(pos,1);
		return true;
	}
	else {
		return false;
	}
}

function show() {
	return this.dataStore;
}

function contains(data) {
	if (this.dataStore.indexOf(data) > -1) {
	return true;
	}
	else {
		return false;
	}
}

function union(set) {
	var tempSet = new Set();
	for (var i = 0; i < this.dataStore.length; ++i) {
		tempSet.add(this.dataStore[i]);
	}
	for (var i = 0; i < set.dataStore.length; ++i) {
		if (!tempSet.contains(set.dataStore[i])) {
			tempSet.dataStore.push(set.dataStore[i]);
		}
	}
	return tempSet;
}

function intersect(set) {
	var tempSet = new Set();
	for (var i = 0; i < this.dataStore.length; ++i) {
		if (set.contains(this.dataStore[i])) {
			tempSet.add(this.dataStore[i]);
		}
	}
	return tempSet;
}

function subset(set) {
	if (this.size() > set.size()) {
		return false;
	}
	else {
		// foreach (var member in this.dataStore) {
		// 	if (!set.contains(member)) {
		// 		return false;
		// 	}
		// }
	}
	return true;
}

function size() {
	return this.dataStore.length;
}

function difference(set) {
	var tempSet = new Set();
	for (var i = 0; i < this.dataStore.length; ++i) {
		if (!set.contains(this.dataStore[i])) {
			tempSet.add(this.dataStore[i]);
		}
	}
	return tempSet;
}

  

set.js

原文:http://www.cnblogs.com/carlyin/p/6654822.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!