Skip to content

Optimization idea for subsetArray #291

Open
@sjakobi

Description

@sjakobi

-- | /O(n*m)/ Check if the first array is a subset of the second array.
subsetArray :: Eq k => (v1 -> v2 -> Bool) -> A.Array (Leaf k v1) -> A.Array (Leaf k v2) -> Bool
subsetArray cmpV ary1 ary2 = A.length ary1 <= A.length ary2 && A.all inAry2 ary1
where
inAry2 (L k1 v1) = lookupInArrayCont (\_ -> False) (\v2 _ -> cmpV v1 v2) k1 ary2
{-# INLINE inAry2 #-}

Once we've identified an element of ary2 to correspond to an element of ary1, it would be nice if we wouldn't check the same element again against the subsequent elements of ary1.

@treeowl had a neat idea for that in #282 (comment):

I wouldn't record indices. But we could thaw one of the arrays and play mutation games. Say we have

1 2 3 4

and we search for 3. Then we can mutate the array to

undefined 2 1 4

That is, swap the 3 to the front and replace it with undefined. Then start the next scan in the second slot.

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