1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use crate::map::{IndexType, Point};
use lazy_static::lazy_static;
use std::collections::VecDeque;
use std::sync::{Arc, RwLock};

pub struct PointPool {
    pool: VecDeque<Point>,
}

impl PointPool {
    pub fn get_instance() -> Arc<RwLock<PointPool>> {
        Arc::clone(&crate::map::point_pool::POINT_POOL)
    }
    pub fn new() -> Arc<RwLock<PointPool>> {
        Arc::new(RwLock::new(PointPool {
            pool: VecDeque::new(),
        }))
    }

    pub fn allocate(
        &mut self,
        x: IndexType,
        y: IndexType,
        f: IndexType,
        g: IndexType,
        h: IndexType,
    ) -> Box<Point> {
        match self.pool.pop_front() {
            Some(mut point) => {
                point.x = x;
                point.y = y;
                point.f = f;
                point.g = g;
                point.h = h;
                Box::new(point)
            }
            None => Box::new(Point::new(x, y, f, g, h)),
        }
    }

    pub fn deallocate(&mut self, point: Point) {
        self.pool.push_back(point);
    }
}

lazy_static! {
    static ref POINT_POOL: Arc<RwLock<PointPool>> = PointPool::new();
}