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

There is no O(1) way of checking if a dict is nonempty #251

Closed
imeckler opened this Issue May 23, 2015 · 3 comments

Comments

Projects
None yet
4 participants
@imeckler
Contributor

imeckler commented May 23, 2015

You could do Dict.foldl (\_ _ _ -> True) False, but this is O(n).

@imeckler imeckler changed the title from There is no O(1) way of checking if a dict is empty to There is no O(1) way of checking if a dict is nonempty May 23, 2015

@lachenmayer

This comment has been minimized.

Show comment
Hide comment
@lachenmayer

lachenmayer May 24, 2015

Looking at the source, it seems like isEmpty x = x == Dict.empty should always work.

The reason why the documentation states that "Dictionary equality with (==) is unreliable and should not be used" is that a Dict is a self-balancing red-black tree, and the distribution of red and black nodes depends on the insertion order.
That being said:

Dict.empty is defined as RBEmpty LBlack.
From a non-empty tree, the only way to arrive at an empty Dict is to remove an element.
Every remove operation performs blacken (here).
blacken will result in RBEmpty LBlack if the tree is empty (here).

I've only glanced at the code briefly, do let me know if you find a counter-example.
You're right though, Dict should have an isEmpty function, since List and String both contain one.

lachenmayer commented May 24, 2015

Looking at the source, it seems like isEmpty x = x == Dict.empty should always work.

The reason why the documentation states that "Dictionary equality with (==) is unreliable and should not be used" is that a Dict is a self-balancing red-black tree, and the distribution of red and black nodes depends on the insertion order.
That being said:

Dict.empty is defined as RBEmpty LBlack.
From a non-empty tree, the only way to arrive at an empty Dict is to remove an element.
Every remove operation performs blacken (here).
blacken will result in RBEmpty LBlack if the tree is empty (here).

I've only glanced at the code briefly, do let me know if you find a counter-example.
You're right though, Dict should have an isEmpty function, since List and String both contain one.

@evancz

This comment has been minimized.

Show comment
Hide comment
@evancz

evancz May 25, 2015

Member

I think the reason this function does not exist is because I wanted a uniform API across all the containers, but wasn't sure what that name should be / what all these libraries would look like. I think isEmpty is a nice way to go, so I think it's cool to add that as long as we do it in List, Dict, Set, and Array.

Can someone do a PR for this?

Member

evancz commented May 25, 2015

I think the reason this function does not exist is because I wanted a uniform API across all the containers, but wasn't sure what that name should be / what all these libraries would look like. I think isEmpty is a nice way to go, so I think it's cool to add that as long as we do it in List, Dict, Set, and Array.

Can someone do a PR for this?

@jvoigtlaender

This comment has been minimized.

Show comment
Hide comment
@jvoigtlaender

jvoigtlaender Jun 4, 2015

Contributor

I think this issue can be closed.

Contributor

jvoigtlaender commented Jun 4, 2015

I think this issue can be closed.

@evancz evancz closed this Jun 4, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment