Skip to content

How come fromList is O(n) instead of O(n*log n)? #309

Open
@sjakobi

Description

@sjakobi

-- | /O(n)/ Construct a map with the supplied mappings. If the list
-- contains duplicate mappings, the later mappings take precedence.
fromList :: (Eq k, Hashable k) => [(k, v)] -> HashMap k v
fromList = L.foldl' (\ m (k, v) -> unsafeInsert k v m) empty
{-# INLINABLE fromList #-}

By contrast, fromListWith is documented to be O(n*log n).

The difference between these functions is that the former uses unsafeInsert while the latter uses unsafeInsertWith. Both are recursive though, so I don't understand how fromList gets away with linear complexity?!

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