欢迎光临
我们一直在努力

存储数据键和项目对的类(Dictionary对象)

建站超值云服务器,限时71元/月

<%

############################################################################

# #

# 存储数据键和项目对的类(dictionary对象) #

# #

# 本类功能用法完全按照 microsoft visual basic scripting edition #

# 中的dictionary对象编写,使用本类完全可以参照其的功能和用法。 #

# 下面便是该对象的中文使用说明 #

# http://www.microsoft.com/china/vbscript/vbslang/vsobjdictionary.htm #

# 本类完全由简单的vbscript编写,所以您可以在任何支持asp的空间使用它 #

# 从而获的使用dictionary对象的便利。 #

# 您可以随意使用,但请保留版权信息!谢谢! #

# #

# 编写者:chinaok #

# http://www.chinaok.net #

# 2002.8.3 #

# #

############################################################################

class dictionary

public copyright, developer, name, version, web

private arykey()

private aryitem()

private icomparemode

private sub class_initialize()

请保留此信息

copyright = "2002 www.chinaok.net, all rights reserved."

developer = "chinaok"

name = "dictionary"

version = "1.0b"

web = "http://www.chinaok.net"

redim arykey(0)

redim aryitem(0)

arykey(0)=""

aryitem(0)=""

icomparemode=0

end sub

public function add(skey,item)

insertsort skey,item

end function

public function exists(skey)

if binsearch(skey)=0 then

exists=false

else

exists=true

end if

end function

public function items()

items=aryitem

end function

public function keys()

keys=arykey

end function

public function remove(skey)

deletesort skey

end function

public function removeall()

redim arykey(0)

redim aryitem(0)

arykey(0)=""

aryitem(0)=""

end function

property get count()

dim len1,len2

len1=ubound(arykey)

len2=ubound(aryitem)

if len1<>len2 then redim preserve aryitem(len1)

count=len1

end property

property get item(skey)

dim itop

itop=0

itop = binsearch(skey)

if itop<>0 then

item=aryitem(itop)

else

add skey,""

item=""

end if

end property

property let item(skey,newitem)

dim itop

itop=0

itop = binsearch(skey)

if itop<>0 then

aryitem(itop)=newitem

else

add skey,newitem

end if

end property

property let key(skey,snewkey)

dim itop

itop = 0

itop = binsearch(skey)

if itop<>0 then

arykey(itop)=snewkey

else

err.raise 19782,"mydictionary","未找到元素" & skey,"",0

end if

end property

property let comparemode(imode)

if count()>0 then err.raise 19783,"mydictionary","设置字符串关键字比较模式必须在items为空时设置","",0

if (imode<>0 and imode<>1) then imode=0

icomparemode=imode

end property

property get comparemode()

comparemode=icomparemode

end property

private function binsearch(skey)

折半查找算法

dim result

result=0

dim ihigh,ilow,imid

ihigh = count()

ilow = 1

do while (ilow<=ihigh)

imid=(ilow+ihigh)\2

if strcomp(arykey(imid),skey,icomparemode)=0 then

result=imid

exit do

end if

if strcomp(arykey(imid),skey,icomparemode)=1 then

ihigh=imid-1

else

ilow=imid+1

end if

loop

binsearch=result

end function

private function deletesort(skey)

dim itop,i,ilen

itop=binsearch(skey)

if itop=0 then

err.raise 19782,"mydictionary","未找到元素" & skey,"",0

else

ilen=count()

for i=itop+1 to ilen

arykey(i-1)=arykey(i)

aryitem(i-1)=aryitem(i)

next

redim preserve arykey(ilen-1)

redim preserve aryitem(ilen-1)

end if

end function

private function insertsort(skey,item)

dim i,j,ilen

ilen=count()

查找插入 ,直接查找插入算法

for i=1 to ilen

if (strcomp(arykey(i),skey,icomparemode)<>-1) then

exit for

end if

next

if (i>ilen) then

直接插入

redim preserve arykey(i)

redim preserve aryitem(i)

arykey(i)=skey

aryitem(i)=item

else

if (strcomp(arykey(i),skey,icomparemode)=0) then

err.raise 19781,"mydictionary","此键已与该集合的一个元素关联","",0

else

redim preserve arykey(ilen+1)

redim preserve aryitem(ilen+1)

for j=ilen+1 to i+1 step -1

arykey(j) = arykey(j-1)

aryitem(j)= aryitem(j-1)

next

arykey(i)=skey

aryitem(i)=item

end if

end if

end function

类销毁

private sub class_terminate()

end sub

end class

%>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 存储数据键和项目对的类(Dictionary对象)
分享到: 更多 (0)

相关推荐

  • 暂无文章