CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2010
    Posts
    2

    Better than 'x*hash(key) mod N' !?

    Hi,

    when creating random values within an interval [0,N-1] from a specified Key you often see this calculation,

    Index = randomUniformOnInterval(Key, 0, 2^b - 1) mod N, where N << (2^b - 1)


    I propose to use this calculation instead. It is faster and in some applications memory utilization will be better, see further down.

    Index = N * randomUniformOnInterval(Key, 0, 2^b - 1) / 2^b

    Where the divition by 2^b can of course be replace with a b-right-bit-shift operation.
    The calculation is better understood looking at the formula from where it was derived.

    Index = randomUniformOnInterval(Key, 0, 2^b - 1) / (2^b / N)


    Hash tables implementations are often speed up by forcing the table size to be a power of two. This to be able to replace the modulo operation with an and-bit operation, i.e.

    Index = randomUniformOnInterval(Key, 0, 2^b - 1) & (2^B), where B << b


    So with one additional multiplication you can use arbitrary sizes of the hash table using the above proposed index calculation.



    Cheers!
    /Christofer

  2. #2
    Join Date
    Feb 2010
    Posts
    2

    Re: Better than 'x*hash(key) mod N' !?

    Hi,

    as no one have had any objections about the proposed calculation of hash indexes,
    I updated the wiki page below.
    http://en.wikipedia.org/wiki/Hash_fu...Variable_range

    Cheers!
    /Christofer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured