Skip to content

Commit a296112

Browse files
committed
Remove comparable constraints - use SeqSet instead of Set
Fixes feedback from @MartinSStewart: The modules were unnecessarily restricting type variables to 'comparable'. Changes: - Replace Set with SeqSet (no comparable constraint needed) - Rename type vars from 'comparable1/comparable2' to 'k/v' for clarity - All three modules (BiSeqDict, MultiSeqDict, MultiBiSeqDict) now work with any types, not just comparables This fully leverages SeqDict's FNV hashing implementation.
1 parent 1498739 commit a296112

3 files changed

Lines changed: 128 additions & 128 deletions

File tree

src/BiSeqDict.elm

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,27 +63,27 @@ Example usage:
6363

6464
import SeqDict exposing (SeqDict)
6565

66-
import Set exposing (Set)
66+
import SeqSet exposing (SeqSet)
6767

6868

6969
{-| The underlying data structure. Think about it as
7070
7171
type alias BiSeqDict a b =
7272
{ forward : SeqDict a b -- just a normal Dict!
73-
, reverse : SeqDict b (Set a) -- the reverse mappings!
73+
, reverse : SeqDict b (SeqSet a) -- the reverse mappings!
7474
}
7575
7676
-}
77-
type BiSeqDict comparable1 comparable2
77+
type BiSeqDict k v
7878
= BiSeqDict
79-
{ forward : SeqDict comparable1 comparable2
80-
, reverse : SeqDict comparable2 (Set comparable1)
79+
{ forward : SeqDict k v
80+
, reverse : SeqDict v (SeqSet k)
8181
}
8282

8383

8484
{-| Create an empty dictionary.
8585
-}
86-
empty : BiSeqDict comparable1 comparable2
86+
empty : BiSeqDict k v
8787
empty =
8888
BiSeqDict
8989
{ forward = SeqDict.empty
@@ -93,18 +93,18 @@ empty =
9393

9494
{-| Create a dictionary with one key-value pair.
9595
-}
96-
singleton : comparable1 -> comparable2 -> BiSeqDict comparable1 comparable2
96+
singleton : k -> v -> BiSeqDict k v
9797
singleton from to =
9898
BiSeqDict
9999
{ forward = SeqDict.singleton from to
100-
, reverse = SeqDict.singleton to (Set.singleton from)
100+
, reverse = SeqDict.singleton to (SeqSet.singleton from)
101101
}
102102

103103

104104
{-| Insert a key-value pair into a dictionary. Replaces value when there is
105105
a collision.
106106
-}
107-
insert : comparable1 -> comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
107+
insert : k -> v -> BiSeqDict k v -> BiSeqDict k v
108108
insert from to (BiSeqDict d) =
109109
BiSeqDict
110110
{ d
@@ -122,7 +122,7 @@ insert from to (BiSeqDict d) =
122122
Just oldTo_ ->
123123
d.reverse
124124
|> SeqDict.update oldTo_
125-
(Maybe.map (Set.remove from)
125+
(Maybe.map (SeqSet.remove from)
126126
>> Maybe.andThen normalizeSet
127127
)
128128
in
@@ -133,7 +133,7 @@ insert from to (BiSeqDict d) =
133133

134134
{-| Update the value of a dictionary for a specific key with a given function.
135135
-}
136-
update : comparable1 -> (Maybe comparable2 -> Maybe comparable2) -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
136+
update : k -> (Maybe v -> Maybe v) -> BiSeqDict k v -> BiSeqDict k v
137137
update from fn (BiSeqDict d) =
138138
SeqDict.update from fn d.forward
139139
|> fromDict
@@ -142,7 +142,7 @@ update from fn (BiSeqDict d) =
142142
{-| In our model, (Just Set.empty) has the same meaning as Nothing.
143143
Make it be Nothing!
144144
-}
145-
normalizeSet : Set comparable -> Maybe (Set comparable)
145+
normalizeSet : Set comparable -> Maybe (SeqSet comparable)
146146
normalizeSet set =
147147
if Set.isEmpty set then
148148
Nothing
@@ -154,7 +154,7 @@ normalizeSet set =
154154
{-| Remove a key-value pair from a dictionary. If the key is not found,
155155
no changes are made.
156156
-}
157-
remove : comparable1 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
157+
remove : k -> BiSeqDict k v -> BiSeqDict k v
158158
remove from (BiSeqDict d) =
159159
BiSeqDict
160160
{ d
@@ -168,14 +168,14 @@ remove from (BiSeqDict d) =
168168
isEmpty empty == True
169169
170170
-}
171-
isEmpty : BiSeqDict comparable1 comparable2 -> Bool
171+
isEmpty : BiSeqDict k v -> Bool
172172
isEmpty (BiSeqDict d) =
173173
SeqDict.isEmpty d.forward
174174

175175

176176
{-| Determine if a key is in a dictionary.
177177
-}
178-
member : comparable1 -> BiSeqDict comparable1 comparable2 -> Bool
178+
member : k -> BiSeqDict k v -> Bool
179179
member from (BiSeqDict d) =
180180
SeqDict.member from d.forward
181181

@@ -191,23 +191,23 @@ dictionary.
191191
get "Spike" animals == Nothing
192192
193193
-}
194-
get : comparable1 -> BiSeqDict comparable1 comparable2 -> Maybe comparable2
194+
get : k -> BiSeqDict k v -> Maybe v
195195
get from (BiSeqDict d) =
196196
SeqDict.get from d.forward
197197

198198

199199
{-| Get the keys associated with a value. If the value is not found,
200200
return an empty set.
201201
-}
202-
getReverse : comparable2 -> BiSeqDict comparable1 comparable2 -> Set comparable1
202+
getReverse : v -> BiSeqDict k v -> Set k
203203
getReverse to (BiSeqDict d) =
204204
SeqDict.get to d.reverse
205205
|> Maybe.withDefault Set.empty
206206

207207

208208
{-| Determine the number of key-value pairs in the dictionary.
209209
-}
210-
size : BiSeqDict comparable1 comparable2 -> Int
210+
size : BiSeqDict k v -> Int
211211
size (BiSeqDict d) =
212212
SeqDict.size d.forward
213213

@@ -217,7 +217,7 @@ size (BiSeqDict d) =
217217
keys (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ]) == [ 0, 1 ]
218218
219219
-}
220-
keys : BiSeqDict comparable1 comparable2 -> List comparable1
220+
keys : BiSeqDict k v -> List k
221221
keys (BiSeqDict d) =
222222
SeqDict.keys d.forward
223223

@@ -227,50 +227,50 @@ keys (BiSeqDict d) =
227227
values (fromList [ ( 0, "Alice" ), ( 1, "Bob" ) ]) == [ "Alice", "Bob" ]
228228
229229
-}
230-
values : BiSeqDict comparable1 comparable2 -> List comparable2
230+
values : BiSeqDict k v -> List v
231231
values (BiSeqDict d) =
232232
SeqDict.values d.forward
233233

