Repository navigation
Expand file tree
/
Copy pathCollection.lua
More file actions
778 lines (684 loc) · 17 KB
/
Copy pathCollection.lua
File metadata and controls
778 lines (684 loc) · 17 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
--[[
Copyright 2017 YANG Huan ([email protected]).
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--]]
local System = System
local throw = System.throw
local sr = System.sr
local ArgumentOutOfRangeException = System.ArgumentOutOfRangeException
local InvalidOperationException = System.InvalidOperationException
local ArgumentNullException = System.ArgumentNullException
local NullReferenceException = System.NullReferenceException
local EqualityComparer_1 = System.EqualityComparer_1
local Comparer_1 = System.Comparer_1
local tinsert = table.insert
local tremove = table.remove
local tsort = table.sort
local tmove = table.move
local unpack = table.unpack
local setmetatable = setmetatable
local select = select
local type = type
local assert = assert
local coroutine = coroutine
local ccreate = coroutine.create
local cstatus = coroutine.status
local cresume = coroutine.resume
local ipairs = ipairs
local Collection = {}
local null = {}
local versons = setmetatable({}, { __mode = "k" })
local function getVersion(t)
return versons[t] or 0
end
local function changeVersion(t)
local verson = getVersion(t)
versons[t] = verson + 1
end
local function checkVersion(t, verson)
if verson ~= getVersion(t) then
throw(InvalidOperationException("has change when iterator"))
end
end
Collection.getVersion = getVersion
Collection.changeVersion = changeVersion
Collection.checkVersion = checkVersion
local counts = setmetatable({}, { __mode = "k" })
local function getCount(t)
return counts[t] or 0
end
local function addCount(t, inc)
if inc ~= 0 then
local v = (counts[t] or 0) + inc
assert(v >= 0)
counts[t] = v
end
end
local function clearCount(t)
counts[t] = nil
end
Collection.getCount = getCount
Collection.addCount = addCount
Collection.clearCount = clearCount
local function checkIndex(t, index)
if index < 0 or index >= #t then
throw(ArgumentOutOfRangeException("index"))
end
end
local function checkIndexAndCount(t, index, count)
if count < 0 or index > #t - count then
throw(ArgumentOutOfRangeException("index or count"))
end
end
Collection.checkIndex = checkIndex
Collection.checkIndexAndCount = checkIndexAndCount
local function wrap(v)
if v ~= nil then
return v
end
return null
end
local function unWrap(v)
if v ~= null then
return v
end
return nil
end
Collection.wrap = wrap
Collection.unWrap = unWrap
function Collection.getArray(t, index)
checkIndex(t, index)
local item = t[index + 1]
return unWrap(item)
end
function Collection.setArray(t, index, v)
checkIndex(t, index)
t[index + 1] = wrap(v)
changeVersion(t)
end
function Collection.pushArray(t, v)
tinsert(t, wrap(v))
changeVersion(t)
end
function Collection.buildArray(t, size, ...)
local len = select("#", ...)
for i = 1, len do
local v = select(i, ...)
tinsert(t, wrap(v))
end
if len < size then
local default = t.__genericT__.__default__()
if default == nil then
default = null
end
for i = len + 1, size do
tinsert(t, default)
end
end
end
local function checkInsertIndex(t, index)
if index < 0 or index > #t then
throw(ArgumentOutOfRangeException("index"))
end
end
function Collection.insertArray(t, index, v)
checkInsertIndex(t, index)
tinsert(t, index + 1, wrap(v))
changeVersion(t)
end
function Collection.removeArrayAll(t)
local size = #t
if size > 0 then
for i = 1, size do
t[i] = nil
end
changeVersion(t)
end
end
local function copyArray(sourceArray, sourceIndex, destinationArray, destinationIndex, length, reliable)
if not reliable then
if sourceArray == nil then throw(ArgumentNullException("sourceArray")) end
if destinationArray == nil then throw(ArgumentNullException("destinationArray")) end
checkIndexAndCount(sourceArray, sourceIndex, length)
checkIndexAndCount(destinationArray, destinationIndex, length)
end
tmove(sourceArray, sourceIndex + 1, sourceIndex + length, destinationIndex + 1, destinationArray)
end
function Collection.copyArray(t, ...)
local len = select("#", ...)
if len == 2 then
local array, length = ...
copyArray(t, 0, array, 0, length)
else
copyArray(t, ...)
end
end
function Collection.removeArray(t, index, count)
checkIndexAndCount(t, index, count)
if count > 0 then
local size = #t - count
if index < size then
copyArray(t, index + count, t, index, size - index)
end
for i = size + 1, size + count do
t[i] = nil
end
changeVersion(t)
end
end
function Collection.removeAtArray(t, index)
checkIndex(t, index)
tremove(t, index + 1)
changeVersion(t)
end
local function binarySearchArray(t, index, count, v, comparer)
checkIndexAndCount(t, index, count)
local compare
if comparer == nil then
compare = Comparer_1(t.__genericT__).getDefault().Compare
else
compare = comparer.compare
end
local lo = index
local hi = index + count - 1
while lo <= hi do
local i = lo + sr(hi - lo, 1)
local order = compare(unWrap(t[i + 1]), v);
if order == 0 then return i end
if order < 0 then
lo = i + 1
else
hi = i - 1
end
end
return -1
end
function Collection.binarySearchArray(t, ...)
local v, index, count, comparer
local len = select("#", ...)
if len == 1 then
v = ...
index = 0
count = #t
elseif len == 2 then
v, index = ...
count = #t - index
elseif len == 3 then
index, count, v = ...
else
index, count, v, comparer = ...
end
return binarySearchArray(t, index, count, v, comparer)
end
local function findIndexOfArray(t, ...)
local startIndex, count, match
local len = select("#", ...)
if len == 1 then
startIndex = 0
count = #t
match = ...
elseif len == 2 then
startIndex, match = ...
count = #t - startIndex
else
startIndex, count, match = ...
end
if match == nil then
throw(ArgumentNullException("match"))
end
checkIndexAndCount(t, startIndex, count)
local endIndex = startIndex + count
for i = startIndex + 1, endIndex do
local item = unWrap(t[i])
if match(item) then
return i - 1
end
end
return -1
end
Collection.findIndexOfArray = findIndexOfArray
function Collection.existsOfArray(t, match)
return findIndexOfArray(t, match) ~= -1
end
function Collection.findOfArray(t, match)
if match == nil then
throw(ArgumentNullException("match"))
end
for _, i in ipairs(t) do
local item = unWrap(i)
if match(item) then
return item
end
end
return t.__genericT__.__default__()
end
function Collection.findAllOfArray(t, match)
if match == nil then
throw(ArgumentNullException("match"))
end
local list = System.List(t.__genericT__)()
for _, i in ipairs(t) do
local item = unWrap(i)
if match(item) then
list:Add(item)
end
end
return list
end
function Collection.findLastOfArray(t, match)
if match == nil then
throw(ArgumentNullException("match"))
end
for i = #t, 1, -1 do
local item = unWrap(t[i])
if match(item) then
return item
end
end
return t.__genericT__.__default__()
end
function Collection.findLastIndexOfArray(t, ...)
local startIndex, count, match
local len = select("#", ...)
if len == 1 then
startIndex = 0
count = #t
match = ...
elseif len == 2 then
startIndex, match = ...
count = #t - startIndex
else
startIndex, count, match = ...
end
if match == nil then
throw(ArgumentNullException("match"))
end
if count < 0 or startIndex - count + 1 < 0 then
throw(ArgumentOutOfRangeException("count"))
end
local endIndex = startIndex - count
checkIndex(endIndex)
for i = startIndex + 1, endIndex, -1 do
local item = unWrap(t[i])
if match(item) then
return i - 1
end
end
return -1
end
local function indexOfArray(t, v, index, count)
checkIndexAndCount(t, index, count)
local equals = EqualityComparer_1(t.__genericT__).getDefault().Equals
for i = index + 1, index + count do
if equals(unWrap(t[i]), v) then
return i - 1
end
end
return -1
end
function Collection.indexOfArray(t, ...)
local v, index, count
local len = select("#", ...)
if len == 1 then
v = ...
index = 0
count = #t
elseif len == 2 then
v, index = ...
count = #t - index
else
v, index, count = ...
end
return indexOfArray(t, v, index, count)
end
function Collection.contains(t, item)
return indexOfArray(t, item, 0, #t) ~= -1
end
local function lastIndexOfArray(t, v, index, count)
if count < 0 or index - count + 1 < 0 then
throw(ArgumentOutOfRangeException("count"))
end
checkIndex(t, index - count)
local equals = EqualityComparer_1(t.__genericT__).getDefault().Equals
for i = index + 1, index - count, -1 do
if equals(unWrap(t[i]), v) then
return i - 1
end
end
return -1
end
function Collection.lastIndexOfArray(t, ...)
local v, index, count
local len = select("#", ...)
if len == 1 then
v = ...
count = #t
index = count - 1
elseif len == 2 then
v, index = ...
count = #t == 0 and 0 or (index + 1)
else
v, index, count = ...
end
return lastIndexOfArray(t, v, index, count)
end
function Collection.reverseArray(t, index, count)
if not index then
index = 0
count = #t
end
checkIndexAndCount(t, index, count)
local i, j = index + 1, index + count
while i <= j do
t[i], t[j] = t[j], t[i]
i = i + 1
j = j - 1
end
changeVersion(t)
end
local function getComp(t, comparer)
local compare
if comparer == nil then
compare = Comparer_1(t.__genericT__).getDefault().Compare
elseif comparer.Compare then
compare = comparer.Compare
else
compare = comparer
end
return function(x, y)
return compare(unWrap(x), unWrap(y)) < 0
end
end
local function sort(t, comparer)
if #t > 1 then
tsort(t, getComp(t, comparer))
changeVersion(t)
end
end
Collection.sort = sort
local function sortArray(t, index, count, comparer)
if count > 1 then
local comp = getComp(t, comparer)
if index == 0 and count == #t then
tsort(t, comp)
else
checkIndexAndCount(t, index, count)
local arr = {}
for i = index + 1, index + count do
tinsert(arr, t[i])
end
tsort(arr, comp)
for i = index + 1, index + count do
t[i] = arr[i - index]
end
end
changeVersion(t)
end
end
function Collection.sortArray(t, ...)
local len = select("#", ...)
if len == 0 then
sort(t)
elseif len == 1 then
local comparer = ...
sort(t, comparer)
else
index, count, comparer = ...
sortArray(t, index, count, comparer)
end
end
function Collection.trueForAllOfArray(t, match)
if match == nil then
throw(ArgumentNullException("match"))
end
for _, i in ipairs(t) do
if not match(unWrap(i)) then
return false
end
end
return true
end
local ipairsFn = ipairs(null)
function ipairsArray(t)
local version = getVersion(t)
return function(t, inx)
checkVersion(t, version)
local k, v = ipairsFn(t, inx)
return k, unWrap(v)
end, t, 0
end
Collection.ipairs = ipairsArray
local pairsFn = next
function Collection.pairs(t)
local version = getVersion(t)
return function(t, inx)
checkVersion(t, version)
local k, v = pairsFn(t, inx)
return k, unWrap(v)
end, t, nil
end
function Collection.forEachArray(t, action)
if action == null then
throw(ArgumentNullException("action"))
end
local verson = getVersion(t)
for _, i in ipairs(t) do
checkVersion(t, verson)
action(unWrap(i))
end
end
local ArrayEnumerator = {}
ArrayEnumerator.__index = ArrayEnumerator
function ArrayEnumerator.MoveNext(this)
local t = this.list
checkVersion(t, this.verson)
local index = this.index
if index < #t then
local i, v = ipairsFn(t, index)
this.current = unWrap(v)
this.index = i
return true
end
this.current = nil
return false
end
function ArrayEnumerator.getCurrent(this)
return this.current
end
function ArrayEnumerator.Reset(this)
this.index = 0
this.current = nil
end
local function arrayEnumerator(t)
local en = {
list = t,
index = 0,
verson = getVersion(t),
}
setmetatable(en, ArrayEnumerator)
return en
end
Collection.arrayEnumerator = arrayEnumerator
local function isArrayLike(t)
return t.GetEnumerator == arrayEnumerator
end
Collection.isArrayLike = isArrayLike
function Collection.isEnumerableLike(t)
return type(t) == "table" and t.GetEnumerator ~= nil
end
local function eachFn(en)
if en:MoveNext() then
return true, en:getCurrent()
end
return nil
end
local function each(t)
if t == nil then
throw(NullReferenceException(), 1)
end
if isArrayLike(t) then
return ipairsArray(t)
end
local en = t:GetEnumerator()
return eachFn, en
end
Collection.each = each
function Collection.insertRangeArray(t, index, collection)
if collection == nil then
throw(ArgumentNullException("collection"))
end
checkInsertIndex(t, index)
for _, v in each(collection) do
index = index + 1
tinsert(t, index, wrap(v))
end
changeVersion(t)
end
function Collection.toArray(t)
local array = {}
if isArrayLike(t) then
for _, v in ipairs(t) do
tinsert(array, v)
end
else
for _, v in each(t) do
tinsert(array, wrap(v))
end
end
return System.arrayFromTable(array, t.__genericT__)
end
local function toLuaTable(array)
local t = {}
for i, v in ipairs(array) do
t[i] = unWrap(v)
end
return t
end
local DictionaryEnumerator = {}
DictionaryEnumerator.__index = DictionaryEnumerator
function DictionaryEnumerator.MoveNext(this)
local t = this.dict
checkVersion(t, this.version)
local k, v = pairsFn(t, this.index)
if k ~= nil then
if this.kind == 0 then
local pair = this.pair
pair.key = k
pair.value = unWrap(v)
this.current = pair
elseif this.kind == 1 then
this.current = unWrap(k)
else
this.current = unWrap(v)
end
this.index = k
return true
end
this.current = nil
return false
end
function DictionaryEnumerator.getCurrent(this)
return this.current
end
function Collection.dictionaryEnumerator(t, kind)
local en = {
dict = t,
version = getVersion(t),
kind = kind,
pair = kind == 0 and { key = false, value = false } or nil
}
setmetatable(en, DictionaryEnumerator)
return en
end
local LinkedListEnumerator = {}
LinkedListEnumerator.__index = LinkedListEnumerator
function LinkedListEnumerator.MoveNext(this)
local list = this.list
local node = this.node
checkVersion(list, this.version)
if node == nil then
return false
end
this.current = node.Value
node = node.next
if node == list.head then
node = nil
end
this.node = node
return true
end
function LinkedListEnumerator.getCurrent(this)
return this.current
end
function Collection.linkedListEnumerator(t)
local en = {
list = t,
version = getVersion(t),
node = t.head
}
setmetatable(en, LinkedListEnumerator)
return en
end
local YieldEnumerator = {}
YieldEnumerator.__inherits__ = { System.IEnumerator }
function YieldEnumerator.MoveNext(this)
local co = this.co
if cstatus(co) == "dead" then
this.current = nil
return false
else
local args = this.args
local ok, v
if args then
ok, v = cresume(co, unpack(args))
this.args = nil
else
ok, v = cresume(co)
end
if ok then
if cstatus(co) == "dead" then
this.current = nil
return false
end
this.current = v
return true
else
throw(v)
end
end
end
function YieldEnumerator.getCurrent(this)
return this.current
end
System.define("System.YieldEnumerator", YieldEnumerator)
function Collection.yieldIEnumerator(f, T, ...)
return setmetatable({ co = ccreate(f), __genericT__ = T, args = { ... } }, YieldEnumerator)
end
local YieldEnumerable = {}
YieldEnumerable.__inherits__ = { System.IEnumerable }
function YieldEnumerable.GetEnumerator(this)
return setmetatable({ co = ccreate(this.f), __genericT__ = this.__genericT__, args = this.args }, YieldEnumerator)
end
System.define("System.YieldEnumerable", YieldEnumerable)
function Collection.yieldIEnumerable(f, T, ...)
return setmetatable({ f = f, __genericT__ = T, args = { ... } }, YieldEnumerable)
end
Collection.yieldReturn = coroutine.yield
System.Collection = Collection
System.each = Collection.each
System.ipairs = Collection.ipairs
System.pairs = Collection.pairs
System.isArrayLike = Collection.isArrayLike
System.isEnumerableLike = Collection.isEnumerableLike
System.yieldIEnumerable = Collection.yieldIEnumerable
System.yieldIEnumerator = Collection.yieldIEnumerator
System.yieldReturn = Collection.yieldReturn
System.toLuaTable = toLuaTable