Skip to content

Commit e030c6e

Browse files
committed
Algorithm improvements (2)
1 parent e320328 commit e030c6e

File tree

1 file changed

+33
-25
lines changed

1 file changed

+33
-25
lines changed

SmartImage.Lib/SearchClient.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Diagnostics;
1111
using System.Linq;
1212
using System.Runtime.CompilerServices;
13+
using System.Threading;
1314
using System.Threading.Tasks;
1415
using SimpleCore.Net;
1516
using SimpleCore.Utilities;
@@ -211,7 +212,7 @@ public List<SearchResult> MaximizeResults<T>(Func<SearchResult, T> property)
211212
public ImageResult FindDirectResult() => FindDirectResults().FirstOrDefault();
212213

213214

214-
public ImageResult[] FindDirectResults()
215+
public ImageResult[] FindDirectResults(int lim = 5)
215216
{
216217

217218
var best = FindBestResults().ToList();
@@ -236,51 +237,58 @@ public ImageResult[] FindDirectResults()
236237

237238
//Task.WaitAll(tasks.ToArray());
238239

239-
var bestCopy = best;
240+
var cts = new CancellationTokenSource();
240241

241242
var options = new ParallelOptions()
242243
{
243244
MaxDegreeOfParallelism = Int32.MaxValue,
244245
TaskScheduler = TaskScheduler.Default,
246+
CancellationToken = cts.Token
245247
};
246248

247-
Parallel.For(0, best.Count, options, (i) =>
248-
{
249-
bestCopy[i].FindDirectImages();
250-
251-
});
252-
253249

254-
best = bestCopy;
250+
var rx = new ConcurrentBag<ImageResult>();
255251

256252

257-
bestCopy = best;
253+
Parallel.For(0, best.Count, options, i =>
254+
{
255+
256+
if (options.CancellationToken.IsCancellationRequested || rx.Count >= lim) {
257+
Debug.WriteLine("stop");
258+
259+
return;
260+
}
258261

262+
var item = best[i];
259263

260-
var rx = new ConcurrentBag<ImageResult>();
264+
item.FindDirectImages();
261265

262-
Parallel.For(0, best.Count, options,(i) =>
263-
{
264-
if (ImageHelper.IsDirect(bestCopy[i].Direct?.ToString(), DirectImageType.Binary))
265-
{
266-
Debug.WriteLine($"Adding {bestCopy[i].Direct}");
267-
rx.Add(bestCopy[i]);
266+
if (item.Direct == null) {
267+
return;
268268
}
269-
});
270269

270+
if (ImageHelper.IsDirect(item.Direct.ToString(), DirectImageType.Binary)) {
271+
Debug.WriteLine($"Adding {item.Direct}");
272+
rx.Add(item);
273+
}
271274

272-
best = rx.ToList();
275+
});
276+
273277

274-
best = best.Where(x => x.Direct != null)
275-
.OrderByDescending(r => r.Similarity)
276-
.ToList();
278+
Task.Factory.StartNew(() =>
279+
{
280+
//SpinWait.SpinUntil(() => rx.Count >= lim);
281+
while (!(rx.Count >= lim)) { }
277282

278-
//best = best.OrderByDescending(b => b.PixelResolution).ToList();
283+
Debug.WriteLine($"Cancel");
284+
cts.Cancel();
285+
});
279286

280287

281-
Debug.WriteLine($"Found {best.Count} direct results");
288+
Debug.WriteLine($"Found {rx.Count} direct results");
282289

283-
return best.ToArray();
290+
return rx.OrderByDescending(r => r.Similarity)
291+
.ToArray();
284292
}
285293

286294
[CanBeNull]

0 commit comments

Comments
 (0)