lattice-graph 0.7.0

Set of Lattice(Grid) based Graph Structures
Documentation
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
//! Square graph using Abstract Lattice Graph. If you want a undirected square graph, it is recommended to use specialized [`SquareGraph`](`crate::SquareGraph`).
//! Use this for directed graph or square with diagonal direction graph.

use super::*;
use petgraph::{Directed, Undirected};

/// Undirected Square Graph based on [`LatticeGraph`], recommended to use [`SquareGraph`](`crate::SquareGraph`) instead.
pub type SquareGraphAbstract<N, E> = LatticeGraph<N, E, SquareShape>;
/// Directed Square Graph based on [`LatticeGraph`].
pub type DirectedSquareGraph<N, E> = LatticeGraph<N, E, SquareShape<Directed>>;
/// Undirected Square Graph with edge to diagonal direction.
pub type DiagonalSquareGraph<N, E> = LatticeGraph<N, E, SquareDiagonalShape>;
/// Directed Square Graph with edge to diagonal direction.
pub type DirectedDiagonalSquareGraph<N, E> = LatticeGraph<N, E, SquareDiagonalShape<Directed>>;

/// Axis for square graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SquareAxis {
    X = 0,
    Y = 1,
}

impl Axis for SquareAxis {
    const COUNT: usize = 2;
    const DIRECTED: bool = false;
    type Direction = DirectedSquareAxis;
    const UNDIRECTED_COUNT: usize = if Self::DIRECTED {
        Self::COUNT
    } else {
        Self::COUNT * 2
    };

    fn to_index(&self) -> usize {
        match self {
            SquareAxis::X => 0,
            SquareAxis::Y => 1,
        }
    }

    #[allow(unused_unsafe)]
    unsafe fn from_index_unchecked(index: usize) -> Self {
        match index {
            0 => SquareAxis::X,
            1 => SquareAxis::Y,
            _ => unsafe { core::hint::unreachable_unchecked() },
        }
    }

    fn from_index(index: usize) -> Option<Self>
    where
        Self: Sized,
    {
        match index {
            0 => Some(SquareAxis::X),
            1 => Some(SquareAxis::Y),
            _ => None,
        }
    }

    fn foward(self) -> Self::Direction {
        unsafe { DirectedSquareAxis::from_index_unchecked(self.to_index()) }
    }

    fn backward(self) -> Self::Direction {
        unsafe { DirectedSquareAxis::from_index_unchecked(self.to_index() + 2) }
    }

    fn from_direction(dir: Self::Direction) -> Self {
        let i = dir.dir_to_index();
        unsafe { Self::from_index_unchecked(if i >= 2 { i - 2 } else { i }) }
    }
}

/// Offset for square lattice graph.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct SquareOffset(pub Offset);

impl PartialEq<(usize, usize)> for SquareOffset {
    fn eq(&self, other: &(usize, usize)) -> bool {
        self.0.horizontal == other.0 && self.0.vertical == other.1
    }
}

impl From<(usize, usize)> for SquareOffset {
    fn from(x: (usize, usize)) -> Self {
        SquareOffset(Offset {
            horizontal: x.0,
            vertical: x.1,
        })
    }
}

impl Coordinate for SquareOffset {}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Shape for Square Graph.
pub struct SquareShape<E = Undirected> {
    h: usize,
    v: usize,
    e: PhantomData<E>,
}

impl<E> SquareShape<E> {
    /// Create a new graph.
    pub fn new(h: usize, v: usize) -> Self {
        Self {
            h,
            v,
            e: PhantomData,
        }
    }
}

fn range_check<S: Shape>(s: S, coord: SquareOffset) -> Result<Offset, ()> {
    if coord.0.horizontal < s.horizontal() && coord.0.vertical < s.vertical() {
        Ok(coord.0)
    } else {
        Err(())
    }
}

fn move_coord<S: Shape>(
    s: S,
    coord: SquareOffset,
    dir: DirectedSquareAxis,
) -> Result<SquareOffset, ()> {
    let o = match dir {
        DirectedSquareAxis::X => coord.0.add_x(1).check_x(s.horizontal()),
        DirectedSquareAxis::Y => coord.0.add_y(1).check_y(s.vertical()),
        DirectedSquareAxis::RX => coord.0.sub_x(1),
        DirectedSquareAxis::RY => coord.0.sub_y(1),
    };
    o.map(SquareOffset).ok_or(())
}

