use of searcher.pack.memento.MinoFieldMemento in project solution-finder by knewjade.
the class BasicMinoPackingHelper method fixResult.
@Override
public Stream<Result> fixResult(PackSearcher searcher, long innerFieldBoard, MinoFieldMemento nextMemento) {
SizedBit sizedBit = searcher.getSizedBit();
SolutionFilter solutionFilter = searcher.getSolutionFilter();
long fillBoard = sizedBit.getFillBoard();
long board = innerFieldBoard & fillBoard;
int resultIndex = searcher.getLastIndex() + 1;
ColumnSmallField nextInnerField = ColumnFieldFactory.createField(board);
if (searcher.isFilled(nextInnerField, resultIndex)) {
if (solutionFilter.testLast(nextMemento))
return Stream.of(createResult(nextMemento));
return Stream.empty();
} else {
ColumnSmallField over = ColumnFieldFactory.createField(innerFieldBoard & ~fillBoard);
MinoFields minoFields = searcher.getSolutions(resultIndex).parse(nextInnerField);
return minoFields.stream().filter(minoField -> over.canMerge(minoField.getOuterField())).map(nextMemento::concat).filter(solutionFilter::testLast).map(this::createResult);
}
}
use of searcher.pack.memento.MinoFieldMemento in project solution-finder by knewjade.
the class PerfectPackSearcher method collect.
public <A, R> R collect(Collector<? super Result, A, R> callback) throws InterruptedException, ExecutionException {
// 探索準備
MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
ColumnField innerField = inOutPairFields.get(0).getInnerField();
PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
// 探索
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTask<R> submitTask = forkJoinPool.submit(() -> {
// Streamは終端操作を実行するまで実際には計算を行わない
// そのため、終端操作をPool内でしなければ、Pool外のスレッド場で動くため注意が必要
// (終端操作をしなかった場合、Pool内ではStream自体の作成をして終了する)
Stream<Result> compute = toStream(task);
return compute.collect(callback);
});
// 結果を取得するまで待つ
R result = submitTask.get();
assert result != null;
// 終了処理
forkJoinPool.shutdown();
return result;
}
use of searcher.pack.memento.MinoFieldMemento in project solution-finder by knewjade.
the class SetupPackSearcher method count.
public Long count() throws InterruptedException, ExecutionException {
// 探索準備
MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
ColumnField innerField = inOutPairFields.get(0).getInnerField();
PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
// 探索
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTask<Long> submitTask = forkJoinPool.submit(() -> {
return task.compute().parallel().count();
});
// 結果を取得するまで待つ
Long result = submitTask.get();
assert result != null;
// 終了処理
forkJoinPool.shutdown();
return result;
}
use of searcher.pack.memento.MinoFieldMemento in project solution-finder by knewjade.
the class SetupPackSearcher method collect.
public <A, R> R collect(Collector<? super Result, A, R> callback) throws InterruptedException, ExecutionException {
// 探索準備
MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
ColumnField innerField = inOutPairFields.get(0).getInnerField();
PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
// 探索
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTask<R> submitTask = forkJoinPool.submit(() -> {
return task.compute().parallel().collect(callback);
});
// 結果を取得するまで待つ
R result = submitTask.get();
assert result != null;
// 終了処理
forkJoinPool.shutdown();
return result;
}
use of searcher.pack.memento.MinoFieldMemento in project solution-finder by knewjade.
the class SetupPackSearcher method findAny.
public Optional<Result> findAny() throws InterruptedException, ExecutionException {
// 探索準備
MinoFieldMemento emptyMemento = MinoFieldMementoFactory.create();
ColumnField innerField = inOutPairFields.get(0).getInnerField();
PackingTask task = createPackingTask(sizedBit, emptyMemento, innerField);
// 探索
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTask<Optional<Result>> submitTask = forkJoinPool.submit(() -> {
return task.compute().parallel().findAny();
});
// 結果を取得するまで待つ
Optional<Result> result = submitTask.get();
assert result != null;
// 終了処理
forkJoinPool.shutdown();
return result;
}
Aggregations