javascript Set data structures
集合(set)是一组无序的,但彼此之间又有一定相关性的数据集。每个成员在数组中只能出现一次。
在使用集合(set)之前最好先理解一下内容:
1、不包含任何成员的集合称为空集合。
2、如果两个集合的成员都相等,两个集合相等。
3、如果一个集合中的成员都在另一个集合中,则两个具有父子集的关系。
在集合中我们常用的操作就是:求集合的并集,交集,补集等。
下面我们基于数组该构建一个集合(Set)类。
<html>
<head>
<title>Date Example</title>
</head>
<body>
<div id="content"></div>
<input type="button" value="Set InnerHTML" onclick="testDate()"> <script type="text/javascript"> function Set() {
this.dataStore = []; // 添加元素
this.add = function (data){
if (this.dataStore.indexOf(data) < 0) {
this.dataStore.push(data);
return true;
}
else {
return false;
}
}; // 删除元素
this.remove = function (){
var pos = this.dataStore.indexOf(data);
if (pos > -1) {
this.dataStore.splice(pos,1);
return true;
}
else {
return false;
}
}; this.size = function (){
return this.dataStore.length;
}; // 求两个集合的并集
this.union = function (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;
}; // 查询集合中是否包含某个元素
this.contains = function (data{
if (this.dataStore.indexOf(data) > -1) {
return true;
}
else {
return false;
}
}; // 两个集合的交集
this.intersect = function (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是不是现在集合的子集
this.subset = function (set){
if (this.size() > set.size()) {
return false;
}
else {
for each (var member in this.dataStore) {
if (!set.contains(member)) {
return false;
}
}
}
return true;
}; // 两个集合的补集
this.difference = function (){
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;
};
this.show = function (){
return "[" + this.dataStore + "]";
};
} </script>
</body>
</html>
javascript Set data structures的更多相关文章
- javascript Dictionary data structures
Dictionary常被称为数据字典,是一种用来保存键值对的数据结构,比如我们日常的电话本,就是一个Dictionary.我们通过键(名字),就可以访问到对应的值(电话号码).在C++与java中我们 ...
- javascript linkedlist data structures
在使用C++的时候我们经常会使用到各种容器,这些容器其实就是一种数据结构.在java中其实也是如此.但是由于javascript只给我们提供了一种内置的数据结构数组,准备来说是对象.没有我们常见的那些 ...
- JavaScript data types and data structures
JavaScript data types and data structures Programming languages all have built-in data structures, b ...
- [Javascript] Avoiding Mutations in JavaScript with Immutable Data Structures
To demonstrate the difference between mutability and immutability, imagine taking a drink from a gla ...
- Go Data Structures: Interfaces
refer:http://research.swtch.com/interfaces Go Data Structures: Interfaces Posted on Tuesday, Decembe ...
- A library of generic data structures
A library of generic data structures including a list, array, hashtable, deque etc.. https://github. ...
- The Swiss Army Knife of Data Structures … in C#
"I worked up a full implementation as well but I decided that it was too complicated to post in ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- Persistent Data Structures
原文链接:http://www.codeproject.com/Articles/9680/Persistent-Data-Structures Introduction When you hear ...
随机推荐
- Linux上磁盘挂载
Linux磁盘挂载 一. 磁盘分区 在终端输入fdisk –l 命令查看整个系统的分区情况. 能够看到另一个32G的/dev/vdb磁盘没有挂载使用 watermark/2/text/aHR0c ...
- BZOJ 1264 AHOI2006 基因匹配Match 动态规划+树状数组
题目大意:给定n个数和两个长度为n*5的序列,每一个数恰好出现5次,求两个序列的LCS n<=20000.序列长度就是10W.朴素的O(n^2)一定会超时 所以我们考虑LCS的一些性质 LCS的 ...
- 表结构变更后出现的ERROR OGG-01161 Bad column index (88)
2014-07-31 09:38:31 ERROR OGG-01668 PROCESS ABENDING. 2014-07-31 09:38:31 ERROR OGG-01161 Bad column ...
- git 使用流程(使用代码库github)
一:先在github 上注册账号,并创建一个项目: 二:mac 命令行-进入自己的工作空间 1:建立库 git init 2:初始化配置 git config --global user.na ...
- The jQuery HTML5 Audio / Video Library (jQuery jPlayer插件给你的站点增加视频和音频功能)
http://jplayer.org/ The jQuery HTML5 Audio / Video Library jPlayer is the completely free and open s ...
- 算法笔记_207:第五届蓝桥杯软件类决赛部分真题(Java语言C组)
目录 1 数字拆分 2 稍大的串 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 数字拆分 正整数可以表示为若干正整数的累加和. 如,对于正整数n=6,可以分划为: 6 5+1 4+2 4+1+ ...
- Junit和Spring
@ContextConfiguration 用来指定加载的Spring配置文件的位置,会加载默认配置文件 例如下例会加载:classpath:/com/example/MyTest-context.x ...
- Ubuntu系统安装VMware Tools的简单方法
不少网友反映在VMWare虚拟机下安装Ubuntu系统后无法安装VMware Tools,这里给出一个简单方法,只需要几步即可解决. 第一步:进入系统后,点击虚拟机上的安装vmware tools,回 ...
- OpenERP Client Error
SyntaxError: JSON.parse: unexpected end of data http/mydomain:8069/web/webclient/js:23114 Solutions: ...
- MVC3中给Html.TextAreaFor设置默认值(初始值)
<div class="editor-field"> @Html.TextAreaFor(model => model.Comments) @Html.Valid ...