Jump to page: 1 2
Thread overview
Unexpected behaviour in associative array
Apr 19, 2019
Arredondo
Apr 19, 2019
Andre Pany
Apr 19, 2019
Arredondo
Apr 19, 2019
Andre Pany
Apr 19, 2019
Andre Pany
Apr 19, 2019
Arredondo
Apr 19, 2019
Andre Pany
Apr 19, 2019
Adam D. Ruppe
Apr 19, 2019
Arredondo
Apr 19, 2019
Arredondo
Apr 19, 2019
Adam D. Ruppe
Apr 19, 2019
Arredondo
Apr 19, 2019
H. S. Teoh
Apr 19, 2019
JN
Apr 20, 2019
9il
Apr 20, 2019
Arredondo
Apr 21, 2019
9il
Apr 21, 2019
Arredondo
April 19, 2019
Hi all,

I'm using a custom Struct as the key type in an associative array. I have defined the toHash() and opEquals(...) functions, and the problem I'm having is that the test

mykey in aa

always fails (returns null) even though there are keys in the aa that return identical toHash() values than mykey and return true for opEquals. This is beyond frustrating, because at this point I'm pretty much out of ideas.

Have you had this problem before? Any tips or suggestions would be much appreciated.
Arredondo.
April 19, 2019
On Friday, 19 April 2019 at 11:10:16 UTC, Arredondo wrote:
> Hi all,
>
> I'm using a custom Struct as the key type in an associative array. I have defined the toHash() and opEquals(...) functions, and the problem I'm having is that the test
>
> mykey in aa
>
> always fails (returns null) even though there are keys in the aa that return identical toHash() values than mykey and return true for opEquals. This is beyond frustrating, because at this point I'm pretty much out of ideas.
>
> Have you had this problem before? Any tips or suggestions would be much appreciated.
> Arredondo.

Could you please post the coding, otherwise it is quite hard to help you.

Kind regards
Andre
April 19, 2019
On Friday, 19 April 2019 at 11:32:17 UTC, Andre Pany wrote:
>
> Could you please post the coding, otherwise it is quite hard to help you.
>
> Kind regards
> Andre

Yes, I'm working on isolating the problem. It's a bit laborious because the custom Struct is actually a wrapper around an ndslice matrix, and I still don't know if the issue is reproducible without this dependency.
April 19, 2019
On Friday, 19 April 2019 at 11:41:53 UTC, Arredondo wrote:
> On Friday, 19 April 2019 at 11:32:17 UTC, Andre Pany wrote:
>>
>> Could you please post the coding, otherwise it is quite hard to help you.
>>
>> Kind regards
>> Andre
>
> Yes, I'm working on isolating the problem. It's a bit laborious because the custom Struct is actually a wrapper around an ndslice matrix, and I still don't know if the issue is reproducible without this dependency.

With D the tool Dustmite is included, it reduces the code automatically for you. You just provide the expected compiler or linked output. It is also very well integrated into Dub.

Kind regards
Andre
April 19, 2019
On Friday, 19 April 2019 at 11:53:37 UTC, Andre Pany wrote:
> On Friday, 19 April 2019 at 11:41:53 UTC, Arredondo wrote:
>> On Friday, 19 April 2019 at 11:32:17 UTC, Andre Pany wrote:
>>>
>>> Could you please post the coding, otherwise it is quite hard to help you.
>>>
>>> Kind regards
>>> Andre
>>
>> Yes, I'm working on isolating the problem. It's a bit laborious because the custom Struct is actually a wrapper around an ndslice matrix, and I still don't know if the issue is reproducible without this dependency.
>
> With D the tool Dustmite is included, it reduces the code automatically for you. You just provide the expected compiler or linked output. It is also very well integrated into Dub.
>
> Kind regards
> Andre

https://dub.pm/commandline.html#dustmite
April 19, 2019
On Friday, 19 April 2019 at 11:55:41 UTC, Andre Pany wrote:
>
> https://dub.pm/commandline.html#dustmite

Thanks, but it appears that this tool is used to isolate the cause of build errors, and I'm not having build errors, just unexpected behavior at runtime.

Something I have observed while continuing the tinkering is that sometimes the call

key in aa

segfaults (Program exited with code -11) when key is not in the aa. Very strange indeed.
April 19, 2019
On Friday, 19 April 2019 at 12:03:33 UTC, Arredondo wrote:
> On Friday, 19 April 2019 at 11:55:41 UTC, Andre Pany wrote:
>>
>> https://dub.pm/commandline.html#dustmite
>
> Thanks, but it appears that this tool is used to isolate the cause of build errors, and I'm not having build errors, just unexpected behavior at runtime.
>
> Something I have observed while continuing the tinkering is that sometimes the call
>
> key in aa
>
> segfaults (Program exited with code -11) when key is not in the aa. Very strange indeed.

You can also use it for runtime, either use --program-status or --program-regex.

Kind regards
Andre
April 19, 2019
On Friday, 19 April 2019 at 11:32:17 UTC, Andre Pany wrote:
> Could you please post the coding, otherwise it is quite hard to help you.
>

Here's a reasonably-sized code fragment that demonstrates the issue. I hope the comments along the way are descriptive enough

Thanks,
Arredondo
----------

// this is a thin wrapper around a 2D byte matrix
// that uses an ndslice internally
struct State {
    import std.digest.murmurhash;
	import mir.ndslice;

    this(byte rows, byte cols) inout @safe pure nothrow {
        payload = slice!byte([rows, cols], 0);
    }

    size_t toHash() pure nothrow {
        byte[] data = payload.field();
        immutable digest = digest!(MurmurHash3!(128, 64))(data);
        immutable hash = *cast(size_t*) &digest[0];
        return hash;
    }

    bool opEquals(ref inout State q) inout @safe pure nothrow {
        return payload == q;
    }

    Slice!(Contiguous, [2], byte*) payload;
    alias payload this;
}

void main(string[] args) {
	import std.stdio;

	// create a key
	auto key1 = State(2, 2);
	key1[0, 0] = cast(byte) 1;

	// insert it in an assoc. array
	int[State] map;
	map[key1] = 101;

	// create the exact same key
	auto key2 = State(2, 2);
	key2[0, 0] = cast(byte) 1;

	// it is an identical key as far as the aa is concerned
	assert(key1.opEquals(key2));
	assert(key2.opEquals(key1));
	assert(key1.toHash == key2.toHash);

	// yet it is not in the map
	writeln(key1 in map); // prints some memory address
	writeln(key2 in map); // prints null <-- unexpected behaviour!!!!
}

April 19, 2019
On Friday, 19 April 2019 at 12:03:33 UTC, Arredondo wrote:
> key in aa

Keep in mind that D's `in` operator returns a *pointer* to the element, or null if it isn't there.

If you aren't treating the return value as a pointer, you could hit trouble.
April 19, 2019
On Friday, 19 April 2019 at 12:43:06 UTC, Adam D. Ruppe wrote:
> On Friday, 19 April 2019 at 12:03:33 UTC, Arredondo wrote:
>> key in aa
>
> Keep in mind that D's `in` operator returns a *pointer* to the element, or null if it isn't there.
>
> If you aren't treating the return value as a pointer, you could hit trouble.

I understand that. The issue is that it should't return null if theres a matching element in the aa!
« First   ‹ Prev
1 2