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
use crate::def::*; #[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord)] pub struct Bset(pub u128, pub u128); impl Debug for Bset { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { ll(*self).fmt(f) } } fn ll(set: Bset) -> Vec<(u8, u8)> { let mut sink = vec![]; for i in set.get() { if let Some((_, b)) = sink.last_mut() { if *b == i - 1 { *b += 1; continue; } } sink.push((i, i)); } sink } fn tolist(i: u128, offset: u8, sink: &mut Vec<(u8, u8)>) { for j in 0..128 { if 0 != ((i >> j) & 1) { if let Some((_, b)) = sink.last_mut() { if *b == (j + offset) - 1 { *b += 1; continue; } } sink.push((j, j)); } } } pub fn brange(a: u128, b: u128) -> u128 { let hi = !0 >> (127 - b); let lo = (1 << a) - 1; hi ^ lo } impl Bset { pub fn getb(self) -> [bool; 256] { let mut e = [false; 256]; for (o, i) in [self.0, self.1].iter().enumerate() { let o = o * 128; for j in 0..128 { e[(j + o)] = 0 != ((i >> j) & 1); } } e } pub fn pairs(self) -> Vec<(u8, u8)> { let mut e = vec![]; tolist(self.0, 000, &mut e); tolist(self.1, 128, &mut e); e } pub fn get(self) -> Vec<u8> { self.getb() .iter() .enumerate() .filter(|x| *x.1) .map(|x| x.0 as u8) .collect() } pub fn range(a: char, b: char) -> Bset { Bset::brange(a as u8, b as u8) } pub fn brange(a: u8, b: u8) -> Bset { let a = a as u128; let b = b as u128; let llo = if a < 128 { brange(a, b.min(127)) } else { 0 }; let hhi = if b >= 128 { brange(a.max(128) - 128, b.max(128) - 128) } else { 0 }; Bset(llo, hhi) } pub fn fromchar(a: char) -> Bset { Bset::range(a, a) } pub fn frombyte(a: u8) -> Bset { Bset::brange(a, a) } pub fn str(s: &str) -> Bset { s.as_bytes() .iter() .fold(Bset(0, 0), |a, b| a | Bset::frombyte(*b)) } } impl Not for Bset { type Output = Bset; fn not(self) -> Bset { let l = self; Bset(!l.0, !l.1) } } impl BitOr for Bset { type Output = Self; fn bitor(self, r: Bset) -> Bset { let l = self; Bset(l.0 | r.0, l.1 | r.1) } } impl BitOrAssign for Bset { fn bitor_assign(&mut self, r: Bset) { *self = *self | r; } } impl BitAnd for Bset { type Output = Self; fn bitand(self, r: Bset) -> Bset { let l = self; Bset(l.0 & r.0, l.1 & r.1) } } impl BitXor for Bset { type Output = Self; fn bitxor(self, r: Bset) -> Bset { let l = self; Bset(l.0 ^ r.0, l.1 ^ r.1) } }