Skip to content

Commit

Permalink
added ApplyDeltaV and ApplyDeltaNoRefCountV
Browse files Browse the repository at this point in the history
  • Loading branch information
luithefirst committed Sep 24, 2024
1 parent 6a78e29 commit f66fd41
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/FSharp.Data.Adaptive/AdaptifyHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ChangeableModelMap<'K, 'V, 'C, 'A>(map : HashMap<'K, 'V>, init : 'V -> 'C,
| Remove ->
struct (ValueNone, ValueNone)

let s, ops = HashMap.ApplyDelta(history.State, HashMapDelta.toHashMap ops, apply)
let struct(s, ops) = HashMap.ApplyDeltaV(history.State, HashMapDelta.toHashMap ops, apply)
history.PerformUnsafe(s, HashMapDelta.ofHashMap ops) |> ignore

member x.GetReader() =
Expand Down
6 changes: 3 additions & 3 deletions src/FSharp.Data.Adaptive/AdaptiveHashSet/AdaptiveHashSet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ module AdaptiveHashSetImplementation =

struct (outRef, outDelta)

let newState, delta = HashMap.ApplyDelta(state, changes, apply)
let struct(newState, delta) = HashMap.ApplyDeltaV(state, changes, apply)
state <- newState
HashSetDelta.ofHashMap delta

Expand Down Expand Up @@ -700,7 +700,7 @@ module AdaptiveHashSetImplementation =

struct (outRef, outDelta)

let newState, delta = HashMap.ApplyDelta(state, changes, apply)
let struct(newState, delta) = HashMap.ApplyDeltaV(state, changes, apply)
state <- newState
HashSetDelta.ofHashMap delta

Expand Down Expand Up @@ -748,7 +748,7 @@ module AdaptiveHashSetImplementation =

struct (outRef, outDelta)

let newState, delta = HashMap.ApplyDelta(state, changes, apply)
let struct(newState, delta) = HashMap.ApplyDeltaV(state, changes, apply)
state <- newState
HashSetDelta.ofHashMap delta

Expand Down
8 changes: 8 additions & 0 deletions src/FSharp.Data.Adaptive/Datastructures/HashCollections.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3846,6 +3846,14 @@ and [<Struct; DebuggerDisplay("Count = {Count}"); DebuggerTypeProxy(typedefof<Ha
let state = HashMap<'K, 'V>(a.Comparer, state)
let delta = HashMap<'K, 'U>(a.Comparer, delta)
state, delta

[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
static member ApplyDeltaV(a : HashMap<'K, 'V>, b : HashMap<'K, 'T>, apply : 'K -> voption<'V> -> 'T -> struct(voption<'V> * voption<'U>)) =
let state = a.Root
let struct(delta, state) = MapNode.applyDelta a.Comparer (OptimizedClosures.FSharpFunc<_,_,_,_>.Adapt apply) state b.Root
let state = HashMap<'K, 'V>(a.Comparer, state)
let delta = HashMap<'K, 'U>(a.Comparer, delta)
struct(state, delta)

[<MethodImpl(MethodImplOptions.AggressiveInlining)>]
member x.Choose2V(other : HashMap<'K, 'T>, mapping : 'K -> voption<'V> -> voption<'T> -> voption<'U>) =
Expand Down
35 changes: 31 additions & 4 deletions src/FSharp.Data.Adaptive/Traceable/CountingHashSet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ type CountingHashSet<'T>(store : HashMap<'T, int>) =
let mutable res = HashMap<'B, int>.Empty
for (k,ro) in store do
let r = mapping k
let rr, _ =
HashMap<'B, int>.ApplyDelta(res, r.Store, fun _ oldRef delta ->
let struct(rr, _) =
HashMap<'B, int>.ApplyDeltaV(res, r.Store, fun _ oldRef delta ->
let oldRef = match oldRef with | ValueSome o -> o | ValueNone -> 0
struct (ValueSome (oldRef + ro * delta), ValueNone)
)
Expand Down Expand Up @@ -309,7 +309,7 @@ type CountingHashSet<'T>(store : HashMap<'T, int>) =

struct(value, delta)

let s, d = HashMap.ApplyDelta(store, deltas.Store, apply)
let struct(s, d) = HashMap.ApplyDeltaV(store, deltas.Store, apply)
CountingHashSet s, HashSetDelta d


Expand All @@ -333,9 +333,32 @@ type CountingHashSet<'T>(store : HashMap<'T, int>) =

struct(value, delta)

let s, d = HashMap.ApplyDelta(store, deltas.Store, apply)
let struct(s, d) = HashMap.ApplyDeltaV(store, deltas.Store, apply)
CountingHashSet s, HashSetDelta d

/// Integrates the given delta into the set, returns a new set and the effective deltas.
member x.ApplyDeltaNoRefCountV (deltas : HashSetDelta<'T>) =
let apply (_k : 'T) (o : voption<int>) (d : int) =
let o = match o with | ValueSome _ -> 1 | ValueNone -> 0
let n =
if d > 0 then 1
elif d < 0 then 0
else o

let delta =
if o = 0 && n > 0 then ValueSome 1
elif o > 0 && n = 0 then ValueSome -1
else ValueNone

let value =
if n <= 0 then ValueNone
else ValueSome n

struct(value, delta)

let struct(s, d) = HashMap.ApplyDeltaV(store, deltas.Store, apply)
struct(CountingHashSet s, HashSetDelta d)

/// Compares two sets.
static member internal Compare(l : CountingHashSet<'T>, r : CountingHashSet<'T>) =
let i = l.Intersect r
Expand Down Expand Up @@ -511,6 +534,10 @@ module CountingHashSet =
/// Integrates the given delta into the set without ref-counting, returns a new set and the effective deltas.
let inline applyDeltaNoRefCount (set : CountingHashSet<'T>) (delta : HashSetDelta<'T>) =
set.ApplyDeltaNoRefCount delta

/// Integrates the given delta into the set without ref-counting, returns a new set and the effective deltas.
let inline applyDeltaNoRefCountV (set : CountingHashSet<'T>) (delta : HashSetDelta<'T>) =
set.ApplyDeltaNoRefCountV delta

/// Compares two sets.
let internal compare (l : CountingHashSet<'T>) (r : CountingHashSet<'T>) =
Expand Down

0 comments on commit f66fd41

Please sign in to comment.