Skip to content

Commit 8f6ef9a

Browse files
authored
Elaborate on asymptotics of IntMap (#957)
1 parent 3c13e0b commit 8f6ef9a

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

containers/src/Data/IntMap.hs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,29 @@
5050
--
5151
-- Operation comments contain the operation time complexity in
5252
-- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>.
53+
--
5354
-- Many operations have a worst-case complexity of \(O(\min(n,W))\).
5455
-- This means that the operation can become linear in the number of
5556
-- elements with a maximum of \(W\) -- the number of bits in an 'Int'
56-
-- (32 or 64).
57+
-- (32 or 64). These peculiar asymptotics are determined by the depth
58+
-- of the Patricia trees:
59+
--
60+
-- * even for an extremely unbalanced tree, the depth cannot be larger than
61+
-- the number of elements \(n\),
62+
-- * each level of a Patricia tree determines at least one more bit
63+
-- shared by all subelements, so there could not be more
64+
-- than \(W\) levels.
65+
--
66+
-- If all \(n\) keys in the tree are between 0 and \(N\) (or, say, between \(-N\) and \(N\)),
67+
-- the estimate can be refined to \(O(\min(n, \log N))\). If the set of keys
68+
-- is sufficiently "dense", this becomes \(O(\min(n, \log n))\) or simply
69+
-- the familiar \(O(\log n)\), matching balanced binary trees.
70+
--
71+
-- The most performant scenario for 'IntMap' are keys from a contiguous subset,
72+
-- in which case the complexity is proportional to \(\log n\), capped by \(W\).
73+
-- The worst scenario are exponentially growing keys \(1,2,4,\ldots,2^n\),
74+
-- for which complexity grows as fast as \(n\) but again is capped by \(W\).
75+
--
5776
-----------------------------------------------------------------------------
5877

5978
module Data.IntMap

0 commit comments

Comments
 (0)