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.
- Find files without any dependencies
- Add those to the build first
- Find files dependent on those in build and nothing else
- Add those to build next
- 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.
精彩评论