aggregate

aggregate (set) It's a data structure with different elements .

The elements in a collection are called members .

Two of the most important properties of sets are : First , The members in the set are unordered ; secondly , The same member is not allowed in the collection .

code

function Set(){
var me = this;
me.dataStore = [];
me.add = add;
me.remove =remove;
me.size = size;
me.union = union;
me.intersect = intersect;
me.subset = subset;
me.difference = difference;
me.show = show;
me.contains = contains; function add(data){
if(me.dataStore.indexOf(data)<0){
me.dataStore.push(data)
me.dataStore.sort();
return true;
}else{
return false;
}
}
function remove(data){
var pos = me.dataStore.indexOf(data);
if(pos>-1){
me.dataStore.splice(pos,1)
return true;
}else{
return false;
} } /**
* Merge sets
* @param set
*/
function union(set){
var tempSet = new Set();
for(var i=0;i<me.dataStore.length;i++){
tempSet.add(me.dataStore[i])
}
for(var i=0;i<set.dataStore.length;i++){
if(!tempSet.contains(set.dataStore[i])){
tempSet.add(set.dataStore[i])
}
}
return tempSet;
} /**
* Looking for intersection
*/
function intersect(set){
var tempSet = new Set();
for(var i=0;i<me.dataStore.length;i++){
if(set.contains(me.dataStore[i])){
tempSet.add(me.dataStore[i])
}
}
return tempSet;
} /**
* Determine whether a subset
* @param set
* @returns {boolean}
*/
function subset(set){
if(me.size()>set.size()) {
return false;
}else{
for(var i in me.dataStore){
if(!(set.contains(me.dataStore[i]))){
return false;
}
} }
return true; } /**
* Compare 2 The set is different And back to
* @param set
* @returns {Set}
*/
function difference(set){
var tempSet = new Set();
for(var i=0;i<me.dataStore.length;i++){
if(!set.contains(me.dataStore[i])){
tempSet.add(me.dataStore[i])
}
}
return tempSet;
} /**
* Gets the length of the set
* @returns {Number}
*/
function size(){
return me.dataStore.length;
}
/**
* Show the collection
* @returns {Array}
*/
function show(){
console.log(me.dataStore)
return me.dataStore;
} /**
* Check whether the member belongs to the collection
* @param data
* @returns {boolean}
*/
function contains(data){
if(me.dataStore.indexOf(data)>-1){
return true;
}else{
return false;
}
}
}

tests

// Set test 
var names = new Set();
names.add("David");
names.add("Jennifer");
names.add("Cynthia");
names.add("Mike");
names.add("Raymond");
//show test
names.show();
//remove test
names.remove("David")
names.show() //union Merge sets test
var cis = new Set();
cis.add("Mike");
cis.add("Clayton");
cis.add("Jennifer");
cis.add("Raymond");
var dmp = new Set();
dmp.add("Raymond");
dmp.add("Cynthia");
dmp.add("Jonathan"); var it = cis.union(dmp);
it.show() // Intersection test
var inter = cis.intersect(dmp)
inter.show(); // Determine whether a subset
var it2 = new Set();
it2.add("Cynthia");
it2.add("Clayton");
it2.add("Jennifer");
it2.add("Danny");
it2.add("Jonathan");
it2.add("Terrill");
it2.add("Raymond");
it2.add("Mike");
var dmp2 = new Set();
dmp2.add("Cynthia");
dmp2.add("Raymond");
dmp2.add("Jonathan");
console.log("dmp2 Whether it is it2 A subset of :"+dmp2.subset(it2)) // Make a difference var d = it2.difference(dmp2)
d.show();

