Skip to content

Commit 502919a

Browse files
authored
Merge pull request #519 from Orden4/base/master
Fix a few issues with HashSet/ordering.
2 parents cef7aae + d17c352 commit 502919a

File tree

7 files changed

+499
-26
lines changed

7 files changed

+499
-26
lines changed

CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Dictionary.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,7 @@ local Dictionary = (function ()
375375
end,
376376
TryAdd = function (this, key, value)
377377
if key == nil then throw(ArgumentNullException("key")) end
378-
local exists = this:TryGetValue(key)
379-
if exists then
378+
if this:ContainsKey(key) then
380379
return false
381380
end
382381
this:set(key, value)
@@ -708,8 +707,7 @@ local ArrayDictionary = (function ()
708707
end,
709708
TryAdd = function (this, key, value)
710709
if key == nil then throw(ArgumentNullException("key")) end
711-
local exists = this:TryGetValue(key)
712-
if exists then
710+
if this:ContainsKey(key) then
713711
return false
714712
end
715713
this:set(key, value)

CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/HashSet.lua

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,11 @@ end, {
7070
}, 1)
7171

7272
local HashSet = {
73-
__ctor__ = function (this, ...)
74-
local n = select("#", ...)
75-
local collection, comparer
76-
if n == 1 then
77-
local c = ...
78-
if type(c) == "table" then
79-
if c.GetEnumerator then
80-
collection = c
81-
end
82-
comparer = c
83-
end
84-
elseif n == 2 then
85-
collection, comparer = ...
73+
__ctor__ = function (this, collection, comparer)
74+
if type(collection) ~= "table" then
75+
collection = nil
76+
elseif not collection.GetEnumerator then
77+
collection, comparer = nil, collection
8678
end
8779
this.dict = System.Dictionary(this.__genericT__, System.Boolean)(comparer)
8880
if collection then
@@ -236,7 +228,35 @@ local HashSet = {
236228
return match(unWrap(k))
237229
end)
238230
end,
239-
TrimExcess = System.emptyFn
231+
TrimExcess = System.emptyFn,
232+
CopyTo = function (this, array, arrayIndex, count)
233+
arrayIndex = arrayIndex or 0
234+
count = count or this:getCount()
235+
if array == nil then
236+
throw(ArgumentNullException("array"))
237+
end
238+
if arrayIndex < 0 then
239+
throw(System.ArgumentOutOfRangeException("arrayIndex"))
240+
end
241+
if count < 0 then
242+
throw(System.ArgumentOutOfRangeException("count"))
243+
end
244+
local arrayCount = array:getCount()
245+
if arrayIndex >= arrayCount then
246+
throw(System.ArgumentException("arrayIndex"))
247+
end
248+
if arrayIndex + count > arrayCount then
249+
throw(System.ArgumentException("count is greater than the available space from arrayIndex to the end of the destination array"))
250+
end
251+
count = count + arrayIndex
252+
for _, kv in each(this.dict) do
253+
if arrayIndex >= count then
254+
return
255+
end
256+
array:set(arrayIndex, kv[1])
257+
arrayIndex = arrayIndex + 1
258+
end
259+
end
240260
}
241261

242262
function System.hashSetFromTable(t, T)
@@ -245,7 +265,7 @@ end
245265

246266
System.HashSet = System.define("System.Collections.Generic.HashSet", function(T)
247267
return {
248-
base = { System.ICollection_1(T), System.ISet_1(T) },
268+
base = { System.ISet_1(T), System.IReadOnlySet_1(T) },
249269
__genericT__ = T,
250270
__genericTKey__ = T,
251271
}

CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/Linq.lua

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ local isArrayLike = System.isArrayLike
2828
local isDictLike = System.isDictLike
2929
local Array = System.Array
3030
local arrayEnumerator = Array.GetEnumerator
31+
local null = System.null
3132

3233
local NullReferenceException = System.NullReferenceException
3334
local ArgumentNullException = System.ArgumentNullException
@@ -436,10 +437,11 @@ local function orderBy(source, keySelector, comparer, TKey, descending)
436437
if comparer == nil then comparer = Comparer_1(TKey).getDefault() end
437438
local keys = {}
438439
local function getKey(t)
439-
local k = keys[t]
440+
local item = wrap(t)
441+
local k = keys[item]
440442
if k == nil then
441443
k = keySelector(t)
442-
keys[t] = k
444+
keys[item] = k
443445
end
444446
return k
445447
end
@@ -485,10 +487,11 @@ local function thenBy(source, keySelector, comparer, TKey, descending)
485487
if comparer == nil then comparer = Comparer_1(TKey).getDefault() end
486488
local keys = {}
487489
local function getKey(t)
488-
local k = keys[t]
490+
local item = wrap(t)
491+
local k = keys[item]
489492
if k == nil then
490493
k = keySelector(t)
491-
keys[t] = k
494+
keys[item] = k
492495
end
493496
return k
494497
end

CSharp.lua/CoreSystem.Lua/CoreSystem/Collections/SortedSet.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ local SortedSet = {
4848

4949
local SortedSetFn = System.define("System.Collections.Generic.SortedSet", function(T)
5050
return {
51-
base = { System.ICollection_1(T), System.IReadOnlyCollection_1(T), System.ISet_1(T) },
51+
base = { System.ISet_1(T), System.IReadOnlySet_1(T) },
5252
__genericT__ = T,
5353
}
5454
end, SortedSet, 1)

CSharp.lua/CoreSystem.Lua/CoreSystem/Interfaces.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ defInf("System.ISet_1", function(T)
104104
}
105105
end)
106106

107+
defInf("System.IReadOnlySet_1", function(T)
108+
return {
109+
base = { IReadOnlyCollection_1(T) }
110+
}
111+
end)
112+
107113
defInf("System.IComparer")
108114
defInf("System.IComparer_1", emptyFn)
109115
defInf("System.IEqualityComparer")

0 commit comments

Comments
 (0)