Skip to content

Commit b0885b8

Browse files
committed
update
1 parent 0a4d881 commit b0885b8

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

DoubleBuffer.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,43 @@ class DoubleBuffer {
2626
using read_lock = std::shared_lock<std::shared_mutex>;
2727
using write_lock = std::unique_lock<WriteMutex>;
2828

29-
// Returns a const reference to the current read buffer along with a shared read lock.
29+
// Return a const reference to the current read buffer along with a shared read lock.
3030
std::pair<const T&, read_lock> readBuffer() const
3131
{
3232
read_lock lock(readMutex_);
3333
return { buffer_[index_], std::move(lock) };
3434
}
3535

36-
// Returns a reference to the current write buffer along with an exclusive write lock.
36+
// Return a reference to the current write buffer along with an exclusive write lock.
3737
std::pair<T&, write_lock> writeBuffer()
3838
{
3939
write_lock lock(writeMutex_);
4040
return { buffer_[1 - index_], std::move(lock) };
4141
}
4242

43-
// Clones the current read buffer.
43+
// Clone the current read buffer.
4444
T clone() const
4545
{
4646
read_lock lock(readMutex_);
4747
return buffer_[index_];
4848
}
4949

50-
// Sets new data in the write buffer and swaps the buffers.
50+
// Set new data in the write buffer and swap the buffers.
5151
void setAndSwap(T newData)
5252
{
5353
write_lock writeLock(writeMutex_);
5454
buffer_[1 - index_] = std::move(newData);
5555
swap(std::move(writeLock));
5656
}
5757

58-
// Swaps the read and write buffers.
58+
// Swap the read and write buffers.
5959
void swap()
6060
{
6161
write_lock writeLock(writeMutex_);
6262
swap(std::move(writeLock));
6363
}
6464

65-
// Swaps the read and write buffers using an existing write lock.
65+
// Swap the read and write buffers using an existing write lock.
6666
void swap(write_lock&& writeLock)
6767
{
6868
std::unique_lock<std::shared_mutex> readLock(readMutex_); // Exclusive read lock to prevent reads during the swap.

Grid.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <array>
77
#include <random>
88

9-
#if 1 // Enable multi-threaded calculation for the grid updates
9+
#if 1 // Enable multithreaded calculation of grid updates
1010
#include <numeric>
1111
#include <poolstl/poolstl.hpp>
1212
#define PARALLEL_GRID 1
@@ -40,7 +40,7 @@ class alignas(128) Grid {
4040
#endif
4141
};
4242

43-
// Function to count the number of live neighbors for a cell at (x, y)
43+
// Count the number of live neighbors for the cell at (x, y)
4444
template <int SIZE>
4545
int Grid<SIZE>::countLiveNeighbors(const Point& p) const
4646
{
@@ -72,7 +72,7 @@ int Grid<SIZE>::countLiveNeighbors(const Point& p) const
7272
return liveNeighbors;
7373
}
7474

75-
// Function to toggle a 3x3 block of cells at the given position
75+
// Toggle a 3x3 block of cells at the given position
7676
template <int SIZE>
7777
void Grid<SIZE>::toggleBlock(const Point& p)
7878
{
@@ -87,7 +87,7 @@ void Grid<SIZE>::toggleBlock(const Point& p)
8787
}
8888
}
8989

90-
// Function to apply the rules of Conway's Game of Life
90+
// Apply the rules of Conway's Game of Life
9191
static inline bool gameOfLife(const bool cell, const int liveNeighbors)
9292
{
9393
// A cell is alive in the next generation if it has 3 neighbors,
@@ -105,7 +105,7 @@ void Grid<SIZE>::updateP(const Grid<SIZE>& current, const Point& p)
105105

106106
#ifndef PARALLEL_GRID
107107

108-
// Function to update the grid based on the rules of Conway's Game of Life
108+
// Update the grid based on the rules of Conway's Game of Life
109109
template <int SIZE>
110110
void Grid<SIZE>::updateGrid(const Grid<SIZE>& current)
111111
{
@@ -138,7 +138,7 @@ void Grid<SIZE>::updateGrid(const Grid<SIZE>& current)
138138
static std::mt19937 generator(0);
139139
static std::uniform_int_distribution<int> distribution(0, 2'000'000'000);
140140

141-
// Function to add random noise to the grid
141+
// Add random noise to the grid
142142
template <int SIZE>
143143
void Grid<SIZE>::addNoise(int n)
144144
{
@@ -149,7 +149,7 @@ void Grid<SIZE>::addNoise(int n)
149149
}
150150
}
151151

152-
// Function to clear the grid
152+
// Clear the grid
153153
template <int SIZE>
154154
void Grid<SIZE>::clear()
155155
{

main-sfml.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
std::atomic_bool mouseRightPressed = false;
1313
std::atomic_bool mouseLeftPressed = false;
1414

15-
// Update the next grid
15+
// Update the next grid state
1616
static void updateGrid(const sf::RenderWindow& window)
1717
{
18-
// Get the next grid to write to
18+
// Get the writable next grid
1919
auto [nextGrid, writeLock] = grid.writeBuffer();
2020

2121
// Handle right mouse button click
@@ -31,7 +31,7 @@ static void updateGrid(const sf::RenderWindow& window)
3131
nextGrid.updateGrid(currGrid);
3232
}
3333

34-
// Add some noise to the grid
34+
// Add random noise to the grid
3535
nextGrid.addNoise();
3636

3737
// Handle mouse movement while the left button is pressed
@@ -40,15 +40,15 @@ static void updateGrid(const sf::RenderWindow& window)
4040
const int x = mousePos.x / CELL_SIZE;
4141
const int y = mousePos.y / CELL_SIZE;
4242
if (x >= 0 && x < GRID_SIZE && y >= 0 && y < GRID_SIZE) {
43-
nextGrid.toggleBlock({ x, y }); // Turn on a 3x3 block
43+
nextGrid.toggleBlock({ x, y }); // Toggle a 3x3 block
4444
}
4545
}
4646

4747
// Swap the buffers
4848
grid.swap(std::move(writeLock));
4949
}
5050

51-
// Color map for cells based on number of live neighbors
51+
// Color map for cells based on the number of live neighbors
5252
static const std::array<sf::Color, 9> colorMap {
5353
sf::Color::Red, // 0 live neighbors
5454
sf::Color::Green, // 1 live neighbor
@@ -64,7 +64,7 @@ static const std::array<sf::Color, 9> colorMap {
6464
// Function to update the vertex array
6565
int updateVertices(sf::RenderWindow& window, sf::VertexArray& vertices)
6666
{
67-
int numAlive = 0; // Count the number of alive cells
67+
int numAlive = 0; // Count of alive cells
6868
auto [currGrid, lock] = grid.readBuffer();
6969
for (int i = 0; i < GRID_SIZE; ++i) {
7070
for (int j = 0; j < GRID_SIZE; ++j) {
@@ -134,7 +134,7 @@ int main()
134134
}
135135
}
136136

137-
// Text to display the number of alive cells
137+
// Load font for displaying text
138138
sf::Font font;
139139
const std::vector<std::string> fontPaths {
140140
#ifdef _WIN32

0 commit comments

Comments
 (0)