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

Length check in sameArray1 is redundant #465

Open
sjakobi opened this issue May 14, 2022 · 0 comments
Open

Length check in sameArray1 is redundant #465

sjakobi opened this issue May 14, 2022 · 0 comments

Comments

@sjakobi
Copy link
Member

sjakobi commented May 14, 2022

sameArray1 :: (a -> b -> Bool) -> Array a -> Array b -> Bool
sameArray1 eq !xs0 !ys0
| lenxs /= lenys = False
| otherwise = go 0 xs0 ys0
where
go !k !xs !ys
| k == lenxs = True
| (# x #) <- index# xs k
, (# y #) <- index# ys k
= eq x y && go (k + 1) xs ys
!lenxs = length xs0
!lenys = length ys0

The sameArray1 function used in equal1 and equalKeys compares the lengths of the two arrays as a first step. This is unnecessary because in both use-cases the lengths are known to equal because the corresponding bitmaps are the same or because the array of a Full node is known to have size maxChildren.

equal1 :: Eq k
=> (v -> v' -> Bool)
-> HashMap k v -> HashMap k v' -> Bool
equal1 eq = go
where
go Empty Empty = True
go (BitmapIndexed bm1 ary1) (BitmapIndexed bm2 ary2)
= bm1 == bm2 && A.sameArray1 go ary1 ary2
go (Leaf h1 l1) (Leaf h2 l2) = h1 == h2 && leafEq l1 l2
go (Full ary1) (Full ary2) = A.sameArray1 go ary1 ary2
go (Collision h1 ary1) (Collision h2 ary2)
= h1 == h2 && isPermutationBy leafEq (A.toList ary1) (A.toList ary2)
go _ _ = False
leafEq (L k1 v1) (L k2 v2) = k1 == k2 && eq v1 v2

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

No branches or pull requests

1 participant