Пример #1
0
        private Maybe <T> TryGetNonAlreadyProvidedValues <T>(MemoizerKey memoizerKey, out SortedSet <object> alreadyProvidedValues, Func <T> generationFunction, int maxFailingAttempts)
        {
            alreadyProvidedValues = _memoizer.GetAlreadyProvidedValues(memoizerKey);

            var maybe = GenerateNotAlreadyProvidedValue <T>(alreadyProvidedValues, maxFailingAttempts,
                                                            generationFunction);

            return(maybe);
        }
Пример #2
0
        public SortedSet <object> GetAlreadyProvidedValues(MemoizerKey memoizerKey)
        {
            SortedSet <object> alreadyProvidedValues = null;

            if (!_memoizer.ContainsKey(memoizerKey))
            {
                alreadyProvidedValues  = new SortedSet <object>();
                _memoizer[memoizerKey] = alreadyProvidedValues;
            }
            else
            {
                alreadyProvidedValues = _memoizer[memoizerKey];
            }

            return(alreadyProvidedValues);
        }
Пример #3
0
        private T GenerateWithoutDuplication <T>(MethodBase currentMethod, int argumentsHashCode, int maxFailingAttemptsBeforeLastChanceFunctionIsCalled,
                                                 Func <T> regularGenerationFunction, Func <SortedSet <object>, Maybe <T> > lastChanceGenerationFunction = null)
        {
            var memoizerKey = new MemoizerKey(currentMethod, argumentsHashCode);
            var maybe       = TryGetNonAlreadyProvidedValues <T>(memoizerKey, out var alreadyProvidedValues, regularGenerationFunction, maxFailingAttemptsBeforeLastChanceFunctionIsCalled);

            if (!maybe.HasItem && lastChanceGenerationFunction != null)
            {
                // last attempt, we randomly pick the missing bits from the memoizer
                maybe = lastChanceGenerationFunction(_memoizer.GetAlreadyProvidedValues(memoizerKey));
            }

            if (!maybe.HasItem)
            {
                throw new DuplicationException(typeof(T), maxFailingAttemptsBeforeLastChanceFunctionIsCalled, alreadyProvidedValues);
            }

            alreadyProvidedValues.Add(maybe.Item);
            return(maybe.Item);
        }