Skip to content

Commit

Permalink
* fixed complexity annotation (#61)
Browse files Browse the repository at this point in the history
* added `AList.toASetIndexed`
  • Loading branch information
krauthaufen committed Mar 31, 2020
1 parent 1f2b375 commit 96da1f8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
3 changes: 3 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 0.0.20
* added `AList.toASetIndexed`

### 0.0.19
* added ChangeableLazyVal
* more C# interop (MarkOutdated, AList creators)
Expand Down
39 changes: 38 additions & 1 deletion src/FSharp.Data.Adaptive/CollectionExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,30 @@ module CollectionExtensions =
)
|> HashSetDelta.ofSeq

/// Reader for AList.toIndexedASet
type IndexedListSetReader<'T>(list: alist<'T>) =
inherit AbstractReader<HashSetDelta<Index * 'T>>(HashSetDelta.empty)

let reader = list.GetReader()

override x.Compute(token: AdaptiveToken) =
let old = reader.State.Content
reader.GetChanges(token).Content |> Seq.collect (fun (KeyValue(i, op)) ->
match op with
| Remove ->
match MapExt.tryFind i old with
| Some v -> Seq.singleton (Rem(i, v))
| None -> Seq.empty
| Set v ->
match MapExt.tryFind i old with
| Some ov ->
if DefaultEquality.equals v ov then Seq.empty
else [Add(i,v); Rem(i,ov)] :> seq<_>
| None ->
Seq.singleton (Add(i,v))
)
|> HashSetDelta.ofSeq

/// Reader for AList.ofASet
type ToListReader<'a>(input : aset<'a>) =
inherit AbstractReader<IndexListDelta<'a>>(IndexListDelta.empty)
Expand Down Expand Up @@ -206,6 +230,16 @@ module CollectionExtensions =
else
ASet.ofReader (fun () -> ListSetReader(list))

/// Creates an aset holding all index/elements pairs of the given list.
let ofAListIndexed (list: alist<'T>) =
if list.IsConstant then
list.Content
|> AVal.force
|> IndexList.toSeqIndexed
|> ASet.ofSeq
else
ASet.ofReader (fun () -> IndexedListSetReader(list))

/// Creates an amap with the keys from the set and the values given by mapping.
let mapToAMap (mapping: 'Key -> 'Value) (set: aset<'Key>) = AMap.mapSet mapping set

Expand Down Expand Up @@ -255,7 +289,10 @@ module CollectionExtensions =

/// Creates an aset holding all elements of the given list.
let toASet (list: alist<'T>) = ASet.ofAList list


/// Creates an aset holding all index/elements pairs of the given list.
let toASetIndexed (list: alist<'T>) = ASet.ofAListIndexed list

/// Creates an alist from the set with undefined element order.
let ofASet (set: aset<'T>) = ASet.toAList set

Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Data.Adaptive/Datastructures/IndexList.fs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ type IndexList< [<EqualityConditionalOn>] 'T> internal(l : Index, h : Index, con
member x.IndexOf(item : 'T) =
x |> Seq.tryFindIndex (DefaultEquality.equals item) |> Option.defaultValue -1

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

Expand Down

0 comments on commit 96da1f8

Please sign in to comment.