Пример #1
0
        public StoryGraph(ISceneNode root, IReadOnlyList <Pair <int> > edges, bool sameLayout)
        {
            SameLayout = sameLayout;
            var aspectsUnsorted = root.GetComponent <IStoryComponent>().EnumerateStoryAspectsDeep(SameLayout).ToArray();
            var maxId           = aspectsUnsorted.Max(x => x.Node.Id);

            EnoughIntegers = Enumerable.Range(0, maxId + 1).ToArray();
            Aspects        = aspectsUnsorted.ToDictionary(x => x.Node.Id, x => x);
            NodeObjects    = aspectsUnsorted.ToDictionary(x => x.Node.Id, x => x.Node);
            NodeIds        = aspectsUnsorted.Select(x => x.Node.Id).ToArray();
            IsUsed         = EnoughIntegers.Select(x => NodeObjects.ContainsKey(x)).ToArray();
            Indices        = NodeObjects.ToDictionary(x => x.Value, x => x.Key);
            Children       = Aspects.ToDictionary(x => x.Key, x => (IReadOnlyList <int>)x.Value.EnumerateImmediateStoryChildren(SameLayout).Select(y => y.Node.Id).ToArray());
            Root           = root.Id;
            Parents        = NodeObjects.ToDictionary(x => x.Key, x => x.Value.ParentNode?.Id ?? -1);
            Depths         = NodeIds.ToDictionary(x => x, CalculateNodeDepth);
            Depth          = CalculateDepth(Root);
            Leaves         = EnumerateLeaves(Root).ToArray();
            NonLeaves      = EnumerateNonLeaves(Root).ToArray();
            BuildPrevNext(edges, out var next, out var previous, out var goodEdges);
            Next         = next;
            Previous     = previous;
            Neighbors    = Leaves.ToDictionary(x => x, x => (IReadOnlyList <int>)Next[x].Concat(Previous[x]).ToArray());
            Edges        = goodEdges;
            LeveledEdges = BuildLeveledEdges();
            BuildLeveledPrevNext(out var leveledNext, out var leveledPrev);
            LeveledNext         = leveledNext;
            LeveledPrevious     = leveledPrev;
            LeveledNeighbors    = NodeIds.ToDictionary(x => x, x => (IReadOnlyList <int>)LeveledNext[x].Concat(LeveledPrevious[x]).ToArray());
            ExternalConnections = BuildExternal();
            NodesInBfsOrder     = BreadthFirstSearch().ToArray();
        }