private static void bwSearch_Work(object sender, DoWorkEventArgs e) { var arg = e.Argument as SearchArgs; var args = arg.ls; arg.ls = null; DateTime dt = DateTime.Now; ManualResetEvent[] events = new ManualResetEvent[args.Length]; SearchPoolResponder[] thread_objects = new SearchPoolResponder[args.Length]; for (int i = 0; i < args.Length; i++) { events[i] = new ManualResetEvent(false); thread_objects[i] = new SearchPoolResponder(args[i].ToArray(), events[i]); ThreadPool.QueueUserWorkItem(thread_objects[i].ThreadPoolCallback, arg); } WaitHandle.WaitAll(events); var top_points = new SortableData[knn + 1]; for (int i = 0; i <= knn; i++) { top_points[i] = SortableData.Minimum; } foreach (var resp in thread_objects) { foreach (var top_point_file in resp._Data) { top_points[0] = top_point_file; for (int offset = 0; // Bubble Sort (offset < knn) && (top_points[offset]._Sum > top_points[offset + 1]._Sum); offset++) { MiscUtil.Swap( ref top_points[offset], ref top_points[offset + 1]); } } } e.Result = new Tuple <RectangleF, SortableData[]>(arg._Rect, top_points.SubArray(1, knn)); }
public void ThreadPoolCallback(Object o) { var arg = o as SearchArgs; var top_points = new SortableData[knn + 1]; for (int i = 0; i <= knn; i++) { top_points[i] = SortableData.Minimum; } foreach (var grp in this._Groups) { string grp_name = grp.Item1; foreach (var kvp_file in grp.Item2) { var top_point_file = SortableData.Minimum; // Find best for each file foreach (var data in kvp_file.Value) { int lens = Math.Min(data._Data.Length, arg.current_norm.Length); float running_sum_norm = 0; float running_sum_flip = 0; int compare_black = 0; for (int i = 0; i < lens; i++) { // char - 48 converts char 1 or 0 to the int value of 1 or 0 // 49 - char converts char 1 or 0 to the int value of 0 or 1 int read = (49 - data._Data[i]); if (read == 1) { compare_black++; } running_sum_norm += arg.current_norm[i] * read; running_sum_flip += arg.current_flip[i] * read; } running_sum_norm /= Math.Max(compare_black, arg.current_black); running_sum_flip /= Math.Max(compare_black, arg.current_black); if (running_sum_norm > top_point_file._Sum) { top_point_file = new SortableData( grp_name, kvp_file.Key, data, running_sum_norm, false); } if (running_sum_flip > top_point_file._Sum) { top_point_file = new SortableData( grp_name, kvp_file.Key, data, running_sum_flip, true); } } // Each file only gets one best if (top_point_file._Data != null) { top_points[0] = top_point_file; for (int offset = 0; // Bubble Sort (offset < knn) && (top_points[offset]._Sum > top_points[offset + 1]._Sum); offset++) { MiscUtil.Swap( ref top_points[offset], ref top_points[offset + 1]); } } } } this._Data = top_points.SubArray(1, knn); _DoneEvent.Set(); }