コード例 #1
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> that doesn't keep cache entry keys alive.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys. This type has to be a reference type.</typeparam>
            /// <typeparam name="R">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, R> Create <T, R>(Func <T, R> function, MemoizationOptions options) where T : class
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                return(new MemoizationCacheFactory.NopImpl.Cache <T, R>(function));
            }
コード例 #2
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> that doesn't keep cache entry keys alive.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys. This type has to be a reference type.</typeparam>
            /// <typeparam name="R">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, R> Create <T, R>(Func <T, R> function, MemoizationOptions options) where T : class
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                var cacheError = (options & MemoizationOptions.CacheException) > MemoizationOptions.None;

                return(new Cache <T, R>(function, _ranker, _maxCapacity, _descending, _ageThreshold, cacheError, _stopwatchFactory));
            }
コード例 #3
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> that doesn't keep cache entry keys alive.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys. This type has to be a reference type.</typeparam>
            /// <typeparam name="R">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, R> Create <T, R>(Func <T, R> function, MemoizationOptions options) where T : class
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                var cacheError = (options & MemoizationOptions.CacheException) > MemoizationOptions.None;

                return(new Cache <T, R>(function, _maxCapacity, cacheError));
            }
コード例 #4
0
            public IMemoizationCache <T, R> Create <T, R>(Func <T, R> function, MemoizationOptions options) where T : class
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                if (_exposeGlobalView)
                {
                    return(new MemoizationCacheFactoryExtensions.ThreadLocalFactory.ImplUnion <T, R>(() => _factory.Create(function, options)));
                }

                return(new MemoizationCacheFactoryExtensions.ThreadLocalFactory.Impl <T, R>(() => _factory.Create(function, options)));
            }
コード例 #5
0
            public IMemoizationCache <T, R> Create <T, R>(Func <T, R> function, MemoizationOptions options, IEqualityComparer <T> comparer)
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                if (_exposeGlobalView)
                {
                    return(new ImplUnion <T, R>(() => _factory.Create(function, options, comparer)));
                }

                return(new Impl <T, R>(() => _factory.Create(function, options, comparer)));
            }
コード例 #6
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> that doesn't keep cache entry keys alive.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys. This type has to be a reference type.</typeparam>
            /// <typeparam name="R">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, R> Create <T, R>(Func <T, R> function, MemoizationOptions options) where T : class
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                if ((options & MemoizationOptions.CacheException) > MemoizationOptions.None)
                {
                    return(new CacheWithException <T, R>(function));
                }
                else
                {
                    return(new Cache <T, R>(function));
                }
            }
コード例 #7
0
ファイル: Memoizer.cs プロジェクト: tamirdresher/reaqtor
            /// <summary>
            /// Memoizes the specified <paramref name="function"/>.
            /// </summary>
            /// <typeparam name="T">Type of the function argument.</typeparam>
            /// <typeparam name="TResult">Type of the function result.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <param name="comparer">Comparer to compare the function argument during lookup in the memoization cache.</param>
            /// <returns>A memoized delegate containing the memoized function and providing access to the memoization cache.</returns>
            public IMemoizedDelegate <Func <T, TResult> > Memoize <T, TResult>(Func <T, TResult> function, MemoizationOptions options = MemoizationOptions.None, IEqualityComparer <T> comparer = null)
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                var cache = _factory.Create(function, options, comparer);

                return(new MemoizedDelegate <Func <T, TResult> >(cache.GetOrAdd, cache));
            }
コード例 #8
0
 public IMemoizationCache <T, TResult> Create <T, TResult>(Func <T, TResult> function, MemoizationOptions options) where T : class
 {
     return(new MyCache <T, TResult>());
 }
コード例 #9
0
 public IMemoizationCache <T, TResult> Create <T, TResult>(Func <T, TResult> function, MemoizationOptions options, IEqualityComparer <T> comparer)
 {
     return(new MyCache <T, TResult>());
 }
コード例 #10
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> using the specified <paramref name="comparer"/> to compare cache entries.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys.</typeparam>
            /// <typeparam name="TResult">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <param name="comparer">Comparer to compare the key during lookup in the memoization cache.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, TResult> Create <T, TResult>(Func <T, TResult> function, MemoizationOptions options, IEqualityComparer <T> comparer)
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                if ((options & MemoizationOptions.CacheException) > MemoizationOptions.None)
                {
                    return(new CacheWithException <T, TResult>(function, comparer));
                }
                else
                {
                    return(new Cache <T, TResult>(function, comparer));
                }
            }
