sqrid/sqrid/
setpos.rs

1// Copyright (C) 2022 Leandro Lisboa Penz <lpenz@lpenz.org>
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4
5#![warn(missing_debug_implementations)]
6#![warn(missing_docs)]
7
8//! Module that abstracts sets of [`super::pos::Pos`] values
9
10use std::collections;
11
12use super::gridbool::Gridbool;
13use super::postrait::PosT;
14
15/* SetPos */
16
17/// Trait that abstracts sets of [`super::pos::Pos`] values
18pub trait SetPos<P: PosT, const WORDS: usize, const SIZE: usize> {
19    /// Check if the provided [`super::pos::Pos`] is in the set
20    fn contains(&self, pos: &P) -> bool;
21    /// Insert the provided [`super::pos::Pos`]
22    fn insert(&mut self, pos: P);
23    /// Remove the provided [`super::pos::Pos`]
24    fn remove(&mut self, pos: &P);
25    /// Insert or remove the provided [`super::pos::Pos`]
26    fn set(&mut self, pos: P, add: bool) {
27        if add {
28            self.insert(pos);
29        } else {
30            self.remove(&pos);
31        }
32    }
33}
34
35impl<P: PosT, const WORDS: usize, const SIZE: usize> SetPos<P, WORDS, SIZE> for Gridbool<P, WORDS> {
36    fn contains(&self, pos: &P) -> bool {
37        self.get(pos)
38    }
39    fn insert(&mut self, pos: P) {
40        self.set_t(&pos)
41    }
42    fn remove(&mut self, pos: &P) {
43        self.set_f(pos)
44    }
45}
46
47impl<P: PosT, const WORDS: usize, const SIZE: usize> SetPos<P, WORDS, SIZE>
48    for collections::HashSet<P>
49where
50    P: Eq + std::hash::Hash,
51{
52    fn contains(&self, pos: &P) -> bool {
53        self.contains(pos)
54    }
55    fn insert(&mut self, pos: P) {
56        self.insert(pos);
57    }
58    fn remove(&mut self, pos: &P) {
59        self.remove(pos);
60    }
61}
62
63impl<P: PosT, const WORDS: usize, const SIZE: usize> SetPos<P, WORDS, SIZE>
64    for collections::BTreeSet<P>
65where
66    P: Ord,
67{
68    fn contains(&self, pos: &P) -> bool {
69        self.contains(pos)
70    }
71    fn insert(&mut self, pos: P) {
72        self.insert(pos);
73    }
74    fn remove(&mut self, pos: &P) {
75        self.remove(pos);
76    }
77}