Exemplo n.º 1
0
        public void Evaluate(int SpreadMax)
        {
            int inputStreamsLength   = FInputStreams.Length;
            int maxInputStreamLength = FInputStreams.GetMaxLength();

            FOutputStream.Length = maxInputStreamLength * inputStreamsLength;

            var buffer = MemoryPool <T> .GetArray();

            try
            {
                using (var writer = FOutputStream.GetWriter())
                {
                    int numSlicesToRead = Math.Min(maxInputStreamLength, buffer.Length);
                    int i = 0;
                    foreach (var inputStream in FInputStreams)
                    {
                        writer.Position = i++;
                        using (var reader = inputStream.GetCyclicReader())
                        {
                            while (!writer.Eos)
                            {
                                reader.Read(buffer, 0, numSlicesToRead);
                                writer.Write(buffer, 0, numSlicesToRead, inputStreamsLength);
                            }
                        }
                    }
                }
            }
            finally
            {
                MemoryPool <T> .PutArray(buffer);
            }
        }
Exemplo n.º 2
0
 //called when data for any output pin is requested
 public void Evaluate(int spreadMax)
 {
     if (FInput.IsChanged || FGamma.IsChanged || FBinSize.IsChanged)
     {
         int sliceCount = 0;
         FOutput.Length = 0;
         for (int i = 0; i < spreadMax; i++)
         {
             double curBin   = (double)FBinSize[i];
             double curGamma = FGamma[i];
             FOutput.Length += FBinSize[i];
             using (var ow = FOutput.GetWriter())
             {
                 ow.Position = sliceCount;
                 double range = (curBin - 1.0) / curBin / curGamma;
                 range = 1 - range;
                 for (int j = 0; j < curBin; j++)
                 {
                     double start = (double)j / curBin / curGamma;
                     double val   = (FInput[i] - start) / range;
                     val = Math.Max(Math.Min(val, 1.0), 0.0);
                     ow.Write(val);
                 }
             }
             sliceCount = FOutput.Length;
         }
     }
 }
Exemplo n.º 3
0
        public void Evaluate(int spreadMax)
        {
            FOutput.Length = FInputGroup.GetMaxLength();
            var inputStreamReaders = FInputGroup.Select(i => i.GetCyclicReader());

            using (var intersperseReader = FIntersperseSequence.GetCyclicReader())
                using (var outputWriter = FOutput.GetWriter())
                {
                    while (!outputWriter.Eos)
                    {
                        var intersperseElement = intersperseReader.Read();
                        var streams            = inputStreamReaders
                                                 .Select(r => r.Read());
                        if (intersperseElement.Length > 0)
                        {
                            streams = streams.Intersperse(intersperseElement);
                        }
                        else
                        {
                            intersperseElement.Dispose();
                        }
                        outputWriter.Write(new AggregatedStream(streams));
                    }
                }
        }
Exemplo n.º 4
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    spread.Sync(FInput, vecSize, FBin);

                    FOutput.Length = spread.Count * vecSize;
                    using (var dataWriter = FOutput.GetWriter())
                        for (int b = 0; b < spread.Count; b++)
                        {
                            for (int v = 0; v < vecSize; v++)
                            {
                                dataWriter.Write(spread.GetBinColumn(b, v).Sum(), 1);
                            }
                        }
                }
                else
                {
                    FOutput.Length = 0;
                }
            }
        }
Exemplo n.º 5
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();
            FSelect.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged || FSelect.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0 && FSelect.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin, FSelect.Length);

                    List <double> container = new List <double>();
                    List <int>    formerId  = new List <int>();
                    using (var selReader = FSelect.GetCyclicReader())
                    {
                        int offset = 0;
                        for (int b = 0; b < spread.Count; b++)
                        {
                            int binSize = spread[b].Length / vecSize;
                            int sel     = selReader.Read();

                            if (sel > 0 && binSize > 0)
                            {
                                int[] ids = new int[binSize];
                                for (int i = 0; i < binSize; i++)
                                {
                                    ids[i] = offset + i;
                                }
                                for (int s = 0; s < sel; s++)
                                {
                                    container.AddRange(spread[b]);
                                    formerId.AddRange(ids);
                                }
                            }
                            offset += binSize;
                        }
                    }
                    FOutput.Length = container.Count;
                    if (FOutput.Length > 0)
                    {
                        FOutput.GetWriter().Write(container.ToArray(), 0, container.Count);
                    }

                    FFormer.Length = formerId.Count;
                    if (FFormer.Length > 0)
                    {
                        FFormer.GetWriter().Write(formerId.ToArray(), 0, formerId.Count);
                    }
                }
                else
                {
                    FOutput.Length = FFormer.Length = 0;
                }
            }
        }