impl Shape for SquareShape {
    type Axis = SquareAxis;
    type Coordinate = SquareOffset;
    type OffsetConvertError = ();
    type CoordinateMoveError = ();

    #[inline]
    fn to_offset(&self, coord: Self::Coordinate) -> Result<Offset, ()> {
        range_check(self, coord)
    }

    #[inline]
    unsafe fn to_offset_unchecked(&self, coord: Self::Coordinate) -> Offset {
        coord.0
    }

    #[inline]
    fn offset_to_coordinate(&self, offset: Offset) -> Self::Coordinate {
        SquareOffset(offset)
    }

    #[inline]
    fn horizontal(&self) -> usize {
        self.h
    }

    #[inline]
    fn vertical(&self) -> usize {
        self.v
    }

    fn horizontal_edge_size(&self, axis: Self::Axis) -> usize {
        let h = self.horizontal();
        match axis {
            SquareAxis::X => h - 1,
            SquareAxis::Y => h,
        }
    }

    fn vertical_edge_size(&self, axis: Self::Axis) -> usize {
        let v = self.vertical();
        match axis {
            SquareAxis::X => v,
            SquareAxis::Y => v - 1,
        }
    }

    fn move_coord(&self, coord: SquareOffset, dir: DirectedSquareAxis) -> Result<SquareOffset, ()> {
        move_coord(self, coord, dir)
    }
}

/// Axis for directed square graph.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DirectedSquareAxis {
    X = 0,
    Y = 1,
    RX = 2,
    RY = 3,
}

impl Axis for DirectedSquareAxis {
    const COUNT: usize = 4;
    const DIRECTED: bool = true;
    type Direction = Self;

    fn to_index(&self) -> usize {
        match self {
            DirectedSquareAxis::X => 0,
            DirectedSquareAxis::Y => 1,
            DirectedSquareAxis::RX => 2,
            DirectedSquareAxis::RY => 3,
        }
    }

    fn from_index(index: usize) -> Option<Self> {
        Some(match index {
            0 => DirectedSquareAxis::X,
            1 => DirectedSquareAxis::Y,
            2 => DirectedSquareAxis::RX,
            3 => DirectedSquareAxis::RY,
            _ => return None,
        })
    }

    fn foward(self) -> Self::Direction {
        self
    }

    fn backward(self) -> Self::Direction {
        let x = self.to_index();
        let x2 = if x > 2 { x - 2 } else { x + 2 };
        unsafe { Self::from_index_unchecked(x2) }
    }

    fn from_direction(dir: Self::Direction) -> Self {
        dir
    }
}

impl Shape for SquareShape<petgraph::Directed> {
    type Axis = DirectedSquareAxis;
    type Coordinate = SquareOffset;
    type OffsetConvertError = ();
    type CoordinateMoveError = ();

    fn horizontal(&self) -> usize {
        self.h
    }

    fn vertical(&self) -> usize {
        self.v
    }

    fn to_offset(&self, coord: Self::Coordinate) -> Result<Offset, Self::OffsetConvertError> {
        range_check(self, coord)
    }

    fn offset_to_coordinate(&self, offset: Offset) -> Self::Coordinate {
        SquareOffset(offset)
    }

    fn move_coord(
        &self,
        coord: Self::Coordinate,
        dir: DirectedSquareAxis,
    ) -> Result<Self::Coordinate, Self::CoordinateMoveError> {
        move_coord(self, coord, dir)
    }
}

/// Axis for lattice graph with Square and Diagonal Edge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SquareDiagonalAxis {
    N,
    NE,
    E,
    SE,
}

impl Axis for SquareDiagonalAxis {
    const COUNT: usize = 4;
    const DIRECTED: bool = false;
    const UNDIRECTED_COUNT: usize = if Self::DIRECTED {
        Self::COUNT
    } else {
        Self::COUNT * 2
    };
    type Direction = DirectedSquareDiagonalAxis;

    fn to_index(&self) -> usize {
        match self {
            SquareDiagonalAxis::N => 0,
            SquareDiagonalAxis::NE => 1,
            SquareDiagonalAxis::E => 2,
            SquareDiagonalAxis::SE => 3,
        }
    }

    fn from_index(index: usize) -> Option<Self>
    where
        Self: Sized,
    {
        Some(match index {
            0 => SquareDiagonalAxis::N,
            1 => SquareDiagonalAxis::NE,
            2 => SquareDiagonalAxis::E,
            3 => SquareDiagonalAxis::SE,
            _ => return None,
        })
    }

    fn foward(self) -> Self::Direction {
        unsafe { DirectedSquareDiagonalAxis::from_index_unchecked(self.to_index()) }
    }

    fn backward(self) -> Self::Direction {
        unsafe { DirectedSquareDiagonalAxis::from_index_unchecked(self.to_index() + 4) }
    }

    fn from_direction(dir: Self::Direction) -> Self {
        unsafe {
            let i = dir.to_index();
            Self::from_index_unchecked(if i < 4 { i } else { i - 4 })
        }
    }
}

/// Axis for lattice graph with Square and Diagonal Edge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DirectedSquareDiagonalAxis {
    N,
    NE,
    E,
    SE,
    S,
    SW,
    W,
    NW,
}

impl Axis for DirectedSquareDiagonalAxis {
    const COUNT: usize = 8;
    const DIRECTED: bool = true;
    const UNDIRECTED_COUNT: usize = if Self::DIRECTED {
        Self::COUNT
    } else {
        Self::COUNT * 2
    };
    type Direction = Self;

    fn to_index(&self) -> usize {
        match self {
            DirectedSquareDiagonalAxis::N => 0,
            DirectedSquareDiagonalAxis::NE => 1,
            DirectedSquareDiagonalAxis::E => 2,
            DirectedSquareDiagonalAxis::SE => 3,
            DirectedSquareDiagonalAxis::S => 4,
            DirectedSquareDiagonalAxis::SW => 5,
            DirectedSquareDiagonalAxis::W => 6,
            DirectedSquareDiagonalAxis::NW => 7,
        }
    }

    unsafe fn from_index_unchecked(index: usize) -> Self {
        match index {
            0 => DirectedSquareDiagonalAxis::N,
            1 => DirectedSquareDiagonalAxis::NE,
            2 => DirectedSquareDiagonalAxis::E,
            3 => DirectedSquareDiagonalAxis::SE,
            4 => DirectedSquareDiagonalAxis::S,
            5 => DirectedSquareDiagonalAxis::SW,
            6 => DirectedSquareDiagonalAxis::W,
            7 => DirectedSquareDiagonalAxis::NW,
            _ => core::hint::unreachable_unchecked(),
        }
    }

    fn from_index(index: usize) -> Option<Self> {
        Some(match index {
            0 => DirectedSquareDiagonalAxis::N,
            1 => DirectedSquareDiagonalAxis::NE,
            2 => DirectedSquareDiagonalAxis::E,
            3 => DirectedSquareDiagonalAxis::SE,
            4 => DirectedSquareDiagonalAxis::S,
            5 => DirectedSquareDiagonalAxis::SW,
            6 => DirectedSquareDiagonalAxis::W,
            7 => DirectedSquareDiagonalAxis::NW,
            _ => return None,
        })
    }

    fn foward(self) -> Self::Direction {
        self
    }

    fn backward(self) -> Self::Direction {
        let i = self.to_index();
        unsafe { Self::from_index_unchecked(if i < 4 { i + 4 } else { i - 4 }) }
    }

    fn from_direction(dir: Self::Direction) -> Self {
        dir
    }
}

/// Shape for lattice graph with Square and Diagonal Edge.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct SquareDiagonalShape<E = Undirected> {
    h: usize,
    v: usize,
    e: PhantomData<E>,
}

impl<E> SquareDiagonalShape<E> {
    /// Create a new graph.
    pub fn new(h: usize, v: usize) -> Self {
        Self {
            h,
            v,
            e: PhantomData,
        }
    }
}

impl Shape for SquareDiagonalShape {
    type Axis = SquareDiagonalAxis;
    type Coordinate = SquareOffset;
    type OffsetConvertError = ();
    type CoordinateMoveError = ();

    fn horizontal(&self) -> usize {
        self.h
    }

    fn vertical(&self) -> usize {
        self.v
    }

    fn to_offset(&self, coord: Self::Coordinate) -> Result<Offset, Self::OffsetConvertError> {
        if coord.0.horizontal < self.horizontal() && coord.0.vertical < self.vertical() {
            Ok(coord.0)
        } else {
            Err(())
        }
    }

    unsafe fn to_offset_unchecked(&self, coord: Self::Coordinate) -> Offset {
        coord.0
    }

