@@ -20,29 +20,24 @@ local throw = System.throw
2020local each = System .each
2121local er = System .er
2222local Array = System .Array
23+ local Comparer_1 = System .Comparer_1
2324
2425local ArgumentNullException = System .ArgumentNullException
2526local ArgumentOutOfRangeException = System .ArgumentOutOfRangeException
2627local InvalidOperationException = System .InvalidOperationException
2728
28- local Comparer_1 = System .Comparer_1
29-
3029local select = select
3130local 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
5449end
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)
6960end
7061
7162local 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
7868end
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
109100end
110101
111102local 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 )
115106end
116107
117108local 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
0 commit comments