Skip to content

Commit 96da1f8

Browse files
committed
* fixed complexity annotation (#61)
* added `AList.toASetIndexed`
1 parent 1f2b375 commit 96da1f8

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 0.0.20
2+
* added `AList.toASetIndexed`
3+
14
### 0.0.19
25
* added ChangeableLazyVal
36
* more C# interop (MarkOutdated, AList creators)

src/FSharp.Data.Adaptive/CollectionExtensions.fs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

src/FSharp.Data.Adaptive/Datastructures/IndexList.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ type IndexList< [<EqualityConditionalOn>] 'T> internal(l : Index, h : Index, con
314314
member x.IndexOf(item : 'T) =
315315
x |> Seq.tryFindIndex (DefaultEquality.equals item) |> Option.defaultValue -1
316316

317-
/// Tries to find the position for the given Index or -1 if the Index does not exist. O(N)
317+
/// Tries to find the position for the given Index or -1 if the Index does not exist. O(log N)
318318
member x.IndexOf(index : Index) =
319319
MapExt.tryIndexOf index content |> Option.defaultValue -1
320320

0 commit comments

Comments
 (0)