Skip to main content
added 159 characters in body
Source Link
Ibrahim
  • 196
  • 1
  • 11

Say I have a canonized (alphabetical order) struct:

{
    health: 100,
    items: ["apple", "knife"],
    name: "Bobby"
}

"Canonized" here means any 2 structs with the same key,value pairs are equal. {a:1, b:2} is the same as {b:2, a:1}. The given canonized struct's keys are in alphabetical order.

I want to make a dictionary where I can lookup if I have set a "state" in it or not. I am using structs to represent "state". These structs have string-keys, and string/number/array-values.

Currently, I am simply converting the canonized struct into a string via string(struct) and then hashing that via variable_get_hash(str). Then I am storing that hash into another struct called "hashmap" like so: struct_set_from_hash(hashmap, hash, struct). Here it is all together:

function put_into_hashmap (hashmap={}, struct_key={}, any_value) {

    var str = string(struct_key); // convert struct into string
    var hash = variable_get_hash(str); // get hash from string
    struct_set_from_hash(hashmap, hash, any_value); // map value with hash
}

But when I print out the value of hash, it is a relatively small number. And since my canonized struct string can be much longer than this hash number, I am afraid of 2 different structs being set at the same hash, which will break my program.

So how can I hash this struct efficiently and reliably so that it is not possible to set 2 different structs into the same hash? Also, is this the most efficient way to hash a struct in GML? Should I be looking at a different algorithm?

Say I have a canonized (alphabetical order) struct:

{
    health: 100,
    items: ["apple", "knife"],
    name: "Bobby"
}

I want to make a dictionary where I can lookup if I have set a "state" in it or not. I am using structs to represent "state". These structs have string-keys, and string/number/array-values.

Currently, I am simply converting the canonized struct into a string via string(struct) and then hashing that via variable_get_hash(str). Then I am storing that hash into another struct called "hashmap" like so: struct_set_from_hash(hashmap, hash, struct). Here it is all together:

function put_into_hashmap (hashmap={}, struct_key={}, any_value) {

    var str = string(struct_key); // convert struct into string
    var hash = variable_get_hash(str); // get hash from string
    struct_set_from_hash(hashmap, hash, any_value); // map value with hash
}

But when I print out the value of hash, it is a relatively small number. And since my canonized struct string can be much longer than this hash number, I am afraid of 2 different structs being set at the same hash, which will break my program.

So how can I hash this struct efficiently and reliably so that it is not possible to set 2 different structs into the same hash? Also, is this the most efficient way to hash a struct in GML? Should I be looking at a different algorithm?

Say I have a canonized struct:

{
    health: 100,
    items: ["apple", "knife"],
    name: "Bobby"
}

"Canonized" here means any 2 structs with the same key,value pairs are equal. {a:1, b:2} is the same as {b:2, a:1}. The given canonized struct's keys are in alphabetical order.

I want to make a dictionary where I can lookup if I have set a "state" in it or not. I am using structs to represent "state". These structs have string-keys, and string/number/array-values.

Currently, I am simply converting the canonized struct into a string via string(struct) and then hashing that via variable_get_hash(str). Then I am storing that hash into another struct called "hashmap" like so: struct_set_from_hash(hashmap, hash, struct). Here it is all together:

function put_into_hashmap (hashmap={}, struct_key={}, any_value) {

    var str = string(struct_key); // convert struct into string
    var hash = variable_get_hash(str); // get hash from string
    struct_set_from_hash(hashmap, hash, any_value); // map value with hash
}

But when I print out the value of hash, it is a relatively small number. And since my canonized struct string can be much longer than this hash number, I am afraid of 2 different structs being set at the same hash, which will break my program.

So how can I hash this struct efficiently and reliably so that it is not possible to set 2 different structs into the same hash? Also, is this the most efficient way to hash a struct in GML? Should I be looking at a different algorithm?

Don't repeat tags in title
Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

GML - How to efficiently hash canonized structs

Source Link
Ibrahim
  • 196
  • 1
  • 11

GML - How to efficiently hash canonized structs

Say I have a canonized (alphabetical order) struct:

{
    health: 100,
    items: ["apple", "knife"],
    name: "Bobby"
}

I want to make a dictionary where I can lookup if I have set a "state" in it or not. I am using structs to represent "state". These structs have string-keys, and string/number/array-values.

Currently, I am simply converting the canonized struct into a string via string(struct) and then hashing that via variable_get_hash(str). Then I am storing that hash into another struct called "hashmap" like so: struct_set_from_hash(hashmap, hash, struct). Here it is all together:

function put_into_hashmap (hashmap={}, struct_key={}, any_value) {

    var str = string(struct_key); // convert struct into string
    var hash = variable_get_hash(str); // get hash from string
    struct_set_from_hash(hashmap, hash, any_value); // map value with hash
}

But when I print out the value of hash, it is a relatively small number. And since my canonized struct string can be much longer than this hash number, I am afraid of 2 different structs being set at the same hash, which will break my program.

So how can I hash this struct efficiently and reliably so that it is not possible to set 2 different structs into the same hash? Also, is this the most efficient way to hash a struct in GML? Should I be looking at a different algorithm?