Exemplo n.º 6
0
 public static void AssignFrom <T>(this IOutStream <T> outStream, IReadOnlyCollection <T> source)
 {
     outStream.Length = source.Count;
     using (var writer = outStream.GetWriter())
         foreach (var entry in source)
         {
             writer.Write(entry);
         }
 }
Exemplo n.º 7
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged || FClose.IsChanged)
            {
                if (FVec.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin, FClose.SliceCount);

                    FOutBin.Length = spread.Count;
                    FOutput.Length = FInput.Length * 2;
                    using (var binWriter = FOutBin.GetWriter())
                        using (var dataWriter = FOutput.GetWriter())
                        {
                            int incr = 0;
                            for (int b = 0; b < spread.Count; b++)
                            {
                                int c       = FClose[b] ? 0:1;
                                int binSize = Math.Max((spread[b].Length / vecSize - c) * 2, 0);
                                if (spread[b].Length > 0)
                                {
                                    for (int v = 0; v < vecSize; v++)
                                    {
                                        dataWriter.Position = incr + v;
                                        double[] src = spread.GetBinColumn(b, v).ToArray();

                                        for (int s = 0; s < binSize / 2; s++)
                                        {
                                            dataWriter.Write(src[s], vecSize);
                                            if (s + 1 < binSize / 2 || !(FClose[b]))
                                            {
                                                dataWriter.Write(src[s + 1], vecSize);
                                            }
                                            else
                                            {
                                                dataWriter.Write(src[0], vecSize);
                                            }
                                        }
                                    }
                                    incr += binSize * vecSize;
                                }
                                binWriter.Write(binSize, 1);
                            }
                            FOutput.Length = incr;
                        }
                }
                else
                {
                    FOutBin.Length = FOutput.Length = 0;
                }
            }
        }
Exemplo n.º 8
0
 public static void Append<T>(this IOutStream<T> outStream, IInStream<T> inStream, T[] buffer)
 {
     var initialOutLength = outStream.Length;
     outStream.Length += inStream.Length;
     using (var writer = outStream.GetWriter())
     {
         writer.Position = initialOutLength;
         writer.Write(inStream, buffer);
     }
 }
Exemplo n.º 9
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin);

                    FOutput.Length = Math.Max(spread.ItemCount - (spread.Count * vecSize), 0);
                    FOutBin.Length = spread.Count;
                    FOffset.Length = spread.Count * vecSize;
                    using (var offWriter = FOffset.GetWriter())
                        using (var binWriter = FOutBin.GetWriter())
                            using (var dataWriter = FOutput.GetWriter())
                            {
                                int incr = 0;
                                for (int b = 0; b < spread.Count; b++)
                                {
                                    if (spread[b].Length > 0)
                                    {
                                        for (int v = 0; v < vecSize; v++)
                                        {
                                            dataWriter.Position = incr + v;
                                            double[] column = spread.GetBinColumn(b, v).ToArray();
                                            for (int s = 0; s < column.Length - 1; s++)
                                            {
                                                dataWriter.Write(column[s + 1] - column[s], vecSize);
                                            }
                                        }
                                        incr += spread[b].Length - vecSize;
                                        binWriter.Write((spread[b].Length / vecSize) - 1, 1);

                                        offWriter.Write(spread.GetBinRow(b, 0).ToArray(), 0, vecSize);
                                    }
                                    else
                                    {
                                        binWriter.Write(0, 1);
                                        double[] zero = new double[vecSize];
                                        offWriter.Write(zero, 0, vecSize);
                                    }
                                }
                            }
                }
                else
                {
                    FOutput.Length = FOutBin.Length = FOffset.Length;
                }
            }
        }
Exemplo n.º 10
0
        public void Evaluate(int SpreadMax)
        {
            if (!FInputStreams.IsChanged && !FAllowEmptySpreadsConfig.IsChanged)
            {
                return;
            }

            IEnumerable <IInStream <T> > inputStreams;
            int inputStreamsLength;

            if (FAllowEmptySpreads)
            {
                inputStreams       = FInputStreams.Where(s => s.Length > 0);
                inputStreamsLength = inputStreams.Count();
            }
            else
            {
                inputStreams       = FInputStreams;
                inputStreamsLength = FInputStreams.Length;
            }
            int maxInputStreamLength = inputStreams.GetMaxLength();

            FOutputStream.Length = maxInputStreamLength * inputStreamsLength;

            if (FOutputStream.Length > 0)
            {
                var buffer = MemoryPool <T> .GetArray();

                try
                {
                    using (var writer = FOutputStream.GetWriter())
                    {
                        int numSlicesToRead = Math.Min(maxInputStreamLength, buffer.Length);
                        int i = 0;
                        foreach (var inputStream in inputStreams)
                        {
                            writer.Position = i++;
                            using (var reader = inputStream.GetCyclicReader())
                            {
                                while (!writer.Eos)
                                {
                                    reader.Read(buffer, 0, numSlicesToRead);
                                    writer.Write(buffer, 0, numSlicesToRead, inputStreamsLength);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    MemoryPool <T> .PutArray(buffer);
                }
            }
        }
Exemplo n.º 11
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    spread.Sync(FInput, vecSize, FBin);

                    FLast.Length = spread.NonEmptyBinCount * vecSize;
                    if (FLast.Length == spread.ItemCount || spread.ItemCount == 0)
                    {
                        FRemainder.Length = 0;
                        if (spread.ItemCount != 0)
                        {
                            FLast.AssignFrom(FInput);
                        }
                        else
                        {
                            FLast.Length = 0;
                        }
                    }
                    else
                    {
                        FRemainder.Length = spread.ItemCount - FLast.Length;
                        using (var rWriter = FRemainder.GetWriter())
                            using (var lWriter = FLast.GetWriter())
                            {
                                for (int b = 0; b < spread.Count; b++)
                                {
                                    if (spread[b].Length > 0)
                                    {
                                        int rLength = spread[b].Length - vecSize;
                                        rWriter.Write(spread[b], 0, rLength);

                                        lWriter.Write(spread[b], rLength, vecSize);
                                    }
                                }
                            }
                    }
                }
                else
                {
                    FRemainder.Length = FLast.Length = 0;
                }
            }
        }
Exemplo n.º 12
0
        public void Evaluate(int SpreadMax)
        {
            // Early exit - important for expensive types like strings and streams.
            if (!Prepare())
            {
                return;
            }

            var inputStreams = FInputContainer.IOObject;
            var outputStream = FOutputContainer.IOObject;

            var outputLength = inputStreams.GetLengthSum();

            outputStream.Length         = outputLength;
            FOutputBinSizeStream.Length = inputStreams.Length;

            var buffer = MemoryPool <T> .GetArray();

            try
            {
                using (var writer = outputStream.GetWriter())
                    using (var binSizeWriter = FOutputBinSizeStream.GetWriter())
                    {
                        foreach (var inputStream in inputStreams)
                        {
                            using (var reader = inputStream.GetReader())
                            {
                                var numSlicesToRead = reader.Length;
                                binSizeWriter.Write(numSlicesToRead);
                                if (numSlicesToRead == 1)
                                {
                                    writer.Write(reader.Read());
                                }
                                else
                                {
                                    while (numSlicesToRead > 0)
                                    {
                                        var numSlicesRead = reader.Read(buffer, 0, buffer.Length);
                                        writer.Write(buffer, 0, numSlicesRead);
                                        numSlicesToRead -= numSlicesRead;
                                    }
                                }
                            }
                        }
                    }
            }
            finally
            {
                MemoryPool <T> .PutArray(buffer);
            }
        }
Exemplo n.º 13
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged || FOffset.IsChanged || FInclOffset.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0 && FOffset.SliceCount > 0 && FInclOffset.SliceCount > 0)
                {
                    int vecSize     = Math.Max(1, FVec.GetReader().Read());
                    int offsetCount = (int)Math.Ceiling(FOffset.SliceCount / (double)vecSize);
                    offsetCount = offsetCount.CombineWith(FInclOffset);
                    spread.Sync(FInput, vecSize, FBin, offsetCount);

                    FOutBin.Length = spread.Count;
                    FOutput.Length = spread.ItemCount + (spread.Count * spread.VectorSize);
                    using (var binWriter = FOutBin.GetWriter())
                        using (var dataWriter = FOutput.GetWriter())
                        {
                            int incr = 0;
                            for (int b = 0; b < spread.Count; b++)
                            {
                                for (int v = 0; v < vecSize; v++)
                                {
                                    dataWriter.Position = incr + v;
                                    double sum = FOffset[b * vecSize + v];
                                    if (FInclOffset[b])
                                    {
                                        dataWriter.Write(sum, vecSize);
                                    }
                                    double[] column = spread.GetBinColumn(b, v).ToArray();
                                    for (int s = 0; s < column.Length; s++)
                                    {
                                        sum += column[s];
                                        dataWriter.Write(sum, vecSize);
                                    }
                                }
                                incr += spread[b].Length + (FInclOffset[b] ? vecSize:0);
                                binWriter.Write((spread[b].Length / vecSize) + 1, 1);
                            }
                            FOutput.Length = incr;
                        }
                }
                else
                {
                    FOutput.Length = FOutBin.Length = 0;
                }
            }
        }
Exemplo n.º 14
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged)
            {
                if (FVec.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin);

                    FCenter.Length = FWidth.Length = FMin.Length = FMax.Length = spread.Count * vecSize;
                    using (var cWriter = FCenter.GetWriter())
                        using (var wWriter = FWidth.GetWriter())
                            using (var minWriter = FMin.GetWriter())
                                using (var maxWriter = FMax.GetWriter())
                                {
                                    for (int b = 0; b < spread.Count; b++)
                                    {
                                        for (int v = 0; v < vecSize; v++)
                                        {
                                            if (spread[b].Length > 0)
                                            {
                                                double min   = spread.GetBinColumn(b, v).Min();
                                                double max   = spread.GetBinColumn(b, v).Max();
                                                double width = max - min;
                                                cWriter.Write(max - width / 2);
                                                wWriter.Write(width);
                                                minWriter.Write(min);
                                                maxWriter.Write(max);
                                            }
                                            else
                                            {
                                                cWriter.Write(double.NaN);
                                                wWriter.Write(double.NaN);
                                                minWriter.Write(double.MinValue);
                                                maxWriter.Write(double.MaxValue);
                                            }
                                        }
                                    }
                                }
                }
                else
                {
                    FCenter.Length = FWidth.Length = FMin.Length = FMax.Length = 0;
                }
            }
        }
Exemplo n.º 15
0
        public static void AssignFrom <T>(this IOutStream <T> outStream, IInStream <T> inStream, T[] buffer)
        {
            outStream.Length = inStream.Length;

            using (var reader = inStream.GetReader())
                using (var writer = outStream.GetWriter())
                {
                    while (!reader.Eos)
                    {
                        int numSlicesRead = reader.Read(buffer, 0, buffer.Length);
                        writer.Write(buffer, 0, numSlicesRead);
                    }
                }
        }
Exemplo n.º 16
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin);

                    FFirst.Length = spread.Count * vecSize;
                    if (FFirst.Length == spread.ItemCount || spread.ItemCount == 0)
                    {
                        FRemainder.Length = 0;
                        if (spread.ItemCount != 0)
                        {
                            FFirst.AssignFrom(FInput);
                        }
                        else
                        {
                            FFirst.Length = 0;
                        }
                    }
                    else
                    {
                        FRemainder.Length = spread.ItemCount - FFirst.Length;
                        using (var fWriter = FFirst.GetWriter())
                            using (var rWriter = FRemainder.GetWriter())
                            {
                                for (int b = 0; b < spread.Count; b++)
                                {
                                    fWriter.Write(spread.GetBinRow(b, 0).ToArray(), 0, vecSize);
                                    rWriter.Write(spread[b], vecSize, spread[b].Length - vecSize);
                                }
                            }
                    }
                }
                else
                {
                    FFirst.Length = FRemainder.Length = 0;
                }
            }
        }
Exemplo n.º 17
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FBin.IsChanged || FTogPhase.IsChanged || FPhase.IsChanged)
            {
                VecBinSpread <T> spread = new VecBinSpread <T>(FInput, 1, FBin, FPhase.CombineWith(FTogPhase));

                FOutBin.Length = spread.Count;
                FOutput.Length = spread.ItemCount;
                using (var binWriter = FOutBin.GetWriter())
                    using (var dataWriter = FOutput.GetWriter())
                    {
                        for (int b = 0; b < spread.Count; b++)
                        {
                            int phase = 0;
                            if (FTogPhase[b])
                            {
                                phase = (int)FPhase[b];
                            }
                            else
                            {
                                phase = (int)Math.Round(FPhase[b] * spread[b].Length);
                            }

                            T[] src = spread[b];
                            phase = VMath.Zmod(phase, src.Length);

                            if (phase != 0)
                            {
                                T[] dst = new T[src.Length];
                                Array.Copy(src, 0, dst, phase, src.Length - phase);
                                Array.Copy(src, src.Length - phase, dst, 0, phase);
                                dataWriter.Write(dst, 0, dst.Length);
                            }
                            else
                            {
                                dataWriter.Write(src, 0, src.Length);
                            }

                            binWriter.Write(spread[b].Length);
                        }
                    }
            }
        }
Exemplo n.º 18
0
        public void Evaluate(int SpreadMax)
        {
            var outputLength = FInputStreams.GetLengthSum();

            FOutputStream.Length        = outputLength;
            FOutputBinSizeStream.Length = FInputStreams.Length;

            var buffer = MemoryPool <T> .GetArray();

            try
            {
                using (var writer = FOutputStream.GetWriter())
                    using (var binSizeWriter = FOutputBinSizeStream.GetWriter())
                    {
                        foreach (var inputStream in FInputStreams)
                        {
                            using (var reader = inputStream.GetReader())
                            {
                                var numSlicesToRead = reader.Length;
                                binSizeWriter.Write(numSlicesToRead);
                                if (numSlicesToRead == 1)
                                {
                                    writer.Write(reader.Read());
                                }
                                else
                                {
                                    while (numSlicesToRead > 0)
                                    {
                                        var numSlicesRead = reader.Read(buffer, 0, buffer.Length);
                                        writer.Write(buffer, 0, numSlicesRead);
                                        numSlicesToRead -= numSlicesRead;
                                    }
                                }
                            }
                        }
                    }
            }
            finally
            {
                MemoryPool <T> .PutArray(buffer);
            }
        }
Exemplo n.º 19
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FBin.IsChanged)
            {
                spread.Sync(FInput, 1, FBin);

                FLast.Length = spread.Count;
                if (FLast.Length == spread.ItemCount || spread.ItemCount == 0)
                {
                    FRemainder.Length = 0;
                    if (spread.ItemCount != 0)
                    {
                        FLast.AssignFrom(FInput);
                    }
                    else
                    {
                        FLast.Length = 0;
                    }
                }
                else
                {
                    FRemainder.Length = spread.ItemCount - FLast.Length;
                    using (var rWriter = FRemainder.GetWriter())
                        using (var lWriter = FLast.GetWriter())
                        {
                            for (int b = 0; b < spread.Count; b++)
                            {
                                int rLength = spread[b].Length - 1;
                                rWriter.Write(spread[b], 0, rLength);

                                lWriter.Write(spread[b][rLength]);
                            }
                        }
                }
            }
        }
Exemplo n.º 20
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin);

                    if (spread.ItemCount == 0)
                    {
                        FOutput.Length = 0;
                    }
                    else
                    {
                        FOutput.Length = spread.Count * vecSize;
                        using (var dataWriter = FOutput.GetWriter())
                            for (int b = 0; b < spread.Count; b++)
                            {
                                if (spread[b].Length > 0)
                                {
                                    for (int v = 0; v < vecSize; v++)
                                    {
                                        dataWriter.Write(spread.GetBinColumn(b, v).DefaultIfEmpty(1).Aggregate((work, next) => work * next), 1);
                                    }
                                }
                            }
                    }
                }
                else
                {
                    FOutput.Length = 0;
                }
            }
        }
Exemplo n.º 21
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged || FReverse.IsChanged)
            {
                if (FVec.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    spread.Sync(FInput, vecSize, FBin, FReverse.SliceCount);

                    FOutput.Length = spread.ItemCount;
                    using (var dataWriter = FOutput.GetWriter())
                    {
                        for (int b = 0; b < spread.Count; b++)
                        {
                            if (FReverse[b])
                            {
                                for (int r = spread[b].Length - vecSize; r >= 0; r -= vecSize)
                                {
                                    dataWriter.Write(spread[b], r, vecSize);
                                }
                            }
                            else
                            {
                                dataWriter.Write(spread[b], 0, spread[b].Length);
                            }
                        }
                    }
                }
                else
                {
                    FOutput.Length = 0;
                }
            }
        }
Exemplo n.º 22
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged || FReverse.IsChanged)
            {
                if (FVec.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin, FReverse.SliceCount);

                    FOutput.Length = spread.ItemCount;
                    using (var dataWriter = FOutput.GetWriter())
                    {
                        for (int b = 0; b < spread.Count; b++)
                        {
                            if (FReverse[b])
                            {
                                for (int r = (spread[b].Length / vecSize) - 1; r >= 0; r--)
                                {
                                    dataWriter.Write(spread.GetBinRow(b, r).ToArray(), 0, vecSize);
                                }
                            }
                            else
                            {
                                dataWriter.Write(spread[b], 0, spread[b].Length);
                            }
                        }
                    }
                }
                else
                {
                    FOutput.Length = 0;
                }
            }
        }
Exemplo n.º 23
0
        public static void AssignFrom <T>(this IOutStream <T> outStream, IInStream <T> inStream)
        {
            if (inStream.Length != 1)
            {
                var buffer = MemoryPool <T> .GetArray();

                try
                {
                    outStream.AssignFrom(inStream, buffer);
                }
                finally
                {
                    MemoryPool <T> .PutArray(buffer);
                }
            }
            else
            {
                outStream.Length = 1;
                using (var reader = inStream.GetReader())
                    using (var writer = outStream.GetWriter())
                        writer.Write(reader.Read());
            }
        }
Exemplo n.º 24
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FBin.IsChanged)
            {
                VecBinSpread <T> spread = new VecBinSpread <T>(FInput, 1, FBin);

                FFirst.Length = spread.Count;
                if (FFirst.Length == spread.ItemCount || spread.ItemCount == 0)
                {
                    FRemainder.Length = 0;
                    if (spread.ItemCount != 0)
                    {
                        FFirst.AssignFrom(FInput);
                    }
                    else
                    {
                        FFirst.Length = 0;
                    };
                }
                else
                {
                    FRemainder.Length = spread.ItemCount - FFirst.Length;
                    using (var fWriter = FFirst.GetWriter())
                        using (var rWriter = FRemainder.GetWriter())
                        {
                            for (int b = 0; b < spread.Count; b++)
                            {
                                fWriter.Write(spread[b][0]);
                                rWriter.Write(spread[b], 1, spread[b].Length - 1);
                            }
                        }
                }
            }
        }
Exemplo n.º 25
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FVec.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FVec.IsChanged || FBin.IsChanged)
            {
                if (FInput.Length > 0 && FVec.Length > 0 && FBin.Length > 0)
                {
                    int vecSize = Math.Max(1, FVec.GetReader().Read());
                    VecBinSpread <double> spread = new VecBinSpread <double>(FInput, vecSize, FBin);

                    if (spread.ItemCount == 0)
                    {
                        FOutput.Length = 0;
                    }
                    else
                    {
                        FOutput.Length = spread.Count * vecSize;
                        using (var dataWriter = FOutput.GetWriter())
                            for (int b = 0; b < spread.Count; b++)
                            {
                                for (int v = 0; v < vecSize; v++)
                                {
                                    dataWriter.Write(spread.GetBinColumn(b, v).Select(f => f * f).Average(), 1);
                                }
                            }
                    }
                }
                else
                {
                    FOutput.Length = 0;
                }
            }
        }
Exemplo n.º 26
0
                #pragma warning restore
        #endregion fields & pins

        //called when data for any output pin is requested
        public void Evaluate(int SpreadMax)
        {
            FInput.Sync();
            FBin.Sync();

            if (FInput.IsChanged || FBin.IsChanged)
            {
                VecBinSpread <T> spread = new VecBinSpread <T>(FInput, 1, FBin, FReverse.SliceCount);

                FOutput.Length = spread.ItemCount;
                using (var dataWriter = FOutput.GetWriter())
                {
                    for (int b = 0; b < spread.Count; b++)
                    {
                        T[] bin = spread[b];
                        if (FReverse[b])
                        {
                            Array.Reverse(bin);
                        }
                        dataWriter.Write(bin, 0, bin.Length);
                    }
                }
            }
        }
Exemplo n.º 27
0
            public override void Flush(bool force = false)
            {
                var buffer = MemoryPool <T> .GetArray();

                var binSizeBuffer = MemoryPool <int> .GetArray();

                try
                {
                    FBinSizeStream.Length = Length;

                    int dataStreamLength = 0;
                    using (var binSizeWriter = FBinSizeStream.GetWriter())
                    {
                        var numSlicesBuffered = 0;
                        for (int i = 0; i < Length; i++)
                        {
                            var stream = Buffer[i].Stream;
                            binSizeBuffer[numSlicesBuffered++] = stream.Length;
                            dataStreamLength += stream.Length;
                            if (numSlicesBuffered == binSizeBuffer.Length)
                            {
                                binSizeWriter.Write(binSizeBuffer, 0, numSlicesBuffered);
                                numSlicesBuffered = 0;
                            }
                        }
                        if (numSlicesBuffered > 0)
                        {
                            binSizeWriter.Write(binSizeBuffer, 0, numSlicesBuffered);
                        }
                    }

                    FDataStream.Length = dataStreamLength;
                    using (var dataWriter = FDataStream.GetWriter())
                    {
                        bool anyChanged = force || IsChanged;
                        for (int i = 0; i < Length; i++)
                        {
                            var spread = Buffer[i];
                            anyChanged |= spread.IsChanged;
                            if (anyChanged)
                            {
                                var stream = spread.Stream;
                                switch (stream.Length)
                                {
                                case 0:
                                    break;

                                case 1:
                                    dataWriter.Write(stream.Buffer[0]);
                                    break;

                                default:
                                    dataWriter.Write(stream.Buffer, 0, stream.Length);
                                    break;
                                }
                                // Reset the changed flags
                                stream.Flush(force);
                            }
                            else
                            {
                                dataWriter.Position += spread.SliceCount;
                            }
                        }
                    }
                }
                finally
                {
                    MemoryPool <int> .PutArray(binSizeBuffer);

                    MemoryPool <T> .PutArray(buffer);
                }

                FDataStream.Flush();
                FBinSizeStream.Flush();

                base.Flush();
            }
Exemplo n.º 28
0
        public override void Flush(bool force = false)
        {
            var buffer = MemoryPool <T> .GetArray();

            var binSizeBuffer = MemoryPool <int> .GetArray();

            try
            {
                FBinSizeStream.Length = Length;

                int dataStreamLength = 0;
                using (var binSizeWriter = FBinSizeStream.GetWriter())
                {
                    var numSlicesBuffered = 0;
                    for (int i = 0; i < Length; i++)
                    {
                        var length = Buffer[i]?.Length ?? 0;
                        binSizeBuffer[numSlicesBuffered++] = length;
                        dataStreamLength += length;
                        if (numSlicesBuffered == binSizeBuffer.Length)
                        {
                            binSizeWriter.Write(binSizeBuffer, 0, numSlicesBuffered);
                            numSlicesBuffered = 0;
                        }
                    }
                    if (numSlicesBuffered > 0)
                    {
                        binSizeWriter.Write(binSizeBuffer, 0, numSlicesBuffered);
                    }
                }

                FDataStream.Length = dataStreamLength;
                using (var dataWriter = FDataStream.GetWriter())
                {
                    bool anyChanged        = force || IsChanged;
                    var  numSlicesBuffered = 0;
                    for (int i = 0; i < Length; i++)
                    {
                        var stream = Buffer[i];
                        if (stream != null)
                        {
                            anyChanged |= stream.IsChanged;
                            if (anyChanged)
                            {
                                using (var reader = stream.GetReader())
                                {
                                    switch (reader.Length)
                                    {
                                    case 0:
                                        break;

                                    case 1:
                                        buffer[numSlicesBuffered++] = reader.Read();
                                        WriteIfBufferIsFull(dataWriter, buffer, ref numSlicesBuffered);
                                        break;

                                    default:
                                        while (!reader.Eos)
                                        {
                                            numSlicesBuffered += reader.Read(buffer, numSlicesBuffered, buffer.Length - numSlicesBuffered);
                                            WriteIfBufferIsFull(dataWriter, buffer, ref numSlicesBuffered);
                                        }
                                        break;
                                    }
                                }
                                // Reset the changed flags
                                var flushable = stream as IFlushable;
                                if (flushable != null)
                                {
                                    flushable.Flush(force);
                                }
                            }
                            else
                            {
                                dataWriter.Position += stream.Length;
                            }
                        }
                    }
                    if (numSlicesBuffered > 0)
                    {
                        dataWriter.Write(buffer, 0, numSlicesBuffered);
                    }
                }
            }
            finally
            {
                MemoryPool <int> .PutArray(binSizeBuffer);

                MemoryPool <T> .PutArray(buffer);
            }

            FBinSizeStream.Flush(force);
            FDataStream.Flush(force);

            base.Flush(force);
        }
