diff options
| author | Hisham <hisham@gobolinux.org> | 2016-07-28 18:15:13 -0300 |
|---|---|---|
| committer | Hisham <hisham@gobolinux.org> | 2016-07-28 18:15:13 -0300 |
| commit | 628779b5a2357685221f62141bec22c0e84a24f1 (patch) | |
| tree | ba05946ce1d29ebdeb5890cb8cdfeef7e00cba68 /src | |
| parent | 813ad75a7f87d96bd372e7c2eca6a677009722a2 (diff) | |
| download | luarocks-628779b5a2357685221f62141bec22c0e84a24f1.tar.gz luarocks-628779b5a2357685221f62141bec22c0e84a24f1.tar.bz2 luarocks-628779b5a2357685221f62141bec22c0e84a24f1.zip | |
Move sortedpairs to core; used by luarocks.loader.
Diffstat (limited to 'src')
| -rw-r--r-- | src/luarocks/core/util.lua | 63 | ||||
| -rw-r--r-- | src/luarocks/util.lua | 62 |
2 files changed, 63 insertions, 62 deletions
diff --git a/src/luarocks/core/util.lua b/src/luarocks/core/util.lua index 4f896b17..f2e409d1 100644 --- a/src/luarocks/core/util.lua +++ b/src/luarocks/core/util.lua | |||
| @@ -198,5 +198,68 @@ function util.printerr(...) | |||
| 198 | io.stderr:write("\n") | 198 | io.stderr:write("\n") |
| 199 | end | 199 | end |
| 200 | 200 | ||
| 201 | --- Simple sort function used as a default for util.sortedpairs. | ||
| 202 | local function default_sort(a, b) | ||
| 203 | local ta = type(a) | ||
| 204 | local tb = type(b) | ||
| 205 | if ta == "number" and tb == "number" then | ||
| 206 | return a < b | ||
| 207 | elseif ta == "number" then | ||
| 208 | return true | ||
| 209 | elseif tb == "number" then | ||
| 210 | return false | ||
| 211 | else | ||
| 212 | return tostring(a) < tostring(b) | ||
| 213 | end | ||
| 214 | end | ||
| 215 | |||
| 216 | --- The iterator function used internally by util.sortedpairs. | ||
| 217 | -- @param tbl table: The table to be iterated. | ||
| 218 | -- @param sort_function function or nil: An optional comparison function | ||
| 219 | -- to be used by table.sort when sorting keys. | ||
| 220 | -- @see sortedpairs | ||
| 221 | local function sortedpairs_iterator(tbl, sort_function) | ||
| 222 | local ks = util.keys(tbl) | ||
| 223 | if not sort_function or type(sort_function) == "function" then | ||
| 224 | table.sort(ks, sort_function or default_sort) | ||
| 225 | for _, k in ipairs(ks) do | ||
| 226 | coroutine.yield(k, tbl[k]) | ||
| 227 | end | ||
| 228 | else | ||
| 229 | local order = sort_function | ||
| 230 | local done = {} | ||
| 231 | for _, k in ipairs(order) do | ||
| 232 | local sub_order | ||
| 233 | if type(k) == "table" then | ||
| 234 | sub_order = k[2] | ||
| 235 | k = k[1] | ||
| 236 | end | ||
| 237 | if tbl[k] then | ||
| 238 | done[k] = true | ||
| 239 | coroutine.yield(k, tbl[k], sub_order) | ||
| 240 | end | ||
| 241 | end | ||
| 242 | table.sort(ks, default_sort) | ||
| 243 | for _, k in ipairs(ks) do | ||
| 244 | if not done[k] then | ||
| 245 | coroutine.yield(k, tbl[k]) | ||
| 246 | end | ||
| 247 | end | ||
| 248 | end | ||
| 249 | end | ||
| 250 | |||
| 251 | --- A table iterator generator that returns elements sorted by key, | ||
| 252 | -- to be used in "for" loops. | ||
| 253 | -- @param tbl table: The table to be iterated. | ||
| 254 | -- @param sort_function function or table or nil: An optional comparison function | ||
| 255 | -- to be used by table.sort when sorting keys, or an array listing an explicit order | ||
| 256 | -- for keys. If a value itself is an array, it is taken so that the first element | ||
| 257 | -- is a string representing the field name, and the second element is a priority table | ||
| 258 | -- for that key. | ||
| 259 | -- @return function: the iterator function. | ||
| 260 | function util.sortedpairs(tbl, sort_function) | ||
| 261 | return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end) | ||
| 262 | end | ||
| 263 | |||
| 201 | return util | 264 | return util |
| 202 | 265 | ||
diff --git a/src/luarocks/util.lua b/src/luarocks/util.lua index f41b1c38..9760fafd 100644 --- a/src/luarocks/util.lua +++ b/src/luarocks/util.lua | |||
| @@ -292,68 +292,6 @@ function util.variable_substitutions(tbl, vars) | |||
| 292 | end | 292 | end |
| 293 | end | 293 | end |
| 294 | 294 | ||
| 295 | local function default_sort(a, b) | ||
| 296 | local ta = type(a) | ||
| 297 | local tb = type(b) | ||
| 298 | if ta == "number" and tb == "number" then | ||
| 299 | return a < b | ||
| 300 | elseif ta == "number" then | ||
| 301 | return true | ||
| 302 | elseif tb == "number" then | ||
| 303 | return false | ||
| 304 | else | ||
| 305 | return tostring(a) < tostring(b) | ||
| 306 | end | ||
| 307 | end | ||
| 308 | |||
| 309 | -- The iterator function used internally by util.sortedpairs. | ||
| 310 | -- @param tbl table: The table to be iterated. | ||
| 311 | -- @param sort_function function or nil: An optional comparison function | ||
| 312 | -- to be used by table.sort when sorting keys. | ||
| 313 | -- @see sortedpairs | ||
| 314 | local function sortedpairs_iterator(tbl, sort_function) | ||
| 315 | local ks = util.keys(tbl) | ||
| 316 | if not sort_function or type(sort_function) == "function" then | ||
| 317 | table.sort(ks, sort_function or default_sort) | ||
| 318 | for _, k in ipairs(ks) do | ||
| 319 | coroutine.yield(k, tbl[k]) | ||
| 320 | end | ||
| 321 | else | ||
| 322 | local order = sort_function | ||
| 323 | local done = {} | ||
| 324 | for _, k in ipairs(order) do | ||
| 325 | local sub_order | ||
| 326 | if type(k) == "table" then | ||
| 327 | sub_order = k[2] | ||
| 328 | k = k[1] | ||
| 329 | end | ||
| 330 | if tbl[k] then | ||
| 331 | done[k] = true | ||
| 332 | coroutine.yield(k, tbl[k], sub_order) | ||
| 333 | end | ||
| 334 | end | ||
| 335 | table.sort(ks, default_sort) | ||
| 336 | for _, k in ipairs(ks) do | ||
| 337 | if not done[k] then | ||
| 338 | coroutine.yield(k, tbl[k]) | ||
| 339 | end | ||
| 340 | end | ||
| 341 | end | ||
| 342 | end | ||
| 343 | |||
| 344 | --- A table iterator generator that returns elements sorted by key, | ||
| 345 | -- to be used in "for" loops. | ||
| 346 | -- @param tbl table: The table to be iterated. | ||
| 347 | -- @param sort_function function or table or nil: An optional comparison function | ||
| 348 | -- to be used by table.sort when sorting keys, or an array listing an explicit order | ||
| 349 | -- for keys. If a value itself is an array, it is taken so that the first element | ||
| 350 | -- is a string representing the field name, and the second element is a priority table | ||
| 351 | -- for that key. | ||
| 352 | -- @return function: the iterator function. | ||
| 353 | function util.sortedpairs(tbl, sort_function) | ||
| 354 | return coroutine.wrap(function() sortedpairs_iterator(tbl, sort_function) end) | ||
| 355 | end | ||
| 356 | |||
| 357 | function util.lua_versions() | 295 | function util.lua_versions() |
| 358 | local versions = { "5.1", "5.2", "5.3" } | 296 | local versions = { "5.1", "5.2", "5.3" } |
| 359 | local i = 0 | 297 | local i = 0 |