コード例 #11
0
        /// <summary>
        /// Memoizes the specified <paramref name="function"/> using the specified <paramref name="memoizer"/>.
        /// </summary>
        /// <typeparam name="TResult">Type of the function result.</typeparam>
        /// <param name="memoizer">The memoizer used to memoize the function.</param>
        /// <param name="function">The function to memoize.</param>
        /// <param name="options">Flags to influence the memoization behavior.</param>
        /// <returns>A memoized delegate containing the memoized function and providing access to the memoization cache.</returns>
        public static IMemoizedDelegate <Func <TResult> > MemoizeWeak <TResult>(this IWeakMemoizer memoizer, Func <TResult> function, MemoizationOptions options = MemoizationOptions.None)
        {
            if (memoizer == null)
            {
                throw new ArgumentNullException(nameof(memoizer));
            }
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            var res = memoizer.MemoizeWeak <object, TResult>(_ => function(), options);
            var del = res.Delegate;

            return(new MemoizedDelegate <Func <TResult> >(() => del(null), res.Cache));
        }
コード例 #12
0
        /// <summary>
        /// Memoizes the specified <paramref name="function"/>.
        /// </summary>
        /// <typeparam name="TResult">Type of the function result.</typeparam>
        /// <param name="function">The function to memoize.</param>
        /// <param name="options">Flags to influence the memoization behavior.</param>
        /// <returns>A memoized delegate containing the memoized function and providing access to the memoization cache.</returns>
        public static IMemoizedDelegate <Func <TResult> > Memoize <TResult>(this Func <TResult> function, MemoizationOptions options = MemoizationOptions.None)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            if ((options & MemoizationOptions.CacheException) > MemoizationOptions.None)
            {
                return(MemoizeWithError(function));
            }
            else
            {
                return(MemoizeWithoutError(function));
            }
        }
コード例 #13
0
        /// <summary>
        /// Memoizes the specified <paramref name="function"/> using the specified <paramref name="memoizer"/>.
        /// </summary>
        /// <typeparam name="T">Type of the function argument.</typeparam>
        /// <typeparam name="TResult">Type of the function result.</typeparam>
        /// <param name="memoizer">The memoizer used to memoize the function.</param>
        /// <param name="function">The function to memoize.</param>
        /// <param name="options">Flags to influence the memoization behavior.</param>
        /// <returns>A memoized delegate containing the memoized function and providing access to the memoization cache.</returns>
        public static IMemoizedDelegate <Func <T, TResult> > Memoize <T, TResult>(this IMemoizer memoizer, Func <T, TResult> function, MemoizationOptions options = MemoizationOptions.None)
        {
            if (memoizer == null)
            {
                throw new ArgumentNullException(nameof(memoizer));
            }
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }

            return(memoizer.Memoize <T, TResult>(function, options, comparer: null));
        }
コード例 #14
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> using the specified <paramref name="comparer"/> to compare cache entries.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys.</typeparam>
            /// <typeparam name="TResult">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <param name="comparer">Comparer to compare the key during lookup in the memoization cache.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, TResult> Create <T, TResult>(Func <T, TResult> function, MemoizationOptions options, IEqualityComparer <T> comparer)
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                var cacheError = (options & MemoizationOptions.CacheException) > MemoizationOptions.None;

                return(new Cache <T, TResult>(function, _maxCapacity, comparer, cacheError));
            }
コード例 #15
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> using the specified <paramref name="comparer"/> to compare cache entries.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys.</typeparam>
            /// <typeparam name="TResult">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <param name="comparer">Comparer to compare the key during lookup in the memoization cache.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, TResult> Create <T, TResult>(Func <T, TResult> function, MemoizationOptions options, IEqualityComparer <T> comparer)
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                return(new Cache <T, TResult>(function));
            }
コード例 #16
0
            /// <summary>
            /// Creates a memoization cache for the specified <paramref name="function"/> that doesn't keep cache entry keys alive.
            /// </summary>
            /// <typeparam name="T">Type of the memoization cache entry keys.</typeparam>
            /// <typeparam name="TResult">Type of the memoization cache entry values.</typeparam>
            /// <param name="function">The function to memoize.</param>
            /// <param name="options">Flags to influence the memoization behavior.</param>
            /// <param name="comparer">Comparer to compare the key during lookup in the memoization cache.</param>
            /// <returns>An empty memoization cache instance.</returns>
            public IMemoizationCache <T, TResult> Create <T, TResult>(Func <T, TResult> function, MemoizationOptions options, IEqualityComparer <T> comparer)
            {
                if (function == null)
                {
                    throw new ArgumentNullException(nameof(function));
                }

                var cacheError = (options & MemoizationOptions.CacheException) > MemoizationOptions.None;

                return(new Cache <T, TResult>(function, _ranker, _maxCapacity, _descending, _ageThreshold, comparer, cacheError, _stopwatchFactory));
            }