234234

235235
{-| Get a list of unique values in the dictionary.
236236
-}
237-
uniqueValues : BiSeqDict comparable1 comparable2 -> List comparable2
237+
uniqueValues : BiSeqDict k v -> List v
238238
uniqueValues (BiSeqDict d) =
239239
SeqDict.keys d.reverse
240240

241241

242242
{-| Get a count of unique values in the dictionary.
243243
-}
244-
uniqueValuesCount : BiSeqDict comparable1 comparable2 -> Int
244+
uniqueValuesCount : BiSeqDict k v -> Int
245245
uniqueValuesCount (BiSeqDict d) =
246246
SeqDict.size d.reverse
247247

248248

249249
{-| Convert a dictionary into an association list of key-value pairs, sorted by keys.
250250
-}
251-
toList : BiSeqDict comparable1 comparable2 -> List ( comparable1, comparable2 )
251+
toList : BiSeqDict k v -> List ( k, v )
252252
toList (BiSeqDict d) =
253253
SeqDict.toList d.forward
254254

255255

256256
{-| Convert a dictionary into a reverse association list of value-keys pairs.
257257
-}
258-
toReverseList : BiSeqDict comparable1 comparable2 -> List ( comparable2, Set comparable1 )
258+
toReverseList : BiSeqDict k v -> List ( v, Set k )
259259
toReverseList (BiSeqDict d) =
260260
SeqDict.toList d.reverse
261261

262262

263263
{-| Convert an association list into a dictionary.
264264
-}
265-
fromList : List ( comparable1, comparable2 ) -> BiSeqDict comparable1 comparable2
265+
fromList : List ( k, v ) -> BiSeqDict k v
266266
fromList list =
267267
SeqDict.fromList list
268268
|> fromDict
269269

270270

271271
{-| Apply a function to all values in a dictionary.
272272
-}
273-
map : (comparable1 -> comparable21 -> comparable22) -> BiSeqDict comparable1 comparable21 -> BiSeqDict comparable1 comparable22
273+
map : (k -> v1 -> v2) -> BiSeqDict k v1 -> BiSeqDict k v2
274274
map fn (BiSeqDict d) =
275275
-- TODO diff instead of throwing away and creating from scratch?
276276
SeqDict.map fn d.forward
@@ -279,14 +279,14 @@ map fn (BiSeqDict d) =
279279

280280
{-| Convert BiSeqDict into a SeqDict. (Throw away the reverse mapping.)
281281
-}
282-
toDict : BiSeqDict comparable1 comparable2 -> SeqDict comparable1 comparable2
282+
toDict : BiSeqDict k v -> SeqDict k v
283283
toDict (BiSeqDict d) =
284284
d.forward
285285

