Skip to content

Commit 155b987

Browse files
committed
added ImMap234.Enumerate and fixing the Fold and releasing v2.1.0
1 parent 1ad2436 commit 155b987

File tree

5 files changed

+88
-40
lines changed

5 files changed

+88
-40
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
- Lib package: [![NuGet Badge](https://buildstats.info/nuget/ImTools.dll)](https://www.nuget.org/packages/ImTools.dll)
99
- Code package: [![NuGet Badge](https://buildstats.info/nuget/ImTools)](https://www.nuget.org/packages/ImTools)
1010

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

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

nuspecs/ImTools.nuspec

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,10 @@
1717
<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>
1818
<releaseNotes>
1919
<![CDATA[
20-
## v2.1.0 Feature release
21-
22-
- added: ImMap234 based on 2-3-4 tree #32
20+
## v2.1.0 Feature release
2321
24-
## v2.0.0 Major feature release
22+
- Added fast and more memory efficient Experimental.ImMap234 implementing 2-3-4 tree (#32)
2523
26-
### Highlights
27-
28-
- Faster and less allocative ImMap and ImHashMap with more methods (plus bucketed ImMapSlots and ImHashMapSlots variants)
29-
- Algebraic sum-type aka descriminated union and one-liner records - Union<TUnion, T1..N>, Box<TBox, T>, Item<TItem, T>
30-
- GrowingList<T>, ImZipper<T>, StackPool<T>, Unit, Fun extensions with forward pipe operators
31-
- Ref.Swap now accepts state to use closure-less lambdas with less allocations
32-
33-
### Details
34-
35-
https://github.com/dadhi/ImTools/milestone/2
3624
]]>
3725
</releaseNotes>
3826
<contentFiles>

src/ImTools/ImTools.Experimental.cs

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Linq;
45
using System.Runtime.CompilerServices;
56
using System.Threading;
67

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

1954-
/// <summary>Folds</summary>
1955-
public virtual S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) => state;
1955+
/// <summary>Fold to fold</summary>
1956+
public virtual S Fold<S>(S state, Func<Entry, S, S> reduce) => state;
1957+
1958+
/// <summary>Enumerable</summary>
1959+
public virtual IEnumerable<Entry> Enumerate() => Enumerable.Empty<Entry>();
19561960

19571961
// todo: @feature add SoftRemove
19581962

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

20062010
/// <inheritdoc />
2007-
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) => reduce(this, state);
2011+
public override S Fold<S>(S state, Func<Entry, S, S> reduce) => reduce(this, state);
2012+
2013+
/// <inheritdoc />
2014+
public override IEnumerable<Entry> Enumerate()
2015+
{
2016+
yield return this;
2017+
}
20082018
}
20092019

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

20752085
/// <inheritdoc />
2076-
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
2086+
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
20772087
reduce(Entry1, reduce(Entry0, state));
2088+
2089+
/// <inheritdoc />
2090+
public override IEnumerable<Entry> Enumerate()
2091+
{
2092+
yield return Entry0;
2093+
yield return Entry1;
2094+
}
20782095
}
20792096

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

21522169
/// <inheritdoc />
2153-
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
2170+
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
21542171
reduce(Entry2, reduce(Entry1, reduce(Entry0, state)));
2172+
2173+
/// <inheritdoc />
2174+
public override IEnumerable<Entry> Enumerate()
2175+
{
2176+
yield return Entry0;
2177+
yield return Entry1;
2178+
yield return Entry2;
2179+
}
21552180
}
21562181

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

22622287
/// <inheritdoc />
2263-
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
2288+
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
22642289
reduce(Entry3, reduce(Entry2, reduce(Entry1, reduce(Entry0, state))));
2290+
2291+
/// <inheritdoc />
2292+
public override IEnumerable<Entry> Enumerate()
2293+
{
2294+
yield return Entry0;
2295+
yield return Entry1;
2296+
yield return Entry2;
2297+
yield return Entry3;
2298+
}
22652299
}
22662300

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

24642498
/// <inheritdoc />
2465-
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
2499+
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
24662500
reduce(Entry4, reduce(Entry3, reduce(Entry2, reduce(Entry1, reduce(Entry0, state)))));
2501+
2502+
/// <inheritdoc />
2503+
public override IEnumerable<Entry> Enumerate()
2504+
{
2505+
yield return Entry0;
2506+
yield return Entry1;
2507+
yield return Entry2;
2508+
yield return Entry3;
2509+
yield return Entry4;
2510+
}
24672511
}
24682512

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

25972641
/// <inheritdoc />
2598-
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
2642+
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
25992643
Right.Fold(reduce(Entry0, Left.Fold(state, reduce)), reduce);
2644+
2645+
/// <inheritdoc />
2646+
public override IEnumerable<Entry> Enumerate()
2647+
{
2648+
foreach (var l in Left.Enumerate())
2649+
yield return l;
2650+
yield return Entry0;
2651+
foreach (var r in Right.Enumerate())
2652+
yield return r;
2653+
}
26002654
}
26012655

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

28202874
/// <inheritdoc />
2821-
public override S Fold<S>(S state, Func<Entry, S, S> reduce, ImMap234<V>[] parentStack = null) =>
2875+
public override S Fold<S>(S state, Func<Entry, S, S> reduce) =>
28222876
Right.Fold(reduce(Entry1, Middle.Fold(reduce(Entry0, Left.Fold(state, reduce)), reduce)), reduce);
2877+
2878+
/// <inheritdoc />
2879+
public override IEnumerable<Entry> Enumerate()
2880+
{
2881+
foreach (var l in Left.Enumerate())
2882+
yield return l;
2883+
yield return Entry0;
2884+
foreach (var m in Middle.Enumerate())
2885+
yield return m;
2886+
yield return Entry1;
2887+
foreach (var r in Right.Enumerate())
2888+
yield return r;
2889+
}
28232890
}
28242891
}
28252892

src/ImTools/ImTools.csproj

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,9 @@
2222
<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>
2323
<PackageReleaseNotes>
2424
<![CDATA[
25-
## v2.1.0 Feature release
26-
27-
- added: ImMap234 based on 2-3-4 tree #32
25+
## v2.1.0 Feature release
2826
29-
## v2.0.0 Major feature release
30-
31-
### Highlights
32-
33-
- Faster and less allocative ImMap and ImHashMap with more methods (plus bucketed ImMapSlots and ImHashMapSlots variants)
34-
- Algebraic sum-type aka descriminated union and one-liner records - Union<TUnion, T1..N>, Box<TBox, T>, Item<TItem, T>
35-
- GrowingList<T>, ImZipper<T>, StackPool<T>, Unit, Fun extensions with forward pipe operators
36-
- Ref.Swap now accepts state to use closure-less lambdas with less allocations
37-
38-
### Details
39-
40-
https://github.com/dadhi/ImTools/milestone/2
27+
- Added fast and more memory efficient Experimental.ImMap234 implementing 2-3-4 tree (#32)
4128
]]>
4229
</PackageReleaseNotes>
4330
</PropertyGroup>

test/ImTools.UnitTests/Experimental.ImMap234Tests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using NUnit.Framework;
34

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

48+
CollectionAssert.AreEqual(Enumerable.Range(1, 6), m.Enumerate().Select(x => x.Value));
49+
4750
m = m.AddOrUpdate(7, 7);
4851
Assert.IsInstanceOf<ImMap234<int>.Branch2>(m);
4952
Assert.AreEqual(7, m.GetValueOrDefault(7));
@@ -57,6 +60,8 @@ public void Adding_keys_from_1_to_10_and_checking_the_tree_shape_on_each_additio
5760

5861
m = m.AddOrUpdate(10, 10);
5962
Assert.AreEqual(10, m.GetValueOrDefault(10));
63+
64+
CollectionAssert.AreEqual(Enumerable.Range(1, 10), m.Enumerate().Select(x => x.Value));
6065
}
6166

6267
[Test]

0 commit comments

Comments
 (0)