www.gusucode.com > bigdata 工具箱 matlab源码程序 > bigdata/+matlab/+bigdata/+internal/+lazyeval/InputFutureMap.m

    %InputFutureMap
% A map from closure dependencies to closure input future that can be used
% by the data processor instances to map data from dependencies into inputs
% required for the operation.
%

%   Copyright 2015-2016 The MathWorks, Inc.

classdef (Sealed) InputFutureMap < handle
    properties (SetAccess = immutable)
        % The number of operation inputs that will be generated by applying
        % this map to dependency inputs.
        NumOperationInputs;
        
        % The index of each function handle input into the list of
        % ExecutionTask inputs.
        InputIndices;
        
        % For each input this is the index into the multiplexed
        % ExecutionTask input.
        ArgoutIndices;
    end
    
    methods (Static)
        % Create a InputFutureMap using the provided ordered list of input
        % futures and ordered list of closure dependencies.
        function [obj, additionalConstants] = createFromClosures(inputFutures, dependencyClosures)
            import matlab.bigdata.internal.lazyeval.InputFutureMap;
            numOperationInputs = numel(inputFutures);
            additionalConstants = {};
            if numOperationInputs == 0
                obj = InputFutureMap([], []);
                return;
            end
            
            inputIndices = zeros(1, numOperationInputs);
            multiplexIndices = zeros(1, numOperationInputs);
            
            inputPromises = [inputFutures.Promise];
            isInputDone = [inputFutures.IsDone];
            
            if any(~isInputDone)
                [~, indices] = ismember([inputPromises(~isInputDone).Closure], dependencyClosures);
                inputIndices(~isInputDone) = indices;
                multiplexIndices(~isInputDone) = [inputPromises(~isInputDone).ArgoutIndex];
            end
            
            if any(isInputDone)
                additionalConstants = {inputFutures(isInputDone).Value};
                inputIndices(isInputDone) = numel(dependencyClosures) + 1;
                multiplexIndices(isInputDone) = 1:nnz(isInputDone);
            end
            
            obj = InputFutureMap(inputIndices, multiplexIndices);
        end
        
        % Create an InputFutureMap that expects only a single upstream
        % dependency, which maps each varargout output of that single
        % upstream dependency to an operation input.
        function obj = createPassthrough(numVariables)
            import matlab.bigdata.internal.lazyeval.InputFutureMap;
            dependencyIndices = ones(1, numVariables);
            multiplexIndices = 1:numVariables;
            obj = InputFutureMap(dependencyIndices, multiplexIndices);
        end
    end
    
    methods
        % Map data received from the upstream dependencies into the inputs
        % to be provided for each operation input.
        function operationInputs = mapData(obj, inputsFromDependencies)
            operationInputs = cell(size(obj.InputIndices));
            
            for ii = 1:numel(operationInputs)
                operationInputs{ii} = inputsFromDependencies{obj.InputIndices(ii)}(:, obj.ArgoutIndices(ii));
            end
        end
        
        % Map an array of scalars with one per upstream dependencies, into
        % an array of scalars with one per operation input.
        function out = mapScalars(obj, in)
            out = in(obj.InputIndices);
        end

        % Map an array of logicals with one per operation input, into an
        % array of logicals with one per upstream dependency. This applies
        % the any operation whenever an upstream dependency maps to
        % multiple operation inputs.
        function out = reverseMapLogicals(obj, in)
            out = accumarray(obj.InputIndices(:), in(:), [numel(obj.InputIndices), 1], @any);
            out = reshape(out, size(obj.InputIndices));
        end
    end
    
    methods (Access = private)
        % The main constructor.
        function obj = InputFutureMap(inputIndices, multiplexIndices)
            obj.NumOperationInputs = numel(inputIndices);
            obj.InputIndices = inputIndices(:)';
            obj.ArgoutIndices = multiplexIndices(:)';
        end
    end
end