286286

287287
{-| Convert Dict into a BiSeqDict. (Compute the reverse mapping.)
288288
-}
289-
fromDict : SeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
289+
fromDict : SeqDict k v -> BiSeqDict k v
290290
fromDict forward =
291291
BiSeqDict
292292
{ forward = forward
@@ -322,7 +322,7 @@ fromDict forward =
322322
-- getAges users == [33,19,28]
323323
324324
-}
325-
foldl : (comparable1 -> comparable2 -> acc -> acc) -> acc -> BiSeqDict comparable1 comparable2 -> acc
325+
foldl : (k -> v -> acc -> acc) -> acc -> BiSeqDict k v -> acc
326326
foldl fn zero (BiSeqDict d) =
327327
SeqDict.foldl fn zero d.forward
328328

@@ -339,14 +339,14 @@ foldl fn zero (BiSeqDict d) =
339339
-- getAges users == [28,19,33]
340340
341341
-}
342-
foldr : (comparable1 -> comparable2 -> acc -> acc) -> acc -> BiSeqDict comparable1 comparable2 -> acc
342+
foldr : (k -> v -> acc -> acc) -> acc -> BiSeqDict k v -> acc
343343
foldr fn zero (BiSeqDict d) =
344344
SeqDict.foldr fn zero d.forward
345345

346346

347347
{-| Keep only the key-value pairs that pass the given test.
348348
-}
349-
filter : (comparable1 -> comparable2 -> Bool) -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
349+
filter : (k -> v -> Bool) -> BiSeqDict k v -> BiSeqDict k v
350350
filter fn (BiSeqDict d) =
351351
-- TODO diff instead of throwing away and creating from scratch?
352352
SeqDict.filter fn d.forward
@@ -357,7 +357,7 @@ filter fn (BiSeqDict d) =
357357
contains all key-value pairs which passed the test, and the second contains
358358
the pairs that did not.
359359
-}
360-
partition : (comparable1 -> comparable2 -> Bool) -> BiSeqDict comparable1 comparable2 -> ( BiSeqDict comparable1 comparable2, BiSeqDict comparable1 comparable2 )
360+
partition : (k -> v -> Bool) -> BiSeqDict k v -> ( BiSeqDict k v, BiSeqDict k v )
361361
partition fn (BiSeqDict d) =
362362
-- TODO diff instead of throwing away and creating from scratch?
363363
let
@@ -372,7 +372,7 @@ partition fn (BiSeqDict d) =
372372
{-| Combine two dictionaries. If there is a collision, preference is given
373373
to the first dictionary.
374374
-}
375-
union : BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
375+
union : BiSeqDict k v -> BiSeqDict k v -> BiSeqDict k v
376376
union (BiSeqDict left) (BiSeqDict right) =
377377
-- TODO diff instead of throwing away and creating from scratch?
378378
SeqDict.union left.forward right.forward
@@ -382,7 +382,7 @@ union (BiSeqDict left) (BiSeqDict right) =
382382
{-| Keep a key-value pair when its key appears in the second dictionary.
383383
Preference is given to values in the first dictionary.
384384
-}
385-
intersect : BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
385+
intersect : BiSeqDict k v -> BiSeqDict k v -> BiSeqDict k v
386386
intersect (BiSeqDict left) (BiSeqDict right) =
387387
-- TODO diff instead of throwing away and creating from scratch?
388388
SeqDict.intersect left.forward right.forward
@@ -391,7 +391,7 @@ intersect (BiSeqDict left) (BiSeqDict right) =
391391

392392
{-| Keep a key-value pair when its key does not appear in the second dictionary.
393393
-}
394-
diff : BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2 -> BiSeqDict comparable1 comparable2
394+
diff : BiSeqDict k v -> BiSeqDict k v -> BiSeqDict k v
395395
diff (BiSeqDict left) (BiSeqDict right) =
396396
-- TODO diff instead of throwing away and creating from scratch?
397397
SeqDict.diff left.forward right.forward
@@ -410,11 +410,11 @@ you want.
410410
411411
-}
412412
merge :
413-
(comparable1 -> comparable21 -> acc -> acc)
414-
-> (comparable1 -> comparable21 -> comparable22 -> acc -> acc)
415-
-> (comparable1 -> comparable22 -> acc -> acc)
416-
-> BiSeqDict comparable1 comparable21
417-
-> BiSeqDict comparable1 comparable22
413+
(k -> v1 -> acc -> acc)
414+
-> (k -> v1 -> v2 -> acc -> acc)
415+
-> (k -> v2 -> acc -> acc)
416+
-> BiSeqDict k v1
417+
-> BiSeqDict k v2
418418
-> acc
419419
-> acc
420420
merge fnLeft fnBoth fnRight (BiSeqDict left) (BiSeqDict right) zero =

0 commit comments

Comments
 (0)