lua-users home
lua-l archive

Re: bug in table.sort (lua 5.1.3)

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> The fix:
> 
> [...]
> - if (i>u) luaL_error(L, "invalid order function for sorting");
> + if (i>=u) luaL_error(L, "invalid order function for sorting");
> lua_pop(L, 1); /* remove a[i] */
> [...]
> - if (j<l) luaL_error(L, "invalid order function for sorting");
> + if (j<=l) luaL_error(L, "invalid order function for sorting");
The first change is ok, but I am not sure about the second. The first
part of the code arranges for a[l] and a[u] to act as sentinels. Because
the pivot goes to a[u-1], it works as a sentinel too. So, i should never
go up to u. But in the lower end the sentinel is still a[l]. So, when
there are no elements left that are smaller than the pivot, it is ok
for j to go down up to l (the sentinel). Of course, this stops the loop
(because j<i), but it does not mean an error in the order function.
-- Roberto

AltStyle によって変換されたページ (->オリジナル) /