Click here to Skip to main content
15,898,134 members
Articles / Desktop Programming / WPF

The Time Machine

Rate me:
Please Sign up or sign in to vote.
4.91/5 (13 votes)
9 May 2012CPOL6 min read 34.3K   1.2K   32  
Long time strategy of software design and development
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonService
{
    /// <summary>
    /// Light weight dictionary with ordered keys
    /// </summary>
    /// <typeparam name="TKey">Type of key</typeparam>
    /// <typeparam name="TValue">Type of value</typeparam>
    public class LightDictionary<TKey, TValue>
    {
        #region Fields

        List<TKey> l = new List<TKey>();

        Dictionary<TKey, TValue> d = new Dictionary<TKey, TValue>();

        #endregion


        #region Members

        /// <summary>
        /// Access to element
        /// </summary>
        /// <param name="key">Key</param>
        /// <returns>Value</returns>
        public TValue this[TKey key]
        {
            set
            {
                if (l.Contains<TKey>(key))
                {
                    throw new Exception("Key already exists");
                }
                l.Add(key);
                d[key] = value;
            }
            get
            {
                return d[key];
            }
        }

        /// <summary>
        /// Keys
        /// </summary>
        public IList<TKey> Keys
        {
            get
            {
                return l;
            }
        }

        /// <summary>
        /// Performs foreach ordered action
        /// </summary>
        /// <param name="action">The action</param>
        public void ForEach(Action<TValue> action)
        {
            foreach (TKey key in l)
            {
                action(d[key]);
            }
        }

        /// <summary>
        /// Performs foreach parametrized ordered action
        /// </summary>
        /// <typeparam name="TParameter">Type of parameter</typeparam>
        /// <param name="action">The action</param>
        /// <param name="parameter">The parameter</param>
        public void ForEach<TParameter>(Action<TValue, TParameter> action, TParameter parameter)
        {
            foreach (TKey key in l)
            {
                action(d[key], parameter);
            }
        }

        /// <summary>
        /// Performs foreach parametrized ordered action
        /// </summary>
        /// <typeparam name="P1">Type of 1 - st parameter</typeparam>
        /// <typeparam name="P2">Type of 2 - d parameter</typeparam>
        /// <param name="action">The action</param>
        /// <param name="p1">1 - st parameter</param>
        /// <param name="p2">2 - d parameter</param>
        public void ForEach<P1, P2>(Action<TValue, P1, P2> action, P1 p1, P2 p2)
        {
            foreach (TKey key in l)
            {
                action(d[key], p1, p2);
            }
        }

        /// <summary>
        /// Performs foreach parametrized ordered action
        /// </summary>
        /// <typeparam name="P1">Type of 1 - st parameter</typeparam>
        /// <typeparam name="P2">Type of 2 - d parameter</typeparam>
        /// <typeparam name="P3">Type of 3 - d parameter</typeparam>
        /// <param name="action">The action</param>
        /// <param name="p1">1 - st parameter</param>
        /// <param name="p2">2 - d parameter</param>
        /// <param name="p3">3 - d parameter</param>
        public void ForEach<P1, P2, P3>(Action<TValue, P1, P2, P3> action, P1 p1, P2 p2, P3 p3)
        {
            foreach (TKey key in l)
            {
                action(d[key], p1, p2, p3);
            }
        }

 

/*
        /// <summary>
        /// Performs foreach parametrized ordered action
        /// </summary>
        /// <typeparam name="P1">Type of 1 - st parameter</typeparam>
        /// <typeparam name="P2">Type of 2 - d parameter</typeparam>
        /// <typeparam name="P3">Type of 3 - d parameter</typeparam>
        /// <typeparam name="P4">Type of 4 -th parameter</typeparam>
        /// <param name="action">The action</param>
        /// <param name="p1">1 - st parameter</param>
        /// <param name="p2">2 - d parameter</param>
        /// <param name="p3">3 - d parameter</param>
        /// <param name="p4">4 -th parameter</param>
    /*    public void ForEach<P1, P2, P3, P4>(Action<TValue, P1, P2, P3, P4> action, P1 p1, P2 p2, P3 p3, P4 p4)
        {
            foreach (TKey key in l)
            {
                action(d[key], p1, p2, p3, p4);
            }
        }*/

        /// <summary>
        /// Adds dictionary seetings
        /// </summary>
        /// <param name="dictionary">The dictionary</param>
        public void Add(LightDictionary<TKey, TValue> dictionary)
        {
            foreach (TKey key in dictionary.l)
            {
                this[key] = dictionary.d[key];
            }
        }

        /// <summary>
        /// Adds arrays
        /// </summary>
        /// <param name="keys">Array of keys</param>
        /// <param name="values">Array of values</param>
        public void Add(TKey[] keys, TValue[] values)
        {
            if (keys.Length != values.Length)
            {
                throw new Exception("Lendth are not equal");
            }
            for (int i = 0; i < keys.Length; i++)
            {
                this[keys[i]] = values[i];
            }
        }

        /// <summary>
        /// Adds arrays
        /// </summary>
        /// <param name="keys">Array of keys</param>
        /// <param name="values">Array of values</param>
        public void Add(IList<TKey> keys, IList<TValue> values)
        {
            Add(keys.ToArray<TKey>(), values.ToArray<TValue>());
        }


        #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 Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions