模块依赖排序,保证依赖模块不会先于被依赖模块而注册,50行lua
发布时间:2020-12-14 22:55:57 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 --[[ Date: 2014-8-1 Licence: MIT Author: [email?protected] [email?protected]]]--[[ module_relation table key -- module name value -- depande
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 --[[ Date: 2014-8-1 Licence: MIT Author: <[email?protected]> <[email?protected]> ]] --[[ module_relation table key -- module name value -- depandences (those the module refers to) ]] local module_relation = { ['a'] = { 'c' },['b'] = { 'c' },['c'] = { },['d'] = { },} --[[ Graph matrix deps a b c d a 0 0 1 0 b 0 0 1 0 c 0 0 0 0 d 0 0 0 0 ]] local module_relation = { ['a'] = { 'b','c' },} --[[ Graph matrix deps a b c a 0 1 1 b 0 0 1 c 0 0 0 ]] local module_relation = { ['a'] = { },['b'] = { 'a' },['c'] = { 'a' },['d'] = { 'b',} --[[ Graph matrix deps a b c d a 0 1 1 0 b 0 0 1 0 c 0 0 0 0 d 0 0 0 0 ]] local module_relation = { ['a'] = { 'b' },['c'] = { 'd' },['d'] = { 'a' },['e'] = { 'a' },} --[[ Graph matrix deps a b c d e a 0 1 0 0 0 b 0 0 1 0 0 c 0 0 0 1 0 d 0 0 0 0 1 e 1 0 0 0 0 ]] local modules = 0 local graph_matrix = {} local reg_seq = {} for mod,deps in pairs(module_relation) do for _,v in ipairs(deps) do if graph_matrix[v] == nil then graph_matrix[v] = {} end graph_matrix[v][mod] = true end modules = modules + 1 end mod = next(module_relation) while mod ~= nil do if graph_matrix[mod] == nil or next(graph_matrix[mod]) == nil then table.insert(reg_seq,mod) for k in pairs(graph_matrix) do graph_matrix[k][mod] = nil end module_relation[mod] = nil mod = next(module_relation) else mod = next(module_relation,mod) end end local function rev_tab(t) local i = 1 local j = #t while i < j do local tmp = t[i] t[i] = t[j] t[j] = tmp i = i + 1 j = j - 1 end return t end -- Register sequence print("Register sequence:",unpack(rev_tab(reg_seq))) if #reg_seq ~= modules then error("There's reference loop relationship amang these modules!") end 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- lua has more than 60 upvalues 解决办法
- Delphi中Chrome Chromium、Cef3学习笔记(一)
- perl – 如何获取__LINE__用于调用函数?
- Lua游戏开发(二)---cocos2d VS libgdx VS unity3d
- Lua的类实现继承、多态以及setmetatable方法
- ActiveState Perl Dev Kit Pro v9.2.0.296171 for Windows
- django.urls.exceptions.NoReverseMatch: 'xxx'
- php – Laravel 4多用户身份验证
- 大数求和(hdu 1047)
- 大数运算(c、java)