Example #1
0
        private int GetPartitionIndexFromIteration(BigInteger iteration)
        {
            var count = Count;

            if (iteration >= count)
            {
                throw new IndexOutOfRangeException($"Index out of range! Select between 0 and {count}");
            }

            // Lets say total count is 8007 words from 8 partitions.
            // Part[0-6] is 1k words and Part[7] is 1007 words.
            // Iteration is 3003, return partition index 3.
            var minRange = 0;
            var maxRange = PartitionedWords.First().Count;
            int partitionIndex;

            for (partitionIndex = 1; partitionIndex < PartitionedWords.Count; partitionIndex++)
            {
                minRange += PartitionedWords.ElementAt(partitionIndex).Count;
                if (minRange < iteration && iteration < maxRange)
                {
                    return(partitionIndex);
                }

                maxRange += minRange;
            }

            return(-1);
        }
Example #2
0
        /// <summary>
        /// Returns a specific partition of words given the partition index.
        /// </summary>
        /// <param name="partitionIndex">The index of the parent collection.</param>
        /// <returns>A specific partition </returns>
        /// <exception cref="PartitionIndexOutOfRange">Thrown if the partitionIndex is out of range.</exception>
        public IDictionary <int, IGeneratedWord> GetWordsAtIndex(int partitionIndex)
        {
            var count        = PartitionedWords.Count();
            var emptyWordSet = new ConcurrentDictionary <int, IGeneratedWord>();

            if (partitionIndex < 0 || partitionIndex >= count)
            {
                throw new PartitionIndexOutOfRange(partitionIndex, nameof(partitionIndex));
            }

            return(PartitionedWords.ElementAt(partitionIndex));
        }