Click here to Skip to main content
15,896,557 members
Articles / General Programming / Performance

B-Tree Sorted Dictionary

Rate me:
Please Sign up or sign in to vote.
4.96/5 (48 votes)
26 Jan 2024MIT8 min read 182.5K   4.8K   140  
In-memory B-Tree sorted dictionary
/* 
 * Copyright(c) 2002 Gerardo Recinto/4A Technologies (http://www.4atech.net)
 */
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Sop.Collections.BTree
{
    /// <summary>
    /// B-Tree Base interface. Both In-Memory and On-Disk
    /// B-Tree versions implement IBTreeBase interface.
    /// </summary>
    public interface IBTreeBase : System.Collections.IDictionary,
        System.Collections.ICollection,
        System.Collections.IEnumerable,
        System.ICloneable
    {
        /// <summary>
        /// Returns current sort order. Setting to a different sort order will 
        /// reset BTree. First item according to sort order will be current item.
        /// </summary>
        SortOrderType SortOrder
        {
            get;
            set;
        }
        /// <summary>
        /// Returns current item's key
        /// </summary>
        object CurrentKey
        {
            get;
        }
        /// <summary>
        /// Returns/sets current item's value
        /// </summary>
        object CurrentValue
        {
            get;
            set;
        }
        /// <summary>
        /// Returns true if current record pointer is beyond last item in tree.
        /// </summary>
        /// <returns></returns>
        bool EndOfTree();
        /// <summary>
        /// Search BTreeAlgorithm for the entry having its key equal to 'Key'
        /// </summary>
        /// <param name="Key">Key of record to search for</param>
        /// <returns>true if successful, false otherwise</returns>
        bool Search(object Key);
        /// <summary>
        /// Search btree for the entry having its key equal to 'Key'
        /// </summary>
        /// <param name="Key">Key of record to search for</param>
        /// <param name="GoToFirstInstance">if true and Key is duplicated, will make first instance of duplicated keys the current record so one can easily get/traverse all records having the same keys using 'MoveNext' function</param>
        /// <returns>true if successful, false otherwise</returns>
        bool Search(object Key, bool GoToFirstInstance);
        /// <summary>
        /// Returns the number of items in the btree
        /// </summary>
        /// <summary>
        /// Returns the current item (value and key pair) contained in 'DictionaryEntry' object.
        /// </summary>
        DictionaryEntry CurrentEntry
        {
            get;
        }
    }
    /// <summary>
    /// B-Tree interface defines the available members of B-Tree manager.
    /// Extends the following .Net Framework interfaces:
    /// System.Collections.IDictionary, System.Collections.ICollection, 
    /// System.Collections.IEnumerable,	System.ICloneable, 
    /// System.Runtime.Serialization.ISerializable, System.Runtime.Serialization.IDeserializationCallback
    /// </summary>
    public interface IBTree : IBTreeBase
#if !DEVICE
,
        System.Runtime.Serialization.ISerializable,
        System.Runtime.Serialization.IDeserializationCallback
#endif
    {
        /// <summary>
        /// Returns the Synchronized (or Multi-Thread Safe) version of BTree
        /// </summary>
        /// <returns>Synchronized BTree object</returns>
        IBTree Synchronized();
    }
}

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 MIT License


Written By
United States United States
Ex Google, ex Microsoft
I'm just fresh off from a software eng'g gig & is looking forward to my next.
Pls. do drop me a line @gerardorecinto@yahoo.com if interested or having any question/feedback on these Open Source projects.

I'm excited to help/volunteer my services.
Have a great day!

Comments and Discussions