    fn offset_to_coordinate(&self, offset: Offset) -> Self::Coordinate {
        SquareOffset(offset)
    }

    fn move_coord(
        &self,
        coord: Self::Coordinate,
        dir: DirectedSquareDiagonalAxis,
    ) -> Result<Self::Coordinate, Self::CoordinateMoveError> {
        let offset = coord.0;
        match dir {
            DirectedSquareDiagonalAxis::N => offset.add_y(1).check_y(self.vertical()),
            DirectedSquareDiagonalAxis::NE => offset
                .add_x(1)
                .check_x(self.horizontal())
                .and_then(|o| o.add_y(1).check_y(self.vertical())),
            DirectedSquareDiagonalAxis::E => offset.add_x(1).check_x(self.horizontal()),
            DirectedSquareDiagonalAxis::SE => offset
                .add_x(1)
                .check_x(self.horizontal())
                .and_then(|o| o.sub_y(1)),

            DirectedSquareDiagonalAxis::S => offset.sub_y(1),
            DirectedSquareDiagonalAxis::SW => offset.sub_x(1).and_then(|o| o.sub_y(1)),
            DirectedSquareDiagonalAxis::W => offset.sub_x(1),
            DirectedSquareDiagonalAxis::NW => offset
                .sub_x(1)
                .and_then(|o| o.add_y(1).check_y(self.vertical())),
        }
        .map(SquareOffset)
        .ok_or(())
    }
}

impl Shape for SquareDiagonalShape<Directed> {
    type Axis = DirectedSquareDiagonalAxis;
    type Coordinate = SquareOffset;
    type OffsetConvertError = ();
    type CoordinateMoveError = ();

    fn horizontal(&self) -> usize {
        self.h
    }

    fn vertical(&self) -> usize {
        self.v
    }

    fn to_offset(&self, coord: Self::Coordinate) -> Result<Offset, Self::OffsetConvertError> {
        if coord.0.horizontal < self.horizontal() && coord.0.vertical < self.vertical() {
            Ok(coord.0)
        } else {
            Err(())
        }
    }

    unsafe fn to_offset_unchecked(&self, coord: Self::Coordinate) -> Offset {
        coord.0
    }

    fn offset_to_coordinate(&self, offset: Offset) -> Self::Coordinate {
        SquareOffset(offset)
    }

    fn move_coord(
        &self,
        coord: Self::Coordinate,
        dir: DirectedSquareDiagonalAxis,
    ) -> Result<Self::Coordinate, Self::CoordinateMoveError> {
        let offset = coord.0;
        match dir {
            DirectedSquareDiagonalAxis::N => offset.add_y(1).check_y(self.vertical()),
            DirectedSquareDiagonalAxis::NE => offset
                .add_x(1)
                .check_x(self.horizontal())
                .and_then(|o| o.add_y(1).check_y(self.vertical())),
            DirectedSquareDiagonalAxis::E => offset.add_x(1).check_x(self.horizontal()),
            DirectedSquareDiagonalAxis::SE => offset
                .add_x(1)
                .check_x(self.horizontal())
                .and_then(|o| o.sub_y(1)),

            DirectedSquareDiagonalAxis::S => offset.sub_y(1),
            DirectedSquareDiagonalAxis::SW => offset.sub_x(1).and_then(|o| o.sub_y(1)),
            DirectedSquareDiagonalAxis::W => offset.sub_x(1),
            DirectedSquareDiagonalAxis::NW => offset
                .sub_x(1)
                .and_then(|o| o.add_y(1).check_y(self.vertical())),
        }
        .map(SquareOffset)
        .ok_or(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use petgraph::visit::*;

    type SquareGraph<N, E> = super::SquareGraphAbstract<N, E>;

    #[test]
    fn gen_test() {
        let sq = SquareGraph::new_with(
            SquareShape::new(4, 3),
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             })| x + 2 * y,
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             }),
             _d| (x + 2 * y) as i32,
        );
        assert_eq!(sq.s.horizontal(), 4);
        assert_eq!(sq.s.vertical(), 3);
        assert_eq!(sq.node_weight((0, 0).into()), Some(&0));
        assert_eq!(sq.node_weight((0, 1).into()), Some(&2));
        assert_eq!(sq.node_weight((1, 0).into()), Some(&1));
        assert_eq!(sq.node_weight((2, 0).into()), Some(&2));
        assert_eq!(sq.node_weight((3, 0).into()), Some(&3));
        assert_eq!(sq.node_weight((4, 0).into()), None);
        assert_eq!(sq.node_weight((0, 2).into()), Some(&4));
        assert_eq!(sq.node_weight((0, 3).into()), None);
        assert_eq!(
            sq.edge_weight(((0, 0).into(), SquareAxis::X).into()),
            Some(&0)
        );
        assert_eq!(
            sq.edge_weight(((0, 2).into(), SquareAxis::X).into()),
            Some(&4)
        );
        assert_eq!(sq.edge_weight(((0, 2).into(), SquareAxis::Y)), None);
        assert_eq!(sq.edge_weight(((3, 0).into(), SquareAxis::X)), None);
        assert_eq!(sq.edge_weight(((3, 0).into(), SquareAxis::Y)), Some(&3));
    }

    #[test]
    fn node_identifiers() {
        let sq = SquareGraph::new_with(
            SquareShape::new(4, 3),
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             })| x + 2 * y,
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             }),
             _d| Some((x + 2 * y) as i32),
        );
        let mut count = 0;
        for (i, x) in sq.node_identifiers().enumerate() {
            let x2 = sq.to_index(x);
            assert_eq!(x2, i);
            let x3 = sq.from_index(x2);
            assert_eq!(x, x3);
            count += 1;
        }
        assert_eq!(count, 12);
    }

    #[test]
    fn neighbors() {
        let sq = SquareGraph::new_with(
            SquareShape::new(3, 5),
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             })| x + 2 * y,
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             }),
             _d| Some((x + 2 * y) as i32),
        );

        let v00 = sq.neighbors((0, 0).into());
        debug_assert!(v00.eq([(1, 0), (0, 1)]));

        let v04 = sq.neighbors((0, 4).into());
        debug_assert!(v04.eq([(1, 4), (0, 3)]));

        let v20 = sq.neighbors((2, 0).into());
        debug_assert!(v20.eq([(2, 1), (1, 0)]));

        let v24 = sq.neighbors((2, 4).into());
        debug_assert!(v24.eq([(1, 4), (2, 3)]));

        let v12 = sq.neighbors((1, 2).into());
        debug_assert!(v12.eq([(2, 2), (1, 3), (0, 2), (1, 1)]));
    }

    #[test]
    fn edges() {
        let sq = SquareGraph::new_with(
            SquareShape::new(3, 5),
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             })| x + 2 * y,
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             }),
             _d| (x + 2 * y) as i32,
        );

        debug_assert!(sq
            .edges((0, 0).into())
            .map(|e| e.target())
            .eq([(1, 0), (0, 1)]));

        debug_assert!(sq.edges((0, 0).into()).map(|e| e.edge_weight).eq(&[0, 0]));
        debug_assert!(sq
            .edges((1, 1).into())
            .map(|e| e.edge_weight)
            .eq(&[3, 3, 2, 1]));

        debug_assert!(sq.edges((1, 2).into()).map(|e| e.target()).eq([
            (2, 2),
            (1, 3),
            (0, 2),
            (1, 1)
        ]));
    }

    #[test]
    fn astar() {
        let sq = SquareGraph::new_with(
            SquareShape::new(4, 3),
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             })| x + 2 * y,
            |SquareOffset(Offset {
                 horizontal: x,
                 vertical: y,
             }),
             d| { (x + 2 * y) as i32 * if d == SquareAxis::X { 1 } else { 3 } },
        );

        let x = petgraph::algo::astar(
            &sq,
            (0, 0).into(),
            |x| x == (2, 1),
            |e| *e.weight(),
            |x| (x.0.horizontal as i32 - 2).abs() + (x.0.vertical as i32 - 1).abs(),
        );
        assert!(x.is_some());
        let (d, p) = x.unwrap();
        assert_eq!(d, 5);
        assert_eq!(p, [(0, 0), (0, 1), (1, 1), (2, 1)]);

        let x = petgraph::algo::astar(
            &sq,
            (2, 1).into(),
            |x| x == (0, 0),
            |e| *e.weight(),
            |x| (x.0.horizontal as i32).abs() + (x.0.vertical as i32).abs(),
        );
        assert!(x.is_some());
        let (d, p) = x.unwrap();
        assert_eq!(d, 5);
        assert_eq!(p, [(2, 1), (1, 1), (0, 1), (0, 0)])
    }
}