shawl.qiu Javascript 哈希表类 / HashTable v1.0
说明:
HashTable 就是键值对数据类型,
表键可以通过关键字访问,
不过本类可以通过索引访问和关键字访问.
一般哈希表键值一经定义就不能更改, 不过本类是模拟的, 因此可以更改原有数据项.
本类另外增加了子键功能, 在添加项方法 Add() 中添加, 详细请看调用演示.
目录:
1. 属性及方法说明
2. 调用演示
3. HashTable 类
shawl.qiu
2007-06-28
http://blog.csdn.net/btbtd
演示: http://files.myopera.com/btbtd/javascript/class/hashtable/HashTable.htm
下载: http://files.myopera.com/btbtd/javascript/class/hashtable/sq_js_HashTable_v1.0.7z
内容:
1. 属性及方法说明
- 属性:
- .Items 为项集合
- .Keys 为键集合
- .Values 为值集合
- .Object 为类内部键访问
- .Index 为当前索引位置
- 方法:
- .Item() 为访问表键方法
- .Add() 为添加项方法
- .First() 为移动索引位置到首项方法
- .Last() 为移动索引位置到末项方法
- .Previous() 为移动索引位置到上一项方法
- .Next() 为移动索引位置到下一项方法
- .Clear() 为清除表数据方法
- .Remove() 为清除项方法
- .Count() 为计算所有项方法
- .IsEmpty() 为判断是否为空方法
- .ContainsKey() 为判断是否已有某项名方法
- .ContainsValue() 为判断是否已有某项值方法
- .ToString() 为输出所有名值对为字串方法
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <!-- DW6 -->
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <title>Untitled Document</title>
- <style type="text/css">
- /* <![CDATA[ */
- /* ]]> */
- </style>
- <script type="text/javascript" src="HashTable.js"></script>
- <script type="text/javascript">
- //<![CDATA[
- function fSetItem(Hb, iLen, iStart, bSubKey)
- {
- if(!iLen) iLen = 5;
- if(!iStart) iStart = 65;
- for(var i=0; i<iLen; i++)
- {
- var iCur = iStart+i;
- var sKey = String.fromCharCode(iCur);
- //function fPut(sKey, Value, SubKeyObj, iIndex/*default: Items.length*/)
- if(bSubKey)
- {
- Hb.Add(sKey, sKey+" value", {NameEn:sKey+" test", NameZh:sKey+" 测试"});
- }
- else
- {
- Hb.Add(sKey, sKey+" value");
- }
- }
- } // end function fSetItem;
- function fForNextListHashTable(Hb, bSubKey)
- {
- document.write
- (
- "<h2>Item HashTable Key Item with ",
- ("fForNextListHashTable ").fontcolor("blue")+": </h2>"
- );
- for(var i=0, j = Hb.Keys.length; i<j; i++)
- {
- document.write("<li/>Hb.Item("+i+"): "+Hb.Item(i));
- if(bSubKey)
- {
- document.write('<li/>Hb.Item('+i+')["NameEn"]: '+Hb.Item(i)["NameEn"]);
- document.write('<li/>Hb.Item('+i+')["NameZh"]: '+Hb.Item(i)["NameZh"]);
- document.write("<p></p><p></p>");
- }
- document.write("<p></p><p></p>");
- document.write("<hr/>");
- }
- } // end function fListSqObject
- function fForInListHashTable(Hb, bSubKey)
- {
- document.write
- (
- "<h2>Item HashTable Key Item with ",
- ("fForInListHashTable ").fontcolor("blue")+": </h2>"
- );
- for(var i in Hb.Object)
- {
- document.write("<li/>Hb.Item("+i+"): "+Hb.Item(i));
- if(bSubKey)
- {
- document.write('<li/>Hb.Item('+i+')["NameEn"]: '+Hb.Item(i)["NameEn"]);
- document.write('<li/>Hb.Item('+i+')["NameZh"]: '+Hb.Item(i)["NameZh"]);
- }
- document.write("<p></p><p></p>");
- document.write("<hr/>");
- }
- } // end function fListSqObject
- function fListHashTableWithKey(Hb, Key, sBy)
- {
- if(!sBy) sBy = "fListHashTableWithKey";
- document.write
- (
- "<h2>Item HashTable Key Item with ",
- (sBy).fontcolor("blue")+": </h2>"
- );
- document.write("<li/>Hb.Item("+Key+"): "+Hb.Item(Key));
- document.write('<li/>Hb.Item('+Key+')["NameEn"]: '+Hb.Item(Key)["NameEn"]);
- document.write('<li/>Hb.Item('+Key+')["NameZh"]: '+Hb.Item(Key)["NameZh"]);
- document.write("<p></p><p></p>");
- document.write("<p></p><p></p>");
- document.write("<hr/>");
- } // end function fListSqObject
- var Hb = new HashTable()
- document.write("<h1>Has Subkey</h1><hr/><hr/>");
- fSetItem(Hb, 5, 65, true);
- fForNextListHashTable(Hb, true);
- fForInListHashTable(Hb, true);
- fListHashTableWithKey(Hb, false, 'fListHashTableWithKey(Hb)');;
- fListHashTableWithKey(Hb, 0, 'fListHashTableWithKey(Hb, 0)');;
- fListHashTableWithKey(Hb, "B", 'fListHashTableWithKey(Hb, "B")');
- Hb.First();
- fListHashTableWithKey(Hb, false, "First()");
- Hb.Last();
- fListHashTableWithKey(Hb, false, "Last()");
- while(Hb.Previous())
- {
- fListHashTableWithKey(Hb, false, "Previous()");
- }
- while(Hb.Next())
- {
- fListHashTableWithKey(Hb, false, "Next()");
- }
- document.write("<h2>By Clear()</h2>");
- Hb.Clear();
- document.write("<li/>Hb.Keys.length: "+Hb.Keys.length);
- document.write("<br/><br/><br/><h1>No Subkey</h1><hr/><hr/>");
- fSetItem(Hb, false, 97, false);
- fForNextListHashTable(Hb, false);
- fForInListHashTable(Hb, false);
- Hb.First();
- document.write("<h2>By Remove()</h2>");
- Hb.Remove();
- document.write("<li/>Hb.Index: "+Hb.Index);
- fForInListHashTable(Hb, false);
- document.write("<h2>By Remove(1)</h2>");
- Hb.Remove(1);
- document.write("<li/>Hb.Index: "+Hb.Index);
- fForInListHashTable(Hb, false);
- document.write('<h2>By Remove("c")</h2>');
- Hb.Remove("c");
- document.write("<li/>Hb.Index: "+Hb.Index);
- fForInListHashTable(Hb, false);
- document.write('<h2>By Remove("d")</h2>');
- Hb.Remove("d");
- document.write("<li/>Hb.Index: "+Hb.Index);
- fForInListHashTable(Hb, false);
- document.write("<h2>By Count()</h2>");
- document.write("<li/>Hb.Count(): "+Hb.Count());
- document.write("<h2>By IsEmpty()</h2>");
- document.write("<li/>Hb.IsEmpty(): "+Hb.IsEmpty());
- document.write("<h2>By ContainsKey()</h2>");
- document.write("<li/>Hb.ContainsKey(): "+Hb.ContainsKey());
- document.write("<h2>By ContainsKey('hello')</h2>");
- document.write("<li/>Hb.ContainsKey('hello'): "+Hb.ContainsKey('hello'));
- document.write("<h2>By ContainsKey('e')</h2>");
- document.write("<li/>Hb.ContainsKey('e'): "+Hb.ContainsKey('e'));
- document.write("<h2>By ContainsValue('hello')</h2>");
- document.write("<li/>Hb.ContainsValue('hello'): "+Hb.ContainsValue('hello'));
- document.write("<h2>By ContainsValue('e value')</h2>");
- document.write("<li/>Hb.ContainsValue('e value'): "+Hb.ContainsValue('e value'));
- document.write("<h2>By ToString()</h2>");
- document.write("<li/>Hb.ToString(): <pre>"+Hb.ToString()+"</pre>");
- document.write("<h2>By ToString('##', '##@')</h2>");
- document.write("<li/>Hb.ToString('##', '##@'): <pre>"+Hb.ToString('##', '##@')+"</pre>");
- document.write("<h2>By Keys</h2>");
- document.write("<li/>Hb.Keys: "+Hb.Keys);
- document.write("<h2>By Values</h2>");
- document.write("<li/>Hb.Values: "+Hb.Values);
- document.title = Hb.Au.Subject +" "+ Hb.Au.Version;
- Hb = null;
- //]]>
- </script>
- </head>
- <body>
- </body>
- </html>
- /*-----------------------------------------------------------------------------------*/
- * shawl.qiu Javascript HashTable class v1.0
- /*-----------------------------------------------------------------------------------*/
- //---------------------------------begin class HashTable()-------------------------------//
- function HashTable()
- { // shawl.qiu code
- //------------------------------------begin public variable
- //---------------begin about
- this.Au = {}
- this.Au.Subject = "shawl.qiu Javascript HashTable class";
- this.Au.Version = "v1.0";
- this.Au.Name = "shawl.qiu";
- this.Au.Email = "shawl.qiu@gmail.com";
- this.Au.Blog = "http://blog.csdn.net/btbtd";
- this.Au.CreatedDate = "2007-6-27";
- this.Au.Update = {};
- this.Au.Update["1"] = "";
- //---------------end about
- this.Items = [];
- this.Keys = [];
- this.Values = [];
- this.Object = {};
- this.Index = null; // Number
- //------------------------------------end public variable
- //------------------------------------begin private variable
- var Tl = this;
- //------------------------------------end private variable
- //------------------------------------begin public method
- this.Item = fItem;
- this.Add = fAdd;
- this.First = fFirst;
- this.Last = fLast;
- this.Previous = fPrevious;
- this.Next = fNext;
- this.Clear = fClear;
- this.Remove = fRemove;
- this.Count = fCount;
- this.IsEmpty = fIsEmpty;
- this.ContainsKey = fContainsKey;
- this.ContainsValue = fContainsValue;
- this.ToString = fToString;
- //------------------------------------end public method
- //------------------------------------begin private method
- function fToString(sColumnDilimiter, sRowDilimter)
- {
- if(!sColumnDilimiter) sColumnDilimiter = ",";
- if(!sRowDilimter) sRowDilimter = "/n";
- var iCount=0;
- var iLen = Tl.Keys.length;
- var sTemp = "";
- while(iCount<iLen)
- {
- sTemp += Tl.Items[iCount]["Key"]+sColumnDilimiter+Tl.Items[iCount]+sRowDilimter;
- iCount++;
- }
- return sTemp;
- } // end function fToString
- function fContainsValue(Value)
- {
- var iCount=0;
- var iLen = Tl.Keys.length;
- while(iCount<iLen)
- {
- if(Tl.Items[iCount] == Value) return true;
- iCount++;
- }
- return false;
- } // end fContainsValue
- function fContainsKey(sKey)
- {
- if(typeof(sKey)=="undefined") return false;
- return (sKey in Tl.Object);
- } // end function fContainsKey
- function fIsEmpty()
- {
- return Tl.Keys.length===0?true:false;
- } // end fIsEmpty
- function fCount()
- {
- return Tl.Keys.length;
- } // end fCount
- function fRemove(Key)
- {
- var Debug = false;
- if(Key == null)
- {
- if(Tl.Keys.length<0||Tl.Index>0||Tl.Index>=Tl.Keys.length-1) return false;
- if(Tl.Index!=null)
- {
- if(Debug)
- {
- document.write("<li/>Tl.Index: "+Tl.Index);
- document.write("<li/>Tl.Keys[Tl.Index]: "+Tl.Keys[Tl.Index]);
- document.write("<li/>Tl.Object[Tl.Keys[Tl.Index]]: "+Tl.Object[Tl.Keys[Tl.Index]]);
- }
- delete Tl.Object[Tl.Keys[Tl.Index]];
- Tl.Items.splice(Tl.Index, 1);
- Tl.Keys.splice(Tl.Index, 1);
- Tl.Values.splice(Tl.Index, 1);
- if(Tl.Index===0&&Tl.Keys.length===0)
- {
- Tl.Index = null;
- }
- else if(Tl.Index===0&&Tl.Keys.length!==0)
- {
- Tl.Index = 0;
- }
- else if(Tl.Index!=null&&Tl.Index>0)
- {
- Tl.Index = Tl.Index-1;
- }
- for(var i=0, j=Tl.Keys.length; i<j; i++)
- {
- Tl.Object[Tl.Keys[i]] = Tl.Object[Tl.Keys[i]]-0-1;
- }
- fRemoveDebug(Debug);
- }
- return;
- }
- switch(Key.constructor)
- {
- case Number:
- if(Key>=0&&Key<Tl.Keys.length)
- {
- }
- else
- {
- return false;
- }
- Tl.Items.splice(Key, 1);
- delete Tl.Object[Tl.Keys[Key]];
- Tl.Keys.splice(Key, 1);
- Tl.Values.splice(Key, 1);
- if(Tl.Index===0) Tl.Index = null;
- else Tl.Index = Tl.Index-1;
- for(var i=Key, j=Tl.Keys.length; i<j; i++)
- {
- Tl.Object[Tl.Keys[i]] = Tl.Object[Tl.Keys[i]]-0-1;
- }
- fRemoveDebug(false);
- break;
- case String:
- if(Key in Tl.Object)
- {
- }
- else
- {
- return false;
- }
- Tl.Items.splice(Tl.Object[Key], 1);
- Tl.Keys.splice(Tl.Object[Key], 1);
- Tl.Values.splice(Tl.Object[Key], 1);
- for(var i=Tl.Object[Key], j=Tl.Keys.length; i<j; i++)
- {
- Tl.Object[Tl.Keys[i]] = Tl.Object[Tl.Keys[i]]-0-1;
- }
- delete Tl.Object[Key];
- if(Tl.Index===0) Tl.Index = null;
- else Tl.Index = Tl.Index-1;
- fRemoveDebug(false);
- break;
- default:
- }
- } // end function Remove
- function fRemoveDebug(bDebug)
- {
- if(!bDebug) return false;
- for(var i in Tl.Keys)
- {
- document.write("<li/>i: "+i);
- }
- document.write("<hr/>");
- for(var i in Tl.Items)
- {
- document.write("<li/>i: "+i);
- }
- document.write("<hr/>");
- for(var i in Tl.Object)
- {
- document.write("<li/>i: "+i);
- }
- document.write("<hr/>");
- for(var i in Tl.Object)
- {
- document.write("<li/>Tl.Object[i]: "+Tl.Object[i]);
- }
- document.write("<hr/>");
- } // end function fRemoveDebug
- function fClear()
- {
- Tl.Items = [];
- Tl.Keys = [];
- Tl.Values = [];
- Tl.Object = {};
- Tl.Index = null; // Number
- } // end function fClear
- function fNext()
- {
- if(Tl.Keys.length === 0||Tl.Index >= Tl.Keys.length-1) return false;
- Tl.Index = Tl.Index-0+1;
- return true;
- } // end function fNext
- function fPrevious()
- {
- if(Tl.Keys.length === 0||Tl.Index === 0) return false;
- Tl.Index = Tl.Index-0-1;
- return true;
- } // end function fPrevious
- function fLast()
- {
- if(Tl.Keys.length===0) return null;
- Tl.Index = Tl.Keys.length-1;
- } // end function fLast
- function fFirst()
- {
- if(Tl.Keys.length===0) return null;
- Tl.Index = 0;
- } // end function fFirst
- function fAdd(sKey, Value, SubKeyObj, iIndex/*default: Items.length*/)
- {
- if(typeof(sKey)=="undefined"||sKey=="")
- {
- alert("键不能为空!");
- return false;
- }
- sKey+="";
- if(sKey in Tl.Object)
- {
- Tl.Items[Tl.Object[sKey]] = fSetKey(sKey, Value, SubKeyObj);;
- return true;
- }
- var Ar = [];
- Ar[0] = Value;
- Ar["Key"] = sKey;
- if(SubKeyObj&&SubKeyObj.constructor==Object)
- {
- for(var i in SubKeyObj)
- {
- Ar[i+""] = SubKeyObj[i];
- }
- }
- if(typeof(iIndex)=="undefined") iIndex = Tl.Items.length;
- Tl.Index = iIndex;
- Tl.Keys[iIndex] = sKey;
- Tl.Values[iIndex] = Value;
- Tl.Items[iIndex] = fSetKey(sKey, Value, SubKeyObj);
- Tl.Object[sKey] = iIndex;
- } // end function fAdd
- function fItem(Key)
- {
- if(typeof(Key)=="undefined"||(Key.constructor==String&&Key==""))
- {
- return Tl.Items[Tl.Index];
- }
- if(/^[0-9]+$/.test(Key))
- {
- Key -= 0;
- }
- switch(Key.constructor)
- {
- case Number:
- Tl.Index = Key;
- return Tl.Items[Key];
- case String:
- Tl.Index = Tl.Object[Key];
- return Tl.Items[Tl.Object[Key]];
- default:
- return Tl.Items[Tl.Index];
- }
- } // end function fItem
- function fSetKey(sKey, Value, SubKeyObj)
- { // return Array;
- var Ar = [];
- Ar[0] = Value;
- Ar["Key"] = sKey;
- Ar["Name"] = sKey;
- if(SubKeyObj&&SubKeyObj.constructor==Object)
- {
- for(var i in SubKeyObj)
- {
- Ar[i+""] = SubKeyObj[i];
- }
- }
- return Ar;
- } // end function fSetKey
- //------------------------------------end private method
- } // shawl.qiu code
- //---------------------------------end class HashTable()---------------------------------//