/* Copyright (c) Microsoft Corporation All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. */ using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Microsoft.Research.DryadLinq { /// /// Defines the DryadLINQ interface for decomposable functions. It allows a function to be /// decomposed into the composition of several functions that can be executed more efficiently. /// /// The record type of the original input. /// The record type of an intermediate result. /// The record type of the final result. public interface IDecomposable { /// /// Initializes the state of this IDecomposable object. /// /// The state. void Initialize(object state); /// /// Converts an input record to an intermediate value. /// /// An input record. /// An intermediate result. TAccumulate Seed(TSource val); /// /// Adds a new input record into the intermediate value. /// /// The current intermediate value. /// A new input record. /// The new intermediate value. TAccumulate Accumulate(TAccumulate acc, TSource val); /// /// Combines two intermediate values into a new intermediate value. /// /// The first intermediate value. /// The second intermediate value. /// The new intermediate value. TAccumulate RecursiveAccumulate(TAccumulate acc, TAccumulate val); /// /// Computes the final result from the current intermediate value. /// /// An intermediate value. /// The final result. TResult FinalReduce(TAccumulate val); } /// /// A helper class for calling IDecomposable methods more efficiently. It is used in /// auto-generated vertex code. A DryadLINQ user should not need to use this class directly. /// /// The type that implements the IDecomposable interface /// The element type of the input sequence /// The element type of an intermediate result /// The element type of the final result public static class GenericDecomposable where TDecomposable : IDecomposable, new() { private static TDecomposable d = new TDecomposable(); /// /// Initializes the initial state of the IDecomposable object. /// /// The initial state of this IDecomposable object public static void Initialize(object state) { d.Initialize(state); } /// /// Converts an input element to an intermediate accumulator value. /// /// An input element /// An accumulator value public static TAccumulate Seed(TSource val) { return d.Seed(val); } /// /// Accumulates an input element into the accumulator value. /// /// An accumulator value /// An input element /// An accumulator value resulting from applying this Accumulate method /// on the two arguments public static TAccumulate Accumulate(TAccumulate acc, TSource val) { return d.Accumulate(acc, val); } /// /// Combines two accumulator values into one. /// /// The first accumulator value /// The second accumulator value /// An accumulator value resulting from combining two accumulator values public static TAccumulate RecursiveAccumulate(TAccumulate acc, TAccumulate val) { return d.RecursiveAccumulate(acc, val); } /// /// Produces the final value from an accumulator value. /// /// An accumulator value /// The value of the final result public static TResult FinalReduce(TAccumulate val) { return d.FinalReduce(val); } } }