Skip to content

Commit 269b862

Browse files
committed
Merge branch 'fable-3' of https://github.com/delneg/FSharp.Data.Adaptive into fable-3
2 parents 6ca6192 + 2ff9eed commit 269b862

4 files changed

Lines changed: 47 additions & 35 deletions

File tree

RELEASE_NOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
### 1.2.2
2+
* added [HashMap|HashSet|IndexList] computeDeltaCustom
3+
14
### 1.2.1
25
* ShallowEquality now descends into non-recursive DUs (e.g. option)
36

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

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ module DifferentiationExtensions =
77
/// Functional programming operators related to the HashSet<_> type.
88
module HashSet =
99

10+
/// Determines the operations needed to transform l into r, using custom element operation functions.
11+
/// Returns a HashSetDelta containing these operations.
12+
let computeDeltaCustom (l: HashSet<'T>) (r: HashSet<'T>) (add : 'T -> bool) (remove : 'T -> bool) =
13+
l.ComputeDeltaAsHashMap(r, remove, add) |> HashSetDelta
14+
1015
/// Determines the operations needed to transform l into r.
1116
/// Returns a HashSetDelta containing these operations.
1217
let computeDelta (l: HashSet<'T>) (r: HashSet<'T>) =
@@ -43,15 +48,13 @@ module DifferentiationExtensions =
4348
/// Functional programming operators related to the HashMap<_,_> type.
4449
module HashMap =
4550

