shawl.qiu Javascript 哈希表类 / HashTable v1.0

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. 属性及方法说明
  1. 属性:
  2. .Items 为项集合
  3. .Keys 为键集合
  4. .Values 为值集合
  5. .Object 为类内部键访问
  6. .Index 为当前索引位置


  7. 方法:
  8. .Item() 为访问表键方法
  9. .Add() 为添加项方法

  10. .First() 为移动索引位置到首项方法
  11. .Last() 为移动索引位置到末项方法
  12. .Previous() 为移动索引位置到上一项方法
  13. .Next() 为移动索引位置到下一项方法

  14. .Clear() 为清除表数据方法
  15. .Remove() 为清除项方法

  16. .Count() 为计算所有项方法
  17. .IsEmpty() 为判断是否为空方法


  18. .ContainsKey() 为判断是否已有某项名方法
  19. .ContainsValue() 为判断是否已有某项值方法

  20. .ToString() 为输出所有名值对为字串方法


2. 调用演示
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <!-- DW6 -->
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <title>Untitled Document</title>
  7. <style type="text/css">
  8. /* <![CDATA[ */
  9.  
  10. /* ]]> */
  11. </style>
  12. <script type="text/javascript" src="HashTable.js"></script>
  13. <script type="text/javascript">
  14. //<![CDATA
  15.  
  16.  function fSetItem(Hb, iLen, iStart, bSubKey)
  17.  {
  18.   if(!iLen) iLen = 5;
  19.   if(!iStart) iStart = 65;
  20.   for(var i=0; i<iLen; i++)
  21.   {
  22.    var iCur = iStart+i;
  23.    var sKey = String.fromCharCode(iCur);
  24.    
  25.    //function fPut(sKey, Value, SubKeyObj, iIndex/*defaultItems.length*/)
  26.    if(bSubKey)
  27.    {
  28.     Hb.Add(sKey, sKey+" value", {NameEn:sKey+" test", NameZh:sKey+" 测试"});
  29.    }
  30.    else
  31.    {
  32.     Hb.Add(sKey, sKey+" value");
  33.    }
  34.   }
  35.  } // end function fSetItem;
  36.   
  37.  function fForNextListHashTable(Hb, bSubKey)
  38.  {
  39.   
  40.   document.write
  41.   (
  42.    "<h2>Item HashTable Key Item with ",
  43.    ("fForNextListHashTable ").fontcolor("blue")+": </h2>"
  44.   );
  45.   for(var i=0, j = Hb.Keys.length; i<j; i++)
  46.   {
  47.    document.write("<li/>Hb.Item("+i+"): "+Hb.Item(i));
  48.    
  49.    if(bSubKey)
  50.    {
  51.     document.write('<li/>Hb.Item('+i+')["NameEn"]: '+Hb.Item(i)["NameEn"]);
  52.     document.write('<li/>Hb.Item('+i+')["NameZh"]: '+Hb.Item(i)["NameZh"]);
  53.     document.write("<p></p><p></p>");
  54.    }
  55.    
  56.    document.write("<p></p><p></p>");
  57.    document.write("<hr/>");
  58.   }
  59.  } // end function fListSqObject
  60.   
  61.  function fForInListHashTable(Hb, bSubKey)
  62.  {
  63.   
  64.   document.write
  65.   (
  66.    "<h2>Item HashTable Key Item with ",
  67.    ("fForInListHashTable ").fontcolor("blue")+": </h2>"
  68.   );
  69.   
  70.   for(var i in Hb.Object)
  71.   {
  72.    document.write("<li/>Hb.Item("+i+"): "+Hb.Item(i));
  73.    
  74.    if(bSubKey)
  75.    {
  76.     document.write('<li/>Hb.Item('+i+')["NameEn"]: '+Hb.Item(i)["NameEn"]);
  77.     document.write('<li/>Hb.Item('+i+')["NameZh"]: '+Hb.Item(i)["NameZh"]);
  78.    }

  79.    document.write("<p></p><p></p>");
  80.    document.write("<hr/>");
  81.   }
  82.  } // end function fListSqObject
  83.   
  84.  function fListHashTableWithKey(Hb, Key, sBy)
  85.  {
  86.   if(!sBy) sBy = "fListHashTableWithKey";
  87.   
  88.   document.write
  89.   (
  90.    "<h2>Item HashTable Key Item with ",
  91.    (sBy).fontcolor("blue")+": </h2>"
  92.   );
  93.   
  94.   document.write("<li/>Hb.Item("+Key+"): "+Hb.Item(Key));
  95.   document.write('<li/>Hb.Item('+Key+')["NameEn"]: '+Hb.Item(Key)["NameEn"]);
  96.   document.write('<li/>Hb.Item('+Key+')["NameZh"]: '+Hb.Item(Key)["NameZh"]);
  97.   document.write("<p></p><p></p>");  

  98.   document.write("<p></p><p></p>");
  99.   document.write("<hr/>");
  100.  } // end function fListSqObject
  101.  
  102.  
  103.  var Hb = new HashTable()
  104.   
  105.   document.write("<h1>Has Subkey</h1><hr/><hr/>");
  106.   fSetItem(Hb, 5, 65, true); 
  107.   
  108.   fForNextListHashTable(Hb, true);
  109.   fForInListHashTable(Hb, true);
  110.   
  111.   fListHashTableWithKey(Hb, false, 'fListHashTableWithKey(Hb)');;
  112.   fListHashTableWithKey(Hb, 0, 'fListHashTableWithKey(Hb, 0)');;
  113.   fListHashTableWithKey(Hb, "B", 'fListHashTableWithKey(Hb, "B")');
  114.   
  115.   Hb.First();
  116.   fListHashTableWithKey(Hb, false, "First()");
  117.   
  118.   Hb.Last();
  119.   fListHashTableWithKey(Hb, false, "Last()");
  120.   
  121.   while(Hb.Previous())
  122.   {
  123.    fListHashTableWithKey(Hb, false, "Previous()");
  124.   }
  125.   
  126.   while(Hb.Next())
  127.   {
  128.    fListHashTableWithKey(Hb, false, "Next()");
  129.   }
  130.   
  131.   document.write("<h2>By Clear()</h2>");
  132.   Hb.Clear();
  133.   document.write("<li/>Hb.Keys.length: "+Hb.Keys.length);
  134.   
  135.   
  136.   document.write("<br/><br/><br/><h1>No Subkey</h1><hr/><hr/>");
  137.   fSetItem(Hb, false, 97, false);
  138.   
  139.   fForNextListHashTable(Hb, false);
  140.   fForInListHashTable(Hb, false);
  141.   
  142.   Hb.First();
  143.   document.write("<h2>By Remove()</h2>");
  144.   Hb.Remove();
  145.   document.write("<li/>Hb.Index: "+Hb.Index);
  146.   fForInListHashTable(Hb, false);
  147.   
  148.   document.write("<h2>By Remove(1)</h2>");
  149.   Hb.Remove(1);
  150.   document.write("<li/>Hb.Index: "+Hb.Index);
  151.   fForInListHashTable(Hb, false);
  152.   
  153.   document.write('<h2>By Remove("c")</h2>');
  154.   Hb.Remove("c");
  155.   document.write("<li/>Hb.Index: "+Hb.Index);
  156.   fForInListHashTable(Hb, false);
  157.   
  158.   document.write('<h2>By Remove("d")</h2>');
  159.   Hb.Remove("d");
  160.   document.write("<li/>Hb.Index: "+Hb.Index);
  161.   fForInListHashTable(Hb, false);
  162.   
  163.   document.write("<h2>By Count()</h2>");
  164.   document.write("<li/>Hb.Count(): "+Hb.Count());
  165.   
  166.   document.write("<h2>By IsEmpty()</h2>");
  167.   document.write("<li/>Hb.IsEmpty(): "+Hb.IsEmpty());
  168.   
  169.   document.write("<h2>By ContainsKey()</h2>");
  170.   document.write("<li/>Hb.ContainsKey(): "+Hb.ContainsKey());
  171.   
  172.   document.write("<h2>By ContainsKey('hello')</h2>");
  173.   document.write("<li/>Hb.ContainsKey('hello'): "+Hb.ContainsKey('hello'));
  174.   
  175.   document.write("<h2>By ContainsKey('e')</h2>");
  176.   document.write("<li/>Hb.ContainsKey('e'): "+Hb.ContainsKey('e'));
  177.   
  178.   document.write("<h2>By ContainsValue('hello')</h2>");
  179.   document.write("<li/>Hb.ContainsValue('hello'): "+Hb.ContainsValue('hello'));
  180.   
  181.   document.write("<h2>By ContainsValue('e value')</h2>");
  182.   document.write("<li/>Hb.ContainsValue('e value'): "+Hb.ContainsValue('e value'));
  183.   
  184.   document.write("<h2>By ToString()</h2>");
  185.   document.write("<li/>Hb.ToString(): <pre>"+Hb.ToString()+"</pre>");
  186.   
  187.   document.write("<h2>By ToString('##', '##@')</h2>");
  188.   document.write("<li/>Hb.ToString('##', '##@'): <pre>"+Hb.ToString('##', '##@')+"</pre>");
  189.   
  190.   document.write("<h2>By Keys</h2>");
  191.   document.write("<li/>Hb.Keys: "+Hb.Keys);
  192.   
  193.   document.write("<h2>By Values</h2>");
  194.   document.write("<li/>Hb.Values: "+Hb.Values);
  195.   
  196.   
  197.   document.title = Hb.Au.Subject +" "+ Hb.Au.Version;
  198.   Hb = null;
  199.   
  200. //]]>
  201. </script>
  202. </head>
  203. <body>
  204. </body>
  205. </html>


3. HashTable 类
  1.  /*-----------------------------------------------------------------------------------*/
  2.   * shawl.qiu Javascript HashTable class v1.0
  3.  /*-----------------------------------------------------------------------------------*/
  4.  //---------------------------------begin class HashTable()-------------------------------//
  5.  function HashTable()
  6.  { // shawl.qiu code
  7.   //------------------------------------begin public variable
  8.   //---------------begin about
  9.   this.Au = {}
  10.   this.Au.Subject = "shawl.qiu Javascript HashTable class";
  11.   this.Au.Version = "v1.0";
  12.   this.Au.Name = "shawl.qiu";
  13.   this.Au.Email = "shawl.qiu@gmail.com";
  14.   this.Au.Blog = "http://blog.csdn.net/btbtd";
  15.   this.Au.CreatedDate = "2007-6-27";
  16.   this.Au.Update = {};
  17.   this.Au.Update["1"] = "";
  18.   //---------------end about
  19.   
  20.   this.Items = [];
  21.   this.Keys = [];
  22.   this.Values = [];
  23.   this.Object = {};
  24.   this.Index = null; // Number
  25.   //------------------------------------end public variable
  26.   
  27.   //------------------------------------begin private variable
  28.   var Tl = this;
  29.   //------------------------------------end private variable
  30.   
  31.   //------------------------------------begin public method
  32.   this.Item = fItem;
  33.   this.Add = fAdd;
  34.   
  35.   this.First = fFirst;
  36.   this.Last = fLast;
  37.   this.Previous = fPrevious;
  38.   this.Next = fNext;
  39.   
  40.   this.Clear = fClear;
  41.   this.Remove = fRemove;
  42.   
  43.   this.Count = fCount;
  44.   this.IsEmpty = fIsEmpty;
  45.   
  46.   this.ContainsKey = fContainsKey;
  47.   this.ContainsValue = fContainsValue;
  48.     
  49.   this.ToString = fToString;
  50.   //------------------------------------end public method
  51.  
  52.   //------------------------------------begin private method
  53.     
  54.   function fToString(sColumnDilimiter, sRowDilimter)
  55.   {
  56.    if(!sColumnDilimiter) sColumnDilimiter = ",";
  57.    if(!sRowDilimter) sRowDilimter = "/n";
  58.    
  59.    var iCount=0;
  60.    var iLen = Tl.Keys.length;
  61.    var sTemp = "";
  62.    while(iCount<iLen)
  63.    {
  64.     sTemp += Tl.Items[iCount]["Key"]+sColumnDilimiter+Tl.Items[iCount]+sRowDilimter;
  65.     iCount++;
  66.    }
  67.    return sTemp;
  68.   } // end function fToString
  69.   
  70.   function fContainsValue(Value)
  71.   {
  72.    var iCount=0;
  73.    var iLen = Tl.Keys.length;
  74.    
  75.    while(iCount<iLen)
  76.    {
  77.     if(Tl.Items[iCount] == Value) return true;
  78.     iCount++;
  79.    }
  80.    return false;
  81.   } // end fContainsValue
  82.   
  83.   function fContainsKey(sKey)
  84.   {
  85.    if(typeof(sKey)=="undefined") return false;
  86.    return (sKey in Tl.Object);
  87.   } // end function fContainsKey
  88.   
  89.   function fIsEmpty()
  90.   {
  91.    return Tl.Keys.length===0?true:false;
  92.   } // end fIsEmpty
  93.   
  94.   function fCount()
  95.   {
  96.    return Tl.Keys.length; 
  97.   } // end fCount
  98.   
  99.   function fRemove(Key)
  100.   {
  101.    var Debug = false;
  102.    
  103.    if(Key == null)
  104.    {
  105.     if(Tl.Keys.length<0||Tl.Index>0||Tl.Index>=Tl.Keys.length-1) return false;
  106.     if(Tl.Index!=null)
  107.     {
  108.      if(Debug)
  109.      {
  110.       document.write("<li/>Tl.Index: "+Tl.Index);
  111.       document.write("<li/>Tl.Keys[Tl.Index]: "+Tl.Keys[Tl.Index]);
  112.       document.write("<li/>Tl.Object[Tl.Keys[Tl.Index]]: "+Tl.Object[Tl.Keys[Tl.Index]]);
  113.      }
  114.      
  115.      delete Tl.Object[Tl.Keys[Tl.Index]];
  116.      
  117.      Tl.Items.splice(Tl.Index, 1);
  118.      Tl.Keys.splice(Tl.Index, 1);
  119.      Tl.Values.splice(Tl.Index, 1);
  120.      
  121.      if(Tl.Index===0&&Tl.Keys.length===0) 
  122.      {
  123.       Tl.Index = null;
  124.      }
  125.      else if(Tl.Index===0&&Tl.Keys.length!==0) 
  126.      {
  127.       Tl.Index = 0;
  128.      }
  129.      else if(Tl.Index!=null&&Tl.Index>0) 
  130.      {
  131.       Tl.Index = Tl.Index-1;
  132.      }
  133.      
  134.      for(var i=0, j=Tl.Keys.length; i<j; i++)
  135.      {
  136.       Tl.Object[Tl.Keys[i]] = Tl.Object[Tl.Keys[i]]-0-1;
  137.      }
  138.      
  139.      fRemoveDebug(Debug);
  140.     }
  141.     return;
  142.    }
  143.    
  144.    switch(Key.constructor)
  145.    {
  146.     case Number:
  147.      if(Key>=0&&Key<Tl.Keys.length)
  148.      {
  149.      }
  150.      else
  151.      {
  152.       return false;
  153.      }
  154.      Tl.Items.splice(Key, 1);
  155.      
  156.      delete Tl.Object[Tl.Keys[Key]];
  157.      Tl.Keys.splice(Key, 1);
  158.      Tl.Values.splice(Key, 1);
  159.      
  160.      if(Tl.Index===0) Tl.Index = null;
  161.      else Tl.Index = Tl.Index-1;
  162.      
  163.      for(var i=Key, j=Tl.Keys.length; i<j; i++)
  164.      {
  165.       Tl.Object[Tl.Keys[i]] = Tl.Object[Tl.Keys[i]]-0-1;
  166.      }
  167.      
  168.      fRemoveDebug(false);
  169.      
  170.      break;
  171.     
  172.     case String:
  173.     
  174.      if(Key in Tl.Object)
  175.      {
  176.      }
  177.      else
  178.      {
  179.       return false;
  180.      }
  181.      
  182.      Tl.Items.splice(Tl.Object[Key], 1);
  183.      Tl.Keys.splice(Tl.Object[Key], 1);
  184.      Tl.Values.splice(Tl.Object[Key], 1);
  185.      
  186.      for(var i=Tl.Object[Key], j=Tl.Keys.length; i<j; i++)
  187.      {
  188.       Tl.Object[Tl.Keys[i]] = Tl.Object[Tl.Keys[i]]-0-1;
  189.      }
  190.      
  191.      delete Tl.Object[Key];
  192.      
  193.      if(Tl.Index===0) Tl.Index = null;
  194.      else Tl.Index = Tl.Index-1;

  195.      fRemoveDebug(false);

  196.      break;
  197.      
  198.     default:  
  199.    }
  200.   } // end function Remove
  201.   
  202.   function fRemoveDebug(bDebug)
  203.   {
  204.    if(!bDebug) return false;
  205.    
  206.    for(var i in Tl.Keys)
  207.    {
  208.     document.write("<li/>i: "+i);
  209.    }
  210.    document.write("<hr/>");
  211.    
  212.    for(var i in Tl.Items)
  213.    {
  214.     document.write("<li/>i: "+i);
  215.    }
  216.    document.write("<hr/>");
  217.    
  218.    for(var i in Tl.Object)
  219.    {
  220.     document.write("<li/>i: "+i);
  221.    }
  222.    document.write("<hr/>");
  223.    
  224.    for(var i in Tl.Object)
  225.    {
  226.     document.write("<li/>Tl.Object[i]: "+Tl.Object[i]);
  227.    }
  228.    document.write("<hr/>");
  229.   } // end function fRemoveDebug
  230.   
  231.   function fClear()
  232.   {
  233.    Tl.Items = [];
  234.    Tl.Keys = [];
  235.    Tl.Values = [];
  236.    Tl.Object = {};
  237.    Tl.Index = null; // Number
  238.   } // end function fClear
  239.   
  240.   function fNext()
  241.   {
  242.    if(Tl.Keys.length === 0||Tl.Index >= Tl.Keys.length-1) return false;
  243.    Tl.Index = Tl.Index-0+1;
  244.    return true;
  245.   } // end function fNext
  246.   
  247.   function fPrevious()
  248.   {
  249.    if(Tl.Keys.length === 0||Tl.Index === 0) return false;
  250.    Tl.Index = Tl.Index-0-1;
  251.    return true;
  252.   } // end function fPrevious
  253.   
  254.   function fLast()
  255.   {
  256.    if(Tl.Keys.length===0) return null;
  257.    Tl.Index = Tl.Keys.length-1;
  258.   } // end function fLast
  259.   
  260.   function fFirst()
  261.   {
  262.    if(Tl.Keys.length===0) return null;
  263.    Tl.Index = 0;
  264.   } // end function fFirst
  265.   
  266.   function fAdd(sKey, Value, SubKeyObj, iIndex/*defaultItems.length*/)
  267.   {
  268.    if(typeof(sKey)=="undefined"||sKey=="")
  269.    {
  270.     alert("键不能为空!");
  271.     return false;
  272.    }
  273.    sKey+="";
  274.    
  275.    if(sKey in Tl.Object)
  276.    {   
  277.     Tl.Items[Tl.Object[sKey]] = fSetKey(sKey, Value, SubKeyObj);;
  278.     return true;
  279.    }
  280.    
  281.    var Ar = [];
  282.    Ar[0] = Value;
  283.    Ar["Key"] = sKey;
  284.    
  285.    if(SubKeyObj&&SubKeyObj.constructor==Object)
  286.    {
  287.     for(var i in SubKeyObj)
  288.     {
  289.      Ar[i+""] = SubKeyObj[i];
  290.     }
  291.    }
  292.    
  293.    if(typeof(iIndex)=="undefined") iIndex = Tl.Items.length;
  294.    Tl.Index = iIndex;
  295.    
  296.    Tl.Keys[iIndex] = sKey;
  297.    Tl.Values[iIndex] = Value;
  298.    
  299.    Tl.Items[iIndex] = fSetKey(sKey, Value, SubKeyObj);
  300.    Tl.Object[sKey] = iIndex;
  301.    
  302.   } // end function fAdd
  303.   
  304.   function fItem(Key)
  305.   {
  306.    if(typeof(Key)=="undefined"||(Key.constructor==String&&Key==""))
  307.    {
  308.     return Tl.Items[Tl.Index];
  309.    }
  310.    
  311.    if(/^[0-9]+$/.test(Key)) 
  312.    {
  313.     Key -= 0;
  314.    }
  315.    
  316.    switch(Key.constructor)
  317.    {
  318.     case Number:
  319.      Tl.Index = Key;
  320.      return Tl.Items[Key];
  321.     
  322.     case String:
  323.      Tl.Index = Tl.Object[Key];
  324.      return Tl.Items[Tl.Object[Key]]; 
  325.      
  326.     default:
  327.      return Tl.Items[Tl.Index]; 
  328.    }
  329.   } // end function fItem
  330.   
  331.   function fSetKey(sKey, Value, SubKeyObj)
  332.   { // return Array;
  333.    var Ar = [];
  334.    Ar[0] = Value;
  335.    Ar["Key"] = sKey;
  336.    Ar["Name"] = sKey;
  337.    
  338.    if(SubKeyObj&&SubKeyObj.constructor==Object)
  339.    {
  340.     for(var i in SubKeyObj)
  341.     {
  342.      Ar[i+""] = SubKeyObj[i];
  343.     }
  344.    }
  345.    
  346.    return Ar;
  347.   } // end function fSetKey
  348.   
  349.   //------------------------------------end private method
  350.  } // shawl.qiu code
  351.  //---------------------------------end class HashTable()---------------------------------//


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值