ASP自訂HashTable類

來源:互聯網
上載者:User

ASP中有dictionary對象,類似雜湊表的索引值對,這樣的索引值對可以用在很多地方,可惜的是dictionary本身有很多不足之處,當我發現我無法將它使用的靈活時,我想到不如用數組自訂一個hash結構,記得以前曾寫過一個arraylist的asp封裝,於是有了本文的HashTable。

 
  1. <%  
  2. Class HashTable  
  3. '// Code By Shaoyun  
  4. '// Date:2009-7-9 11:46:14  
  5. '// Site:Devjs.com  
  6. Private m_keys,m_vals,m_cnt,m_repeat  
  7.  
  8. Private Sub Class_Initialize()  
  9.     m_keys=array()  
  10.     m_vals=array()  
  11.     m_cnt=0  
  12.     m_repeat=false  
  13. End Sub 
  14.  
  15. Private Sub Class_Terminate()  
  16. End Sub 
  17.  
  18. ' 是否允許鍵名重複  
  19. Public Property Let Repeat(boolval)  
  20.     m_repeat=boolval  
  21. End Property 
  22.  
  23. ' 擷取索引值對的總數  
  24. Public Property Get Count()  
  25.     Count=m_cnt  
  26. End Property 
  27.  
  28. ' 擷取索引值  
  29. Public Default Property Get Keys(keyname)  
  30.     dim i  
  31.     for i=0 to m_cnt-1  
  32.         if keyname=m_keys(i) then Keys=m_vals(i)  
  33.     next  
  34. End Property 
  35.  
  36. ' 添加索引值  
  37. Public Sub Add(key,val)  
  38.     dim done  
  39.     done=false  
  40.     If m_repeat Then 
  41.         done=true  
  42.     else  
  43.         if Not Exist(key) then done=true  
  44.     End If 
  45.     If done Then 
  46.         Redim Preserve m_keys(m_cnt)  
  47.         Redim Preserve m_vals(m_cnt)  
  48.         m_keys(m_cnt)=key  
  49.         m_vals(m_cnt)=val  
  50.         m_cnt=m_cnt+1  
  51.     end if  
  52. End Sub 
  53.  
  54. ' 檢測鍵是否存在  
  55. Public Function Exist(keyname)  
  56.     Exist=false  
  57.     dim i  
  58.     for i=0 to m_cnt-1  
  59.         if keyname=m_keys(i) then  
  60.             Exist=true  
  61.             Exit For 
  62.         end if  
  63.     next  
  64. End Function 
  65.  
  66. ' 匯出資料  
  67. Public function Dump()  
  68.     dim i,ostr  
  69.     for i= 0 to m_cnt-1  
  70.         ostr=ostr & "Array(""" & m_keys(i) & """," & m_vals(i) & ")," 
  71.     next  
  72.     if m_cnt>0 then ostr=mid(ostr,1,len(ostr)-1)  
  73.     Dump = "Array(" & ostr & ")" 
  74. End function  
  75.  
  76. ' 通過數組賦值  
  77. ' 數組傳值約定:array(array("item1",1),array("item2",2))  
  78. Public Sub AssignByArray(ary)  
  79.     dim i  
  80.     for i=0 to ubound(ary)  
  81.         if IsArray(ary(i)) Then 
  82.             Add ary(i)(0),ary(i)(1)  
  83.         end if  
  84.     next  
  85. End Sub 
  86. End Class 
  87.  
  88. ' 使用執行個體  
  89. set ht=new HashTable  
  90. ht.add "haha",2  
  91. ht.add "wawa",4  
  92. ht.add "xixi",3  
  93. ht.add "xixi",6  
  94. ht.AssignByArray(array(array("a",1),array("b",2)))  
  95. Response.Write ht.dump  
  96. Response.Write ht.count  
  97. Response.Write ht("haha")  
  98. set ht=nothing  
  99. %> 

將以前寫的arraylist連結貼在後面,都是為了用著方便,沒什麼難度。

ASP自訂ArrayList類
連結地址:http://www.devjs.com/Article/25.aspx
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.