|
翻看GA以前的资料,不知道从哪里看到利用Location做链表的想法。
当时觉得麻烦,现在想起来,便用VJass做了个。
现在仅仅是测试。所以只能读取integer类型的数据。不过等到成熟之后。我想它应该可以取代缓存。
[jass]
library DATABASEX
private function H2R takes handle h returns real
return h
return 0.0
endfunction
private function Int2Real takes integer i returns real
return i
return 0.0
endfunction
private function Real2Int takes real r returns integer
return r
return 0
endfunction
private function R2L takes real r returns location
return r
return null
endfunction
private function R2H takes real r returns handle
return r
return null
endfunction
private function H2I takes handle h returns integer
return h
return 0
endfunction
struct IntBase //extends DATABASE
private location Value
private location Subject
private location Id
private method GetNode takes handle subject,integer id returns location
local location tmpsub=this.Subject
local location tmpid=this.Id
local location tmpval=this.Value
loop
exitwhen tmpsub==null
if (GetLocationX(tmpsub)!=H2R(subject) or Real2Int(GetLocationX(tmpid))!=id) then
//找到节点
set tmpsub=R2L(GetLocationY(tmpsub))
set tmpid=R2L(GetLocationY(tmpid))
set tmpval=R2L(GetLocationY(tmpval))
else
return tmpval
endif
endloop
return null
endmethod
private method AddNode takes handle subject,integer id returns location
local location newValue = Location(0,H2R(this.Value))
local location newSub = Location(H2R(subject),H2R(this.Subject))
local location newId = Location(Int2Real(id),H2R(this.Id))
set this.Value=newValue
set this.Subject=newSub
set this.Id=newId
return newValue
endmethod
private method DelNode takes handle subject,integer id returns boolean
local location wanttodelVal = this.Value
local location wanttodelSub = this.Subject
local location wanttodelId = this.Id
local location tmpVal
local location tmpSub
local location tmpId
loop
exitwhen wanttodelVal==null
if GetLocationX(wanttodelSub)==H2R(subject)then
if GetLocationX(wanttodelId)==Int2Real(id)then
//确定要删除的节点
if(wanttodelVal==this.Value)then
set this.Value=R2L(GetLocationY(wanttodelVal))
set this.Subject=R2L(GetLocationY(wanttodelSub))
set this.Id=R2L(GetLocationY(wanttodelId))
call RemoveLocation(wanttodelVal)
call RemoveLocation(wanttodelSub)
call RemoveLocation(wanttodelId)
return true
else
//寻找前驱
set tmpVal=this.Value
set tmpSub=this.Subject
set tmpId=this.Id
loop
exitwhen tmpVal==null
if GetLocationY(tmpSub)!=H2R(wanttodelSub) or GetLocationY(wanttodelId)==H2R(wanttodelId) then
set tmpVal=R2L(GetLocationY(tmpVal))
set tmpSub=R2L(GetLocationY(tmpSub))
set tmpId=R2L(GetLocationY(tmpId))
else //将找到的前驱的后继改为要删除节点的后继
call MoveLocation(tmpVal,GetLocationX(tmpVal),GetLocationY(wanttodelVal))
call MoveLocation(tmpSub,GetLocationX(tmpSub),GetLocationY(wanttodelSub))
call MoveLocation(tmpId,GetLocationX(tmpId),GetLocationY(wanttodelId))
call RemoveLocation(wanttodelVal)
call RemoveLocation(wanttodelSub)
call RemoveLocation(wanttodelId)
return true
endif
endloop
endif
endif
endif
endloop
return false
endmethod
public method Load takes handle subject,integer id returns integer
local location node = this.GetNode(subject,id)
local integer i
if node!=null then
return Real2Int(GetLocationX(node))
endif
return 0
endmethod
public method Save takes handle subject,integer id,integer value returns nothing
local location node = this.GetNode(subject,id)
if node==null then
set node = this.AddNode(subject,id)
endif
call MoveLocation(node,Int2Real(value),GetLocationY(node))
endmethod
public method Del takes handle subject,integer id returns boolean
return this.DelNode(subject,id)
endmethod
endstruct
globals
IntBase ThisIntBase
endglobals
endlibrary
[/jass]
估计可能还是存在一些BUG但自己现在还每找出来。希望大家一起测试,找出BUG。
并发帖告诉我 |
评分
-
查看全部评分
|