Skip to content

Length check in sameArray1 is redundant #465

Open
@sjakobi

Description

@sjakobi

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions