Function hash function without conflict for a specific data structure

Is it possible to create a messy hash function for a data structure with specific properties.

  • Datstructure - int [] [] []
  • It does not contain duplicates
  • The range of integers that it contains is determined. Let's say this is 0..1000, the maximum integer is definitely not more than 10000.

The big problem is that this hash function must also be very fast. Is there any way to create such a hash function? Maybe at runtime depending on a whole range?

ADDITION: I must say that the purpose of this hash function is to check with certainty whether a particular combination has been processed. Therefore, when some combination of numbers in the data structure is processed, I calculate the hash value and save it. Then, when processing another combination of numbers in the data structure, I will compare the hash values.

+3
source share
3 answers

I think what you want is a “perfect hash” or even a “minimum perfect hash”:

http://en.wikipedia.org/wiki/Perfect_hash_function

Edit: , [0... 1000], , , , , "" . , (, , ), 1001 , [0... 1000] [1001] ( int [1001] ), , .

+6

, 64- ?

- ( ): hash = (a << 34) | (b << 17) | (c)

0

, , , .

bool[][][], true , x, y, z ? . - Int32 1024 ( 128 ). 10 000, BitArray [] []. , , , 116 .

, - ( ) . , :

public class ThreeDimensionalBitArray
{
    // todo: consider making the size configurable
    private const int MAX_INDEX = 1000;

    private BitArray _bits = new BitArray(MAX_INDEX * MAX_INDEX * MAX_INDEX);

    public bool this[int x, int y, int z]
    {
        get { return _bits[getBitIndex(x, y, z)]; }
        set { _bits[getBitIndex(x, y, z)] = value; }
    }

    public ThreeDimensionalBitArray()
    {
    }

    private static int getBitIndex(int x, int y, int z)
    {
        // todo: bounds check x, y, and z

        return (x * MAX_INDEX * MAX_INDEX) + (y * MAX_INDEX) + z;
    }
}


public class BitArrayExample
{
    public static void Main()
    {
        ThreeDimensionalBitArray bitArray = new ThreeDimensionalBitArray();
        Console.WriteLine(bitArray[500, 600, 700]); // "false"
        bitArray[500, 600, 700] = true;
        Console.WriteLine(bitArray[500, 600, 700]); // "true"
    }
}
0

Source: https://habr.com/ru/post/1742167/


All Articles