Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#

A Concurrent Collection: a MultiMap Generic Collection Class in C# - Part 2

Rate me:
Please Sign up or sign in to vote.
4.87/5 (19 votes)
5 Jun 2009CPOL10 min read 101.8K   3.4K   73  
MultiMap is similar to a .NET Dictionary collection type, but accepts duplicate Key,Value pairs during addition. The MultiMap collection is also a concurrent collection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BK.Util
{
    internal class ThreadDetails<Key,Value> where Key: IComparable<Key>
    {
        string threadId;
        Key lastSearchedItem;
        int count;

        int currentIndex;
        Key keyForThread;

        public void StoreCurrentIndex(Key KeyToStore, int CurrIndex)
        {
            currentIndex = CurrIndex;
            keyForThread = KeyToStore;
        }

        public int GetStoredIndex(ref Key KeyStored)
        {
            KeyStored = keyForThread;
            return currentIndex;
        }

        private void Check(Key KeyParam)
        {
            if (0 != KeyParam.CompareTo(lastSearchedItem))
            {
                lastSearchedItem = KeyParam;
                ResetCount();
            }
        }

        public ThreadDetails(string ThreadId, Key LastSearchedItem, int Count)
        {
            threadId = ThreadId;
            lastSearchedItem = LastSearchedItem;
            count = Count;
            currentIndex = -1;
        }

        public string ThreadId
        {
            get
            {
                return threadId;
            }
        }

        public Key LastSearchedItem
        {
            get
            {
                return lastSearchedItem;
            }

            set
            {
                lastSearchedItem = value;
            }
        }

        public int GetCurrentCount(Key KeyParam)
        {
            Check(KeyParam);
            return count;
        }

        public void IncrementCurrentCount(Key KeyParam)
        {
            Check(KeyParam);
            count++;
        }

        public void ResetCount()
        {
            count = -1;
        }

        internal int Counter
        {
            get
            {
                return count;
            }
        }
    }
}

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
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions