Skip to content

Commit

Permalink
added ImMap234.Enumerate and fixing the Fold and releasing v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dadhi committed Jun 28, 2020
1 parent 1ad2436 commit 155b987
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 40 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
- Lib package: [![NuGet Badge](https://buildstats.info/nuget/ImTools.dll)](https://www.nuget.org/packages/ImTools.dll)
- Code package: [![NuGet Badge](https://buildstats.info/nuget/ImTools)](https://www.nuget.org/packages/ImTools)

Fast immutable persistent collections and other data structures
with focus on thread-safety without locks.
Latest stable version is [v2.1.0](https://github.com/dadhi/ImTools/releases/tag/v2.1.0)

Fast and memory-efficient immutable collections and helper data structures.

Split from [DryIoc](https://github.com/dadhi/dryioc).

Expand Down
16 changes: 2 additions & 14 deletions nuspecs/ImTools.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,10 @@
<tags>FP Performance Simple Functional Immutable Persistent Map Avl 2-3-4 Self Balanced Tree Dictionary Thread-safe Functional Atomic Ref Algebraic Discriminated Union SumType</tags>
<releaseNotes>
<![CDATA[
## v2.1.0 Feature release
- added: ImMap234 based on 2-3-4 tree #32
## v2.1.0 Feature release
## v2.0.0 Major feature release
- Added fast and more memory efficient Experimental.ImMap234 implementing 2-3-4 tree (#32)
### Highlights
- Faster and less allocative ImMap and ImHashMap with more methods (plus bucketed ImMapSlots and ImHashMapSlots variants)
- Algebraic sum-type aka descriminated union and one-liner records - Union<TUnion, T1..N>, Box<TBox, T>, Item<TItem, T>
- GrowingList<T>, ImZipper<T>, StackPool<T>, Unit, Fun extensions with forward pipe operators
- Ref.Swap now accepts state to use closure-less lambdas with less allocations
### Details
https://github.com/dadhi/ImTools/milestone/2
]]>
</releaseNotes>
<contentFiles>
Expand Down
85 changes: 76 additions & 9 deletions src/ImTools/ImTools.Experimental.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;

Expand Down Expand Up @@ -1951,8 +1952,11 @@ protected virtual ImMap234<V> AddOrKeepOrSplitEntry(int key, ref Entry entry, ou
/// <summary>Lookup for the entry, if not found returns `null`. You can define other Lookup methods on top of it.</summary>
public virtual Entry GetEntryOrDefault(int key) => null;

/// <summary>Folds</summary>
public virtual S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) => state;
/// <summary>Fold to fold</summary>
public virtual S Fold<S>(S state, Func<Entry, S, S> reduce) => state;

/// <summary>Enumerable</summary>
public virtual IEnumerable<Entry> Enumerate() => Enumerable.Empty<Entry>();

// todo: @feature add SoftRemove

Expand Down Expand Up @@ -2004,7 +2008,13 @@ public override Entry GetEntryOrDefault(int key) =>
key == Key ? this : null;

/// <inheritdoc />
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) => reduce(this, state);
public override S Fold<S>(S state, Func<Entry, S, S> reduce) => reduce(this, state);

/// <inheritdoc />
public override IEnumerable<Entry> Enumerate()
{
yield return this;
}
}

/// <summary>2 leafs</summary>
Expand Down Expand Up @@ -2073,8 +2083,15 @@ public override Entry GetEntryOrDefault(int key) =>
null;

/// <inheritdoc />
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
reduce(Entry1, reduce(Entry0, state));

/// <inheritdoc />
public override IEnumerable<Entry> Enumerate()
{
yield return Entry0;
yield return Entry1;
}
}

/// <summary>3 leafs</summary>
Expand Down Expand Up @@ -2150,8 +2167,16 @@ public override Entry GetEntryOrDefault(int key) =>
null;

/// <inheritdoc />
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
reduce(Entry2, reduce(Entry1, reduce(Entry0, state)));

/// <inheritdoc />
public override IEnumerable<Entry> Enumerate()
{
yield return Entry0;
yield return Entry1;
yield return Entry2;
}
}

/// <summary>3 leafs</summary>
Expand Down Expand Up @@ -2260,8 +2285,17 @@ public override Entry GetEntryOrDefault(int key) =>
null;

/// <inheritdoc />
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
reduce(Entry3, reduce(Entry2, reduce(Entry1, reduce(Entry0, state))));

/// <inheritdoc />
public override IEnumerable<Entry> Enumerate()
{
yield return Entry0;
yield return Entry1;
yield return Entry2;
yield return Entry3;
}
}

/// <summary>3 leafs</summary>
Expand Down Expand Up @@ -2462,8 +2496,18 @@ public override Entry GetEntryOrDefault(int key) =>
null;

/// <inheritdoc />
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
reduce(Entry4, reduce(Entry3, reduce(Entry2, reduce(Entry1, reduce(Entry0, state)))));

/// <inheritdoc />
public override IEnumerable<Entry> Enumerate()
{
yield return Entry0;
yield return Entry1;
yield return Entry2;
yield return Entry3;
yield return Entry4;
}
}

/// <summary>2 branches - it is never split itself, but may produce Branch3 if the lower branches are split</summary>
Expand Down Expand Up @@ -2595,8 +2639,18 @@ public override Entry GetEntryOrDefault(int key) =>
Entry0;

/// <inheritdoc />
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
Right.Fold(reduce(Entry0, Left.Fold(state, reduce)), reduce);

/// <inheritdoc />
public override IEnumerable<Entry> Enumerate()
{
foreach (var l in Left.Enumerate())
yield return l;
yield return Entry0;
foreach (var r in Right.Enumerate())
yield return r;
}
}

