Click here to Skip to main content
15,898,134 members
Articles / Programming Languages / C#

GUID Tree Data Structure

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
14 Feb 2013CPOL2 min read 13.5K   176   4  
Data structure designed to store billions of items using GUID as a key.
// Author: Trevy
//
namespace TrevyCo.Tools
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    using System.Runtime.Serialization;

    /// <summary>
    /// It uses computer memory to store data for memory usage under 1 MG
    /// Above that, it switches to using computer storage as a medium
    /// </summary>
    public class GuidCollection<T>
    {
        #region Data
        private GuidTreeBase<T> guidTree;

        public long Count
        {
            get
            {
                return count;
            }
        }
        private long count = 0; 
        #endregion

        #region Constructors
        /// <summary>
        /// Uses computer memory exclusively.
        /// </summary>
        /// <remarks>
        /// Can slow computer if too many entries added
        /// </remarks>
        public GuidCollection(bool overwriteValues = false)
        {
            guidTree = new GuidArrayTree<T>();
            guidTree.OverwriteValues = overwriteValues;
        }

        /// <summary>
        /// Uses computer memory to store data for memory usage under 1 MG
        /// Above that, it switches to using computer storage as a medium
        /// </summary>
        /// <param name="scratchArea">Area to store data.</param>
        /// <remarks>
        /// WARNING: Choose a secure are with sufficient memory to store all the data you need to add
        /// </remarks>
        public GuidCollection(string scratchArea, bool overwriteValues = false)
        {
            Attribute attribute = Attribute.GetCustomAttribute(typeof(T), typeof(System.SerializableAttribute));
            if (attribute == null)
            {
                throw new ApplicationException(typeof(T).Name + " must have the System.Serializable Attribute, since we are storing the value in the file system.");
            }

            guidTree = new GuidFileTree<T>(scratchArea);
            guidTree.OverwriteValues = overwriteValues;
        } 
        #endregion

        #region Public methods
        /// <summary>
        /// Add key, value pair to dictionary
        /// </summary>
        public void Add(Guid key, T val)
        {
            if (guidTree.Add(key, val))
            {
                count++;
            }
        }

        /// <summary>
        /// Set weather we can overwrite existing values
        /// </summary>
        public bool OverwriteValues
        {
            get
            {
                return guidTree.OverwriteValues;
            }
            set
            {
                guidTree.OverwriteValues = value;
            }
        }

        /// <summary>
        /// Index into collection
        /// </summary>
        /// <param name="key">GUID</param>
        /// <returns>the value</returns>
        public T this[Guid key]
        {
            get
            {
                T val;
                if (TryGetValue(key, out val))
                {
                    return val;
                }
                else
                {
                    throw new InvalidOperationException("Key doesn't exist");
                }
            }
            set
            {
                Add(key, value);
            }
        }

        /// <summary>
        /// Try to get the value
        /// </summary>
        /// <returns>True if vlue exists, false othervise</returns>
        public bool TryGetValue(Guid key, out T val)
        {
            return guidTree.TryGetValue(key, out val);
        }

        /// <summary>
        /// Can be time-consuming
        /// </summary>
        public void Clear()
        {
            guidTree.Clear();
            count = 0;
        }

        /// <summary>
        /// Remove item corresponding to the key
        /// </summary>
        /// <returns>true if value removed</returns>
        public bool Remove(Guid key)
        {
            bool res = guidTree.Remove(key);
            if (res)
            {
                count--;
            }

            return res;
        }

        /// <summary>
        /// Check if key exists
        /// </summary>
        /// <returns>true if key exists, false otherwise</returns>
        public bool ContainsKey(Guid key)
        {
            return guidTree.ContainsKey(key);
        }

        /// <summary>
        /// Gets the enummerator
        /// </summary>
        /// <returns>Enumerator</returns>
        public IEnumerable<T> GetEnumerator()
        {
            return guidTree.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 Code Project Open License (CPOL)


Written By
Software Developer
United States United States
I’ve been a software developer for the last 11 years.
My language of choice is C#.
My strength lies in windows based programming as an SDET.

Strengths include:
• Object Oriented Programming
• Design Patterns
• Data Structures, Algorithms
• Windows Automation

Principle technologies include:
• Visual Studios 2010 (Ultimate, TFS, Test Manager)
• C# 4.0, XML, WPF, SQL Server

Education includes:
• BSEE - Concordia University, Quebec, Canada
• Programmer Analyst Certificate - John Abbott College, Qc, Canada
• Certified Scrum Master Training - Danube, Bellevue, WA
• Windows Azure Training - Wintellect, Bellevue, WA

Certification:
• Microsoft Certified Solution Developer for the MS.NET Platform

Comments and Discussions