JavaScript SET Data Structures

Source: Internet
Author: User

A collection (set) is a set of unordered sets of data that have a certain relevance to each other. Each member can appear only once in the array.

It's a good idea to understand the content before using the set (set):

1. A collection that does not contain any members is called an empty collection.

2, if the members of the two collection are equal, the two sets are equal.

3. If the members in one collection are in another collection, then two have a relationship with the parent-child set.

In the collection we commonly use the operation is: to find the set of the set, intersection, complement set and so on.

Here we build a collection (set) class based on the array.

functionSet () { This. DataStore = []; //adding elements    This. Add =function(data) {if( This. Datastore.indexof (data) < 0) {               This. Datastore.push (data); return true; }         Else {              return false;   }   }; //Delete Element    This. remove =function (){        varpos = This. Datastore.indexof (data); if(Pos >-1) {             This. Datastore.splice (pos,1); return true; }        Else {            return false;   }   };  This. Size =function (){        return  This. Datastore.length;   }; //ask for a set of two sets    This. Union =function(set) {varTempset =NewSet ();  for(vari = 0; I < This. datastore.length; ++i) {Tempset.add ( This. Datastore[i]); }         for(vari = 0; i < set.dataStore.length; ++i) {if(!tempset.contains (Set.datastore[i]))            {TempSet.dataStore.push (set.datastore[i]); }        }        returnTempset;  }; //query Whether the collection contains an element    This. contains =function(data{if( This. Datastore.indexof (data) >-1) {          return true; }      Else {         return false;   }   }; //intersection of two sets    This. intersect =function(set) {varTempset =NewSet ();  for(vari = 0; I < This. datastore.length; ++i) {if(Set.contains ( This. Datastore[i])) {Tempset.add ( This. Datastore[i]); }        }        returnTempset;   }; //query set is not a subset of the collection now    This. subset =function(set) {if( This. Size () >set.size ()) {           return false; }        Else {            foreach (varMemberinch  This. DataStore) {              if(!Set.contains (member)) {                 return false; }           }        }       return true;   }; //complement of two sets    This. Difference =function (){        varTempset =NewSet ();  for(vari = 0; I < This. datastore.length; ++i) {if(!set.contains ( This. Datastore[i])) {Tempset.add ( This. Datastore[i]); }       }       returnTempset;   };  This. Show =function (){       return"[" + This. DataStore + "]"; };}</script></body>

JavaScript SET Data Structures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.