|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Diagnostics; |
| 4 | +using System.Linq; |
4 | 5 | using System.Runtime.CompilerServices; |
5 | 6 | using System.Threading; |
6 | 7 |
|
@@ -1951,8 +1952,11 @@ protected virtual ImMap234<V> AddOrKeepOrSplitEntry(int key, ref Entry entry, ou |
1951 | 1952 | /// <summary>Lookup for the entry, if not found returns `null`. You can define other Lookup methods on top of it.</summary> |
1952 | 1953 | public virtual Entry GetEntryOrDefault(int key) => null; |
1953 | 1954 |
|
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>(); |
1956 | 1960 |
|
1957 | 1961 | // todo: @feature add SoftRemove |
1958 | 1962 |
|
@@ -2004,7 +2008,13 @@ public override Entry GetEntryOrDefault(int key) => |
2004 | 2008 | key == Key ? this : null; |
2005 | 2009 |
|
2006 | 2010 | /// <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 | + } |
2008 | 2018 | } |
2009 | 2019 |
|
2010 | 2020 | /// <summary>2 leafs</summary> |
@@ -2073,8 +2083,15 @@ public override Entry GetEntryOrDefault(int key) => |
2073 | 2083 | null; |
2074 | 2084 |
|
2075 | 2085 | /// <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) => |
2077 | 2087 | reduce(Entry1, reduce(Entry0, state)); |
| 2088 | + |
| 2089 | + /// <inheritdoc /> |
| 2090 | + public override IEnumerable<Entry> Enumerate() |
| 2091 | + { |
| 2092 | + yield return Entry0; |
| 2093 | + yield return Entry1; |
| 2094 | + } |
2078 | 2095 | } |
2079 | 2096 |
|
2080 | 2097 | /// <summary>3 leafs</summary> |
@@ -2150,8 +2167,16 @@ public override Entry GetEntryOrDefault(int key) => |
2150 | 2167 | null; |
2151 | 2168 |
|
2152 | 2169 | /// <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) => |
2154 | 2171 | 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 | + } |
2155 | 2180 | } |
2156 | 2181 |
|
2157 | 2182 | /// <summary>3 leafs</summary> |
@@ -2260,8 +2285,17 @@ public override Entry GetEntryOrDefault(int key) => |
2260 | 2285 | null; |
2261 | 2286 |
|
2262 | 2287 | /// <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) => |
2264 | 2289 | 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 | + } |
2265 | 2299 | } |
2266 | 2300 |
|
2267 | 2301 | /// <summary>3 leafs</summary> |
@@ -2462,8 +2496,18 @@ public override Entry GetEntryOrDefault(int key) => |
2462 | 2496 | null; |
2463 | 2497 |
|
2464 | 2498 | /// <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) => |
2466 | 2500 | 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 | + } |
2467 | 2511 | } |
2468 | 2512 |
|
2469 | 2513 | /// <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) => |
2595 | 2639 | Entry0; |
2596 | 2640 |
|
2597 | 2641 | /// <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) => |
2599 | 2643 | 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 | + } |
2600 | 2654 | } |
2601 | 2655 |
|
2602 | 2656 | /// <summary>3 branches</summary> |
@@ -2818,8 +2872,21 @@ public override Entry GetEntryOrDefault(int key) => |
2818 | 2872 | key == Entry0.Key ? Entry0 : Entry1; |
2819 | 2873 |
|
2820 | 2874 | /// <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) => |
2822 | 2876 | 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 | + } |
2823 | 2890 | } |
2824 | 2891 | } |
2825 | 2892 |
|
|
0 commit comments