Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hash method to Rect and Box to enable caching #86

Merged
merged 1 commit into from
Apr 30, 2023

Conversation

Niels-NTG
Copy link
Contributor

I've implemented a __hash__ to both Rect and Box. This allows the results of functions that use these types in their arguments to be cached when using @functools.cache annotation with the function.

Here is a (be it somewhat trivial) example:

@functools.cache
def isRectinRect(rectA: Rect, rectB: Rect) -> bool:
    return (
        rectB.begin.x >= rectA.begin.x and
        rectB.begin.y >= rectA.begin.y and
        rectB.end.x <= rectA.end.x and
        rectB.end.y <= rectA.end.y
    )

By hashing rectA and rectB the result of this function can be cached for any combination of arguments, meaning that it can skip over re-calculating the results if the combination of arguments is already cached.

@avdstaaij
Copy link
Owner

Very nice!

@avdstaaij avdstaaij merged commit 2615126 into avdstaaij:dev Apr 30, 2023
@Niels-NTG Niels-NTG deleted the optimise-for-function-caching branch May 1, 2023 05:23
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.

2 participants