Implementation of set -- Data structure and algorithm javascript describe More related articles in Chapter 9

  1. Search algorithm -- Data structure and algorithm javascript describe The first 13 Chapter

    Search algorithm - How to find a specific value in the list . In order to find Starting from the first element of the list, judge the list elements one by one , Until we find the result we want , It's one of the violent search techniques , When you perform a lookup, you may access all the elements in the data structure . Code : / ...

  2. Implementation of hash table -- Data structure and algorithm javascript describe Chapter viii.

    Hash table ( Hashtable Hash is a common data storage technology , Hashed data can be quickly inserted or accessed . Hash table needs a hash value (key) To store the specified data , Data collection also depends on this . Hash values can depend on the value of the calculated data ASCII Code to get , But this ...

  3. Sorting algorithm -- Data structure and algorithm javascript describe The first 12 Chapter

    Sorting is a common feature , Given a set of data , Sort it . Before that , We need to prepare a basic job -- Automatically generate arrays , And can do any processing to this group of data . /** * Test class , Array * @param numElements * ...

  4. The realization of binary tree -- Data structure and algorithm javascript describe Chapter ten

    /** * Trees , A nonlinear data structure . Storing data in a hierarchical way . * The top node of a tree becomes the root node , If there are multiple nodes under a node , This node is called the parent node , The following nodes are called child nodes * Nodes without any children , Roland ...

  5. Dictionaries -- Data structure and algorithm javascript describe Chapter vii.

    Dictionaries A dictionary is a kind of dictionary - A data structure that stores data in the form of value pairs The most basic function planning add Add data to dictionary remove Remove data from Dictionary get Take data out of the dictionary count Statistics dictionary data volume find Look up the data in the dictionary ...

  6. The realization of linked list -- Data structure and algorithm javascript describe Chapter six

    Linked list A linked list is a set of nodes . Each node uses a reference to an object to point to its successors . A reference to another node is called a chain Structural sketch : The chain header needs our identification head { element:head,next:obj1 ...

  7. Implementation of queues -- Data structure and algorithm javascript describe The fifth chapter

    Queues are also a kind of lists , There are rules different from lists . fifo The method of entrance A team approach You can find the team leader You can find the tail of the team You can see how long the queue is You can see if the queue is empty It's a basic need , Around him to achieve , Of course, we can expand the list ourselves ...

  8. The realization of the stack -- Data structure and algorithm javascript describe Chapter four

    Stack :last-in-first-out The stack has its own special rules , Can only After entering the element , First to be pushed out , We just need to simulate this rule , Just implement this rule . peek Is to return the top of the stack element ( The last one to enter ). /** * Stack ...

  9. The least positive subsequence ( The sum of sequences is the smallest , At the same time, the sum value should be minimum )( Data structure and algorithm analysis ——C Language description chapter two exercises 2.12 Second questions )

    #include "stdio.h" #include "stdlib.h" #define random(x) (rand()%x) void creat_a ...

Random recommendation

  1. IIS FAQ records

    win7 iis7.5 Detailed error information module IIS Web Core notice BeginRequest The handler It has not been determined Error code 0x80070021 Configuration error This configuration section cannot be used in this path . If at the parent level ...

  2. How to be in Crystal Portlet Return correctly in JSON Data to AJAX request ?

    When Crystal Portlet We need to use Ajax request , And let the background go back Json Data time , How to be right . Easy return Json Data? ? Both of the following methods can be used : Method 1 :Ajax When asked , use RenderURL, Corresponding P ...

  3. ASIHttpRequest Abstract

    towards server End to end upload data ASIFormDataRequest , simulation Form Form submission , Its submission format is similar to Header I will identify myself . No files :application/x-www-form-urlen ...

  4. PXE+kickstart Automatic installation ubuntu14.04

    This article refers to many articles , First of all, thank the authors of these articles . Use pxe The installation system needs to be installed dhcp,tftp,http Etc ( Of course, you can also use other file sharing methods, such as nfs,ftp). Experimental environment : 1. vmware 12 2 ...

  5. as3 Component definition

    package kingBook{ import flash.display.MovieClip; import flash.events.Event; import flash.utils.setT ...

  6. Object creation process (chapter5.7.3)

    Summarize the process of creating objects , Let's say I have a name Dog Class : 1. Even if you don't use static keyword , Constructors are actually static methods , therefore , When first created, the type is Dog The object is ( Constructors can be seen as static methods ), perhaps Dog class ...

  7. ANDROID Window management service implementation mechanism and architecture analysis

     One . function Window management is ANDROID An important part of the framework , It mainly includes the following functions : )Z-ordered The maintenance of the ) Window creation . The destruction ) Window drawing . Layout )Token management ,AppToken ) Active window management (F ...

  8. clone Rockey6 Dongles copy data

    clone Rockey6 Dongle copy data download describe :Rockey6 Dongle replication cloning method Rockey6 Case analysis of dongle replication ! One . use OD load DLL, And analyze : 10001320 >/$ B8 4C140000 ...

  9. 20165231 2017-2018-2 《Java Programming 》 The first 2 Weekly learning summary

    Preface The second week is a formal study java Programming . Previous pair java It's a blank , Now it's the first sign , got it java The basic beginning of the program , Multiple class Which one to run when , Which is the output print statement and so on . Currently I use java ...

  10. android layout File optimization

    performance optimization 1--UI Optimize 1. The usage system provides us with several abstract tags ①include: reusing include in layout Property to specify an external layout file , In this way, you don't need to write the layout file repeatedly in the code . if ...