public NNResultsVisual(double width, double height, Tuple<Point, double, bool>[] values, bool shouldOutlineNonMatches) { #region Min/Max Value, Colors bool hasNegative = values.Any(o => o.Item2 < 0); double maxValue = Math.Max(Math.Abs(values.Min(o => o.Item2)), Math.Abs(values.Max(o => o.Item2))); if (maxValue < 1d) // should never be greater, but leave alone if it is { maxValue = 1d; } Color positiveColor = hasNegative ? Colors.Blue : Colors.Black; Color negativeColor = Colors.Red; #endregion #region XY Scale double radius = ((width + height) / 2) / 100; bool hasNegativePosition = values.Any(o => o.Item1.X < 0 || o.Item1.Y < 0); double maxX = Math.Max(Math.Abs(values.Min(o => o.Item1.X)), Math.Abs(values.Max(o => o.Item1.X))); double maxY = Math.Max(Math.Abs(values.Min(o => o.Item1.Y)), Math.Abs(values.Max(o => o.Item1.Y))); // If they are somewhat near 1, then cap at 1 if (maxX > .5 && maxX < 1) maxX = 1; if (maxY > .5 && maxY < 1) maxY = 1; double offsetX = hasNegativePosition ? (width / 2) : 0; double offsetY = hasNegativePosition ? (height / 2) : 0; double scaleX = maxX > 0d ? (width - offsetX) / maxX : 1; double scaleY = maxY > 0d ? (height - offsetY) / maxY : 1; #endregion Pen matchPen = new Pen(Brushes.Lime, radius * .32); Pen otherPen = shouldOutlineNonMatches ? new Pen(new SolidColorBrush(Color.FromArgb(192, 192, 192, 192)), radius * .18) : null; _visual = new DrawingVisual(); using (DrawingContext dc = _visual.RenderOpen()) { foreach (var value in values) { Color color = value.Item2 < 0 ? negativeColor : positiveColor; double alpha = Math.Abs(value.Item2) / maxValue; Color finalColor = Color.FromArgb(Convert.ToByte(alpha * 255), color.R, color.G, color.B); Point point = new Point(offsetX + (value.Item1.X * scaleX), offsetY + (value.Item1.Y * scaleY)); Pen pen = value.Item3 ? matchPen : otherPen; dc.DrawEllipse(new SolidColorBrush(finalColor), pen, point, radius, radius); } } }
public void DoubleMaxOnTwoTuple() { var sut = new Tuple<double, double>(1, 2); double expected = sut.AsEnumerable().Cast<double>().Max(); double actual = sut.Max(x => (double) x); Assert.AreEqual(expected, actual); }
public void ShortMaxOnTwoTuple() { var sut = new Tuple<short, short>(1, 2); short expected = sut.AsEnumerable().Cast<short>().Max(); short actual = sut.Max(x => (short) x); Assert.AreEqual(expected, actual); }
public void LongMaxOnTwoTuple() { var sut = new Tuple<long, long>(1, 2); long expected = sut.AsEnumerable().Cast<long>().Max(); long actual = sut.Max(x => (long) x); Assert.AreEqual(expected, actual); }
public void IntegerMaxOnTwoTuple() { var sut = new Tuple<int, int>(1, 2); int expected = sut.AsEnumerable().Cast<int>().Max(); int actual = sut.Max(x => (int) x); Assert.AreEqual(expected, actual); }
public void ShortMaxOnSixTuple() { var sut = new Tuple <short, short, short, short, short, short>(1, 2, 3, 4, 5, 6); short expected = sut.AsEnumerable().Cast <short>().Max(); short actual = sut.Max(x => (short)x); Assert.AreEqual(expected, actual); }
public void DoubleMaxOnOneTuple() { var sut = new Tuple <double>(1); double expected = sut.AsEnumerable().Cast <double>().Max(); double actual = sut.Max(x => (double)x); Assert.AreEqual(expected, actual); }
public void DoubleMaxOnSevenTuple() { var sut = new Tuple <double, double, double, double, double, double, double>(1, 2, 3, 4, 5, 6, 7); double expected = sut.AsEnumerable().Cast <double>().Max(); double actual = sut.Max(x => (double)x); Assert.AreEqual(expected, actual); }
public void LongMaxOnOneTuple() { var sut = new Tuple <long>(1); long expected = sut.AsEnumerable().Cast <long>().Max(); long actual = sut.Max(x => (long)x); Assert.AreEqual(expected, actual); }
public void LongMaxOnSevenTuple() { var sut = new Tuple <long, long, long, long, long, long, long>(1, 2, 3, 4, 5, 6, 7); long expected = sut.AsEnumerable().Cast <long>().Max(); long actual = sut.Max(x => (long)x); Assert.AreEqual(expected, actual); }
public void IntegerMaxOnOneTuple() { var sut = new Tuple <int>(1); int expected = sut.AsEnumerable().Cast <int>().Max(); int actual = sut.Max(x => (int)x); Assert.AreEqual(expected, actual); }
static void Main(string[] args) { // 入力の取得 const int DishCount = 5; var timeWithPaddings = new Tuple <int, int> [DishCount]; for (var i = 0; i < DishCount; i++) { var cookingTime = int.Parse(Console.ReadLine()); var padding = cookingTime % 10 == 0 ? 0 : 10 - cookingTime % 10; timeWithPaddings[i] = Tuple.Create(cookingTime, padding); } // 一番 PaddingTime の長い料理を最後に注文することで最短時間となる var maxPadding = timeWithPaddings.Max(t => t.Item2); var sum = timeWithPaddings.Sum(t => t.Item1 + t.Item2) - maxPadding; // 解答の出力 Console.WriteLine(sum); }
/// <summary> /// This returns a visual of a graph that can be added to a canvas /// </summary> public static IEnumerable<UIElement> GetGradientGraph(double width, double height, Tuple<double, double>[] gradient, Color fill, Color stroke) { if (gradient == null || gradient.Length <= 1) // need at least two for a gradient { return new UIElement[0]; } else if (width.IsNearZero() || height.IsNearZero()) { return new UIElement[0]; } List<UIElement> retVal = new List<UIElement>(); double maxPercent = gradient.Max(o => o.Item2); if (maxPercent > 1) { #region 100% line // Draw a dashed line at 1 (showing the 100% mark) Color color100 = UtilityWPF.AlphaBlend(UtilityWPF.AlphaBlend(stroke, Colors.Gray, .85), Colors.Transparent, .66); double y100 = ((maxPercent - 1) / maxPercent) * height; Line line100 = new Line() { Stroke = new SolidColorBrush(color100), StrokeThickness = 1, StrokeDashArray = new DoubleCollection(new[] { 4d, 2d }), X1 = 0, X2 = width, Y1 = y100, Y2 = y100, }; retVal.Add(line100); #endregion } if (maxPercent < 1) { // Do this so the graph is to scale (doesn't always go to the top) maxPercent = 1; } double lastGradX = gradient[gradient.Length - 1].Item1; if (!Math1D.IsNearZero(lastGradX) && lastGradX > 0) { Polyline polyLine = new Polyline(); Polygon polyFill = new Polygon(); polyLine.Stroke = new SolidColorBrush(stroke); polyLine.StrokeThickness = 2; polyFill.Fill = new SolidColorBrush(fill); //NOTE: gradient must be sorted on Item1 double xScale = width / lastGradX; for (int cntr = 0; cntr < gradient.Length; cntr++) { double x = gradient[cntr].Item1 * xScale; double y = ((maxPercent - gradient[cntr].Item2) / maxPercent) * height; polyLine.Points.Add(new Point(x, y)); polyFill.Points.Add(new Point(x, y)); } // Circle back fill to make a polygon polyFill.Points.Add(new Point(polyFill.Points[polyFill.Points.Count - 1].X, height)); polyFill.Points.Add(new Point(polyFill.Points[0].X, height)); retVal.Add(polyFill); retVal.Add(polyLine); } return retVal; }
internal static void Run() { if (string.IsNullOrEmpty(Endpoints)) { throw new ArgumentException("FhirEndpoints value is empty"); } endpoints = Endpoints.Split(";", StringSplitOptions.RemoveEmptyEntries).ToList(); var globalPrefix = $"RequestedBlobRange=[{NumberOfBlobsToSkip + 1}-{MaxBlobIndexForImport}]"; Console.WriteLine($"{globalPrefix}: Starting..."); var blobContainerClient = GetContainer(ConnectionString, ContainerName); var blobs = blobContainerClient.GetBlobs().OrderBy(_ => _.Name).Where(_ => _.Name.EndsWith(".ndjson", StringComparison.OrdinalIgnoreCase)).ToList(); Console.WriteLine($"Found ndjson blobs={blobs.Count} in {ContainerName}."); var take = MaxBlobIndexForImport == 0 ? blobs.Count : MaxBlobIndexForImport - NumberOfBlobsToSkip; blobs = blobs.Skip(NumberOfBlobsToSkip).Take(take).ToList(); var swWrites = Stopwatch.StartNew(); var swReport = Stopwatch.StartNew(); var currentBlobRanges = new Tuple <int, int> [ReadThreads]; BatchExtensions.ExecuteInParallelBatches(blobs, ReadThreads, BlobRangeSize, (reader, blobRangeInt) => { var localSw = Stopwatch.StartNew(); var writes = 0L; var blobRangeIndex = blobRangeInt.Item1; var blobsInt = blobRangeInt.Item2; var firstBlob = NumberOfBlobsToSkip + (blobRangeIndex * BlobRangeSize) + 1; var lastBlob = NumberOfBlobsToSkip + (blobRangeIndex * BlobRangeSize) + blobsInt.Count; currentBlobRanges[reader] = Tuple.Create(firstBlob, lastBlob); var prefix = $"Reader={reader}.BlobRange=[{firstBlob}-{lastBlob}]"; var incrementor = new IndexIncrementor(endpoints.Count); Console.WriteLine($"{prefix}: Starting..."); // 100 below is a compromise between processing with maximum available threads (value 1) and inefficiency in wrapping single resource in a list. BatchExtensions.ExecuteInParallelBatches(GetLinesInBlobRange(blobsInt, prefix), WriteThreads / ReadThreads, 100, (thread, lineBatch) => { Interlocked.Increment(ref writers); foreach (var line in lineBatch.Item2) { Interlocked.Increment(ref totalWrites); Interlocked.Increment(ref writes); PutResource(line, incrementor); if (swReport.Elapsed.TotalSeconds > ReportingPeriodSec) { lock (swReport) { if (swReport.Elapsed.TotalSeconds > ReportingPeriodSec) { var currWrites = Interlocked.Read(ref totalWrites); var currReads = Interlocked.Read(ref totalReads); var minBlob = currentBlobRanges.Min(_ => _.Item1); var maxBlob = currentBlobRanges.Max(_ => _.Item2); Console.WriteLine($"{globalPrefix}.WorkingBlobRange=[{minBlob}-{maxBlob}].Readers=[{Interlocked.Read(ref readers)}/{ReadThreads}].Writers=[{Interlocked.Read(ref writers)}].EndPointCalls=[{Interlocked.Read(ref epCalls)}].Waits=[{Interlocked.Read(ref waits)}]: reads={currReads} writes={currWrites} secs={(int)swWrites.Elapsed.TotalSeconds} read-speed={(int)(currReads / swReads.Elapsed.TotalSeconds)} lines/sec write-speed={(int)(currWrites / swWrites.Elapsed.TotalSeconds)} res/sec"); swReport.Restart(); } } } } Interlocked.Decrement(ref writers); }); Console.WriteLine($"{prefix}: Completed writes. Total={writes} secs={(int)localSw.Elapsed.TotalSeconds} speed={(int)(writes / localSw.Elapsed.TotalSeconds)} res/sec"); }); Console.WriteLine($"{globalPrefix}.Readers=[{readers}/{ReadThreads}].Writers=[{writers}].EndPointCalls=[{epCalls}].Waits=[{waits}]: total reads={totalReads} total writes={totalWrites} secs={(int)swWrites.Elapsed.TotalSeconds} read-speed={(int)(totalReads / swReads.Elapsed.TotalSeconds)} lines/sec write-speed={(int)(totalWrites / swWrites.Elapsed.TotalSeconds)} res/sec"); }
public static Tuple<int, int, double>[] Normalize(Tuple<int, int, double>[] flattened) { if (flattened.Length == 0) { return flattened; } double maxPercent = flattened.Max(o => o.Item3); if (maxPercent.IsNearZero() || maxPercent.IsNearValue(1d)) // if it's zero, then no thrusters are firing. If one, it's already normalized { return flattened; } double scale = 1d / maxPercent; return flattened. Select(o => Tuple.Create(o.Item1, o.Item2, o.Item3 * scale)). ToArray(); }
protected async Task TableAsync <T>(Dictionary <string, Func <T, object> > columns, T[] values, ConsoleColor headerColor = ConsoleColor.Cyan, int columnSpace = 2) { var columnNames = columns.Keys.ToArray(); var columnSelectors = columns.Values.ToArray(); Tuple <ConsoleColor, string>[][] contents = new Tuple <ConsoleColor, string> [values.Length + 1][]; contents[0] = new Tuple <ConsoleColor, string> [columns.Count]; for (int i = 0; i < columns.Count; i++) { contents[0][i] = new Tuple <ConsoleColor, string>(headerColor, columnNames[i]); } for (int i = 0; i < values.Length; i++) { contents[i + 1] = new Tuple <ConsoleColor, string> [columns.Count]; for (int j = 0; j < columns.Count; j++) { var value = columnSelectors[j].Invoke(values[i]); var d = value.GetType(); if (value.GetType().Name == "ValueTuple`2" && value.GetType().GetGenericArguments().FirstOrDefault() == typeof(ConsoleColor)) { var color = (ConsoleColor)value.GetType().GetField("Item1").GetValue(value); string text = value.GetType().GetField("Item2").GetValue(value).ToString(); contents[i + 1][j] = new Tuple <ConsoleColor, string>(color, text); continue; } contents[i + 1][j] = new Tuple <ConsoleColor, string>(ConsoleColor.Gray, value.ToString()); } } int[][] spacings = new int[values.Length + 1][]; for (int i = 0; i < values.Length + 1; i++) { spacings[i] = new int[columns.Count - 1]; } for (int i = 0; i < columns.Count - 1; i++) { int maxValueLength = contents.Max(x => x[i].Item2.Length) + columnSpace; for (int j = 0; j < contents.Length; j++) { spacings[j][i] = maxValueLength - contents[j][i].Item2.Length; } } for (int i = 0; i < values.Length + 1; i++) { for (int j = 0; j < columns.Count; j++) { if (j != 0) { await WriteAsync(Space(spacings[i][j - 1])); } await WriteAsync(contents[i][j].Item2, contents[i][j].Item1); } await WriteLineAsync(); } }