Skip to content

Commit 5e48b8f

Browse files
committed
Simplify PriorityQueue
1 parent 8c4099d commit 5e48b8f

2 files changed

Lines changed: 34 additions & 44 deletions

File tree

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/PriorityQueue.lua‎

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,24 @@ local throw = System.throw
2020
local each = System.each
2121
local er = System.er
2222
local Array = System.Array
23+
local Comparer_1 = System.Comparer_1
2324

2425
local ArgumentNullException = System.ArgumentNullException
2526
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
2627
local InvalidOperationException = System.InvalidOperationException
2728

28-
local Comparer_1 = System.Comparer_1
29-
3029
local select = select
3130
local type = type
3231

33-
local function fixDown(t, k, n, comparer, c)
34-
if not comparer then
35-
comparer = t.comparer
36-
c = comparer.Compare
37-
end
32+
local function down(t, k, n, c)
3833
local j
3934
while true do
4035
j = k * 2
4136
if j <= n and j > 0 then
42-
if j < n and c(comparer, t[j][2], t[j + 1][2]) > 0 then
37+
if j < n and c(t[j], t[j + 1]) > 0 then
4338
j = j + 1
4439
end
45-
if c(comparer, t[k][2], t[j][2]) <= 0 then
40+
if c(t[k], t[j]) <= 0 then
4641
break
4742
end
4843
t[j], t[k] = t[k], t[j]
@@ -53,14 +48,10 @@ local function fixDown(t, k, n, comparer, c)
5348
end
5449
end
5550

56-
local function fixUp(t, k, n, comparer, c)
57-
if not comparer then
58-
comparer = t.comparer
59-
c = comparer.Compare
60-
end
51+
local function up(t, k, n, c)
6152
while k > 1 do
6253
local j = div(k, 2)
63-
if c(comparer, t[j][2], t[k][2]) <= 0 then
54+
if c(t[j], t[k]) <= 0 then
6455
break
6556
end
6657
t[j], t[k] = t[k], t[j]
@@ -69,11 +60,10 @@ local function fixUp(t, k, n, comparer, c)
6960
end
7061

7162
local function heapify(t)
72-
local comparer = t.comparer
73-
local c = comparer.Compare
63+
local c = t.c
7464
local n = #t
7565
for i = div(n, 2), 1, -1 do
76-
fixDown(t, i, n, comparer, c)
66+
down(t, i, n, c)
7767
end
7868
end
7969

@@ -86,7 +76,8 @@ local function __ctor__(t, ...)
8676
if items == nil then throw(ArgumentNullException("items")) end
8777
end
8878
if comparer == nil then comparer = Comparer_1(t.__genericTPriority__).getDefault() end
89-
t.comparer = comparer
79+
local compare = comparer.Compare
80+
t.comparer, t.c = comparer, function (a, b) return compare(comparer, a[2], b[2]) end
9081
if type(items) == "table" then
9182
local i = 1
9283
for _, tuple in each(items) do
@@ -104,25 +95,25 @@ local function dequeue(t, n)
10495
local v = t[1][1]
10596
t[1] = t[n]
10697
t[n] = nil
107-
fixDown(t, 1, n -1)
98+
down(t, 1, n - 1, t.c)
10899
return v
109100
end
110101

111102
local function enqueue(t, element, priority)
112103
local n = #t + 1
113104
t[n] = { element, priority }
114-
fixUp(t, n, n)
105+
up(t, n, n, t.c)
115106
end
116107

117108
local function enqueueDequeue(t, element, priority)
118109
local n = #t
119110
if n ~= 0 then
120-
local comparer = t.comparer
121-
local c = comparer.Compare
111+
local c = t.c
112+
local item = { element, priority }
122113
local root = t[1]
123-
if c(comparer, priority, root[2]) > 0 then
124-
t[1] = { element, priority }
125-
fixDown(t, 1, n, comparer, c)
114+
if c(item, root) > 0 then
115+
t[1] = item
116+
down(t, 1, n, c)
126117
return root[1]
127118
end
128119
end
@@ -152,22 +143,21 @@ local function enqueueRange(t, ...)
152143
heapify(t)
153144
end
154145
else
155-
local comparer = t.comparer
156-
local c = comparer.Compare
146+
local c = t.c
157147
if len == 1 then
158148
local items = ...
159149
for _, tuple in each(items) do
160150
local element, priority = tuple[1], tuple[2]
161151
n = n + 1
162152
t[n] = { element, priority }
163-
fixUp(t, n, n, comparer, c)
153+
up(t, n, n, c)
164154
end
165155
else
166156
local elements, priority = ...
167157
for _, element in each(elements) do
168158
n = n + 1
169159
t[n] = { element, priority }
170-
fixUp(t, n, n, comparer, c)
160+
up(t, n, n, c)
171161
end
172162
end
173163
end

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Threading/Timer.lua‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,16 @@ local setTimeout = config.setTimeout
3030
local clearTimeout = config.clearTimeout
3131

3232
if setTimeout and clearTimeout then
33-
System.post = function (fn)
34-
setTimeout(fn, 0)
35-
end
33+
System.post = function (fn)
34+
setTimeout(fn, 0)
35+
end
3636
else
37-
System.post = function (fn)
38-
fn()
39-
end
40-
local function notset()
41-
throw(NotImplementedException("System.config.setTimeout or clearTimeout is not registered."))
42-
end
37+
System.post = function (fn)
38+
fn()
39+
end
40+
local function notset()
41+
throw(NotImplementedException("System.config.setTimeout or clearTimeout is not registered."))
42+
end
4343
setTimeout = notset
4444
clearTimeout = notset
4545
end
@@ -120,11 +120,11 @@ local TimeoutQueue = define("System.TimeoutQueue", (function ()
120120
Contains = function (this, id)
121121
return this.ids_[id] ~= nil
122122
end
123-
IsNext = function (this, id)
124-
local first = this.events_:getFirst()
125-
local nextId = first and first.Value.Id
126-
return nextId == id
127-
end
123+
IsNext = function (this, id)
124+
local first = this.events_:getFirst()
125+
local nextId = first and first.Value.Id
126+
return nextId == id
127+
end
128128
return {
129129
MaxExpiration = maxExpiration,
130130
nextId_ = 1,

0 commit comments

Comments
 (0)