Skip to content

Commit 4b44a47

Browse files
committed
Rename Grid cells field to nodes for consistency
1 parent 8a6dd8c commit 4b44a47

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/grid.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::Point;
55
pub struct Grid {
66
width: usize,
77
height: usize,
8-
cells: Vec<bool>,
8+
nodes: Vec<bool>,
99
}
1010

1111
impl Grid {
@@ -14,7 +14,7 @@ impl Grid {
1414
Self {
1515
width,
1616
height,
17-
cells: vec![false; width * height],
17+
nodes: vec![false; width * height],
1818
}
1919
}
2020

@@ -24,16 +24,16 @@ impl Grid {
2424
pub fn from_2d(grid: Vec<Vec<bool>>) -> Self {
2525
let height = grid.len();
2626
let width = grid.first().map_or(0, Vec::len);
27-
let mut cells = Vec::with_capacity(width * height);
27+
let mut nodes = Vec::with_capacity(width * height);
2828

2929
for row in grid {
30-
cells.extend(row);
30+
nodes.extend(row);
3131
}
3232

3333
Self {
3434
width,
3535
height,
36-
cells,
36+
nodes: nodes,
3737
}
3838
}
3939

@@ -49,7 +49,7 @@ impl Grid {
4949
self.height
5050
}
5151

52-
/// Returns the index of the cell at (x, y) coordinates.
52+
/// Returns the index of the node at (x, y) coordinates.
5353
#[inline]
5454
#[must_use]
5555
pub fn index(&self, x: isize, y: isize) -> Option<usize> {
@@ -60,21 +60,21 @@ impl Grid {
6060
}
6161
}
6262

63-
/// Returns the value of the cell at (x, y) coordinates.
63+
/// Returns the value of the node at (x, y) coordinates.
6464
#[must_use]
6565
pub fn get(&self, x: isize, y: isize) -> Option<bool> {
66-
self.index(x, y).map(|i| self.cells[i])
66+
self.index(x, y).map(|i| self.nodes[i])
6767
}
6868

69-
/// Returns a mutable reference to the cell at (x, y) coordinates.
69+
/// Returns a mutable reference to the node at (x, y) coordinates.
7070
#[must_use]
7171
pub fn get_mut(&mut self, x: isize, y: isize) -> Option<&mut bool> {
72-
self.index(x, y).map(|i| &mut self.cells[i])
72+
self.index(x, y).map(|i| &mut self.nodes[i])
7373
}
7474

75-
/// Returns whether the cell at a given `Point` is walkable.
75+
/// Returns whether the node at a given `Point` is walkable.
7676
#[must_use]
7777
pub fn is_walkable(&self, point: Point) -> bool {
78-
self.get(point.x, point.y).map_or(false, |cell| !cell)
78+
self.get(point.x, point.y).map_or(false, |node| !node)
7979
}
8080
}

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn astar(grid: &Grid, start: Point, end: Point) -> Option<Vec<Point>> {
1717
let height = grid.height();
1818
let capacity = width * height;
1919

20-
let mut open_nodes = BinaryHeap::new();
20+
let mut open_nodes = BinaryHeap::new(); // Min-heap; see `Node` impl
2121
let mut closed_nodes = vec![false; capacity];
2222
let mut g_scores = vec![u16::MAX; capacity];
2323
let mut all_nodes = Vec::with_capacity(capacity);
@@ -114,14 +114,15 @@ fn manhattan_distance(a: &Point, b: &Point) -> isize {
114114
(a.x - b.x).abs() + (a.y - b.y).abs()
115115
}
116116

117+
// TODO: Support diagonal movement?
117118
const NEIGHBORS: [(isize, isize); 4] = [(0, 1), (1, 0), (0, -1), (-1, 0)];
118119

119120
fn get_neighbor_points(grid: &Grid, point: Point) -> impl Iterator<Item = Point> + '_ {
120121
NEIGHBORS.iter().filter_map(move |&(dx, dy)| {
121122
let new_x = point.x + dx;
122123
let new_y = point.y + dy;
123124
grid.get(new_x, new_y)
124-
.filter(|&cell| !cell)
125+
.filter(|&node| !node)
125126
.map(|_| Point::new(new_x, new_y))
126127
})
127128
}

0 commit comments

Comments
 (0)