Tests/IMToolsTests.cs
/// <summary> Folds all map tree nodes without the order </summary>
Folds all map tree nodes without the order
Folds all map tree nodes without the order
public static S Fold<V, S>(this ImMap<V>[] slots, S state, Func<ImMap<V>, S, S> reduce)
        {
            var parentStack = ArrayTools.Empty<ImMap<V>>();
            for (var i = 0; i < slots.Length; i++)
            {
                var map = slots[i];
                var height = map.Height;
                if (height == 0)
                    continue;
                if (height == 1)
                    state = reduce(map, state);
                else if (height == 2)
                    state = map.ReduceTwoLevelTree(state, reduce);
                else
                {
                    if (parentStack.Length < height - 2)
                        parentStack = new ImMap<V>[height - 2];
                    var parentIndex = -1;
                    do
                    {
                        if (map.Height == 1)
                        {
                            state = reduce(map, state);
                            if (parentIndex == -1)
                                break;
                            state = reduce(map = parentStack[parentIndex--], state);
                            map = map.Right;
                        }
                        else if (map.Height == 2)
                        {
                            state = map.ReduceTwoLevelTree(state, reduce);
                            if (parentIndex == -1)
                                break;
                            state = reduce(map = parentStack[parentIndex--], state);
                            map = map.Right;
                        }
                        else
                        {
                            parentStack[++parentIndex] = map;
                            map = map.Left;
                        }
                    } while (map.Height != 0);
                }
            }
            return state;
        }
public static S Fold < V , S > ( this ImMap < V > [ ] slots , S state , Func < ImMap < V > , S , S > reduce ) { var parentStack = ArrayTools . Empty < ImMap < V > > ( ) ; for ( var i = 0 ; i < slots . Length ; i ++ ) { var map = slots [ i ] ; var height = map . Height ; if ( height == 0 ) continue ; if ( height == 1 ) state = reduce ( map , state ) ; else if ( height == 2 ) state = map . ReduceTwoLevelTree ( state , reduce ) ; else { if ( parentStack . Length < height - 2 ) parentStack = new ImMap < V > [ height - 2 ] ; var parentIndex = - 1 ; do { if ( map . Height == 1 ) { state = reduce ( map , state ) ; if ( parentIndex == - 1 ) break ; state = reduce ( map = parentStack [ parentIndex -- ] , state ) ; map = map . Right ; } else if ( map . Height == 2 ) { state = map . ReduceTwoLevelTree ( state , reduce ) ; if ( parentIndex == - 1 ) break ; state = reduce ( map = parentStack [ parentIndex -- ] , state ) ; map = map . Right ; } else { parentStack [ ++ parentIndex ] = map ; map = map . Left ; } } while ( map . Height != 0 ) ; } } return state ; }
Folds all map tree nodes without the order
Folds all map tree nodes without the order
ImMap<V>[]
this
state
S
reduce
Func<ImMap<V>, S, S>