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
// Copyright (C) 2022 Leandro Lisboa Penz <lpenz@lpenz.org>
// This file is subject to the terms and conditions defined in
// file 'LICENSE', which is part of this source code package.

#![warn(missing_debug_implementations)]
#![warn(missing_docs)]

//! Module that abstracts sets of [`Pos`] values

use std::collections;

use super::gridbool::Gridbool;
use super::pos::Pos;

/* SetPos */

/// Trait that abstracts sets of [`Pos`] values
pub trait SetPos<const W: u16, const H: u16, const WORDS: usize, const SIZE: usize> {
    /// Check if the provided [`Pos`] is in the set
    fn contains(&self, pos: &Pos<W, H>) -> bool;
    /// Insert the provided [`Pos`]
    fn insert(&mut self, pos: &Pos<W, H>);
    /// Remove the provided [`Pos`]
    fn remove(&mut self, pos: &Pos<W, H>);
    /// Insert or remove the provided [`Pos`]
    fn set(&mut self, pos: &Pos<W, H>, add: bool) {
        if add {
            self.insert(pos);
        } else {
            self.remove(pos);
        }
    }
}

impl<const W: u16, const H: u16, const WORDS: usize, const SIZE: usize> SetPos<W, H, WORDS, SIZE>
    for Gridbool<W, H, WORDS>
{
    fn contains(&self, pos: &Pos<W, H>) -> bool {
        self.get(pos)
    }
    fn insert(&mut self, pos: &Pos<W, H>) {
        self.set_t(pos)
    }
    fn remove(&mut self, pos: &Pos<W, H>) {
        self.set_f(pos)
    }
}

impl<const W: u16, const H: u16, const WORDS: usize, const SIZE: usize> SetPos<W, H, WORDS, SIZE>
    for collections::HashSet<Pos<W, H>>
{
    fn contains(&self, pos: &Pos<W, H>) -> bool {
        self.contains(pos)
    }
    fn insert(&mut self, pos: &Pos<W, H>) {
        self.insert(*pos);
    }
    fn remove(&mut self, pos: &Pos<W, H>) {
        self.remove(pos);
    }
}

impl<const W: u16, const H: u16, const WORDS: usize, const SIZE: usize> SetPos<W, H, WORDS, SIZE>
    for collections::BTreeSet<Pos<W, H>>
{
    fn contains(&self, pos: &Pos<W, H>) -> bool {
        self.contains(pos)
    }
    fn insert(&mut self, pos: &Pos<W, H>) {
        self.insert(*pos);
    }
    fn remove(&mut self, pos: &Pos<W, H>) {
        self.remove(pos);
    }
}