-
Notifications
You must be signed in to change notification settings - Fork 102
WIP: Improve unordered-containers API docs. #267
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
Merged
sjakobi
merged 1 commit into
haskell-unordered-containers:master
from
m-renaud:m-renaud-haddocks
Jul 20, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,25 +4,94 @@ | |
#endif | ||
|
||
------------------------------------------------------------------------ | ||
-- | | ||
-- Module : Data.HashSet | ||
-- Copyright : 2011 Bryan O'Sullivan | ||
-- License : BSD-style | ||
-- Maintainer : [email protected] | ||
-- Stability : provisional | ||
-- Portability : portable | ||
-- | ||
-- A set of /hashable/ values. A set cannot contain duplicate items. | ||
-- A 'HashSet' makes no guarantees as to the order of its elements. | ||
-- | ||
-- The implementation is based on /hash array mapped trie/. A | ||
-- 'HashSet' is often faster than other tree-based set types, | ||
-- especially when value comparison is expensive, as in the case of | ||
-- strings. | ||
-- | ||
-- Many operations have a average-case complexity of /O(log n)/. The | ||
-- implementation uses a large base (i.e. 16) so in practice these | ||
-- operations are constant time. | ||
{-| | ||
Module : Data.HashSet | ||
Copyright : 2011 Bryan O'Sullivan | ||
License : BSD-style | ||
Maintainer : [email protected] | ||
Stability : provisional | ||
Portability : portable | ||
|
||
= Introduction | ||
|
||
'HashSet' allows you to store /unique/ elements, providing efficient insertion, | ||
lookups, and deletion. A 'HashSet' makes no guarantees as to the order of its | ||
elements. | ||
|
||
If you are storing sets of "Data.Int"s consider using "Data.IntSet" from the | ||
<https://hackage.haskell.org/package/containers containers> package. | ||
|
||
|
||
== Examples | ||
|
||
All the examples below assume @HashSet@ is imported qualified, and uses the following @dataStructures@ set. | ||
|
||
>>> import qualified Data.HashSet as HashSet | ||
>>> let dataStructures = HashSet.fromList ["Set", "Map", "Graph", "Sequence"] | ||
|
||
=== Basic Operations | ||
|
||
Check membership in a set: | ||
|
||
>>> -- Check if "Map" and "Trie" are in the set of data structures. | ||
>>> HashSet.member "Map" dataStructures | ||
True | ||
>>> HashSet.member "Trie" dataStructures | ||
False | ||
|
||
Add a new entry to the set: | ||
|
||
>>> let moreDataStructures = HashSet.insert "Trie" dataStructures | ||
>>> HashSet.member "Trie" moreDataStructures | ||
> True | ||
|
||
Remove the @\"Graph\"@ entry from the set of data structures. | ||
|
||
>>> let fewerDataStructures = HashSet.delete "Graph" dataStructures | ||
>>> HashSet.toList fewerDataStructures | ||
["Map","Set","Sequence"] | ||
|
||
|
||
Create a new set and combine it with our original set. | ||
|
||
>>> let unorderedDataStructures = HashSet.fromList ["HashSet", "HashMap"] | ||
>>> HashSet.union dataStructures unorderedDataStructures | ||
fromList ["Map","HashSet","Graph","HashMap","Set","Sequence"] | ||
|
||
=== Using custom data with HashSet | ||
|
||
To create a @HashSet@ of your custom type, the type must have instances for | ||
'Data.Eq.Eq' and 'Data.Hashable.Hashable'. The @Hashable@ typeclass is defined in the | ||
<https://hackage.haskell.org/package/hashable hashable> package, see the | ||
documentation for information on how to make your type an instance of | ||
@Hashable@. | ||
|
||
We'll start by setting up our custom data type: | ||
|
||
>>> :set -XDeriveGeneric | ||
>>> import GHC.Generics (Generic) | ||
>>> import Data.Hashable | ||
>>> data Person = Person { name :: String, likesDogs :: Bool } deriving (Show, Eq, Generic) | ||
>>> instance Hashable Person | ||
|
||
And now we'll use it! | ||
|
||
>>> let people = HashSet.fromList [Person "Lana" True, Person "Joe" False, Person "Simon" True] | ||
>>> HashSet.filter likesDogs people | ||
fromList [Person {name = "Simon", likesDogs = True},Person {name = "Lana", likesDogs = True}] | ||
|
||
|
||
== Performance | ||
|
||
The implementation is based on /hash array mapped tries/. A | ||
'HashSet' is often faster than other 'Data.Ord.Ord'-based set types, | ||
especially when value comparisons are expensive, as in the case of | ||
strings. | ||
|
||
Many operations have a average-case complexity of /O(log n)/. The | ||
implementation uses a large base (i.e. 16) so in practice these | ||
operations are constant time. | ||
-} | ||
|
||
module Data.HashSet | ||
( | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.