-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflood.hpp
More file actions
697 lines (614 loc) · 26 KB
/
flood.hpp
File metadata and controls
697 lines (614 loc) · 26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#ifndef ATOM_ALGORITHM_FLOOD_GPP
#define ATOM_ALGORITHM_FLOOD_GPP
#include <atomic>
#include <concepts>
#include <mutex>
#include <queue>
#include <ranges>
#include <span>
#include <stack>
#include <thread>
#include <type_traits>
#include <vector>
#if defined(__x86_64__) || defined(_M_X64)
#include <immintrin.h>
#endif
#include "atom/algorithm/rust_numeric.hpp"
#include "atom/error/exception.hpp"
#include <spdlog/spdlog.h>
/**
* @enum Connectivity
* @brief Enum to specify the type of connectivity for flood fill.
*/
enum class Connectivity {
Four, ///< 4-way connectivity (up, down, left, right)
Eight ///< 8-way connectivity (up, down, left, right, and diagonals)
};
// Static assertion to ensure enum values are as expected
static_assert(static_cast<std::int32_t>(Connectivity::Four) == 0 &&
static_cast<std::int32_t>(Connectivity::Eight) == 1,
"Connectivity enum values must be 0 and 1");
/**
* @concept Grid
* @brief Concept that defines requirements for a type to be used as a grid.
*/
template <typename T>
concept Grid = requires(T t, std::size_t i, std::size_t j) {
{ t[i] } -> std::ranges::random_access_range;
{ t[i][j] } -> std::convertible_to<typename T::value_type::value_type>;
requires std::is_default_constructible_v<T>;
// { t.size() } -> std::convertible_to<usize>;
{ t.empty() } -> std::same_as<bool>;
// requires(!t.empty() ? t[0].size() > 0 : true);
};
/**
* @concept SIMDCompatibleGrid
* @brief Concept that defines requirements for a type to be used with SIMD
* operations.
*/
template <typename T>
concept SIMDCompatibleGrid =
Grid<T> &&
(std::same_as<typename T::value_type::value_type, atom::algorithm::i32> ||
std::same_as<typename T::value_type::value_type, atom::algorithm::f32> ||
std::same_as<typename T::value_type::value_type, atom::algorithm::f64> ||
std::same_as<typename T::value_type::value_type, atom::algorithm::u8> ||
std::same_as<typename T::value_type::value_type, atom::algorithm::u32>);
/**
* @concept ContiguousGrid
* @brief Concept that defines requirements for a grid with contiguous memory
* layout.
*/
template <typename T>
concept ContiguousGrid = Grid<T> && requires(T t) {
{ t.data() } -> std::convertible_to<typename T::value_type*>;
requires std::contiguous_iterator<typename T::iterator>;
};
/**
* @concept SpanCompatibleGrid
* @brief Concept for grids that can work with std::span for efficient views.
*/
template <typename T>
concept SpanCompatibleGrid = Grid<T> && requires(T t) {
{ std::span<typename T::value_type>(t) };
};
namespace atom::algorithm {
/**
* @class FloodFill
* @brief A class that provides static methods for performing flood fill
* operations using various algorithms and optimizations.
*/
class FloodFill {
public:
/**
* @brief Configuration struct for flood fill operations
*/
struct FloodFillConfig {
Connectivity connectivity = Connectivity::Four;
u32 numThreads = static_cast<u32>(std::thread::hardware_concurrency());
bool useSIMD = true;
bool useBlockProcessing = true;
u32 blockSize = 32; // Size of cache-friendly blocks
f32 loadBalancingFactor =
1.5f; // Work distribution factor for parallel processing
// Validation method for configuration
[[nodiscard]] constexpr bool isValid() const noexcept {
return numThreads > 0 && blockSize > 0 && blockSize <= 256 &&
loadBalancingFactor > 0.0f;
}
};
/**
* @brief Perform flood fill using Breadth-First Search (BFS).
*
* @tparam GridType The type of grid to perform flood fill on
* @param grid The 2D grid to perform the flood fill on.
* @param start_x The starting x-coordinate for the flood fill.
* @param start_y The starting y-coordinate for the flood fill.
* @param target_color The color to be replaced.
* @param fill_color The color to fill with.
* @param conn The type of connectivity to use (default is 4-way
* connectivity).
* @return Number of cells filled
* @throws std::invalid_argument If grid is empty or coordinates are
* invalid.
* @throws std::runtime_error If operation fails during execution.
*/
template <Grid GridType>
[[nodiscard]] static usize fillBFS(
GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
Connectivity conn = Connectivity::Four);
/**
* @brief Perform flood fill using Depth-First Search (DFS).
*
* @tparam GridType The type of grid to perform flood fill on
* @param grid The 2D grid to perform the flood fill on.
* @param start_x The starting x-coordinate for the flood fill.
* @param start_y The starting y-coordinate for the flood fill.
* @param target_color The color to be replaced.
* @param fill_color The color to fill with.
* @param conn The type of connectivity to use (default is 4-way
* connectivity).
* @return Number of cells filled
* @throws std::invalid_argument If grid is empty or coordinates are
* invalid.
* @throws std::runtime_error If operation fails during execution.
*/
template <Grid GridType>
[[nodiscard]] static usize fillDFS(
GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
Connectivity conn = Connectivity::Four);
/**
* @brief Perform parallel flood fill using multiple threads.
*
* @tparam GridType The type of grid to perform flood fill on
* @param grid The 2D grid to perform the flood fill on.
* @param start_x The starting x-coordinate for the flood fill.
* @param start_y The starting y-coordinate for the flood fill.
* @param target_color The color to be replaced.
* @param fill_color The color to fill with.
* @param config Configuration options for the flood fill operation.
* @return Number of cells filled
* @throws std::invalid_argument If grid is empty or coordinates are
* invalid.
* @throws std::runtime_error If operation fails during execution.
*/
template <Grid GridType>
[[nodiscard]] static usize fillParallel(
GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
const FloodFillConfig& config);
/**
* @brief Perform SIMD-accelerated flood fill for suitable grid types.
*
* @tparam GridType The type of grid to perform flood fill on
* @param grid The 2D grid to perform the flood fill on.
* @param start_x The starting x-coordinate for the flood fill.
* @param start_y The starting y-coordinate for the flood fill.
* @param target_color The color to be replaced.
* @param fill_color The color to fill with.
* @param config Configuration options for the flood fill operation.
* @return Number of cells filled
* @throws std::invalid_argument If grid is empty or coordinates are
* invalid.
* @throws std::runtime_error If operation fails during execution.
* @throws std::logic_error If SIMD operations are not supported for this
* grid type.
*/
template <SIMDCompatibleGrid GridType>
[[nodiscard]] static usize fillSIMD(
GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
const FloodFillConfig& config);
/**
* @brief Asynchronous flood fill generator using C++20 coroutines.
* Returns a generator that yields each filled position.
*
* @tparam GridType The type of grid to perform flood fill on
* @param grid The 2D grid to perform the flood fill on.
* @param start_x The starting x-coordinate for the flood fill.
* @param start_y The starting y-coordinate for the flood fill.
* @param target_color The color to be replaced.
* @param fill_color The color to fill with.
* @param conn The type of connectivity to use.
* @return A generator yielding pairs of coordinates
*/
template <Grid GridType>
static auto fillAsync(
GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
Connectivity conn = Connectivity::Four);
/**
* @brief Cache-optimized flood fill using block-based processing
*
* @tparam GridType The type of grid to perform flood fill on
* @param grid The 2D grid to perform the flood fill on.
* @param start_x The starting x-coordinate for the flood fill.
* @param start_y The starting y-coordinate for the flood fill.
* @param target_color The color to be replaced.
* @param fill_color The color to fill with.
* @param config Configuration options for the flood fill operation.
* @return Number of cells filled
*/
template <Grid GridType>
[[nodiscard]] static usize fillBlockOptimized(
GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
const FloodFillConfig& config);
/**
* @brief Specialized BFS flood fill method for
* std::vector<std::vector<i32>>
* @return Number of cells filled
*/
[[nodiscard]] static usize fillBFS(std::vector<std::vector<i32>>& grid,
i32 start_x, i32 start_y,
i32 target_color, i32 fill_color,
Connectivity conn = Connectivity::Four);
/**
* @brief Specialized DFS flood fill method for
* std::vector<std::vector<i32>>
* @return Number of cells filled
*/
[[nodiscard]] static usize fillDFS(std::vector<std::vector<i32>>& grid,
i32 start_x, i32 start_y,
i32 target_color, i32 fill_color,
Connectivity conn = Connectivity::Four);
private:
/**
* @brief Check if a position is within the bounds of the grid.
*
* @param x The x-coordinate to check.
* @param y The y-coordinate to check.
* @param rows The number of rows in the grid.
* @param cols The number of columns in the grid.
* @return true if the position is within bounds, false otherwise.
*/
[[nodiscard]] static constexpr bool isInBounds(i32 x, i32 y, i32 rows,
i32 cols) noexcept {
return x >= 0 && x < rows && y >= 0 && y < cols;
}
/**
* @brief Get the directions for the specified connectivity.
*
* @param conn The type of connectivity (4-way or 8-way).
* @return A vector of direction pairs.
*/
[[nodiscard]] static auto getDirections(Connectivity conn)
-> std::vector<std::pair<i32, i32>>;
/**
* @brief Validate grid and coordinates before processing.
*
* @tparam GridType The type of grid
* @param grid The 2D grid to validate.
* @param start_x The starting x-coordinate.
* @param start_y The starting y-coordinate.
* @throws std::invalid_argument If grid is empty or coordinates are
* invalid.
*/
template <Grid GridType>
static void validateInput(const GridType& grid, i32 start_x, i32 start_y);
/**
* @brief Extended validation for additional input parameters
*
* @tparam GridType The type of grid
* @param grid The 2D grid to validate
* @param start_x The starting x-coordinate
* @param start_y The starting y-coordinate
* @param target_color The color to be replaced
* @param fill_color The color to fill with
* @param config The configuration options
* @throws std::invalid_argument If any parameters are invalid
*/
template <Grid GridType>
static void validateExtendedInput(
const GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
const FloodFillConfig& config);
/**
* @brief Validate grid size and dimensions
*
* @tparam GridType The type of grid
* @param grid The grid to validate
* @throws std::invalid_argument If grid dimensions exceed maximum limits
*/
template <Grid GridType>
static void validateGridSize(const GridType& grid);
/**
* @brief Process a row of grid data using SIMD instructions
*
* @tparam T Type of grid element
* @param row Pointer to the row data
* @param start_idx Starting index in the row
* @param length Number of elements to process
* @param target_color Color to be replaced
* @param fill_color Color to fill with
* @return Number of cells filled
*/
template <typename T>
[[nodiscard]] static usize processRowSIMD(T* row, i32 start_idx, i32 length,
T target_color, T fill_color);
/**
* @brief Process a block of the grid for block-based flood fill
*
* @tparam GridType The type of grid
* @param grid The grid to process
* @param blockX X coordinate of the block's top-left corner
* @param blockY Y coordinate of the block's top-left corner
* @param blockSize Size of the block
* @param target_color Color to be replaced
* @param fill_color Color to fill with
* @param conn Connectivity type
* @param borderQueue Queue to store border pixels
* @return Number of cells filled in the block
*/
template <Grid GridType>
[[nodiscard]] static usize processBlock(
GridType& grid, i32 blockX, i32 blockY, i32 blockSize,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color, Connectivity conn,
std::queue<std::pair<i32, i32>>& borderQueue);
};
template <Grid GridType>
void FloodFill::validateInput(const GridType& grid, i32 start_x, i32 start_y) {
if (grid.empty() || grid[0].empty()) {
THROW_INVALID_ARGUMENT("Grid cannot be empty");
}
i32 rows = static_cast<i32>(grid.size());
i32 cols = static_cast<i32>(grid[0].size());
if (!isInBounds(start_x, start_y, rows, cols)) {
THROW_INVALID_ARGUMENT("Starting coordinates out of bounds");
}
}
template <Grid GridType>
void FloodFill::validateExtendedInput(
const GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
const FloodFillConfig& config) {
// Basic validation
validateInput(grid, start_x, start_y);
validateGridSize(grid);
// Check configuration validity
if (!config.isValid()) {
THROW_INVALID_ARGUMENT("Invalid flood fill configuration");
}
// Additional validations specific to grid type
if constexpr (std::is_arithmetic_v<
typename GridType::value_type::value_type>) {
// For numeric types, check if colors are within valid ranges
if (target_color == fill_color) {
THROW_INVALID_ARGUMENT(
"Target color and fill color cannot be the same");
}
}
}
template <Grid GridType>
void FloodFill::validateGridSize(const GridType& grid) {
// Check if grid dimensions are within reasonable limits
const usize max_dimension =
static_cast<usize>(atom::algorithm::I32::MAX) / 2;
if (grid.size() > max_dimension) {
THROW_INVALID_ARGUMENT("Grid row count exceeds maximum allowed size");
}
for (const auto& row : grid) {
if (row.size() > max_dimension) {
THROW_INVALID_ARGUMENT(
"Grid column count exceeds maximum allowed size");
}
}
// Check for uniform row sizes
if (!grid.empty()) {
const usize first_row_size = grid[0].size();
for (usize i = 1; i < grid.size(); ++i) {
if (grid[i].size() != first_row_size) {
THROW_INVALID_ARGUMENT("Grid has non-uniform row sizes");
}
}
}
}
template <Grid GridType>
usize FloodFill::fillBFS(GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
Connectivity conn) {
spdlog::info("Starting BFS Flood Fill at position ({}, {})", start_x,
start_y);
usize filled_cells = 0; // Counter for filled cells
try {
validateInput(grid, start_x, start_y);
if (grid[static_cast<usize>(start_x)][static_cast<usize>(start_y)] !=
target_color ||
target_color == fill_color) {
spdlog::warn(
"Start position does not match target color or target color is "
"the same as fill color");
return filled_cells;
}
i32 rows = static_cast<i32>(grid.size());
i32 cols = static_cast<i32>(grid[0].size());
const auto directions = getDirections(conn); // Now returns vector
std::queue<std::pair<i32, i32>> toVisitQueue;
toVisitQueue.emplace(start_x, start_y);
grid[static_cast<usize>(start_x)][static_cast<usize>(start_y)] =
fill_color;
filled_cells++; // Count filled cells
while (!toVisitQueue.empty()) {
auto [x, y] = toVisitQueue.front();
toVisitQueue.pop();
spdlog::debug("Filling position ({}, {})", x, y);
// Now we can directly iterate over the vector
for (const auto& [dx, dy] : directions) {
i32 newX = x + dx;
i32 newY = y + dy;
if (isInBounds(newX, newY, rows, cols) &&
grid[static_cast<usize>(newX)][static_cast<usize>(newY)] ==
target_color) {
grid[static_cast<usize>(newX)][static_cast<usize>(newY)] =
fill_color;
filled_cells++; // Count filled cells
toVisitQueue.emplace(newX, newY);
spdlog::debug("Adding position ({}, {}) to queue", newX,
newY);
}
}
}
return filled_cells;
} catch (const std::exception& e) {
spdlog::error("Exception in fillBFS: {}", e.what());
throw; // Re-throw the exception after logging
}
}
template <Grid GridType>
usize FloodFill::fillDFS(GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
Connectivity conn) {
spdlog::info("Starting DFS Flood Fill at position ({}, {})", start_x,
start_y);
usize filled_cells = 0; // Counter for filled cells
try {
validateInput(grid, start_x, start_y);
if (grid[static_cast<usize>(start_x)][static_cast<usize>(start_y)] !=
target_color ||
target_color == fill_color) {
spdlog::warn(
"Start position does not match target color or target color is "
"the same as fill color");
return filled_cells;
}
i32 rows = static_cast<i32>(grid.size());
i32 cols = static_cast<i32>(grid[0].size());
auto directions = getDirections(conn);
std::stack<std::pair<i32, i32>> toVisitStack;
toVisitStack.emplace(start_x, start_y);
grid[static_cast<usize>(start_x)][static_cast<usize>(start_y)] =
fill_color;
filled_cells++; // Count filled cells
while (!toVisitStack.empty()) {
auto [x, y] = toVisitStack.top();
toVisitStack.pop();
spdlog::debug("Filling position ({}, {})", x, y);
for (auto [dx, dy] : directions) {
i32 newX = x + dx;
i32 newY = y + dy;
if (isInBounds(newX, newY, rows, cols) &&
grid[static_cast<usize>(newX)][static_cast<usize>(newY)] ==
target_color) {
grid[static_cast<usize>(newX)][static_cast<usize>(newY)] =
fill_color;
filled_cells++; // Count filled cells
toVisitStack.emplace(newX, newY);
spdlog::debug("Adding position ({}, {}) to stack", newX,
newY);
}
}
}
return filled_cells;
} catch (const std::exception& e) {
spdlog::error("Exception in fillDFS: {}", e.what());
throw; // Re-throw the exception after logging
}
}
template <Grid GridType>
usize FloodFill::fillParallel(
GridType& grid, i32 start_x, i32 start_y,
typename GridType::value_type::value_type target_color,
typename GridType::value_type::value_type fill_color,
const FloodFillConfig& config) {
spdlog::info(
"Starting Parallel Flood Fill at position ({}, {}) with {} threads",
start_x, start_y, config.numThreads);
usize filled_cells = 0; // Counter for filled cells
try {
// Enhanced validation with the extended input function
validateExtendedInput(grid, start_x, start_y, target_color, fill_color,
config);
if (grid[static_cast<usize>(start_x)][static_cast<usize>(start_y)] !=
target_color ||
target_color == fill_color) {
spdlog::warn(
"Start position does not match target color or target color is "
"the same as fill color");
return filled_cells;
}
i32 rows = static_cast<i32>(grid.size());
i32 cols = static_cast<i32>(grid[0].size());
auto directions = getDirections(config.connectivity);
// First BFS phase to find initial points to process in parallel
std::vector<std::pair<i32, i32>> seeds;
std::queue<std::pair<i32, i32>> queue;
std::vector<std::vector<bool>> visited(
static_cast<usize>(rows),
std::vector<bool>(static_cast<usize>(cols), false));
queue.emplace(start_x, start_y);
visited[static_cast<usize>(start_x)][static_cast<usize>(start_y)] =
true;
grid[static_cast<usize>(start_x)][static_cast<usize>(start_y)] =
fill_color;
filled_cells++; // Count filled cells
// Find seed points for parallel processing
while (!queue.empty() && seeds.size() < config.numThreads) {
auto [x, y] = queue.front();
queue.pop();
// Add current point as a seed if it's not the starting point
if (x != start_x || y != start_y) {
seeds.emplace_back(x, y);
}
// Explore neighbors to find more potential seeds
for (auto [dx, dy] : directions) {
i32 newX = x + dx;
i32 newY = y + dy;
if (isInBounds(newX, newY, rows, cols) &&
grid[static_cast<usize>(newX)][static_cast<usize>(newY)] ==
target_color &&
!visited[static_cast<usize>(newX)]
[static_cast<usize>(newY)]) {
visited[static_cast<usize>(newX)]
[static_cast<usize>(newY)] = true;
grid[static_cast<usize>(newX)][static_cast<usize>(newY)] =
fill_color;
filled_cells++; // Count filled cells
queue.emplace(newX, newY);
}
}
}
// If we didn't find enough seeds, use what we have
if (seeds.empty()) {
spdlog::info(
"Area too small for parallel fill, using single thread");
return filled_cells; // Already filled by the seed finding phase
}
// Use mutex to protect concurrent access to the grid
std::mutex gridMutex;
std::atomic<bool> shouldTerminate{false};
std::atomic<usize> threadFilledCells{0};
// Worker function for each thread
auto worker = [&](const std::pair<i32, i32>& seed) {
std::queue<std::pair<i32, i32>> localQueue;
localQueue.push(seed);
usize localFilledCells = 0;
while (!localQueue.empty() && !shouldTerminate) {
auto [x, y] = localQueue.front();
localQueue.pop();
for (auto [dx, dy] : directions) {
i32 newX = x + dx;
i32 newY = y + dy;
if (isInBounds(newX, newY, rows, cols)) {
std::lock_guard<std::mutex> lock(gridMutex);
if (grid[static_cast<usize>(newX)]
[static_cast<usize>(newY)] == target_color) {
grid[static_cast<usize>(newX)]
[static_cast<usize>(newY)] = fill_color;
localFilledCells++;
localQueue.emplace(newX, newY);
}
}
}
}
threadFilledCells += localFilledCells;
};
// Launch worker threads
std::vector<std::jthread> threads;
threads.reserve(seeds.size());
for (const auto& seed : seeds) {
threads.emplace_back(worker, seed);
}
// No need to join explicitly as std::jthread automatically joins on
// destruction
filled_cells += threadFilledCells.load();
return filled_cells;
} catch (const std::exception& e) {
spdlog::error("Exception in fillParallel: {}", e.what());
throw; // Re-throw the exception after logging
}
}
} // namespace atom::algorithm
#endif // ATOM_ALGORITHM_FLOOD_GPP