pathfinding/map/
point_pool.rs

1use crate::map::{IndexType, Point};
2use lazy_static::lazy_static;
3use std::collections::VecDeque;
4use std::sync::{Arc, RwLock};
5
6pub struct PointPool {
7    pool: VecDeque<Point>,
8}
9
10impl PointPool {
11    pub fn get_instance() -> Arc<RwLock<PointPool>> {
12        Arc::clone(&crate::map::point_pool::POINT_POOL)
13    }
14    pub fn new() -> Arc<RwLock<PointPool>> {
15        Arc::new(RwLock::new(PointPool {
16            pool: VecDeque::new(),
17        }))
18    }
19
20    pub fn allocate(
21        &mut self,
22        x: IndexType,
23        y: IndexType,
24        f: IndexType,
25        g: IndexType,
26        h: IndexType,
27    ) -> Box<Point> {
28        match self.pool.pop_front() {
29            Some(mut point) => {
30                point.x = x;
31                point.y = y;
32                point.f = f;
33                point.g = g;
34                point.h = h;
35                Box::new(point)
36            }
37            None => Box::new(Point::new(x, y, f, g, h)),
38        }
39    }
40
41    pub fn deallocate(&mut self, point: Point) {
42        self.pool.push_back(point);
43    }
44}
45
46lazy_static! {
47    static ref POINT_POOL: Arc<RwLock<PointPool>> = PointPool::new();
48}