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

Elaborate on asymptotics of IntMap #957

Merged
merged 1 commit into from
Mar 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion containers/src/Data/IntMap.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,29 @@
--
-- Operation comments contain the operation time complexity in
-- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>.
--
-- Many operations have a worst-case complexity of \(O(\min(n,W))\).
-- This means that the operation can become linear in the number of
-- elements with a maximum of \(W\) -- the number of bits in an 'Int'
-- (32 or 64).
-- (32 or 64). These peculiar asymptotics are determined by the depth
-- of the Patricia trees:
--
-- * even for an extremely unbalanced tree, the depth cannot be larger than
-- the number of elements \(n\),
-- * each level of a Patricia tree determines at least one more bit
-- shared by all subelements, so there could not be more
-- than \(W\) levels.
--
-- If all \(n\) keys in the tree are between 0 and \(N\) (or, say, between \(-N\) and \(N\)),
-- the estimate can be refined to \(O(\min(n, \log N))\). If the set of keys
-- is sufficiently "dense", this becomes \(O(\min(n, \log n))\) or simply
-- the familiar \(O(\log n)\), matching balanced binary trees.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are negative keys? Can we give a similarly concise refinement in that case?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can probably improve further, but we don't have to do it now.

--
-- The most performant scenario for 'IntMap' are keys from a contiguous subset,
-- in which case the complexity is proportional to \(\log n\), capped by \(W\).
-- The worst scenario are exponentially growing keys \(1,2,4,\ldots,2^n\),
-- for which complexity grows as fast as \(n\) but again is capped by \(W\).
--
-----------------------------------------------------------------------------

module Data.IntMap
Expand Down
Loading