|
发表于 2010-6-3 19:21:23
|
显示全部楼层
试着写了下,有写函数不知道什么作用,你补充下吧
Contains有点难,如果现在的函数OK的话,那我就补充下。
[codes=jass]
globals
integer array DyA_value
integer array DyA_qian
integer array DyA_hou
integer DyA_num=1
integer array DyA_arrsize
endglobals
function DyA_NewNum takes nothing returns integer
local integer i=DyA_num
local integer next=DyA_hou
if(next==0)then
set next=i+1
if(next>8191)then
return 0
endif
endif
set DyA_num=next
set DyA_value=0
return i
endfunction
function DyA_DelNum takes integer i returns nothing
set DyA_hou=DyA_num
set DyA_num=i
endfunction
function DyA_DelNums takes integer x,integer y returns nothing
set DyA_hou[y]=DyA_num
set DyA_num=x
endfunction
function DyA_SetValue takes integer i,integer value returns nothing
set DyA_value=value
endfunction
function DyA_Findnum_hou takes integer i,integer x returns integer
loop
exitwhen x==1
set i=DyA_hou
set x=x-1
endloop
return i
endfunction
function DyA_Findnum_qian takes integer i,integer x returns integer
set i=DyA_qian
loop
exitwhen x==1
set i=DyA_qian
set x=x-1
endloop
return i
endfunction
function DyA_Deljiedian takes integer i returns nothing
local integer x=DyA_hou
local integer y=DyA_qian
set DyA_hou[y]=x
set DyA_qian[x]=y
call DyA_DelNum(i)
endfunction
[/codes]
[codes=jass]
function DynamicArrayCreate takes nothing returns integer
local integer i=DyA_NewNum()
set DyA_qian=i
set DyA_hou=i
set DyA_arrsize=1
return i
endfunction
function DynamicArrayCreateInitialSize takes integer initialSize returns integer
local integer i=DyA_NewNum()
local integer x=i
local integer y=i
set DyA_arrsize=initialSize
loop
exitwhen initialSize==1
set y=DyA_NewNum()
set DyA_hou[x]=y
set DyA_qian[y]=x
set initialSize=initialSize-1
set x=y
endloop
set DyA_hou[x]=i
set DyA_qian=x
return i
endfunction
function DynamicArrayDestroy takes integer darr returns nothing
set DyA_arrsize[darr]=0
call DyA_DelNums(darr,DyA_qian[darr])
endfunction
function DynamicArrayClear takes integer darr returns nothing
local integer i=darr
loop
set DyA_value=0
set i=DyA_hou
exitwhen i==darr
endloop
endfunction
function DynamicArrayHasElement takes integer darr,integer element returns boolean
local integer i=darr
loop
if(element==DyA_value)then
return true
endif
set i=DyA_hou
exitwhen i==darr
endloop
return false
endfunction
function DynamicArrayAppendElement takes integer darr,integer element returns nothing
local integer i=DyA_NewNum()
local integer x=DyA_qian[darr]
set DyA_value=element
set DyA_hou=darr
set DyA_qian=x
set DyA_hou[x]=i
set DyA_qian[darr]=i
set DyA_arrsize[darr]=DyA_arrsize[darr]+1
endfunction
function DynamicArrayMerge takes integer darr1,integer darr2 returns nothing
local integer x=DyA_qian[darr1]
local integer y=DyA_qian[darr2]
set DyA_hou[x]=darr2
set DyA_hou[y]=darr1
set DyA_qian[darr2]=x
set DyA_qian[darr1]=y
set DyA_arrsize[darr1]=DyA_arrsize[darr1]+DyA_arrsize[darr2]
set DyA_arrsize[darr2]=0
endfunction
function DynamicArrayResize takes integer darr,integer newSize returns nothing
local integer size=DyA_arrsize[darr]
local integer x
if(size<newSize)then
call DynamicArrayMerge(darr,DynamicArrayCreateInitialSize(newSize-size))
return
elseif(size==newSize)then
return
elseif(size>newSize)then
if(newSize<=0)then
call DynamicArrayDestroy(darr)
return
else
if(newSize<size/2)then
set x=DyA_Findnum_hou(darr,newSize)
elseif(newSize>=size/2)then
set x=DyA_Findnum_qian(darr,size-newSize+1)
endif
call DyA_DelNums(DyA_hou[x],DyA_qian[darr])
set DyA_hou[x]=darr
set DyA_qian[darr]=x
endif
endif
set DyA_arrsize[darr]=newSize
endfunction
function DynamicArrayRemoveElement takes integer darr,integer element returns nothing
local integer size=DyA_arrsize[darr]
local integer i=darr
if(size<=0)then
return
elseif(size==1)then
if(DyA_value==element)then
set DyA_value=0
endif
else
if(DyA_value==element)then
set DyA_value=DyA_value[DyA_hou]
call DyA_Deljiedian(DyA_hou)
set DyA_arrsize[darr]=size-1
return
endif
set i=DyA_hou
loop
exitwhen i==darr
if(DyA_value==element)then
call DyA_Deljiedian(i)
set DyA_arrsize[darr]=size-1
return
endif
set i=DyA_hou
endloop
endif
endfunction
function DynamicArrayRemoveElementAll takes integer darr,integer element returns nothing
local integer size=DyA_arrsize[darr]
local integer i=darr
if(size<=0)then
return
elseif(size==1)then
if(DyA_value==element)then
set DyA_value=0
endif
else
set i=DyA_hou
loop
exitwhen i==darr
if(DyA_value==element)then
call DyA_Deljiedian(i)
set size=size-1
endif
set i=DyA_hou
endloop
if(DyA_value==element)then
if(size==1)then
set DyA_value=0
else
set DyA_value=DyA_value[DyA_hou]
call DyA_Deljiedian(DyA_hou)
set DyA_arrsize[darr]=size-1
endif
endif
endif
endfunction
function DynamicArraySize takes integer darr returns integer
return DyA_arrsize[darr]
endfunction
//function DynamicArrayDifference takes integer darr1,integer darr2 returns nothing
function DynamicArrayEqual takes integer darr1,integer darr2 returns boolean
local integer x=darr1
local integer y=darr2
if(DyA_arrsize[x]!=DyA_arrsize[y])then
return false
else
loop
if(DyA_value[x]!=DyA_value[y])then
return false
endif
set x=DyA_hou[x]
set y=DyA_hou[y]
exitwhen x==darr1
endloop
endif
return true
endfunction
//function DynamicArrayContains takes integer darr1,integer darr2 returns boolean
function DynamicArraySwap takes integer darr1,integer darr2 returns nothing
local integer x=DyA_arrsize[darr1]
local integer y=DyA_arrsize[darr2]
local integer a
if(x<=0 or y<=0)then
return
endif
if(x<=y)then
else
set a=darr1
set darr1=darr2
set darr2=a
endif
set x=darr1
set y=darr2
loop
set a=DyA_value[x]
set DyA_value[x]=DyA_value[y]
set DyA_value[y]=a
set x=DyA_hou[x]
set y=DyA_hou[y]
exitwhen x==darr1
endloop
endfunction
//function DynamicArrayFront takes integer darr1 returns integer
function DynamicArraySet takes integer darr,integer index,integer value returns nothing
local integer size=DyA_arrsize[darr]
if(index>size or index<=0)then
return
elseif(index<=size/2)then
set DyA_value[DyA_Findnum_hou(darr,index)]=value
else
set DyA_value[DyA_Findnum_qian(darr,size-index+1)]=value
endif
endfunction
function DynamicArrayGet takes integer darr,integer index returns integer
local integer size=DyA_arrsize[darr]
if(index>size or index<=0)then
return 0
elseif(index<=size/2)then
return DyA_value[DyA_Findnum_hou(darr,index)]
endif
return DyA_value[DyA_Findnum_qian(darr,size-index+1)]
endfunction
function DynamicArrayClone takes integer darr returns integer
local integer size=DyA_arrsize[darr]
local integer x=darr
local integer i=DynamicArrayCreateInitialSize(size)
loop
set DyA_value=DyA_value[x]
set x=DyA_hou[x]
set i=DyA_hou
exitwhen x==darr
endloop
return i
endfunction
//function UnitGrabAll takes integer darr returns nothing
[/codes]
----------- 帖子于 19:21 更新 --------- 之前内容发布于 19:17 ------------
就是用循环双向链表做的动态数组,总的上限就是8191。我只测试了没有书写错误,逻辑错误的话我慢慢排查下。 |
|