Exemplo n.º 29
0
        public void Evaluate(int spreadMax)
        {
            // The final output length is known in advance
            Output.Length = spreadMax;

            // Whether or not new slices should be initialized with true
            var bangOnCreate = BangOnCreateIn.SliceCount > 0 ? BangOnCreateIn[0] : false;

            // Fetch readers and writers
            var inputReader     = Input.GetReader();
            var lastInputReader = FLastInput.GetReader();
            var lastInputWriter = FLastInput.GetDynamicWriter();
            var outputWriter    = Output.GetWriter();

            // In case of very low spread counts this saves a few ticks
            if (spreadMax < 16)
            {
                var slicesToWrite   = spreadMax;
                var inputLength     = inputReader.Length;
                var lastInputLength = lastInputReader.Length;
                var minLength       = Math.Min(inputLength, lastInputLength);
                for (int i = 0; i < minLength; i++)
                {
                    var input     = inputReader.Read();
                    var lastInput = lastInputReader.Read();
                    var changed   = !FComparer.Equals(input, lastInput);
                    outputWriter.Write(changed);
                    lastInputWriter.Write(CopySlice(input));
                }
                for (int i = lastInputLength; i < inputLength; i++)
                {
                    var changed = bangOnCreate;
                    outputWriter.Write(changed);
                    var input = inputReader.Read();
                    lastInputWriter.Write(CopySlice(input));
                }
            }
            else
            {
                // Fetch the buffers to work with from the pool
                var inputBuffer = MemoryPool <T> .GetArray();

                var lastInputBuffer = MemoryPool <T> .GetArray();

                var outputBuffer = MemoryPool <bool> .GetArray();

                try
                {
                    var slicesToWrite = spreadMax;
                    while (slicesToWrite > 0)
                    {
                        // Read the input
                        var inputReadCount = inputReader.Read(inputBuffer, 0, inputBuffer.Length);
                        // Read the input from the previous frame
                        var lastInputReadCount = lastInputReader.Read(lastInputBuffer, 0, lastInputBuffer.Length);
                        // Calculate min and max read counts
                        var minCount = Math.Min(inputReadCount, lastInputReadCount);
                        var maxCount = Math.Max(inputReadCount, lastInputReadCount);
                        // Do the equality check for all the slices where values from
                        // the previous frame are available
                        for (int i = 0; i < minCount; i++)
                        {
                            outputBuffer[i] = !FComparer.Equals(inputBuffer[i], lastInputBuffer[i]);
                        }
                        // Set the output for new slices to the value of bang on create
                        for (int i = minCount; i < maxCount; i++)
                        {
                            outputBuffer[i] = bangOnCreate;
                        }
                        // Write the result
                        outputWriter.Write(outputBuffer, 0, maxCount);
                        // Store the input values for the next frame
                        CopySlices(inputBuffer, inputReadCount);
                        lastInputWriter.Write(inputBuffer, 0, inputReadCount);
                        // Decrease the number of slices we still need to look at
                        slicesToWrite -= maxCount;
                    }
                }
                finally
                {
                    // Put the buffers back in the pool
                    MemoryPool <T> .PutArray(inputBuffer);

                    MemoryPool <T> .PutArray(lastInputBuffer);

                    MemoryPool <bool> .PutArray(outputBuffer);
                }
            }

            // Dispose the readers and writers
            inputReader.Dispose();
            lastInputReader.Dispose();
            lastInputWriter.Dispose();
            outputWriter.Dispose();
        }
Exemplo n.º 30
0
        //called when data for any output pin is requested
        public void Evaluate(int spreadMax)
        {
            FOutput.Length = StreamUtils.GetMaxLength(FInput, FOffset, FCount);

            var inputBuffer = MemoryPool <IInStream <T> > .GetArray();

            var offsetBuffer = MemoryPool <int> .GetArray();

            var countBuffer = MemoryPool <int> .GetArray();

            try
            {
                using (var inputReader = FInput.GetCyclicReader())
                    using (var offsetReader = FOffset.GetCyclicReader())
                        using (var countReader = FCount.GetCyclicReader())
                            using (var outputWriter = FOutput.GetWriter())
                            {
                                var numSlicesToWrite = FOutput.Length;
                                while (numSlicesToWrite > 0)
                                {
                                    var blockSize = Math.Min(numSlicesToWrite, inputBuffer.Length);
                                    inputReader.Read(inputBuffer, 0, blockSize);
                                    offsetReader.Read(offsetBuffer, 0, blockSize);
                                    countReader.Read(countBuffer, 0, blockSize);

                                    for (int i = 0; i < blockSize; i++)
                                    {
                                        var source       = inputBuffer[i];
                                        var sourceLength = source.Length;
                                        if (sourceLength > 0)
                                        {
                                            var offset = offsetBuffer[i];
                                            var count  = countBuffer[i];

                                            if (offset < 0 || offset >= sourceLength)
                                            {
                                                offset = VMath.Zmod(offset, sourceLength);
                                            }
                                            if (count < 0)
                                            {
                                                source = source.Reverse();
                                                count  = -count;
                                                offset = sourceLength - offset;
                                            }
                                            // offset and count are positive now
                                            if (offset + count > sourceLength)
                                            {
                                                source = source.Cyclic();
                                            }

                                            inputBuffer[i] = source.GetRange(offset, count);
                                        }
                                    }

                                    numSlicesToWrite -= outputWriter.Write(inputBuffer, 0, blockSize);
                                }
                            }
            }
            finally
            {
                MemoryPool <IInStream <T> > .PutArray(inputBuffer);

                MemoryPool <int> .PutArray(offsetBuffer);

                MemoryPool <int> .PutArray(countBuffer);
            }
        }