Lua   发布时间:2019-10-08  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了模块依赖排序,保证依赖模块不会先于被依赖模块而注册,50行lua。大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
--[[ 
    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

大佬总结

以上是大佬教程为你收集整理的模块依赖排序,保证依赖模块不会先于被依赖模块而注册,50行lua。全部内容,希望文章能够帮你解决模块依赖排序,保证依赖模块不会先于被依赖模块而注册,50行lua。所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: