Ejemplo n.º 1
0
        /// <summary>
        /// Determines if figure directly or indirectly depends
        /// on <paramref name="possibleDependency"/>
        /// </summary>
        /// <param name="figure">figure to check</param>
        /// <param name="possibleDependency"></param>
        /// <returns></returns>
        public static bool DependsOn(this IFigure figure, IFigure possibleDependency)
        {
            // we consider that a figure depends on itself
            if (figure == possibleDependency)
            {
                return(true);
            }

            // quick rejection - if it doesn't depend on anything,
            // it certainly doesn't depend on possibleDependency
            if (figure.Dependencies.IsEmpty())
            {
                return(false);
            }

            // first do the cheap pre-test without going deep
            if (figure.DirectlyDependsOn(possibleDependency))
            {
                return(true);
            }

            // if that failed, go deeper using recursion
            foreach (var directDependency in figure.Dependencies)
            {
                if (directDependency.DependsOn(possibleDependency))
                {
                    return(true);
                }
            }

            // depth-first search didn't find anything
            return(false);
        }