Combines two unique Int32 numbers into an Int64 suitable for use as a dictionary key, optionally taking the order of the numbers into account. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IDictionary

Description

Combines two unique Int32 numbers into an Int64 suitable for use as a dictionary key, optionally taking the order of the numbers into account.

Demo Code


using System.Diagnostics;
using System;/*from  w  ww .  jav  a2  s  .  c o m*/

public class Main{
        //*************************************************************************
    //  Method: GetDictionaryKey()
    //
    /// <summary>
    /// Combines two unique Int32 numbers into an Int64 suitable for use as a
    /// dictionary key, optionally taking the order of the numbers into
    /// account.
    /// </summary>
    ///
    /// <param name="uniqueNumber1">
    /// The first unique Int32.
    /// </param>
    ///
    /// <param name="uniqueNumber2">
    /// The second unique Int32.
    /// </param>
    ///
    /// <param name="useOrder">
    /// true to take the order of the numbers into account.
    /// </param>
    ///
    /// <returns>
    /// An Int64 suitable for use as a dictionary key.
    /// </returns>
    ///
    /// <remarks>
    /// If <paramref name="useOrder" /> is false, the number pairs (A,B) and
    /// (B,A) yield the same Int64.
    ///
    /// <para>
    /// If <paramref name="useOrder" /> is true, the number pairs (A,B) and
    /// (B,A) yield different Int64s.
    /// </para>
    ///
    /// </remarks>
    //*************************************************************************

    public static Int64
    GetDictionaryKey
    (
        Int32 uniqueNumber1,
        Int32 uniqueNumber2,
        Boolean useOrder
    )
    {
        // In the unordered case, guarantee that (A,B) and (B,A) are considered
        // duplicates by always combining them in the same order.

        Int64 i64HighBits, i64LowBits;

        if (useOrder || uniqueNumber1 < uniqueNumber2)
        {
            i64HighBits = (Int64)uniqueNumber1;
            i64LowBits = (Int64)uniqueNumber2;
        }
        else
        {
            i64HighBits = (Int64)uniqueNumber2;
            i64LowBits = (Int64)uniqueNumber1;
        }

        return ( (i64HighBits << 32) + i64LowBits );
    }
}

Related Tutorials