开发者

files build execution order

I have a data structure which is as given below:


 class File
    {
        public string Value { get; set; }
        public File[] Dependencies { get; set; }
        public bool Change { get; private set; }

    public File(string value,File[] dependencies)
    {
        Value = value;
        Dependencies = dependencies;
        Change = false;
    }
}

Basically, this data structure follows a typical build execution of files.

Each File has a value and a list开发者_运维百科 of dependencies which is again of type File. Every file is exposed with a property called Change which tells whether the file is changed or not.

I brainstormed to form a algorithm which goes through all these files and build in an order( i.e typical build process ) but haven't got a better algorithm.

Can anyone throw some light on this?

Thanks a lot.

Mahesh


The algorithm you are looking for is called topological sorting.


Follow these basic steps.

  1. Find files without any dependencies
  2. Add those to the build first
  3. Find files dependent on those in build and nothing else
  4. Add those to build next
  5. Repeat 3 and 4 until all files in build

You'll need some checks to detect circular dependencies which would cause and endless loop on step 5. Other than that, this should work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