@@ -4,12 +4,10 @@ return function(
4
4
-- key_function to map elements to integer keys, defaults to identity
5
5
key_function
6
6
)
7
- -- Default to identity function if no key_function is provided
8
- key_function = key_function or function (x ) return x end
9
-
10
- -- Handle empty list case
11
7
if list [1 ] == nil then return end
12
8
9
+ key_function = key_function or function (x ) return x end
10
+
13
11
-- Find the range of keys (min_key and max_key)
14
12
local min_key , max_key = math.huge , - math.huge
15
13
for _ , elem in ipairs (list ) do
@@ -18,7 +16,6 @@ return function(
18
16
max_key = math.max (max_key , key )
19
17
end
20
18
21
- -- Initialize the count array
22
19
local count = {}
23
20
for i = 1 , (max_key - min_key + 1 ) do
24
21
count [i ] = 0
@@ -35,7 +32,6 @@ return function(
35
32
count [i ] = count [i ] + count [i - 1 ]
36
33
end
37
34
38
- -- Build the output array (in stable order)
39
35
local output = {}
40
36
for i = # list , 1 , - 1 do
41
37
local element = list [i ]
@@ -44,7 +40,6 @@ return function(
44
40
count [key - min_key + 1 ] = count [key - min_key + 1 ] - 1
45
41
end
46
42
47
- -- Copy the output array back to the original list
48
43
for i , elem in ipairs (output ) do
49
44
list [i ] = elem
50
45
end
0 commit comments