Tests/IMToolsTests.cs
// todo: add `FindFirst`
/// <summary>
/// Folds all the map nodes with the state from left to right and from the 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 from left to right and from the 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 from left to right and from the 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>, S, S> reduce, ImMap<V>[] parentStack = null)
        {
            if (Height == 0)
                return state;
            if (Height == 1)
                return reduce(this, state);
            if (Height == 2)
                return this.ReduceTwoLevelTree(state, reduce);
            parentStack = parentStack ?? new ImMap<V>[Height - 2];
            var map = this;
            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 S Fold < S > ( S state , Func < ImMap < V > , S , S > reduce , ImMap < V > [ ] parentStack = null ) { if ( Height == 0 ) return state ; if ( Height == 1 ) return reduce ( this , state ) ; if ( Height == 2 ) return this . ReduceTwoLevelTree ( state , reduce ) ; parentStack = parentStack ?? new ImMap < V > [ Height - 2 ] ; var map = this ; 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 the map nodes with the state from left to right and from the bottom to top
You may pass `parentStacks` to reuse the array memory.
Folds all the map nodes with the state from left to right and from the bottom to top You may pass ` parentStacks ` to reuse the array memory .
state
S
reduce
Func<ImMap<V>, S, S>
parentStack
ImMap<V>[]