fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.Threading;
  5. public class Test
  6. {
  7. public class DataPoint {
  8. public double X {get; set;}
  9. public double Y {get; set;}
  10. }
  11. public static void Main()
  12. {
  13. int count = 39;
  14. // generate our points
  15. List<DataPoint>[] sets = new List<DataPoint>[count];
  16. double[] result = new double[count];
  17. for(var i=0; i<sets.Length; i++) {
  18. sets[i] = GetRandomDataPoints(200);
  19. }
  20. // run our calculations async
  21. int running = count;
  22. using(ManualResetEvent resetEvent = new ManualResetEvent(false)){
  23. for(int i=0; i<count; i++) {
  24. int idx = i;
  25. ThreadPool.QueueUserWorkItem(
  26. new WaitCallback(o => {
  27. result[idx] = GetAbsoluteMax(sets[idx]);
  28. if (Interlocked.Decrement(ref running) == 0) {
  29. resetEvent.Set();
  30. }
  31. }),
  32. null
  33. );
  34. }
  35. resetEvent.WaitOne();
  36. }
  37. foreach(var max in result) {
  38. Console.WriteLine(max);
  39. }
  40. }
  41. public static double GetAbsoluteMax(List<DataPoint> list) {
  42. double max = 0;
  43. // calc dist for each distinct pair Dist(P_1, P_2) == Dist(P_2, P_1)
  44. for(var i=0; i<list.Count-1; i++) {
  45. for(var j=i+1; j<list.Count; j++) {
  46. var dX = list[i].X - list[j].X;
  47. var dY = list[i].Y - list[j].Y;
  48. // don't calculate the Square Root yet
  49. var dist = dX * dX + dY * dY;
  50. if(dist > max) {
  51. max = dist;
  52. }
  53. }
  54. }
  55. return Math.Sqrt(max);
  56. }
  57. public static List<DataPoint> GetRandomDataPoints(int size) {
  58. var result = new List<DataPoint>();
  59. var rnd = new Random();
  60. for(var i=0; i<size; i++) {
  61. result.Add(new DataPoint() {
  62. X=rnd.NextDouble()*100,
  63. Y=rnd.NextDouble()*100
  64. });
  65. }
  66. return result;
  67. }
  68. }
Success #stdin #stdout 0.1s 27272KB
stdin
Standard input is empty
stdout
135.8711135803
127.954447017747
127.954447017747
127.954447017747
127.954447017747
127.954447017747
127.954447017747
127.954447017747
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
131.136269358225
130.717196975732
130.717196975732
130.717196975732
130.717196975732
130.717196975732
130.717196975732
130.717196975732
130.717196975732
130.717196975732
130.717196975732