Click here to Skip to main content
15,891,184 members
Articles / Web Development / ASP.NET

DesktopBrowser - A Web Based File Explorer for Media Desktops

Rate me:
Please Sign up or sign in to vote.
4.21/5 (4 votes)
25 Jun 2011GPL32 min read 34.1K   1.1K   22  
A presentation of DesktopBrowser open-source project, a web based file explorer for media desktops.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace DesktopBrowser.Server.Utils
{
    /// <summary>
    /// Wraps an IEnumerable&lt;T&gt; and provides a thread-safe means of caching the values."/>
    /// </summary>
    /// <typeparam name="T"></typeparam>
    class CachedEnumerable<T> : IEnumerable<T>
    {
        // An enumerator from the original IEnumerable<T>
        private IEnumerator<T> enumerator;

        // The items we have already cached (from this.enumerator)
        private IList<T> cachedItems = new List<T>();

        public CachedEnumerable(IEnumerable<T> enumerable)
        {
            this.enumerator = enumerable.GetEnumerator();
        }

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            // The index into the sequence
            int currentIndex = 0;

            // We will break with yield break 
            while (true)
            {
                // The currentIndex will never be decremented,
                // so we can check without locking first
                if (currentIndex < this.cachedItems.Count)
                {
                    var current = this.cachedItems[currentIndex];
                    currentIndex += 1;
                    yield return current;
                }
                else
                {
                    // See if we have more cached items ...
                    if (currentIndex < this.cachedItems.Count)
                    {
                        var current = this.cachedItems[currentIndex];
                        currentIndex += 1;
                        yield return current;
                    }
                    else
                    {
                        // ... otherwise, we'll need to get the next item from this.enumerator.MoveNext()
                        if (this.enumerator.MoveNext())
                        {
                            // capture the current item and cache it, then increment the currentIndex
                            var current = this.enumerator.Current;
                            this.cachedItems.Add(current);
                            currentIndex += 1;
                            yield return current;
                        }
                        else
                        {
                            // We reached the end of the enumerator - we're done
                            yield break;
                        }
                    }
                }
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Architect SharpKit
Israel Israel
Founder of SharpKit

Comments and Discussions