Tests/IMToolsTests.cs
/// <summary>
/// Folds all the map nodes with the state and index from left to right and from bottom to top
/// You may pass `parentStacks` to reuse the array memory.
/// NOTE: the length of `parentStack` should be at least of map height, content is not important and could be erased.
/// </summary>
Folds all the map nodes with the state and index from left to right and from bottom to top
You may pass `parentStacks` to reuse the array memory.
NOTE: the length of `parentStack` should be at least of map height, content is not important and could be erased.
Folds all the map nodes with the state and index from left to right and from bottom to top You may pass ` parentStacks ` to reuse the array memory . NOTE : the length of ` parentStack ` should be at least of map height content is not important and could be erased .
public S Fold<S>(S state, Func<ImMap<V>, int, S, S> reduce, ImMap<V>[] parentStack = null)
        {
            if (Height == 0)
                return state;
            if (Height == 1)
                return reduce(this, 0, state);
            if (Height == 2)
            {
                if (Left.Height != 0)
                    state = reduce(Left, 0, state);
                state = reduce(this, 1, state);
                if (Right.Height != 0)
                    state = reduce(Right, 2, state);
                return state;
            }
            parentStack = parentStack ?? new ImMap<V>[Height - 2];
            var map = this;
            var parentIndex = -1;
            var index = 0;
            do
            {
                if (map.Height == 1)
                {
                    state = reduce(map, index++, state);
                    if (parentIndex == -1)
                        break;
                    state = reduce(map = parentStack[parentIndex--], index++, state);
                    map = map.Right;
                }
                else if (map.Height == 2)
                {
                    if (map.Left.Height != 0)
                        state = reduce(Left, index++, state);
                    state = reduce(map, index++, state);
                    if (Right.Height != 0)
                        state = reduce(map.Right, index++, state);
                    if (parentIndex == -1)
                        break;
                    state = reduce(map = parentStack[parentIndex--], index++, state);
                    map = map.Right;
                }
                else
                {
                    parentStack[++parentIndex] = map;
                    map = map.Left;
                }
            } while (map.Height != 0);
            return state;
        }
public S Fold < S > ( S state , Func < ImMap < V > , int , S , S > reduce , ImMap < V > [ ] parentStack = null ) { if ( Height == 0 ) return state ; if ( Height == 1 ) return reduce ( this , 0 , state ) ; if ( Height == 2 ) { if ( Left . Height != 0 ) state = reduce ( Left , 0 , state ) ; state = reduce ( this , 1 , state ) ; if ( Right . Height != 0 ) state = reduce ( Right , 2 , state ) ; return state ; } parentStack = parentStack ?? new ImMap < V > [ Height - 2 ] ; var map = this ; var parentIndex = - 1 ; var index = 0 ; do { if ( map . Height == 1 ) { state = reduce ( map , index ++ , state ) ; if ( parentIndex == - 1 ) break ; state = reduce ( map = parentStack [ parentIndex -- ] , index ++ , state ) ; map = map . Right ; } else if ( map . Height == 2 ) { if ( map . Left . Height != 0 ) state = reduce ( Left , index ++ , state ) ; state = reduce ( map , index ++ , state ) ; if ( Right . Height != 0 ) state = reduce ( map . Right , index ++ , state ) ; if ( parentIndex == - 1 ) break ; state = reduce ( map = parentStack [ parentIndex -- ] , index ++ , state ) ; map = map . Right ; } else { parentStack [ ++ parentIndex ] = map ; map = map . Left ; } } while ( map . Height != 0 ) ; return state ; }
Folds all the map nodes with the state and index from left to right and from bottom to top
You may pass `parentStacks` to reuse the array memory.
Folds all the map nodes with the state and index from left to right and from bottom to top You may pass ` parentStacks ` to reuse the array memory .
state
S
reduce
Func<ImMap<V>, int, S, S>
parentStack
ImMap<V>[]