/// <summary>3 branches</summary>
Expand Down Expand Up @@ -2818,8 +2872,21 @@ public override Entry GetEntryOrDefault(int key) =>
key == Entry0.Key ? Entry0 : Entry1;

/// <inheritdoc />
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
Right.Fold(reduce(Entry1, Middle.Fold(reduce(Entry0, Left.Fold(state, reduce)), reduce)), reduce);

/// <inheritdoc />
public override IEnumerable<Entry> Enumerate()
{
foreach (var l in Left.Enumerate())
yield return l;
yield return Entry0;
foreach (var m in Middle.Enumerate())
yield return m;
yield return Entry1;
foreach (var r in Right.Enumerate())
yield return r;
}
}
}

Expand Down
17 changes: 2 additions & 15 deletions src/ImTools/ImTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,9 @@
<PackageTags>FP Performance Simple Functional Immutable Persistent Map Avl 2-3-4 Self Balanced Tree Dictionary Thread-safe Functional Atomic Ref Algebraic Discriminated Union SumType</PackageTags>
<PackageReleaseNotes>
<![CDATA[
## v2.1.0 Feature release
- added: ImMap234 based on 2-3-4 tree #32
## v2.1.0 Feature release
## v2.0.0 Major feature release
### Highlights
- Faster and less allocative ImMap and ImHashMap with more methods (plus bucketed ImMapSlots and ImHashMapSlots variants)
- Algebraic sum-type aka descriminated union and one-liner records - Union<TUnion, T1..N>, Box<TBox, T>, Item<TItem, T>
- GrowingList<T>, ImZipper<T>, StackPool<T>, Unit, Fun extensions with forward pipe operators
- Ref.Swap now accepts state to use closure-less lambdas with less allocations
### Details
https://github.com/dadhi/ImTools/milestone/2
- Added fast and more memory efficient Experimental.ImMap234 implementing 2-3-4 tree (#32)
]]>
</PackageReleaseNotes>
</PropertyGroup>
Expand Down
5 changes: 5 additions & 0 deletions test/ImTools.UnitTests/Experimental.ImMap234Tests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using NUnit.Framework;

namespace ImTools.Experimental.UnitTests
Expand Down Expand Up @@ -44,6 +45,8 @@ public void Adding_keys_from_1_to_10_and_checking_the_tree_shape_on_each_additio
Assert.AreEqual(5, m.GetValueOrDefault(5));
Assert.AreEqual(6, m.GetValueOrDefault(6));

CollectionAssert.AreEqual(Enumerable.Range(1, 6), m.Enumerate().Select(x => x.Value));

m = m.AddOrUpdate(7, 7);
Assert.IsInstanceOf<ImMap234<int>.Branch2>(m);
Assert.AreEqual(7, m.GetValueOrDefault(7));
Expand All @@ -57,6 +60,8 @@ public void Adding_keys_from_1_to_10_and_checking_the_tree_shape_on_each_additio

m = m.AddOrUpdate(10, 10);
Assert.AreEqual(10, m.GetValueOrDefault(10));

CollectionAssert.AreEqual(Enumerable.Range(1, 10), m.Enumerate().Select(x => x.Value));
}

[Test]
Expand Down

0 comments on commit 155b987

Please sign in to comment.