Skip to content

Commit 374d1c9

Browse files
m-renaudtreeowl
authored andcommitted
Improve IntSet and IntMap module docs. (#507)
1 parent 39e5e78 commit 374d1c9

File tree

3 files changed

+109
-90
lines changed

3 files changed

+109
-90
lines changed

Data/IntMap/Lazy.hs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,47 @@
1414
-- Maintainer : [email protected]
1515
-- Portability : portable
1616
--
17-
-- An efficient implementation of maps from integer keys to values
18-
-- (dictionaries).
1917
--
20-
-- API of this module is strict in the keys, but lazy in the values.
21-
-- If you need value-strict maps, use "Data.IntMap.Strict" instead.
22-
-- The 'IntMap' type itself is shared between the lazy and strict modules,
23-
-- meaning that the same 'IntMap' value can be passed to functions in
24-
-- both modules (although that is rarely needed).
18+
-- = Finite Int Maps (lazy interface)
2519
--
26-
-- These modules are intended to be imported qualified, to avoid name
27-
-- clashes with Prelude functions, e.g.
20+
-- The @'IntMap' v@ type represents a finite map (sometimes called a dictionary)
21+
-- from keys of type @Int@ to values of type @v@.
2822
--
29-
-- > import Data.IntMap.Lazy (IntMap)
30-
-- > import qualified Data.IntMap.Lazy as IntMap
23+
-- The functions in "Data.IntMap.Strict" are careful to force values before
24+
-- installing them in an 'IntMap'. This is usually more efficient in cases where
25+
-- laziness is not essential. The functions in this module do not do so.
26+
--
27+
-- For a walkthrough of the most commonly used functions see the
28+
-- <https://haskell-containers.readthedocs.io/en/latest/map.html maps introduction>.
29+
--
30+
-- This module is intended to be imported qualified, to avoid name clashes with
31+
-- Prelude functions:
32+
--
33+
-- > import Data.IntMap.Lazy (IntMap)
34+
-- > import qualified Data.IntMap.Lazy as IntMap
35+
--
36+
-- Note that the implementation is generally /left-biased/. Functions that take
37+
-- two maps as arguments and combine them, such as `union` and `intersection`,
38+
-- prefer the values in the first argument to those in the second.
39+
--
40+
--
41+
-- == Detailed performance information
42+
--
43+
-- The amortized running time is given for each operation, with /n/ referring to
44+
-- the number of entries in the map and /W/ referring to the number of bits in
45+
-- an 'Int' (32 or 64).
46+
--
47+
-- Benchmarks comparing "Data.IntMap.Lazy" with other dictionary
48+
-- implementations can be found at https://github.com/haskell-perf/dictionaries.
49+
--
50+
--
51+
-- == Implementation
3152
--
3253
-- The implementation is based on /big-endian patricia trees/. This data
33-
-- structure performs especially well on binary operations like 'union'
34-
-- and 'intersection'. However, my benchmarks show that it is also
35-
-- (much) faster on insertions and deletions when compared to a generic
36-
-- size-balanced map implementation (see "Data.Map").
54+
-- structure performs especially well on binary operations like 'union' and
55+
-- 'intersection'. Additionally, benchmarks show that it is also (much) faster
56+
-- on insertions and deletions when compared to a generic size-balanced map
57+
-- implementation (see "Data.Map").
3758
--
3859
-- * Chris Okasaki and Andy Gill, \"/Fast Mergeable Integer Maps/\",
3960
-- Workshop on ML, September 1998, pages 77-86,
@@ -43,18 +64,9 @@
4364
-- Information Coded In Alphanumeric/\", Journal of the ACM, 15(4),
4465
-- October 1968, pages 514-534.
4566
--
46-
-- Operation comments contain the operation time complexity in
47-
-- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>.
48-
-- Many operations have a worst-case complexity of /O(min(n,W))/.
49-
-- This means that the operation can become linear in the number of
50-
-- elements with a maximum of /W/ -- the number of bits in an 'Int'
51-
-- (32 or 64).
5267
-----------------------------------------------------------------------------
5368

5469
module Data.IntMap.Lazy (
55-
-- * Strictness properties
56-
-- $strictness
57-
5870
-- * Map type
5971
#if !defined(TESTING)
6072
IntMap, Key -- instance Eq,Show
@@ -211,15 +223,3 @@ module Data.IntMap.Lazy (
211223

212224
import Data.IntMap.Internal as IM hiding (showTree, showTreeWith)
213225
import Data.IntMap.Internal.DeprecatedDebug
214-
215-
-- $strictness
216-
--
217-
-- This module satisfies the following strictness property:
218-
--
219-
-- * Key arguments are evaluated to WHNF
220-
--
221-
-- Here are some examples that illustrate the property:
222-
--
223-
-- > insertWith (\ new old -> old) undefined v m == undefined
224-
-- > insertWith (\ new old -> old) k undefined m == OK
225-
-- > delete undefined m == undefined

Data/IntMap/Strict.hs

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,26 +15,63 @@
1515
-- Maintainer : [email protected]
1616
-- Portability : portable
1717
--
18-
-- An efficient implementation of maps from integer keys to values
19-
-- (dictionaries).
2018
--
21-
-- API of this module is strict in both the keys and the values.
22-
-- If you need value-lazy maps, use "Data.IntMap.Lazy" instead.
23-
-- The 'IntMap' type itself is shared between the lazy and strict modules,
24-
-- meaning that the same 'IntMap' value can be passed to functions in
25-
-- both modules (although that is rarely needed).
19+
-- = Finite Int Maps (strict interface)
2620
--
27-
-- These modules are intended to be imported qualified, to avoid name
28-
-- clashes with Prelude functions, e.g.
21+
-- The @'IntMap' v@ type represents a finite map (sometimes called a dictionary)
22+
-- from key of type @Int@ to values of type @v@.
2923
--
30-
-- > import Data.IntMap.Strict (IntMap)
31-
-- > import qualified Data.IntMap.Strict as IntMap
24+
-- Each function in this module is careful to force values before installing
25+
-- them in a 'Map'. This is usually more efficient when laziness is not
26+
-- necessary. When laziness /is/ required, use the functions in
27+
-- "Data.IntMap.Lazy".
28+
--
29+
-- In particular, the functions in this module obey the following law:
30+
--
31+
-- - If all values stored in all maps in the arguments are in WHNF, then all
32+
-- values stored in all maps in the results will be in WHNF once those maps
33+
-- are evaluated.
34+
--
35+
-- For a walkthrough of the most commonly used functions see the
36+
-- <https://haskell-containers.readthedocs.io/en/latest/map.html maps introduction>.
37+
--
38+
-- This module is intended to be imported qualified, to avoid name clashes with
39+
-- Prelude functions:
40+
--
41+
-- > import Data.IntMap.Strict (IntMap)
42+
-- > import qualified Data.IntMap.Strict as IntMap
43+
--
44+
-- Note that the implementation is generally /left-biased/. Functions that take
45+
-- two maps as arguments and combine them, such as `union` and `intersection`,
46+
-- prefer the values in the first argument to those in the second.
47+
--
48+
--
49+
-- == Detailed performance information
50+
--
51+
-- The amortized running time is given for each operation, with /n/ referring to
52+
-- the number of entries in the map and /W/ referring to the number of bits in
53+
-- an 'Int' (32 or 64).
54+
--
55+
-- Benchmarks comparing "Data.IntMap.Strict" with other dictionary
56+
-- implementations can be found at https://github.com/haskell-perf/dictionaries.
57+
--
58+
--
59+
-- == Warning
60+
--
61+
-- The 'IntMap' type is shared between the lazy and strict modules, meaning that
62+
-- the same 'IntMap' value can be passed to functions in both modules. This
63+
-- means that the 'Functor', 'Traversable' and 'Data' instances are the same as
64+
-- for the "Data.IntMap.Lazy" module, so if they are used the resulting map may
65+
-- contain suspended values (thunks).
66+
--
67+
--
68+
-- == Implementation
3269
--
3370
-- The implementation is based on /big-endian patricia trees/. This data
34-
-- structure performs especially well on binary operations like 'union'
35-
-- and 'intersection'. However, my benchmarks show that it is also
36-
-- (much) faster on insertions and deletions when compared to a generic
37-
-- size-balanced map implementation (see "Data.Map").
71+
-- structure performs especially well on binary operations like 'union' and
72+
-- 'intersection'. Additionally, benchmarks show that it is also (much) faster
73+
-- on insertions and deletions when compared to a generic size-balanced map
74+
-- implementation (see "Data.Map").
3875
--
3976
-- * Chris Okasaki and Andy Gill, \"/Fast Mergeable Integer Maps/\",
4077
-- Workshop on ML, September 1998, pages 77-86,
@@ -44,24 +81,11 @@
4481
-- Information Coded In Alphanumeric/\", Journal of the ACM, 15(4),
4582
-- October 1968, pages 514-534.
4683
--
47-
-- Operation comments contain the operation time complexity in
48-
-- the Big-O notation <http://en.wikipedia.org/wiki/Big_O_notation>.
49-
-- Many operations have a worst-case complexity of /O(min(n,W))/.
50-
-- This means that the operation can become linear in the number of
51-
-- elements with a maximum of /W/ -- the number of bits in an 'Int'
52-
-- (32 or 64).
53-
--
54-
-- Be aware that the 'Functor', 'Traversable' and 'Data' instances
55-
-- are the same as for the "Data.IntMap.Lazy" module, so if they are used
56-
-- on strict maps, the resulting maps will be lazy.
5784
-----------------------------------------------------------------------------
5885

5986
-- See the notes at the beginning of Data.IntMap.Internal.
6087

6188
module Data.IntMap.Strict (
62-
-- * Strictness properties
63-
-- $strictness
64-
6589
-- * Map type
6690
#if !defined(TESTING)
6791
IntMap, Key -- instance Eq,Show
@@ -310,24 +334,6 @@ import Data.Functor((<$>))
310334
#endif
311335
import Control.Applicative (Applicative (..), liftA2)
312336

313-
-- $strictness
314-
--
315-
-- This module satisfies the following strictness properties:
316-
--
317-
-- 1. Key arguments are evaluated to WHNF;
318-
--
319-
-- 2. Keys and values are evaluated to WHNF before they are stored in
320-
-- the map.
321-
--
322-
-- Here's an example illustrating the first property:
323-
--
324-
-- > delete undefined m == undefined
325-
--
326-
-- Here are some examples that illustrate the second property:
327-
--
328-
-- > map (\ v -> undefined) m == undefined -- m is not empty
329-
-- > mapKeys (\ k -> undefined) m == undefined -- m is not empty
330-
331337
{--------------------------------------------------------------------
332338
Query
333339
--------------------------------------------------------------------}

Data/IntSet.hs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,31 @@
1414
-- Maintainer : [email protected]
1515
-- Portability : portable
1616
--
17-
-- An efficient implementation of integer sets.
17+
--
18+
-- = Finite Int Sets
19+
--
20+
-- The @'IntSet'@ type represents a set of elements of type @Int@.
21+
--
22+
-- For a walkthrough of the most commonly used functions see their
23+
-- <https://haskell-containers.readthedocs.io/en/latest/set.html sets introduction>.
1824
--
1925
-- These modules are intended to be imported qualified, to avoid name
2026
-- clashes with Prelude functions, e.g.
2127
--
2228
-- > import Data.IntSet (IntSet)
2329
-- > import qualified Data.IntSet as IntSet
2430
--
31+
--
32+
-- == Performance information
33+
--
34+
-- Many operations have a worst-case complexity of /O(min(n,W))/.
35+
-- This means that the operation can become linear in the number of
36+
-- elements with a maximum of /W/ -- the number of bits in an 'Int'
37+
-- (32 or 64).
38+
--
39+
--
40+
-- == Implementation
41+
--
2542
-- The implementation is based on /big-endian patricia trees/. This data
2643
-- structure performs especially well on binary operations like 'union'
2744
-- and 'intersection'. However, my benchmarks show that it is also
@@ -38,14 +55,10 @@
3855
--
3956
-- Additionally, this implementation places bitmaps in the leaves of the tree.
4057
-- Their size is the natural size of a machine word (32 or 64 bits) and greatly
41-
-- reduce memory footprint and execution times for dense sets, e.g. sets where
42-
-- it is likely that many values lie close to each other. The asymptotics are
43-
-- not affected by this optimization.
58+
-- reduces the memory footprint and execution times for dense sets, e.g. sets
59+
-- where it is likely that many values lie close to each other. The asymptotics
60+
-- are not affected by this optimization.
4461
--
45-
-- Many operations have a worst-case complexity of /O(min(n,W))/.
46-
-- This means that the operation can become linear in the number of
47-
-- elements with a maximum of /W/ -- the number of bits in an 'Int'
48-
-- (32 or 64).
4962
-----------------------------------------------------------------------------
5063

5164
module Data.IntSet (

0 commit comments

Comments
 (0)