Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParallelFixedLength{TIn, TOut}"/> class.
        /// </summary>
        /// <param name="pipeline">The pipeline to add the component to.</param>
        /// <param name="vectorSize">Vector size.</param>
        /// <param name="transform">Function mapping keyed input producers to output producers.</param>
        /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param>
        /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param>
        /// <param name="name">Name for this component (defaults to ParallelFixedLength).</param>
        /// <param name="defaultDeliveryPolicy">Pipeline-level default delivery policy to be used by this component (defaults to <see cref="DeliveryPolicy.Unlimited"/> if unspecified).</param>
        public ParallelFixedLength(Pipeline pipeline, int vectorSize, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped, TOut defaultValue = default, string name = null, DeliveryPolicy defaultDeliveryPolicy = null)
            : base(pipeline, name ?? nameof(ParallelFixedLength <TIn, TOut>), defaultDeliveryPolicy)
        {
            this.inConnector = this.CreateInputConnectorFrom <TIn[]>(pipeline, nameof(this.inConnector));
            this.splitter    = this.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.splitter));
            this.inConnector.PipeTo(this.splitter);
            this.branches = new Emitter <TIn> [vectorSize];
            var branchResults = new IProducer <TOut> [vectorSize];

            for (int i = 0; i < vectorSize; i++)
            {
                var subpipeline  = Subpipeline.Create(this, $"subpipeline{i}");
                var connectorIn  = new Connector <TIn>(this, subpipeline, $"connectorIn{i}");
                var connectorOut = new Connector <TOut>(subpipeline, this, $"connectorOut{i}");
                this.branches[i] = this.CreateEmitter <TIn>(this, $"branch{i}");
                this.branches[i].PipeTo(connectorIn);
                transform(i, connectorIn.Out).PipeTo(connectorOut.In);
                branchResults[i] = connectorOut;
            }

            var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>();

            this.join         = Operators.Join(branchResults, interpolator);
            this.outConnector = this.CreateOutputConnectorTo <TOut[]>(pipeline, nameof(this.outConnector));
            this.join.PipeTo(this.outConnector);
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParallelVariableLength{TIn, TOut}"/> class.
        /// </summary>
        /// <param name="pipeline">Pipeline to which this component belongs.</param>
        /// <param name="transform">Function mapping keyed input producers to output producers.</param>
        /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param>
        /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param>
        public ParallelVariableLength(Pipeline pipeline, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped = false, TOut defaultValue = default)
        {
            this.pipeline          = pipeline;
            this.parallelTransform = transform;
            this.In = pipeline.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.In));
            this.activeBranchesEmitter = pipeline.CreateEmitter <int>(this, nameof(this.activeBranchesEmitter));
            var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>();

            this.join = new Join <int, TOut, TOut[]>(
                pipeline,
                interpolator,
                (count, values) => values,
                0,
                count => Enumerable.Range(0, count));

            this.activeBranchesEmitter.PipeTo(this.join.InPrimary);
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParallelVariableLength{TIn, TOut}"/> class.
        /// </summary>
        /// <param name="pipeline">Pipeline to which this component belongs.</param>
        /// <param name="transform">Function mapping keyed input producers to output producers.</param>
        /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param>
        /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param>
        /// <param name="name">Name for this component (defaults to ParallelVariableLength).</param>
        /// <param name="defaultDeliveryPolicy">Pipeline-level default delivery policy to be used by this component (defaults to <see cref="DeliveryPolicy.Unlimited"/> if unspecified).</param>
        public ParallelVariableLength(Pipeline pipeline, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped = false, TOut defaultValue = default, string name = null, DeliveryPolicy defaultDeliveryPolicy = null)
            : base(pipeline, name ?? nameof(ParallelVariableLength <TIn, TOut>), defaultDeliveryPolicy)
        {
            this.parallelTransform = transform;
            this.inConnector       = this.CreateInputConnectorFrom <TIn[]>(pipeline, nameof(this.inConnector));
            this.splitter          = this.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.splitter));
            this.inConnector.PipeTo(this.splitter);
            this.activeBranchesEmitter = this.CreateEmitter <int>(this, nameof(this.activeBranchesEmitter));
            var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>();

            this.join = new Join <int, TOut, TOut[]>(
                this,
                interpolator,
                (count, values) => values,
                0,
                count => Enumerable.Range(0, count));

            this.activeBranchesEmitter.PipeTo(this.join.InPrimary);
            this.outConnector = this.CreateOutputConnectorTo <TOut[]>(pipeline, nameof(this.outConnector));
            this.join.PipeTo(this.outConnector);
        }
Пример #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParallelFixedLength{TIn, TOut}"/> class.
        /// </summary>
        /// <param name="pipeline">Pipeline to which this component belongs.</param>
        /// <param name="vectorSize">Vector size.</param>
        /// <param name="transform">Function mapping keyed input producers to output producers.</param>
        /// <param name="outputDefaultIfDropped">When true, a result is produced even if a message is dropped in processing one of the input elements. In this case the corresponding output element is set to a default value.</param>
        /// <param name="defaultValue">Default value to use when messages are dropped in processing one of the input elements.</param>
        public ParallelFixedLength(Pipeline pipeline, int vectorSize, Func <int, IProducer <TIn>, IProducer <TOut> > transform, bool outputDefaultIfDropped, TOut defaultValue = default)
        {
            this.In       = pipeline.CreateReceiver <TIn[]>(this, this.Receive, nameof(this.In));
            this.branches = new Emitter <TIn> [vectorSize];
            var branchResults = new IProducer <TOut> [vectorSize];

            for (int i = 0; i < vectorSize; i++)
            {
                var subpipeline  = Subpipeline.Create(pipeline, $"subpipeline{i}");
                var connectorIn  = new Connector <TIn>(pipeline, subpipeline, $"connectorIn{i}");
                var connectorOut = new Connector <TOut>(subpipeline, pipeline, $"connectorOut{i}");
                this.branches[i] = pipeline.CreateEmitter <TIn>(this, $"branch{i}");
                this.branches[i].PipeTo(connectorIn);
                transform(i, connectorIn.Out).PipeTo(connectorOut.In);
                branchResults[i] = connectorOut;
            }

            var interpolator = outputDefaultIfDropped ? Reproducible.ExactOrDefault <TOut>(defaultValue) : Reproducible.Exact <TOut>();

            this.join = Operators.Join(branchResults, interpolator);
        }