private void ValueEnumerationHelper(ValueEnumerationDelegate callback, ValueNode <T> node, int[] indices, object state) { if (node.HasChildren) { int[] childIndices; for (int i = 0; i < node.Children.Count; i++) { childIndices = new int[indices.Length + 1]; Array.Copy(indices, childIndices, indices.Length); childIndices[indices.Length] = i; ValueEnumerationHelper(callback, node.Children[i], childIndices, state); } } else if (indices.Length == Depth) { // do callback and fall out of recursion callback(state, indices); } }
public override void EnumerateValues(object state, ValueEnumerationDelegate callback) { ValueEnumerationHelper(callback, _value, RepeatIndices.Empty, state); }
/// <summary> /// EnumerateValues uses the Visitor design pattern to enumerate all the values associated with this answer. /// For each value that is part of the answer, the supplied state object is passed to the supplied callback /// method, along with the repeat indices for that value. The callback must be a delegate of type ValueEnumerationDelegate. /// </summary> /// <param name="state">An object to keep track of whatever state you will need during the enumeration. /// For simple enumerations, passing a reference to the Answer object in question /// (so you can use the repeat indices to look up values) may be adequate.</param> /// <param name="callback">A delegate of type ValueEnumerationDelegate.</param> public abstract void EnumerateValues(object state, ValueEnumerationDelegate callback);