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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#[cfg(feature = "point_poll")]
use crate::map::PointPool;

pub type IndexType = i32;

#[derive(Debug, Clone, Default)]
pub struct Point {
    pub x: IndexType,
    pub y: IndexType,

    pub f: IndexType,
    pub g: IndexType,
    pub h: IndexType,
}

impl Ord for Point {
    #[inline]
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        other.f.cmp(&self.f)
    }
}

impl PartialOrd for Point {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl PartialEq for Point {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.x == other.x && self.y == other.y
    }

    #[inline]
    fn ne(&self, other: &Self) -> bool {
        self.x != other.x || self.y != other.y
    }
}

impl Eq for Point {}

impl Point {
    #[cfg(feature = "point_poll")]
    #[inline]
    pub fn new_other(
        x: IndexType,
        y: IndexType,
        f: IndexType,
        g: IndexType,
        h: IndexType,
    ) -> Box<Point> {
        PointPool::get_instance()
            .write()
            .unwrap()
            .allocate(x, y, f, g, h)
    }

    #[cfg(not(feature = "point_poll"))]
    #[inline]
    pub fn new_other(
        x: IndexType,
        y: IndexType,
        f: IndexType,
        g: IndexType,
        h: IndexType,
    ) -> Point {
        Point::new(x, y, f, g, h)
    }

    pub fn new(x: IndexType, y: IndexType, f: IndexType, g: IndexType, h: IndexType) -> Point {
        Point { x, y, f, g, h }
    }

    #[inline]
    pub fn f(&self) -> IndexType {
        self.g + self.h
    }

    #[inline]
    pub fn neighbors(&self) -> Vec<(IndexType, IndexType)> {
        vec![
            (-1, 0),
            (-1, -1),
            (0, -1),
            (1, -1),
            (1, 0),
            (1, 1),
            (0, 1),
            (-1, 1),
        ]
    }
}

#[cfg(feature = "point_poll")]
impl Drop for Point {
    fn drop(&mut self) {
        let stolen_point = std::mem::replace(&mut *self, Point::default());
        PointPool::get_instance()
            .write()
            .unwrap()
            .deallocate(stolen_point)
    }
}