@@ -96,6 +96,30 @@ module CollectionExtensions =
9696 )
9797 |> HashSetDelta.ofSeq
9898
99+ /// Reader for AList.toIndexedASet
100+ type IndexedListSetReader < 'T >( list : alist < 'T >) =
101+ inherit AbstractReader< HashSetDelta< Index * 'T>>( HashSetDelta.empty)
102+
103+ let reader = list.GetReader()
104+
105+ override x.Compute ( token : AdaptiveToken ) =
106+ let old = reader.State.Content
107+ reader.GetChanges( token) .Content |> Seq.collect ( fun ( KeyValue ( i , op )) ->
108+ match op with
109+ | Remove ->
110+ match MapExt.tryFind i old with
111+ | Some v -> Seq.singleton ( Rem( i, v))
112+ | None -> Seq.empty
113+ | Set v ->
114+ match MapExt.tryFind i old with
115+ | Some ov ->
116+ if DefaultEquality.equals v ov then Seq.empty
117+ else [ Add( i, v); Rem( i, ov)] :> seq<_>
118+ | None ->
119+ Seq.singleton ( Add( i, v))
120+ )
121+ |> HashSetDelta.ofSeq
122+
99123 /// Reader for AList.ofASet
100124 type ToListReader < 'a >( input : aset < 'a >) =
101125 inherit AbstractReader< IndexListDelta< 'a>>( IndexListDelta.empty)
@@ -206,6 +230,16 @@ module CollectionExtensions =
206230 else
207231 ASet.ofReader ( fun () -> ListSetReader( list))
208232
233+ /// Creates an aset holding all index/elements pairs of the given list.
234+ let ofAListIndexed ( list : alist < 'T >) =
235+ if list.IsConstant then
236+ list.Content
237+ |> AVal.force
238+ |> IndexList.toSeqIndexed
239+ |> ASet.ofSeq
240+ else
241+ ASet.ofReader ( fun () -> IndexedListSetReader( list))
242+
209243 /// Creates an amap with the keys from the set and the values given by mapping.
210244 let mapToAMap ( mapping : 'Key -> 'Value ) ( set : aset < 'Key >) = AMap.mapSet mapping set
211245
@@ -255,7 +289,10 @@ module CollectionExtensions =
255289
256290 /// Creates an aset holding all elements of the given list.
257291 let toASet ( list : alist < 'T >) = ASet.ofAList list
258-
292+
293+ /// Creates an aset holding all index/elements pairs of the given list.
294+ let toASetIndexed ( list : alist < 'T >) = ASet.ofAListIndexed list
295+
259296 /// Creates an alist from the set with undefined element order.
260297 let ofASet ( set : aset < 'T >) = ASet.toAList set
261298
0 commit comments