diff --git a/AK/Vector.h b/AK/Vector.h index 12131ae9584..adc0e768238 100644 --- a/AK/Vector.h +++ b/AK/Vector.h @@ -298,6 +298,19 @@ public: m_capacity = new_capacity; } + void shift_left(int count) + { + ASSERT(count <= m_size); + if (count == m_size) { + clear(); + return; + } + for (int i = 0; i < m_size - count; ++i) { + at(i) = move(at(i + count)); + } + m_size -= count; + } + void resize(int new_size) { if (new_size == size()) diff --git a/Applications/PaintBrush/BucketTool.cpp b/Applications/PaintBrush/BucketTool.cpp index 8f728fae832..cf226f65b63 100644 --- a/Applications/PaintBrush/BucketTool.cpp +++ b/Applications/PaintBrush/BucketTool.cpp @@ -15,21 +15,34 @@ BucketTool::~BucketTool() static void flood_fill(GraphicsBitmap& bitmap, const Point& start_position, Color target_color, Color fill_color) { - SinglyLinkedList queue; + Vector queue; + queue.append(start_position); + int queue_pos = 0; + while (queue_pos != queue.size()) { + auto position = queue[queue_pos++]; + + if (queue_pos > 4096) { + queue.shift_left(4096); + queue_pos = 0; + } - queue.append(Point(start_position)); - while (!queue.is_empty()) { - auto position = queue.take_first(); if (!bitmap.rect().contains(position)) continue; if (bitmap.get_pixel(position) != target_color) continue; bitmap.set_pixel(position, fill_color); - queue.append(position.translated(0, -1)); - queue.append(position.translated(0, 1)); - queue.append(position.translated(1, 0)); - queue.append(position.translated(-1, 0)); + if (position.x() != 0) + queue.append(position.translated(0, -1)); + + if (position.x() != bitmap.width() - 1) + queue.append(position.translated(0, 1)); + + if (position.y() != 0) + queue.append(position.translated(-1, 0)); + + if (position.y() != bitmap.height() - 1) + queue.append(position.translated(1, 0)); } }