用自定义类实现vbs中Dictionary的功能

3 篇文章 0 订阅
2 篇文章 0 订阅

关于这个类的相关说明请参照这篇[关于自定义Dictionary类的说明]

<%



'**********************************************

' File:  Dictionary.asp

' Version: willpower Dictionary Class Version 0.1 Build 20080424

' Author: willpower

' Email: wise.willpower@163.com

' QQ:20934440

' Date:  04/30/2008

' Comments: The code for the dictionary.

'   This can free usage, but please

'   not to delete this copyright information.

'   If you have a modification version,

'   Please send out a duplicate to me.

'**********************************************

' 文件名: Dictionary.asp

' 版本:  willpower Dictionary Class Version 0.1 Build 20080424

' 作者:  willpower(wise.willpower)

' 电子邮件: wise.willpower@163.com

' QQ:20934440

' 日期:  2008年04月30日

' 声明:  字典类

'   本字典类可以自由使用,但请保留此版权声明信息

'   如果您对本字典类进行修改增强,

'   请发送一份给俺。





Class Dictionary

'declare private variables 

Private arrKey(100)   ' Define a Array , Contains the index|定义一个数组用来存储集合索引,数组大小由与Hash表大小一样,这里定为100

Private intCount   ' Define a Integer, Store the Collection's count|定义一个整型变量来存储键值对的个数



Private Sub Class_Initialize()

Call RemoveAll()

End Sub



Private Sub Class_Terminate()

Call RemoveAll()

End Sub



Public Function RemoveAll()

intCount = 0

Erase arrKey

End Function



Public Function Add(ByVal strKey,ByVal strItem)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Function

End If

If Not Exists(strKey) Then

Dim t

t = CHash(strKey)  ' Define t as temporary variable to restore the CHash result|定义一个临时变量来存储Chash返回的值.用空间来节省时间.

If Not IsObject(arrKey(t)) Then ' 检测有没有冲突[这里一定要用IsObject]

Set arrKey(t) = New Data

arrKey(t).DataKey = strKey

If Not IsObject(strItem) Then

arrKey(t).DataItem = strItem

Else

Set arrKey(t).DataItem = strItem

End If

Else

Dim tObject

Set tObject = arrKey(t)

While IsObject(tObject.NextNode)

Set tObject=tObject.NextNode

Wend

Set tObject.NextNode = New Data

Set tObject = tObject.NextNode

tObject.DataKey = strKey

If Not IsObject(strItem) Then

tObject.DataItem = strItem

Else

Set tObject.DataItem = strItem

End If

Set tObject = Nothing

End If

intCount = intCount + 1

Else

err.Raise 2,"Dictionary","The Key is already exist!"

End If

End Function





Public Function Remove(ByVal strKey)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Function

End If



If Not Exists(strKey) Then

err.Raise 3,"Dictionary","The Key is not exist!"

Else

Dim t

t = CHash(strKey)



If arrKey(t).DataKey = strKey Then

If IsObject(arrKey(t).DataItem) Then

Set arrKey(t).DataItem = Nothing

End If

If Not IsObject(arrKey(t).NextNode) Then

Set arrKey(t) = Nothing

arrKey(t) = 0

Else

Set arrKey(t) = arrKey(t).NextNode

End If

intCount = intCount - 1

Else



Dim tObjectPre,tObject

Set tObject = arrKey(t)

Do While IsObject(tObject.NextNode)

If tObject.NextNode.DataKey = strKey Then

Set tObjectPre = tObject

If IsObject(tObject.DataItem) Then

Set tObject.DataItem = Nothing

End If

If IsObject(tObject.NextNode) Then

Set tObjectPre = tObject.NextNode

Else

tObjectPre.NextNode = 0

End If

Set tObjectPre = Nothing

Set tObject = Nothing

intCount = intCount - 1

Exit Do

End If

Loop

End If



End If

End Function





Public Function Keys()

Dim tArr

ReDim tArr(intCount-1)

Dim i

i = 0

For j = 0 To 100

If IsObject(arrKey(j)) Then

Dim tObject

Set tObject = arrKey(j)

tArr(i) = tObject.DataKey

i = i + 1

While IsObject(tObject.NextNode)

Set tObject = tObject.NextNode

tArr(i) = tObject.DataKey

i = i + 1

