Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

The Enumerable Enumerator

, 2 Nov 2006
Iterate over your enums, and other things to do with enums.
enumerator.zip
bin
Debug
Enumerator.exe
Properties
/*
Copyright (c) 2006, Marc Clifton
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list
  of conditions and the following disclaimer. 

* Redistributions in binary form must reproduce the above copyright notice, this 
  list of conditions and the following disclaimer in the documentation and/or other
  materials provided with the distribution. 
 
* Neither the name Marc Clifton nor the names of contributors may be
  used to endorse or promote products derived from this software without specific
  prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

using System;
using System.Collections.Generic;

// Ordinal: Being of a specified position in a numbered series

// The Enumerable Enumerator!

namespace Clifton.Tools.Data
{
    public static class Enumerator<T>
    {
        /// <summary>
        /// Returns the first value in the enumeration, as sorted
        /// by enum value, not ordinal value.
        /// </summary>
        public static T First
        {
            get { return ((T[])Enum.GetValues(typeof(T)))[0]; }
        }

        /// <summary>
        /// Returns the last vaue in the enumeration, as sorted
        /// by enum value, not ordinal value.
        /// </summary>
        public static T Last
        {
            get 
            {
                T[] vals = (T[])Enum.GetValues(typeof(T));
                return vals[vals.Length-1]; 
            }
        }

		public static T PreviousBounded(T val, T min)
		{
			T[] vals = (T[])Enum.GetValues(typeof(T));
			int curIdx = Array.FindIndex<T>(vals, delegate(T v) { return v.Equals(val); });
			int minIdx = Array.FindIndex<T>(vals, delegate(T v) { return v.Equals(min); });

			return curIdx <= minIdx ? vals[minIdx] : vals[curIdx - 1];
		}

		public static T NextBounded(T val, T max)
		{
			T[] vals = (T[])Enum.GetValues(typeof(T));
			int curIdx = Array.FindIndex<T>(vals, delegate(T v) { return v.Equals(val); });
			int maxIdx = Array.FindIndex<T>(vals, delegate(T v) { return v.Equals(max); });

			return curIdx >= maxIdx ? vals[maxIdx] : vals[curIdx + 1];
		}

        /// <summary>
        /// Returns the enumeration value that is previous to the supplied one,
        /// or the first enumeration value, based on enum value, not ordinal value.
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static T PreviousOrFirst(T val)
        {
            T[] vals = (T[])Enum.GetValues(typeof(T));
            int idx = Array.FindIndex<T>(vals, delegate(T v) { return v.Equals(val); });

            return idx == 0 ? vals[0] : vals[idx - 1];
        }

        /// <summary>
        /// Returns the enumeration value that is next to the supplied one,
        /// or the last enumeration value based on enum value, not ordinal value.
        /// </summary>
        /// <param name="val"></param>
        /// <returns></returns>
        public static T NextOrLast(T val)
        {
            T[] vals = (T[])Enum.GetValues(typeof(T));
            int idx = Array.FindIndex<T>(vals, delegate(T v) { return v.Equals(val); });

            return idx == vals.Length-1 ? vals[idx] : vals[idx + 1];
        }

        /// <summary>
        /// Iterates over the enumeration collection by enum value, not ordinal value.
        /// </summary>
        /// <returns></returns>
		public static IEnumerable<T> Items()
		{
			return (T[])Enum.GetValues(typeof(T));
		}
    }
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 2 Nov 2006
Article Copyright 2006 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid