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();
}