Wend

Set tObject = Nothing

End If

Next

Keys = tArr

End Function





Public Function Items()

Dim tArr

ReDim tArr(intCount-1)

Dim i

i = 0

For j = 0 To 100

If IsObject(arrKey(j)) Then

Dim tObject

Set tObject = arrKey(j)

If Not IsObject(tObject.DataItem) Then

tArr(i) = tObject.DataItem

Else

Set tArr(i) = tObject.DataItem

End If

i = i + 1

While IsObject(tObject.NextNode)

Set tObject = tObject.NextNode

If Not IsObject(tObject.DataItem) Then

tArr(i) = tObject.DataItem

Else

Set tArr(i) = tObject.DataItem

End If

i = i + 1

Wend

Set tObject = Nothing

End If

Next

Items = tArr

End Function





Private Function Search(ByVal strKey)

Search = False

Dim t

t = CHash(strKey)

If IsObject(arrKey(t)) Then

If arrKey(t).DataKey = strKey Then

Set Search = arrKey(t)

Else

Dim tObject

Set tObject = arrKey(t)

Do While IsObject(tObject.NextNode)

Set tObject = tObject.NextNode

If tObject.DataKey = strKey Then

Set Search = tObject

Set tObject = Nothing

Exit Do

End If

Loop

End If

End If

End Function





Public Function Exists(ByVal strKey)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Function

End If

Exists = False

If IsObject(Search(strKey)) Then

Exists = True

End If

End Function





Private Function CHash(ByVal strKey)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Function

End If

Dim i,t,s,l,quotient,istep,divisor     ' i:increment,t:temporary variable,s:CHash return value,l:string's length|i:增量,t:临时变量,用来临时存储任意一个字符的ASC码,s:存储符合条件的ASC码的和,l:字符串的长度

divisor = 101

i = 0        

t = 0

s = 0

l = Len(strKey)

quotient = l / 4        '不知可否用右移来使它更快

istep = quotient + 1

For i = 1 To l step istep

t = Asc(Mid(strKey,i,1))

s = s + t

Next

CHash = s mod divisor

End Function



Public Property Get Count

Count = intCount

End Property



Public Property Get Item(ByVal strKey)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Property

End If

Dim tObject

If IsObject(Search(strKey)) Then

Set tObject = Search(strKey)

If IsObject(tObject.DataItem) Then

Set Item = tObject.DataItem

Else

Item = tObject.DataItem

Set tObject = Nothing

End If

Else

err.Raise 3,"Dictionary","The Key is not exist!"

Exit Property

End If

End Property



Public Property Let Item(ByVal strKey,ByVal strItem)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Property

End If

If IsObject(Search(strKey)) Then

Set tObject = Search(strKey)

Item = tObject.DataItem

Set tObject = Nothing

Else

err.Raise 3,"Dictionary","The Key is not exist!"

Exit Property

End If

End Property



Public Property Set Item(ByVal strKey,ByRef oItem)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Property

End If

If IsObject(Search(strKey)) Then

Set tObject = Search(strKey)

Set Item = tObject.DataItem

Set tObject = Nothing

Else

err.Raise 3,"Dictionary","The Key is not exist!"

Exit Property

End If

End Property





Public Property Let Key(ByVal strKey,ByVal str)

If strKey = "" Then

err.Raise 1,"Dictionary","Key's value must not be empty!"

Exit Property

End If

If IsObject(Search(strKey)) Then

Set tObject = Search(strKey)

Item = tObject.DataKey

Set tObject = Nothing

Else

err.Raise 3,"Dictionary","The Key is not exist!"

Exit Property

End If

End Property

End Class



 



Class Data



Public DataKey

Public DataItem

Public NextNode



Private Sub Class_Initialize()

Call RemoveAll()

End Sub



Private Sub Class_Terminate()

Call RemoveAll()

End Sub



Private Sub RemoveAll()

DataKey = ""

DataItem = Empty

NextNode = Null

End Sub



End Class





dim s1

Set s1 = New Dictionary

s1.Add "userName","willpower"

s1.Add "userTel",123456

s1.Add "IsVisible",True

s1.Add "addTime",Now()

Dim arr,i

arr = s1.Items()

For i = 0 To Ubound(arr)

Response.Write arr(i)

Next

Set s1 = Nothing%>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值