ASP自定义HashTable类
ASP中有dictionary对象,类似哈希表的键值对,这样的键值对可以用在很多地方,可惜的是dictionary本身有很多不足之处,当我发现我无法将它使用的灵活时,我想到不如用数组自定义一个hash结构,记得以前曾写过一个arraylist的asp封装,于是有了本文的HashTable。
- <%
- Class HashTable
- '// Code By Shaoyun
- '// Date:2009-7-9 11:46:14
- '// Site:Devjs.com
- Private m_keys,m_vals,m_cnt,m_repeat
- Private Sub Class_Initialize()
- m_keys=array()
- m_vals=array()
- m_cnt=0
- m_repeat=false
- End Sub
- Private Sub Class_Terminate()
- End Sub
- ' 是否允许键名重复
- Public Property Let Repeat(boolval)
- m_repeat=boolval
- End Property
- ' 获取键值对的总数
- Public Property Get Count()
- Count=m_cnt
- End Property
- ' 获取键值
- Public Default Property Get Keys(keyname)
- dim i
- for i=0 to m_cnt-1
- if keyname=m_keys(i) then Keys=m_vals(i)
- next
- End Property
- ' 添加键值
- Public Sub Add(key,val)
- dim done
- done=false
- If m_repeat Then
- done=true
- else
- if Not Exist(key) then done=true
- End If
- If done Then
- Redim Preserve m_keys(m_cnt)
- Redim Preserve m_vals(m_cnt)
- m_keys(m_cnt)=key
- m_vals(m_cnt)=val
- m_cnt=m_cnt+1
- end if
- End Sub
- ' 检测键是否存在
- Public Function Exist(keyname)
- Exist=false
- dim i
- for i=0 to m_cnt-1
- if keyname=m_keys(i) then
- Exist=true
- Exit For
- end if
- next
- End Function
- ' 导出数据
- Public function Dump()
- dim i,ostr
- for i= 0 to m_cnt-1
- ostr=ostr & "Array(""" & m_keys(i) & """," & m_vals(i) & "),"
- next
- if m_cnt>0 then ostr=mid(ostr,1,len(ostr)-1)
- Dump = "Array(" & ostr & ")"
- End function
- ' 通过数组赋值
- ' 数组传值约定:array(array("item1",1),array("item2",2))
- Public Sub AssignByArray(ary)
- dim i
- for i=0 to ubound(ary)
- if IsArray(ary(i)) Then
- Add ary(i)(0),ary(i)(1)
- end if
- next
- End Sub
- End Class
- ' 使用实例
- set ht=new HashTable
- ht.add "haha",2
- ht.add "wawa",4
- ht.add "xixi",3
- ht.add "xixi",6
- ht.AssignByArray(array(array("a",1),array("b",2)))
- Response.Write ht.dump
- Response.Write ht.count
- Response.Write ht("haha")
- set ht=nothing
- %>
将以前写的arraylist链接贴在后面,都是为了用着方便,没什么难度。
ASP自定义ArrayList类
链接地址:http://www.devjs.com/Article/25.aspx
链接地址:http://www.devjs.com/Article/25.aspx