summaryrefslogtreecommitdiff
path: root/Data/DefaultContent/Libraries/containers/list.lua
blob: 59233b97ebf65123c738d5e6ce54702b405f1208 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by Dee.
--- DateTime: 2019/3/7 14:00
--- 高效增删有序表,低效遍历
---

--[[
遍历:
    for _,idx, value in ipairs(l) do .. end
    for _, value in pairs(l) do ... end
]]

list = list or {}

function list.create()
    local lenght = 0
    -- 类似stl的方式,头尾只是作为指针使用
    local first = {front = nil, next = nil, value = nil}
    local last = {front = nil, next = nil, value = nil}
    first.next = last
    last.front = first


    ---查找值
    ---@param value
    ---@return node
    local find = function(value)
        local ret = nil
        local nextNode = first
        while nextNode do
            nextNode = nextNode.next
            if nextNode.value == value then
                ret = nextNode
                break
            end
        end

        return ret
    end

    ---查找(根据下标查找)
    ---@param idx 下标
    ---@return node
    local findByIdx = function(idx)
        local i = 0
        local ret
        local nextNode = first
        while nextNode and i < lenght do
            i = i+1
            nextNode = nextNode.next
            if i == idx then
                ret = nextNode
                break
            end
        end

        return ret
    end

    ---在node前添加
    ---@param node
    ---@param v
    local addBefore = function(node, v)
        assert(node)

        local frontNode = node.front
        local newNode = {}
        newNode.front = frontNode
        newNode.next = node
        newNode.value = v
        node.front = newNode
        frontNode.next = newNode

        lenght = lenght+1
    end

    ---在node后添加
    ---@param node
    ---@param v
    local addAfter = function(node, v)
        assert(node)
        local nextNode = node.next
        local newNode = {}
        newNode.front = node
        newNode.next = nextNode
        newNode.value = v
        node.next = newNode
        nextNode.front = newNode

        lenght = lenght+1
    end

    ---在队首添加
    ---@param v
    local addFirst = function(v)
        addAfter(first, v)
    end

    ---在队尾添加
    ---@param v
    local addLast = function(v)
        addBefore(last, v)
    end

    ---删除节点
    ---@param node
    local removeNode = function(node)
        assert(node)

        local frontNode = node.front
        local nextNode = node.next

        if frontNode == nil then
            first = nextNode
        else
            frontNode.next = nextNode
        end

        if nextNode ~= nil then
            nextNode.front = frontNode
        end
        lenght = lenght - 1
    end

    ---删除节点
    ---@param v
    local remove = function(v)
        local node = find(v)
        if node then
            removeNode(node)
        end
    end

    local t = {
        addFirst = addFirst,
        addLast = addLast,
        addBefore = addBefore,
        addAfter = addAfter,
        removeNode = removeNode,
        remove = remove,
        find = find,
        findByIdx = findByIdx
    }

    local mt = {
        __index = function(i_t, key)
            return findByIdx(key)
        end,
        __newindex = function(i_t,k,v)
            local node = findByIdx(k)
            if not node then
                error("out range: "..k)
            else
                node.value = v
            end
        end,
        __tostring = function()
            local ret = {}
            local next = first.next
            while next and next ~= last do
                ret[#ret+1] = next.value
                next = next.next
            end

            return table.concat(ret, ',')
        end,
        __len = function(v)
            return lenght
        end,
        --迭代器返回node-value
        __ipairs = function(i_t)
            local idx = 0
            local function iter(i_t, node)
                idx = idx + 1
                if node and node.next ~= last then
                    return node.next, idx, node.next.value
                end
            end

            return iter, t, first
        end,
        __pairs = function(i_t)
            local function iter(i_t, node)
                if node and node.next ~= last then
                    return node.next, node.next.value
                end
            end

            return iter, t, first
        end
    }

    setmetatable(t, mt)

    return t
end

return list