private static IEnumerable OrderInterfaces()

in ADTTools/UploadModels/Program.cs [184:226]


        private static IEnumerable<DTInterfaceInfo> OrderInterfaces(IEnumerable<DTInterfaceInfo> interfaces)
        {
            // This function sorts interfaces from interfaces with no dependencies to interfaces with dependencies.
            // Using a depth-first search and post-order processing, we get an ordering from no dependencies to most dependencies.

            // Build the set of all referenced interfaces.
            HashSet<DTInterfaceInfo> referencedInterfaces = new HashSet<DTInterfaceInfo>(new DTInterfaceInfoEqualityComparer());
            foreach (DTInterfaceInfo @interface in interfaces)
            {
                foreach (DTInterfaceInfo referencedInterface in @interface.Extends)
                {
                    referencedInterfaces.Add(referencedInterface);
                }

                IEnumerable<DTInterfaceInfo> componentSchemas = from content in @interface.Contents.Values
                                                                where content.EntityKind == DTEntityKind.Component
                                                                select ((DTComponentInfo)content).Schema;
                foreach (DTInterfaceInfo referencedInterface in componentSchemas)
                {
                    referencedInterfaces.Add(referencedInterface);
                }
            }

            // The roots of the trees are all the interfaces that are not referenced.
            IEnumerable<DTInterfaceInfo> rootInterfaces = interfaces.Except(referencedInterfaces, new DTInterfaceInfoEqualityComparer());

            // For each root, perform depth-first, post-order processing to produce a sorted tree.
            OrderedHashSet<DTInterfaceInfo> orderedInterfaces = new OrderedHashSet<DTInterfaceInfo>(new DTInterfaceInfoEqualityComparer());
            foreach (DTInterfaceInfo rootInterface in rootInterfaces)
            {
                OrderedHashSet<DTInterfaceInfo> rootInterfaceOrderedInterfaces = new OrderedHashSet<DTInterfaceInfo>(new DTInterfaceInfoEqualityComparer());
                OrderInterface(rootInterfaceOrderedInterfaces, rootInterface);
                foreach (DTInterfaceInfo rootInterfaceOrderedInterface in rootInterfaceOrderedInterfaces)
                {
                    if (!orderedInterfaces.Contains(rootInterfaceOrderedInterface))
                    {
                        orderedInterfaces.Add(rootInterfaceOrderedInterface);
                    }
                }
            }

            return orderedInterfaces;
        }