Example #1
0
        /// <summary>
        /// Initializes and incrementally creates a minimal <see cref="IMinimalDAGNode{T}"/> from a sorted collection of sequences of values.
        /// </summary>
        /// <typeparam name="T">The type to be arranged as a <see cref="IMinimalDAG{T}"/></typeparam>
        /// <param name="sortedSequences">The ascending ordered sequences of <typeparamref name="T"/> to be minimized.</param>
        /// <param name="nodeFactory">Factory for <see cref="IMinimalDAGNode{T}"/> creation</param>
        public MinimalDAG(IEnumerable <IEnumerable <T> > sortedSequences, IMinimalDAGNodeFactory <T> nodeFactory)//, T sourceValue = default(T), T sinkValue = default(T))
        {
            _registrationBySuffix = new HashSet <IMinimalDAGNode <T> >();
            _dagNodeFactory       = nodeFactory ?? new MinimalDAGNodeFactory <T>();
            _nodes  = new Dictionary <Guid, IMinimalDAGNode <T> >();
            _source = _dagNodeFactory.CreateNode(default(T), Guid.NewGuid());
            _sink   = _dagNodeFactory.CreateNode(default(T), Guid.NewGuid());
            _source.Children.Add(_sink.ID);

            _registrationBySuffix.Add(_sink);
            _sink.IsSuffixRegistered = true;

            var LastSequence = new List <T>()
            {
                _sink.Value
            }.DefaultIfEmpty();                                                //lazy but simple fix

            foreach (IEnumerable <T> Sequence in sortedSequences)
            {
                AddNextOrderedSequence(Sequence, LastSequence);
                LastSequence = Sequence;
            }
            HandleExistingSuffixes(_source);
            BuildParentLinks();
            BuildNodesByValueDictionary();
        }
Example #2
0
        /// <summary>
        /// Adds a sequence of child <see cref="IMinimalDAGNode{T}"/> nodes to the selected <see cref="IMinimalDAGNode{T}"/>
        /// - the addition of a new sequence of values to the DAG.
        /// </summary>
        /// <param name="currentNode">The parent node to add the sequence to.</param>
        /// <param name="currentSuffix">A new sequence of nodes.</param>
        private void AddSuffix(IMinimalDAGNode <T> currentNode, IEnumerable <T> currentSuffix)
        {
            var CurrentNode = currentNode;

            foreach (var value in currentSuffix)
            {
                var NewNode = _dagNodeFactory.CreateNode(value, Guid.NewGuid());
                RecordNode(NewNode);
                CurrentNode.Children.Add(NewNode.ID);
                CurrentNode = NewNode;
            }
            CurrentNode.Children.Add(_sink.ID);
        }