firewheel_core/
connected_mask.rs

1/// An optional hint on which channels are connected to other
2/// nodes in the graph. A bit set to `1` means that channel
3/// is connected to another node, and a bit set to `0` means
4/// that channel is not connected to any node.
5///
6/// The first bit (`0x1`) is the first channel, the second bit
7/// is the second channel, and so on.
8#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct ConnectedMask(pub u64);
10
11impl ConnectedMask {
12    pub const MONO_CONNECTED: Self = Self(0b1);
13    pub const STEREO_CONNECTED: Self = Self(0b11);
14
15    /// Returns `true` if the channel is connected to another node,
16    /// `false` otherwise.
17    ///
18    /// `i` must be less than `64`.
19    pub const fn is_channel_connected(&self, i: usize) -> bool {
20        self.0 & (0b1 << i) != 0
21    }
22
23    /// Returns `true` if all channels are marked as connected, `false`
24    /// otherwise.
25    ///
26    /// `num_channels` must be less than or equal to `64`.
27    pub const fn all_channels_connected(&self, num_channels: usize) -> bool {
28        if num_channels >= 64 {
29            self.0 == u64::MAX
30        } else {
31            let mask = (0b1 << num_channels) - 1;
32            self.0 & mask == mask
33        }
34    }
35
36    /// Mark/un-mark the given channel as connected.
37    ///
38    /// `i` must be less than `64`.
39    pub fn set_channel(&mut self, i: usize, connected: bool) {
40        if connected {
41            self.0 |= 0b1 << i;
42        } else {
43            self.0 &= !(0b1 << i);
44        }
45    }
46}