fix #476 · Drake53/CSharp.lua@9844824 · GitHub
Skip to content

Commit 9844824

Browse files
committed
1 parent 7191013 commit 9844824

5 files changed

Lines changed: 44 additions & 52 deletions

File tree

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

Lines changed: 4 additions & 4 deletions

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

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,21 @@ local define = System.define
1919
local throw = System.throw
2020
local equalsObj = System.equalsObj
2121
local compareObj = System.compareObj
22+
local hashObj = System.hashObj
2223
local ArgumentException = System.ArgumentException
2324
local ArgumentNullException = System.ArgumentNullException
2425

2526
local type = type
2627

2728
local EqualityComparer
2829
EqualityComparer = define("System.EqualityComparer", function (T)
29-
local equals
30-
local Equals = T.Equals
31-
if Equals then
32-
if T.class == 'S' then
33-
equals = Equals
34-
else
35-
equals = function (x, y)
36-
return x:Equals(y)
37-
end
38-
end
30+
local equals, getHashCode
31+
if T.class == 'S' then
32+
equals = T.Equals or equalsObj
33+
getHashCode = T.GetHashCode
3934
else
40-
equals = equalsObj
41-
end
42-
local function getHashCode(x)
43-
if type(x) == "table" then
44-
return x:GetHashCode()
45-
end
46-
return x
35+
equals = T.Equals and function (x, y) return x:Equals(y) end or equalsObj
36+
getHashCode = hashObj
4737
end
4838
local defaultComparer
4939
return {

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

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,23 @@ local Grouping = define("System.Linq.Grouping", function (TKey, TElement)
279279
}
280280
end, nil, 2)
281281

282-
local function getGrouping(this, key)
283-
local hashCode = this.comparer:GetHashCodeOf(key)
284-
local groupIndex = this.indexs[hashCode]
285-
return this.groups[groupIndex]
282+
local function getGrouping(this, key, create)
283+
local t = this.groups
284+
local found, group = t:TryGetValue(key, true)
285+
if found then return group end
286+
if create then
287+
group = setmetatable({ key = key }, Grouping(this.__genericTKey__, this.__genericTElement__))
288+
t[#t + 1] = setmetatable({ key, group }, t.__genericT__)
289+
end
290+
return group
286291
end
287292

288293
local Lookup = {
289294
__ctor__ = function (this, comparer)
290-
this.comparer = comparer or EqualityComparer(this.__genericTKey__).getDefault()
291-
this.groups = {}
292-
this.indexs = {}
295+
local TKey = this.__genericTKey__
296+
comparer = comparer or EqualityComparer(TKey).getDefault()
297+
local G = Grouping(TKey, this.__genericTElement__)
298+
this.groups = System.Dictionary(TKey, G)(comparer)
293299
end,
294300
get = function (this, key)
295301
local grouping = getGrouping(this, key)
@@ -303,7 +309,7 @@ local Lookup = {
303309
return getGrouping(this, key) ~= nil
304310
end,
305311
GetEnumerator = function (this)
306-
return arrayEnumerator(this.groups, IGrouping)
312+
return this.groups:getValues():GetEnumerator()
307313
end
308314
}
309315

@@ -316,18 +322,7 @@ local LookupFn = define("System.Linq.Lookup", function(TKey, TElement)
316322
end, Lookup, 2)
317323

318324
local function addToLookup(this, key, value)
319-
local hashCode = this.comparer:GetHashCodeOf(key)
320-
local groupIndex = this.indexs[hashCode]
321-
local group
322-
if groupIndex == nil then
323-
groupIndex = #this.groups + 1
324-
this.indexs[hashCode] = groupIndex
325-
group = setmetatable({ key = key }, Grouping(this.__genericTKey__, this.__genericTElement__))
326-
this.groups[groupIndex] = group
327-
else
328-
group = this.groups[groupIndex]
329-
assert(group)
330-
end
325+
local group = getGrouping(this, key, true)
331326
group[#group + 1] = wrap(value)
332327
end
333328

@@ -400,15 +395,15 @@ local function ordered(source, compare)
400395
local orderedEnumerable = createEnumerable(T, function()
401396
local t = {}
402397
local index = 0
403-
return createEnumerator(T, source, function()
398+
return createEnumerator(T, source, function()
404399
index = index + 1
405400
local v = t[index]
406401
if v ~= nil then
407402
return true, unWrap(v)
408403
end
409404
return false
410-
end,
411-
function()
405+
end,
406+
function()
412407
local count = 1
413408
if isDictLike(source) then
414409
for k, v in pairs(source) do
@@ -423,7 +418,7 @@ local function ordered(source, compare)
423418
end
424419
if count > 1 then
425420
tsort(t, function(x, y)
426-
return compare(unWrap(x), unWrap(y)) < 0
421+
return compare(unWrap(x), unWrap(y)) < 0
427422
end)
428423
end
429424
end)
@@ -436,9 +431,9 @@ end
436431
local function orderBy(source, keySelector, comparer, TKey, descending)
437432
if source == nil then throw(ArgumentNullException("source")) end
438433
if keySelector == nil then throw(ArgumentNullException("keySelector")) end
439-
if comparer == nil then comparer = Comparer_1(TKey).getDefault() end
434+
if comparer == nil then comparer = Comparer_1(TKey).getDefault() end
440435
local keys = {}
441-
local function getKey(t)
436+
local function getKey(t)
442437
local k = keys[t]
443438
if k == nil then
444439
k = keySelector(t)
@@ -487,7 +482,7 @@ local function thenBy(source, keySelector, comparer, TKey, descending)
487482
if keySelector == nil then throw(ArgumentNullException("keySelector")) end
488483
if comparer == nil then comparer = Comparer_1(TKey).getDefault() end
489484
local keys = {}
490-
local function getKey(t)
485+
local function getKey(t)
491486
local k = keys[t]
492487
if k == nil then
493488
k = keySelector(t)

‎CSharp.lua/CoreSystem.Lua/CoreSystem/Core.lua‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,13 +1203,23 @@ local function hash(v)
12031203
return addr(v)
12041204
end
12051205

1206+
local function hashObj(obj)
1207+
if obj == nil then return 0 end
1208+
local t = type(obj)
1209+
if t == "table" then
1210+
return obj:GetHashCode()
1211+
end
1212+
return hash(obj)
1213+
end
1214+
12061215
System.hasHash = function (t)
12071216
return t.GetHashCode ~= hash
12081217
end
12091218

12101219
System.equalsObj = equalsObj
12111220
System.compareObj = compareObj
12121221
System.hash = hash
1222+
System.hashObj = hashObj
12131223
System.toString = toString
12141224

12151225
Object = defCls("System.Object", {
@@ -1261,6 +1271,7 @@ ValueType = defCls("System.ValueType", {
12611271
end
12621272
end,
12631273
EqualsObj = function (this, obj)
1274+
if this == obj then return true end
12641275
if getmetatable(this) ~= getmetatable(obj) then return false end
12651276
for k, v in pairs(this) do
12661277
if not equalsObj(v, obj[k]) then

‎CSharp.lua/CoreSystem.Lua/Sample/test.lua‎

Lines changed: 0 additions & 4 deletions

0 commit comments

Comments
 (0)