找回密码
 点一下
查看: 2297|回复: 6

[VJass]某人的数据系统

[复制链接]
发表于 2008-1-31 17:53:52 | 显示全部楼层 |阅读模式
翻看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。
并发帖告诉我

评分

参与人数 1威望 +1 收起 理由
白银の游戏王 + 1

查看全部评分

发表于 2008-1-31 21:29:46 | 显示全部楼层
可以将系统分为三段来做的说~~
一个是储存,一个是读取,再一个是转移。这样似乎就可以构成一个完整数数据链接。
看了LZ的帖子,深受启发,如果 location 再结合一下缓存的话, 是不是会更加强大呢?
回复

使用道具 举报

 楼主| 发表于 2008-2-1 11:07:24 | 显示全部楼层
也许吧,但貌似很多人鄙视缓存的说,这个仅仅用的是ReturnBug,如果某些顽固派连ReturnBug也不认了。那就没得说了。
回复

使用道具 举报

 楼主| 发表于 2008-2-1 11:11:11 | 显示全部楼层
不过转移我看就不必了吧,只要读取跟存入,还有删除,毕竟是Location做的,泄露问题是没得说的。
回复

使用道具 举报

发表于 2008-2-1 15:32:58 | 显示全部楼层
另外,效率也是一个问题~
回复

使用道具 举报

发表于 2008-2-1 16:06:41 | 显示全部楼层
呵呵~魔兽游戏的效率本身就有些问题的说,所以大多数时候偶不怎么重视这个问题。毕竟,只要不让地图玩得一卡一卡的就万事大吉了,效率的问题还是弄到编程里研究吧。
回复

使用道具 举报

发表于 2008-2-1 18:35:56 | 显示全部楼层
还不如用数组~~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 点一下

本版积分规则

Archiver|移动端|小黑屋|地精研究院

GMT+8, 2024-5-3 10:29 , Processed in 0.056695 second(s), 22 queries .

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表