Example #1
0
        public FibonacciItem GetFibonacciNumberInternal(int ordinal)
        {
            if (ordinal < 0)
            {
                Debug.WriteLine("Error:" + "Throwing OUtOfRangeException");
                throw new ArgumentOutOfRangeException("ordinal", "Cannot calculate Fibonacci number for a negative number");
            }

            //if (m_FibonacciCache != null && m_FibonacciCache[ordinal] != null)
            //{
            //    return m_FibonacciCache[ordinal];
            //}

            FibonacciItem result = null;

            int previous = 0;

            int next = 1;

            for (int i = 0; i < ordinal; i++)
            {
                var delayTask = Task.Delay(100);
                delayTask.Wait();

                int sum = next + previous;
                previous = next;

                next = sum;
            }

            result = new FibonacciItem(next, DateTime.Now);

            //if (m_FibonacciCache != null && m_FibonacciCache[ordinal] == null)
            //{
            //    m_FibonacciCache[ordinal] = result;
            //}

            return(result);
        }
Example #2
0
        /// <summary>
        /// Accessor to FibonacciItem references
        /// </summary>
        /// <param name="ordinal"></param>
        /// <returns>FibonacciItem if it is still alive</returns>
        public FibonacciItem this[int ordinal]
        {
            get
            {
                if (!_cache.ContainsKey(ordinal))
                {
                    return(null);
                }

                if (_cache[ordinal].IsAlive)
                {
                    // Object was obtained with the weak reference.
                    FibonacciItem cachedItem = _cache[ordinal].Target as FibonacciItem;
                    return(cachedItem);
                }
                else
                {
                    // Object reference is already disposed of
                    return(null);
                }
            }
            set { _cache[ordinal] = new WeakReference(value); }
        }