46-
/// Determines the operations needed to transform l into r.
51+
/// Determines the operations needed to transform l into r, using custom element operation functions.
4752
/// Returns a HashMapDelta containing all the needed operations.
48-
let computeDelta (l: HashMap<'A, 'B>) (r: HashMap<'A, 'B>): HashMapDelta<'A, 'B> =
49-
let inline add (_k : 'A) (v : 'B) = ValueSome (Set v)
50-
let inline remove (_k : 'A) (v : 'B) = ValueSome Remove
51-
let inline update (_k : 'A) (o : 'B) (n : 'B) =
52-
if DefaultEquality.equals o n then ValueNone
53-
else ValueSome (Set n)
54-
53+
let computeDeltaCustom
54+
(add: 'A -> 'B -> ValueOption<ElementOperation<'B>>)
55+
(remove: 'A -> 'B -> ValueOption<ElementOperation<'B>>)
56+
(update: 'A -> 'B -> 'B -> ValueOption<ElementOperation<'B>>)
57+
(l: HashMap<'A, 'B>) (r: HashMap<'A, 'B>): HashMapDelta<'A, 'B> =
5558
let delta =
5659
HashImplementation.MapNode.computeDelta
5760
l.Comparer
@@ -63,6 +66,16 @@ module DifferentiationExtensions =
6366

6467
HashMap<'A, ElementOperation<'B>>(l.Comparer, delta) |> HashMapDelta
6568

69+
/// Determines the operations needed to transform l into r.
70+
/// Returns a HashMapDelta containing all the needed operations.
71+
let computeDelta (l: HashMap<'A, 'B>) (r: HashMap<'A, 'B>): HashMapDelta<'A, 'B> =
72+
let inline add (_k : 'A) (v : 'B) = ValueSome (Set v)
73+
let inline remove (_k : 'A) (v : 'B) = ValueSome Remove
74+
let inline update (_k : 'A) (o : 'B) (n : 'B) =
75+
if DefaultEquality.equals o n then ValueNone
76+
else ValueSome (Set n)
77+
computeDeltaCustom add remove update l r
78+
6679
let applyDelta (l : HashMap<'K, 'V>) (r : HashMapDelta<'K, 'V>) =
6780
let inline apply (_ : 'K) (o : voption<'V>) (n : ElementOperation<'V>) =
6881
match n with
@@ -90,8 +103,8 @@ module DifferentiationExtensions =
90103
/// Functional programming operators related to the IndexList<_> type.
91104
module IndexList =
92105

93-
/// Determines the operations needed to transform l into r.
94-
/// Returns a IndexListDelta containing these operations.
106+
/// Applies the given operations to the list.
107+
/// Returns the new list and the 'effective' operations.
95108
let applyDelta (x : IndexList<'T>) (deltas : IndexListDelta<'T>) =
96109
let inline apply _ o n =
97110
match n with
@@ -108,14 +121,23 @@ module DifferentiationExtensions =
108121
struct(ValueSome v, ValueSome (Set v))
109122
let s, d = x.Content.ApplyDeltaAndGetEffective(deltas.Content, apply)
110123
IndexList.ofMap s, IndexListDelta.ofMap d
111-
112-
/// Applies the given operations to the list.
124+
125+
/// Applies the given operations to the list, using custom element operation functions.
113126
/// Returns the new list and the 'effective' operations.
127+
let computeDeltaCustom
128+
(add: Index -> 'T -> ElementOperation<'T>)
129+
(remove: Index -> 'T -> ElementOperation<'T>)
130+
(update: Index -> 'T -> 'T -> ValueOption<ElementOperation<'T>>)
131+
(l : IndexList<'T>) (r : IndexList<'T>) : IndexListDelta<'T> =
132+
let res = l.Content.ComputeDeltaTo(r.Content, add, update, remove)
133+
IndexListDelta res
134+
135+
/// Determines the operations needed to transform l into r.
136+
/// Returns a IndexListDelta containing these operations.
114137
let computeDelta (l : IndexList<'T>) (r : IndexList<'T>) : IndexListDelta<'T> =
115138
let inline add _ v = Set v
116139
let inline rem _ _ = Remove
117140
let inline update _ o n =
118141
if DefaultEquality.equals o n then ValueNone
119142
else ValueSome (Set n)
120-
let res = l.Content.ComputeDeltaTo(r.Content, add, update, rem)
121-
IndexListDelta res
143+
computeDeltaCustom add rem update l r

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3383,6 +3383,13 @@ type HashSet<'K> internal(comparer : IEqualityComparer<'K>, root : SetNode<'K>)
33833383
let delta = SetNode.computeDelta comparer remOp addOp root other.Root
33843384
HashMap<'K, int>(comparer, delta)
33853385

3386+
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
3387+
member x.ComputeDeltaAsHashMap(other : HashSet<'K>, remOp : 'K -> bool, addOp : 'K -> bool) =
3388+
let rem = fun k -> if remOp k then ValueSome -1 else ValueNone
3389+
let add = fun k -> if addOp k then ValueSome 1 else ValueNone
3390+
let delta = SetNode.computeDelta comparer rem add root other.Root
3391+
HashMap<'K, int>(comparer, delta)
3392+
33863393
[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
33873394
member x.ApplyDeltaAsHashMap(delta : HashMap<'K, int>) =
33883395
let state = root

src/FSharp.Data.Adaptive/FableHelpers.fs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,29 +101,9 @@ module OptimizedClosures =
101101

102102
namespace System.Runtime.CompilerServices
103103

104-
105-
[<AutoOpen>]
106-
module private WeakTableHelpers =
107-
open Fable.Core
108-
open Fable.Core.JsInterop
109-
110-
type [<AllowNullLiteral>] WeakMap<'K, 'V> =
111-
interface
112-
abstract clear: unit -> unit
113-
abstract delete: key: 'K -> bool
114-
abstract get: key: 'K -> 'V
115-
abstract has: key: 'K -> bool
116-
abstract set: key: 'K * ?value: 'V -> WeakMap<'K, 'V>
117-
end
118-
119-
and [<AllowNullLiteral>] WeakMapConstructor =
120-
[<Emit("new $0($1...)")>] abstract Create: ?iterable: seq<'K * 'V> -> WeakMap<'K, 'V>
121-
122-
let [<Global>] WeakMap: WeakMapConstructor = jsNative
123-
124104
type ConditionalWeakTable<'K, 'V when 'K : not struct and 'V : not struct>() =
125105

126-
let m = WeakMap.Create<'K, 'V> []
106+
let m = Fable.Core.JS.WeakMap.Create<'K, 'V> []
127107

128108
member x.TryGetValue(key : 'K) =
129109
if m.has key then (true, m.get key)

0 commit comments

Comments
 (0)