12 changed files with 189 additions and 116 deletions
@ -0,0 +1,74 @@
|
||||
#include "crawl.hpp" |
||||
|
||||
#include <type_traits> |
||||
|
||||
#include <function_ref.hpp> |
||||
|
||||
#include "engine/displacement.hpp" |
||||
|
||||
namespace devilution { |
||||
namespace { |
||||
|
||||
bool CrawlFlipsX(Displacement mirrored, tl::function_ref<bool(Displacement)> function) |
||||
{ |
||||
for (const Displacement displacement : { mirrored.flipX(), mirrored }) { |
||||
if (!function(displacement)) |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
bool CrawlFlipsY(Displacement mirrored, tl::function_ref<bool(Displacement)> function) |
||||
{ |
||||
for (const Displacement displacement : { mirrored, mirrored.flipY() }) { |
||||
if (!function(displacement)) |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
bool CrawlFlipsXY(Displacement mirrored, tl::function_ref<bool(Displacement)> function) |
||||
{ |
||||
for (const Displacement displacement : { mirrored.flipX(), mirrored, mirrored.flipXY(), mirrored.flipY() }) { |
||||
if (!function(displacement)) |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
} // namespace
|
||||
|
||||
bool DoCrawl(unsigned radius, tl::function_ref<bool(Displacement)> function) |
||||
{ |
||||
if (radius == 0) |
||||
return function(Displacement { 0, 0 }); |
||||
|
||||
if (!CrawlFlipsY({ 0, static_cast<int>(radius) }, function)) |
||||
return false; |
||||
for (unsigned i = 1; i < radius; i++) { |
||||
if (!CrawlFlipsXY({ static_cast<int>(i), static_cast<int>(radius) }, function)) |
||||
return false; |
||||
} |
||||
if (radius > 1) { |
||||
if (!CrawlFlipsXY({ static_cast<int>(radius) - 1, static_cast<int>(radius) - 1 }, function)) |
||||
return false; |
||||
} |
||||
if (!CrawlFlipsX({ static_cast<int>(radius), 0 }, function)) |
||||
return false; |
||||
for (unsigned i = 1; i < radius; i++) { |
||||
if (!CrawlFlipsXY({ static_cast<int>(radius), static_cast<int>(i) }, function)) |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref<bool(Displacement)> function) |
||||
{ |
||||
for (unsigned i = minRadius; i <= maxRadius; i++) { |
||||
if (!DoCrawl(i, function)) |
||||
return false; |
||||
} |
||||
return true; |
||||
} |
||||
|
||||
} // namespace devilution
|
||||
@ -0,0 +1,58 @@
|
||||
#include <type_traits> |
||||
|
||||
#include <function_ref.hpp> |
||||
|
||||
#include "engine/displacement.hpp" |
||||
|
||||
namespace devilution { |
||||
|
||||
/**
|
||||
* CrawlTable specifies X- and Y-coordinate deltas from a missile target coordinate. |
||||
* |
||||
* n=4 |
||||
* |
||||
* y |
||||
* ^ |
||||
* | 1 |
||||
* | 3#4 |
||||
* | 2 |
||||
* +-----> x |
||||
* |
||||
* n=16 |
||||
* |
||||
* y |
||||
* ^ |
||||
* | 314 |
||||
* | B7 8C |
||||
* | F # G |
||||
* | D9 AE |
||||
* | 526 |
||||
* +-------> x |
||||
*/ |
||||
|
||||
bool DoCrawl(unsigned radius, tl::function_ref<bool(Displacement)> function); |
||||
bool DoCrawl(unsigned minRadius, unsigned maxRadius, tl::function_ref<bool(Displacement)> function); |
||||
|
||||
template <typename F> |
||||
auto Crawl(unsigned radius, F function) -> std::invoke_result_t<decltype(function), Displacement> |
||||
{ |
||||
std::invoke_result_t<decltype(function), Displacement> result; |
||||
DoCrawl(radius, [&result, &function](Displacement displacement) -> bool { |
||||
result = function(displacement); |
||||
return !result; |
||||
}); |
||||
return result; |
||||
} |
||||
|
||||
template <typename F> |
||||
auto Crawl(unsigned minRadius, unsigned maxRadius, F function) -> std::invoke_result_t<decltype(function), Displacement> |
||||
{ |
||||
std::invoke_result_t<decltype(function), Displacement> result; |
||||
DoCrawl(minRadius, maxRadius, [&result, &function](Displacement displacement) -> bool { |
||||
result = function(displacement); |
||||
return !result; |
||||
}); |
||||
return result; |
||||
} |
||||
|
||||
} // namespace devilution
|
||||
@ -0,0 +1,25 @@
|
||||
#include <benchmark/benchmark.h> |
||||
|
||||
#include "crawl.hpp" |
||||
#include "engine/displacement.hpp" |
||||
|
||||
namespace devilution { |
||||
namespace { |
||||
|
||||
void BM_Crawl(benchmark::State &state) |
||||
{ |
||||
const int radius = state.range(0); |
||||
for (auto _ : state) { |
||||
int sum; |
||||
Crawl(0, radius, [&sum](Displacement d) { |
||||
sum += d.deltaX + d.deltaY; |
||||
return false; |
||||
}); |
||||
benchmark::DoNotOptimize(sum); |
||||
} |
||||
} |
||||
|
||||
BENCHMARK(BM_Crawl)->RangeMultiplier(4)->Range(1, 20); |
||||
|
||||
} // namespace
|
||||
} // namespace devilution
|
||||
Loading…
Reference in new issue