Initial commit
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Remoting.Messaging;
|
||||
using System.Threading.Tasks;
|
||||
using GameOfLife.Entities;
|
||||
using GameOfLife.IO;
|
||||
|
||||
namespace GameOfLife.Search
|
||||
{
|
||||
public class LibrarySearcher
|
||||
{
|
||||
private readonly PatternLibrary _patternLibrary;
|
||||
private Dictionary<Cell, List<Pattern>> _cellPatterns;
|
||||
|
||||
public LibrarySearcher(PatternLibrary patternLibrary)
|
||||
{
|
||||
_patternLibrary = patternLibrary;
|
||||
|
||||
var cellPatterns =
|
||||
from patternProjections in _patternLibrary.PatternProjections
|
||||
from projection in patternProjections.Item2
|
||||
from cell in projection
|
||||
group projection by cell into cellGroups
|
||||
select cellGroups;
|
||||
|
||||
_cellPatterns = cellPatterns.ToDictionary(t => t.Key, t => t.ToList());
|
||||
}
|
||||
|
||||
//public IEnumerable<Tuple<PatternMetadata, Pattern[]>> MatchLibraryPatternsLookup(IEnumerable<Cell> cells)
|
||||
//{
|
||||
// var pattern = cells.ToPattern();
|
||||
//}
|
||||
|
||||
public IEnumerable<Tuple<PatternMetadata, Pattern[]>> MatchLibraryPatterns(IEnumerable<Cell> cells)
|
||||
{
|
||||
var inputCells = cells.ToPattern();
|
||||
|
||||
var matches = new ConcurrentBag<Tuple<PatternMetadata, Pattern[]>>();
|
||||
|
||||
Parallel.ForEach(_patternLibrary.PatternProjections, tuple =>
|
||||
{
|
||||
var patternMetadata = tuple.Item1;
|
||||
var projections = tuple.Item2;
|
||||
|
||||
var foundPatterns = inputCells.FindPatterns_Serial(projections).ToArray();
|
||||
if (foundPatterns.Any())
|
||||
matches.Add(Tuple.Create(patternMetadata, foundPatterns.ToArray()));
|
||||
}
|
||||
);
|
||||
return matches;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user