//called when data for any output pin is requested public void Evaluate(int SpreadMax) { /// Configure the output count if (FAnalogIns.IsChanged) { FAnalogIns.SliceCount = FAnalogInputCount[0]; } if (FDigitalIns.IsChanged) { FDigitalIns.SliceCount = FDigitalInputCount[0]; } /// If there is nothing new to read, there is nothing to parse if (!FirmataIn.IsChanged) { return; } /// Read in the stream try { using (IStreamReader <Stream> InputReader = FirmataIn.GetReader()) { while (!InputReader.Eos) { HandleStream(InputReader.Read()); } } } catch (Exception e) { return; } }
public static int Sum(this IInStream <int> stream) { var result = 0; var buffer = MemoryPool <int> .GetArray(); var reader = stream.GetReader(); try { while (!reader.Eos) { var itemsRead = reader.Read(buffer, 0, buffer.Length); for (int i = 0; i < itemsRead; i++) { result += buffer[i]; } } } finally { MemoryPool <int> .PutArray(buffer); reader.Dispose(); } return(result); }
public static int Sum(this IInStream <int> stream) { var result = 0; using (var buffer = MemoryPool <int> .GetBuffer()) using (var reader = stream.GetReader()) { var array = buffer.Array; while (!reader.Eos) { var itemsRead = reader.Read(array, 0, array.Length); if (itemsRead != array.Length) { for (int i = 0; i < itemsRead; i++) { result += array[i]; } } else { // No index out of bounds check for (int i = 0; i < array.Length; i++) { result += array[i]; } } } } return(result); }
#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; } } }
public IStreamReader <T> GetReader() { if (Offset == 0 && Length == FDataStream.Length) { return(FDataStream.GetReader()); } return(new InnerStreamReader(this)); }
#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; } } }
#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; } } }
public void Evaluate(int spreadMax) { // Should any of the inputs do not have any data exit early if (spreadMax == 0) { LeftOutStream.Length = 0; RightOutStream.Length = 0; return; } // In case nothing changed also do an early exit - important if T is a string or a reference type if (!InputStream.IsChanged && !IndexStream.IsChanged) { return; } // Grab buffers and reader/writer using (var buffer = MemoryPool <T> .GetBuffer()) using (var reader = InputStream.GetReader()) using (var leftWriter = LeftOutStream.GetDynamicWriter()) using (var rightWriter = RightOutStream.GetDynamicWriter()) { foreach (var index in IndexStream) { // Set reader to its initial position reader.Position = 0; // Write everything up to the given index to the left output int numSlicesToRead; // split at 0 could mean all to the left or all to the right // for now let's put all to the right if (index >= 0) { numSlicesToRead = Math.Min(index, InputStream.Length); } else { numSlicesToRead = Math.Max(InputStream.Length + index, 0); } while (numSlicesToRead > 0) { var numSlicesREAD = reader.Read(buffer, 0, Math.Min(numSlicesToRead, buffer.Length)); leftWriter.Write(buffer, 0, numSlicesREAD); numSlicesToRead -= numSlicesREAD; } // Write whatever remains to the right output while (!reader.Eos) { var numSlicesRead = reader.Read(buffer, 0, buffer.Length); rightWriter.Write(buffer, 0, numSlicesRead); } } } }
public static void Write <T>(this IStreamWriter <T> writer, IInStream <T> inStream, T[] buffer) { using (var reader = inStream.GetReader()) { while (!reader.Eos) { int numSlicesRead = reader.Read(buffer, 0, buffer.Length); writer.Write(buffer, 0, numSlicesRead); } } }
#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; } } }
public override bool Sync() { // Sync source IsChanged = FDataStream.Sync() | FBinSizeStream.Sync(); if (IsChanged) { int dataLength = FDataStream.Length; int binSizeLength = FBinSizeStream.Length; int binSizeSum = 0; foreach (var binSize in FBinSizeStream) { binSizeSum += SpreadUtils.NormalizeBinSize(dataLength, binSize); } int binTimes = SpreadUtils.DivByBinSize(dataLength, binSizeSum); binTimes = binTimes > 0 ? binTimes : 1; Length = binTimes * binSizeLength; var binSizeBuffer = MemoryPool <int> .GetArray(); try { using (var dataReader = FDataStream.GetReader()) using (var binSizeReader = FBinSizeStream.GetCyclicReader()) { var numSlicesToWrite = Length; var offsetIntoDataStream = 0; while (numSlicesToWrite > 0) { var numSlicesToRead = Math.Min(numSlicesToWrite, binSizeBuffer.Length); binSizeReader.Read(binSizeBuffer, 0, numSlicesToRead); var offset = Length - numSlicesToWrite; for (int i = offset; i < offset + numSlicesToRead; i++) { var binSize = SpreadUtils.NormalizeBinSize(dataLength, binSizeBuffer[i - offset]); Sync(i, offsetIntoDataStream, binSize, dataReader); offsetIntoDataStream += binSize; } numSlicesToWrite -= numSlicesToRead; } } } finally { MemoryPool <int> .PutArray(binSizeBuffer); } } return(base.Sync()); }
internal CyclicStreamReader(IInStream <T> stream) { if (stream.Length == 0) { FReader = StreamUtils.GetEmptyStream <T>().GetReader(); } else { FReader = stream.GetReader(); } Eos = stream.Length == 0; Length = stream.Length; }
#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; } } }
#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; } } }
#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; } } }
string ToString <T>(IInStream <T> source) { string result = (source.Length > 1) ? $"({source.Length}) " : string.Empty; if (source.Length > 0) { using (var r = source.GetReader()) { var item = r.Read(); result += (item == null) ? "'null'" : item.ToString(); } } return(result); }
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); } } }
private static void ReadUnbuffered <T>(IInStream <T> stream, int length, int stepSize) { using (var reader = stream.GetReader()) { for (int i = 0; i < length; i++) { if (reader.Eos) { reader.Reset(); } reader.Read(stepSize); } } }
#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()); spread.Sync(FInput, vecSize, FBin, FSelect.Length); using (var selReader = FSelect.GetCyclicReader()) using (var outputWriter = FOutput.GetDynamicWriter()) using (var formerIdWriter = FFormer.GetDynamicWriter()) { int offset = 0; for (int b = 0; b < spread.Count; b++) { var data = spread[b]; int binSize = data.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++) { outputWriter.Write(data, 0, data.Length); formerIdWriter.Write(ids, 0, ids.Length); } } offset += binSize; } } } else { FOutput.Length = FFormer.Length = 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; } } }
public override bool Sync() { IsChanged = FInStream.Sync(); if (IsChanged) { var buffer = MemoryPool <Matrix4x4> .GetArray(); try { var anySliceChanged = this.Length != FInStream.Length; this.Length = FInStream.Length; using (var reader = FInStream.GetReader()) using (var writer = this.GetWriter()) { while (!reader.Eos) { var offset = reader.Position; int numSlicesRead = reader.Read(buffer, 0, buffer.Length); if (!anySliceChanged) { for (int i = 0; i < numSlicesRead; i++) { var previousSlice = this.Buffer[offset + i]; var newSlice = buffer[i]; if (newSlice != previousSlice) { anySliceChanged = true; break; } } } writer.Write(buffer, 0, numSlicesRead); } } IsChanged = anySliceChanged; } finally { MemoryPool <Matrix4x4> .PutArray(buffer); } } return(base.Sync()); }
public static short Sum(this IInStream <short> stream) { short result = 0; using (var buffer = MemoryPool <short> .GetBuffer()) using (var reader = stream.GetReader()) { while (!reader.Eos) { var itemsRead = reader.Read(buffer, 0, buffer.Length); for (int i = 0; i < itemsRead; i++) { result += buffer[i]; } } } return(result); }
private static void ReadBuffered <T>(IInStream <T> stream, int length, int stepSize) { T[] buffer = new T[Math.Min(1024, length)]; using (var reader = stream.GetReader()) { int readCount = 0; while (readCount < length) { if (reader.Eos) { reader.Reset(); } readCount += reader.Read(buffer, 0, Math.Min(buffer.Length, length - readCount), stepSize); } } }
//called when data for any output pin is requested public void Evaluate(int SpreadMax) { /// Configure the output count if (FAnalogIns.IsChanged) { FAnalogIns.SliceCount = FAnalogInputCount[0]; } if (FDigitalIns.IsChanged) { FDigitalIns.SliceCount = FDigitalInputCount[0]; } StringOut.SliceCount = 0; /// If there is nothing new to read, there is nothing to parse if (!FirmataIn.IsChanged) { return; } /// Read in the stream try { using (IStreamReader <Stream> InputReader = FirmataIn.GetReader()) { while (!InputReader.Eos) { Stream InStream = InputReader.Read(); if (InStream == null || InStream.Length == 0 || !InStream.CanRead) { continue; } // Read the incoming bytes to the internal stream buffer for (int i = InStream.ReadByte(); i != -1; i = InStream.ReadByte()) { Decode((byte)i); } } } } catch (Exception e) { return; } }
public static int GetMaxLength <T>(this IInStream <IInStream <T> > streams) { switch (streams.Length) { case 0: return(0); default: using (var reader = streams.GetReader()) { var stream = reader.Read(); int result = stream.Length; while (!reader.Eos) { stream = reader.Read(); result = result.CombineStreams(stream.Length); } return(result); } } }
#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; } } }
public static double Min(this IInStream <double> stream) { var result = double.MaxValue; using (var buffer = MemoryPool <double> .GetBuffer()) using (var reader = stream.GetReader()) { while (!reader.Eos) { var itemsRead = reader.Read(buffer, 0, buffer.Length); for (int i = 0; i < itemsRead; i++) { var item = buffer[i]; if (item < result) { result = item; } } } } return(result); }
public static float Max(this IInStream <float> stream) { var result = float.MinValue; using (var buffer = MemoryPool <float> .GetBuffer()) using (var reader = stream.GetReader()) { while (!reader.Eos) { var itemsRead = reader.Read(buffer, 0, buffer.Length); for (int i = 0; i < itemsRead; i++) { var item = buffer[i]; if (item > result) { result = item; } } } } return(result); }
#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; } } }
#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; } } }