@@ -279,17 +279,23 @@ local Grouping = define("System.Linq.Grouping", function (TKey, TElement)
279279 }
280280end , 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
286291end
287292
288293local 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)
316322end , Lookup , 2 )
317323
318324local 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 )
332327end
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 )
436431local 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 )
0 commit comments