Javascript hash table class definition

Source: Internet
Author: User
// Hash. JS // defines the Javascript class var hash = function (h) {This. _ DATA = new object ();} function hash $ clear () {delete this. _ data; this. _ DATA = new object ();} function hash $ add (Key, value) {If (! Key | typeof (value) ==='undefined') return false; this. _ DATA [Key] = {key: Key, value: Value}; return true;} function hash $ addrange (h) {for (var key in H. _ DATA) {var item = H. _ DATA [Key]; If (typeof (item )! = 'Undefined') {This. Add (item. Key, item. Value) ;}} function hash $ remove (key) {If (! Key) return undefined; var item = This. _ DATA [Key]; delete this. _ DATA [Key]; return item;} function hash $ removeat (INDEX) {If (isnan (INDEX) return undefined; var I = 0; For (var key in this. _ DATA) {if (I = index) {return this. remove (key) ;} I ++;} return undefined;} function hash $ removerange (startindex, endindex) {If (isnan (startindex) | isnan (endindex )) return undefined; var I = 0; var H = new H Ash (); For (var key in this. _ DATA) {if (I> = startindex & I <= endindex) {H. add (key, this. remove (key ). value);} I ++;} return h;} function hash $ getcount () {var I = 0; For (var key in this. _ DATA) I ++; return I;} function hash $ foreach (method, instance) {var I = 0; For (var key in this. _ DATA) {var item = This. _ DATA [Key]; If (typeof (item )! = 'Undefined') {method. call (instance, item, I, this); I ++;} else {delete this. _ DATA [Key] ;}} function hash $ getkeys () {var arr = new array (); For (var key in this. _ DATA) {var item = This. _ DATA [Key]; arr. push (item. key);} return arr;} function hash $ getvalues () {var arr = new array (); For (var key in this. _ DATA) {var item = This. _ DATA [Key]; arr. push (item. value);} return arr;} function hash $ get Item (key) {If (! Key) return undefined; var item = This. _ DATA [Key]; If (typeof (item )! = 'Undefined') {return item;} else {delete this. _ DATA [Key]; return undefined;} function hash $ containskey (key) {If (typeof (this. getitem (key ))! = 'Undefined') {return true;} return false;} function hash $ containsvalue (value) {for (var key in this. _ DATA) {If (value = This. _ DATA [Key]. value) {return true ;}} return false;} // the newly added array automatically calculates the number of keys. Function hash $ addarray (s) {// count var Vc = 0; var item = NULL; For (VAR I = 0; I <S. length; I ++) {If (this. containskey (s [I]) = false) {This. add (s [I], 1);} else {item = This. getitem (s [I]); Vc = item. value; VC ++ = 1; this. remove (s [I]); this. add (s [I], VC) ;}}// obtain the display test information of the Key = (value) pair function hash $ gethashkeyvalueinfo () {// display var INF = ''; var keyarray = This. getkeys (); var valarray = This. getvalues (); For (VAR I = 0; I <keyarray. length; I ++) {INF + = keyarray [I] + '= (' + valarray [I] + "),";} return INF ;} // obtain the function hash $ getvalue (key) {var item = This. getitem (key); var Vc = item. value; return VC;} // The hash defined by the prototype. prototype = {_ DATA: NULL, _ keys: NULL, clear: Hash $ clear, add: Hash $ add, addrange: Hash $ addrange, remove: Hash $ remove, removeat: hash $ removeat, removerange: Hash $ removerange, getcount: Hash $ getcount, foreach: Hash $ foreach, getkeys: Hash $ getkeys, getvalues: Hash $ getvalues, getitem: Hash $ getitem, containskey: Hash $ containskey, containsvalue: Hash $ containsvalue, addarray: Hash $ addarray, gethashkeyvalueinfo: Hash $ gethashkeyvalueinfo, getvalue: Hash $ getvalue} hash. _ typename = 'hash'; hash. _ class = true;

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.