從您提供的錯誤信息來看,問題出現在您試圖使用 for child in self.children.as_mut().unwrap() 來迭代四叉樹的子結構,但 Box<[QuadTree; 4]> 不能直接作為迭代器。您需要將其引用轉換為迭代器。此處的具體行為如下:
錯誤分析
Rust 的 Box<[T]> 不能直接迭代,但可以使用 as_mut() 方法,然後將結果轉換為迭代器,通過 iter_mut() 方法進行迭代。這可以通過對 Box 的引用獲取指向其內容的迭代器來實現。
修正程式碼
以下是修正後的 query 方法:
fn query(&self, min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> Vec<&Rectangle> {
let mut found_rectangles = Vec::new();
// 檢查與給定邊界的相交
if !self.intersects(min_x, min_y, max_x, max_y) {
return found_rectangles;
}
// 查詢當前節點中的矩形
for rectangle in &self.rectangles {
if rectangle.x1 <= max_x && rectangle.x2 >= min_x && rectangle.y1 <= max_y && rectangle.y2 >= min_y {
found_rectangles.push(rectangle);
}
}
// 查詢子樹的矩形
if let Some(ref children) = self.children {
for child in children.iter() { // 修正為迭代 Box<[QuadTree; 4]>
found_rectangles.extend(child.query(min_x, min_y, max_x, max_y));
}
}
found_rectangles
}
完整的程式碼
最終的四叉樹實作程式碼如下:
#[derive(Debug, Clone)]
struct Rectangle {
x1: f64, // 左上角 x
y1: f64, // 左上角 y
x2: f64, // 右下角 x
y2: f64, // 右下角 y
}
struct QuadTree {
boundary: Rectangle,
rectangles: Vec<Rectangle>,
children: Option<Box<[QuadTree; 4]>>, // 四個子樹
}
impl QuadTree {
fn new(boundary: Rectangle) -> Self {
QuadTree {
boundary,
rectangles: Vec::new(),
children: None,
}
}
fn insert(&mut self, rect: Rectangle) {
if !self.contains(&rect) {
return;
}
if self.rectangles.len() < 4 && self.children.is_none() {
self.rectangles.push(rect);
} else {
if self.children.is_none() {
self.subdivide();
}
for child in self.children.as_mut().unwrap().iter_mut() {
child.insert(rect.clone());
}
}
}
fn contains(&self, rect: &Rectangle) -> bool {
self.boundary.x1 <= rect.x1 && self.boundary.x2 >= rect.x2 &&
self.boundary.y1 <= rect.y1 && self.boundary.y2 >= rect.y2
}
fn subdivide(&mut self) {
let mid_x = (self.boundary.x1 + self.boundary.x2) / 2.0;
let mid_y = (self.boundary.y1 + self.boundary.y2) / 2.0;
self.children = Some(Box::new([
QuadTree::new(Rectangle { x1: self.boundary.x1, y1: self.boundary.y1, x2: mid_x, y2: mid_y }), // 第一象限
QuadTree::new(Rectangle { x1: mid_x, y1: self.boundary.y1, x2: self.boundary.x2, y2: mid_y }), // 第二象限
QuadTree::new(Rectangle { x1: self.boundary.x1, y1: mid_y, x2: mid_x, y2: self.boundary.y2 }), // 第三象限
QuadTree::new(Rectangle { x1: mid_x, y1: mid_y, x2: self.boundary.x2, y2: self.boundary.y2 }), // 第四象限
]));
}
fn query(&self, min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> Vec<&Rectangle> {
let mut found_rectangles = Vec::new();
// 檢查與給定邊界的相交
if !self.intersects(min_x, min_y, max_x, max_y) {
return found_rectangles;
}
// 查詢當前節點中的矩形
for rectangle in &self.rectangles {
if rectangle.x1 <= max_x && rectangle.x2 >= min_x &&
rectangle.y1 <= max_y && rectangle.y2 >= min_y {
found_rectangles.push(rectangle);
}
}
// 查詢子樹的矩形
if let Some(ref children) = self.children {
for child in children.iter() {
found_rectangles.extend(child.query(min_x, min_y, max_x, max_y));
}
}
found_rectangles
}
fn intersects(&self, min_x: f64, min_y: f64, max_x: f64, max_y: f64) -> bool {
let (tree_min_x, tree_min_y, tree_max_x, tree_max_y) = (self.boundary.x1, self.boundary.y1, self.boundary.x2, self.boundary.y2);
!(max_x < tree_min_x || min_x > tree_max_x ||
max_y < tree_min_y || min_y > tree_max_y)
}
}
fn main() {
let mut qt = QuadTree::new(Rectangle { x1: 0.0, y1: 0.0, x2: 10.0, y2: 10.0 });
// 插入幾個矩形
qt.insert(Rectangle { x1: 1.0, y1: 1.0, x2: 3.0, y2: 3.0 });
qt.insert(Rectangle { x1: 4.0, y1: 4.0, x2: 6.0, y2: 6.0 });
qt.insert(Rectangle { x1: 7.0, y1: 7.0, x2: 8.0, y2: 8.0 });
qt.insert(Rectangle { x1: 2.0, y1: 2.0, x2: 5.0, y2: 5.0 });
// 查詢給定範圍的矩形
let found_rectangles = qt.query(0.0, 0.0, 5.0, 5.0);
println!("Rectangles found in the query area:");
for rectangle in found_rectangles {
println!("Rectangle: ({}, {}, {}, {})", rectangle.x1, rectangle.y1, rectangle.x2, rectangle.y2);
}
}
記憶體使用過程圖
以下是程序執行過程的記憶體使用圖,包括 Stack 和 Heap 的狀態:
初始狀態 (開始時無變數)
在程式開始執行時,Stack 和 Heap 都是空的。
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| | | |
+-----------+ +--------------------------+
步驟 1: 創建 QuadTree
在 main 函數中創建 QuadTree 實例,並設置邊界 (0.0, 0.0, 10.0, 10.0)。
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| qt | ---> | |
|-----------| | |
| boundary | (0.0, 0.0, 10.0, 10.0) | // `Vec` 尚未分配
| children | None | |
| rectangles | [] | |
+-----------+ +--------------------------+
Stack: Total = 32 bytes
Heap: Total = 0 bytes (尚未使用 Heap)
步驟 2: 插入 Rectangle 實例
每當使用 insert 方法時,會在 Heap 中為新的 Rectangle 實例分配空間。
插入第一個矩形 (1.0, 1.0, 3.0, 3.0):
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| qt | ---> | |
|-----------| | |
| boundary | (0.0, 0.0, 10.0, 10.0) | // Vec 的指針
| children | None | |
| rectangles | ---->| Rectangle |
| | | x1: 1.0 |
| | | y1: 1.0 |
| | | x2: 3.0 |
| | | y2: 3.0 |
+-----------+ +--------------------------+
Stack: Total = 56 bytes (指向 Vec 和 Rectangle pointer)
Heap: Total = 32 bytes (8 bytes for x1, y1, x2, y2 each)
插入第二個矩形 (4.0, 4.0, 6.0, 6.0):
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| qt | ---> | |
|-----------| | |
| boundary | (0.0, 0.0, 10.0, 10.0) | // Vec 的指針
| children | None | |
| rectangles | ---->| Rectangle |
| | | x1: 1.0 |
| | | y1: 1.0 |
| | | x2: 3.0 |
| | | y2: 3.0 |
| | ---->| Rectangle |
| | | x1: 4.0 |
| | | y1: 4.0 |
| | | x2: 6.0 |
| | | y2: 6.0 |
+-----------+ +--------------------------+
Stack: Total = 56 bytes
Heap: Total = 64 bytes (2 * 32 bytes for two Rectangles)
插入第三個矩形 (7.0, 7.0, 8.0, 8.0):
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| qt | ---> | |
|-----------| | |
| boundary | (0.0, 0.0, 10.0, 10.0) | // Vec 的指針
| children | None | |
| rectangles | ---->| Rectangle |
| | | x1: 1.0 |
| | | y1: 1.0 |
| | | x2: 3.0 |
| | | y2: 3.0 |
| | ---->| Rectangle |
| | | x1: 4.0 |
| | | y1: 4.0 |
| | | x2: 6.0 |
| | | y2: 6.0 |
| | ---->| Rectangle |
| | | x1: 7.0 |
| | | y1: 7.0 |
| | | x2: 8.0 |
| | | y2: 8.0 |
+-----------+ +--------------------------+
Stack: Total = 72 bytes
Heap: Total = 96 bytes (3 * 32 bytes for three Rectangles)
插入第四個矩形 (2.0, 2.0, 5.0, 5.0):
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| qt | ---> | |
|-----------| | |
| boundary | (0.0, 0.0, 10.0, 10.0) | // Vec 的指針
| children | None | |
| rectangles | ---->| Rectangle |
| | | x1: 1.0 |
| | | y1: 1.0 |
| | | x2: 3.0 |
| | | y2: 3.0 |
| | ---->| Rectangle |
| | | x1: 4.0 |
| | | y1: 4.0 |
| | | x2: 6.0 |
| | | y2: 6.0 |
| | ---->| Rectangle |
| | | x1: 7.0 |
| | | y1: 7.0 |
| | | x2: 8.0 |
| | | y2: 8.0 |
| | ---->| Rectangle |
| | | x1: 2.0 |
| | | y1: 2.0 |
| | | x2: 5.0 |
| | | y2: 5.0 |
+-----------+ +--------------------------+
Stack: Total = 88 bytes
Heap: Total = 128 bytes (4 * 32 bytes for four Rectangles)
步驟 5: 查詢 Rectangle
在查詢過程中,將遍歷 Vec<Rectangle>,這不會影響 Stack 和 Heap。
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| qt | ---> | |
|-----------| | |
| boundary | (0.0, 0.0, 10.0, 10.0) | // Vec 的指針
| children | None | |
| rectangles | ---->| Rectangle |
| | | x1: 1.0 |
| | | y1: 1.0 |
| | | x2: 3.0 |
| | | y2: 3.0 |
| | ---->| Rectangle |
| | | x1: 4.0 |
| | | y1: 4.0 |
| | | x2: 6.0 |
| | | y2: 6.0 |
| | ---->| Rectangle |
| | | x1: 7.0 |
| | | y1: 7.0 |
| | | x2: 8.0 |
| | | y2: 8.0 |
| | ---->| Rectangle |
| | | x1: 2.0 |
| | | y1: 2.0 |
| | | x2: 5.0 |
| | | y2: 5.0 |
+-----------+ +--------------------------+
Stack: Total = 88 bytes
Heap: Total = 128 bytes
步驟 6: 程式結束,自動釋放
當 main 函數結束時,所有的 Rectangle 實例將自動釋放佔用的 Heap 記憶體。
+-----------+ +--------------------------+
| Stack | | Heap |
+-----------+ +--------------------------+
| | | |
| | | |
| | | |
+-----------+ +--------------------------+
Stack: Total = 0 bytes
Heap: Total = 0 bytes (所有記憶體釋放)
總結
透過這個題目,學習者將學會如何實現一個四叉樹,使其能夠儲存和查詢多維空間中的數據。學生會理解如何使用 Rust 的記憶體管理系統,以及所有權和借用的原則。在處理更複雜的資料結構時,這樣的經驗會幫助他們更有效地找到內存使用的最佳實踐。