Skip to content

KeyBuilder: change int to direct str representation#261

Closed
matthiasdiener wants to merge 7 commits intoinducer:mainfrom
matthiasdiener:keyb-int-str
Closed

KeyBuilder: change int to direct str representation#261
matthiasdiener wants to merge 7 commits intoinducer:mainfrom
matthiasdiener:keyb-int-str

Conversation

@matthiasdiener
Copy link
Contributor

@matthiasdiener matthiasdiener commented Sep 3, 2024

Followup of #260.

Comment on lines +438 to +440
assert keyb(1) == keyb("1")
assert keyb(1.0) != keyb("1.0") # '1.0' uses hex representation
assert keyb(1+1j) == keyb("(1+1j)")
Copy link
Contributor Author

@matthiasdiener matthiasdiener Sep 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is an issue. If it is, we could change the KeyBuilder to (int, str(key)).

assert keyb(np.int32(2)) != keyb(np.int32(1))
assert keyb(np.int64(1)) == keyb(np.int64(1))
assert keyb(1) == keyb(np.int64(1))
assert keyb(1) != keyb(np.int64(1))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, I thought the goal was to keep them the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From your answer to my question in #260, my interpretation was that they don't need to be the same. Changing the code like in this PR perhaps also addresses #262.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They need to be the same. The fundamental property of a hash function $h$ is that $x = y \Rightarrow h(x) = h(y)$. Since

>>> np.int32(1) == 1
True

our hands are tied. I'm sorry if my early comment was ambiguous.

Copy link
Contributor Author

@matthiasdiener matthiasdiener Jan 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, we already break this principle in some (many?) places:

>>> from pytools.persistent_dict import KeyBuilder
>>> keyb = KeyBuilder()
>>> 1.0 == 1
True
>>> keyb(1.0) == keyb(1)
False

(with current main branch)

@mgorny
Copy link
Contributor

mgorny commented Nov 22, 2024

Just FYI, the tests pass on PPC64 BE with this patch applied.

@inducer
Copy link
Owner

inducer commented Jan 16, 2025

Unsubscribing... @-mention or request review once it's ready for a look or needs attention.

@inducer inducer closed this in #285 Jan 17, 2025
@matthiasdiener matthiasdiener deleted the keyb-int-str branch January 17, 2025 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants