Skip to content

To add boolean keys in dictionaries. #1710

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

Closed
faze-geek opened this issue Apr 14, 2023 · 5 comments
Closed

To add boolean keys in dictionaries. #1710

faze-geek opened this issue Apr 14, 2023 · 5 comments

Comments

@faze-geek
Copy link
Contributor

Since boolean values namely True and False are hashable they can be used as dictionary keys -

def f():
    d: dict[bool,i32]
    d = {True: 1, False: 0}
    print(d)
f()
(lp) C:\Users\kunni\lpython>python try.py
{True: 1, False: 0}

(lp) C:\Users\kunni\lpython>src\bin\lpython try.py
Internal Compiler Error: Unhandled exception
Traceback (most recent call last):
LCompilersException: Hashing bool isn't implemented yet.

I am working on this. @czgdp1807 @certik is there a way / design that should be followed to implement the hashing ? Kindly let me know any reference prs / issues I should follow.

@faze-geek
Copy link
Contributor Author

The hashing logic is simple but I want to know how can I register Boolean in key_asr_type switch case. Can someone point me where it is done?

switch( key_asr_type->type ) {
            case ASR::ttypeType::Integer: {
            }
+            case ASR::ttypeType::Boolean: {
+                // Map true to 1 and false to 0
+                llvm::Value* bool_hash = builder->CreateZExt(
+                    key, llvm::Type::getInt32Ty(context)
+                );
+                return builder->CreateSRem(bool_hash, capacity);
+            }

@AtmegaBuzz
Copy link
Contributor

@faze-geek are you still working on this ?

@faze-geek
Copy link
Contributor Author

@faze-geek are you still working on this ?

Hi @AtmegaBuzz . I am working on this.

@czgdp1807
Copy link
Collaborator

dict[bool, f64] -> llvm::ArrayType of size 2. Mixed types dict[i32, dict[str, dict[bool, f64]]] can be dealt with later when we complete #1111. The reason being dict[bool, f64] will have a maximum size of 2 (a boolean can be either True or False, nothing more). So using the complicated dict algorithms when we already know the maximum size which is very small feels unncessary. Rest when @kabra1110 will implement, then we will deal with the drawbacks of this llvm::ArrayType approach.

@faze-geek
Copy link
Contributor Author

Closing as completed in #1771

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

No branches or pull requests

3 participants