Exemplo n.º 1
0
        public ISuffixTree BuildSuffixTree(ISequence sequence)
        {
            if (sequence == null)
            {
                throw new ArgumentNullException("sequence");
            }

            // Initialize
            ValidateReferenceSequence(sequence);
            InitializeReferenceSequence(sequence);

            // Create Tasks
            Dictionary <byte, IList <int> > treeTasks = new Dictionary <byte, IList <int> >();

            // Loop through subset of sequence string and build the suffix tree
            // this will loop through the sequence once and collect all the indexes needed.
            for (int index = 0; index < ReferenceLength; index++)
            {
                IList <int> startIndices = null;

                if (!treeTasks.TryGetValue(GetReferenceSymbol(index), out startIndices))
                {
                    startIndices = new List <int>();
                    treeTasks.Add(GetReferenceSymbol(index), startIndices);
                }

                startIndices.Add(index);
            }

            _distinctSymbolCount = treeTasks.Count;
            if (EdgeStorage == null)
            {
                EdgeStorage = new FileSuffixEdgeStorage();
            }

            // Create Tasks
            IList <Task <IMultiWaySuffixTree> > tasks = treeTasks.Values.Select(
                indices => Task <IMultiWaySuffixTree> .Factory.StartNew(
                    t => AppendSuffix(indices), TaskCreationOptions.None)).ToList();

            // Wait for all the task
            Task.WaitAll(tasks.ToArray());

            _suffixTree = CreateSuffixTree();

            // Merge the branches of tree
            foreach (Task <IMultiWaySuffixTree> task in tasks)
            {
                _suffixTree.Merge(task.Result);
            }

            // return the suffix tree
            return(_suffixTree);
        }