16994f6b1SLoGin use core::ops::{BitAnd, BitAndAssign, BitOrAssign, Not}; 26994f6b1SLoGin 36994f6b1SLoGin /// A trait that defines generalised operations on a `Bits::Store` type. 46994f6b1SLoGin pub trait BitOps: 56994f6b1SLoGin BitAndAssign + Sized + Copy + PartialEq + Not + BitOrAssign + BitOrAssign + BitAnd 66994f6b1SLoGin { 76994f6b1SLoGin fn get(bits: &Self, index: usize) -> bool; 86994f6b1SLoGin fn set(bits: &mut Self, index: usize, value: bool) -> bool; 96994f6b1SLoGin fn set_value(bits: &mut Self, value: Self); 106994f6b1SLoGin fn len(bits: &Self) -> usize; 116994f6b1SLoGin fn first_index(bits: &Self) -> Option<usize>; 126994f6b1SLoGin fn first_false_index(bits: &Self) -> Option<usize>; 136994f6b1SLoGin fn last_index(bits: &Self) -> Option<usize>; 146994f6b1SLoGin fn last_false_index(bits: &Self) -> Option<usize>; 156994f6b1SLoGin fn next_index(bits: &Self, index: usize) -> Option<usize>; 166994f6b1SLoGin fn next_false_index(bits: &Self, index: usize) -> Option<usize>; 176994f6b1SLoGin fn prev_index(bits: &Self, index: usize) -> Option<usize>; 186994f6b1SLoGin fn prev_false_index(bits: &Self, index: usize) -> Option<usize>; 196994f6b1SLoGin fn bit_and(bits: &mut Self, other_bits: &Self); 206994f6b1SLoGin fn bit_or(bits: &mut Self, other_bits: &Self); 216994f6b1SLoGin fn bit_xor(bits: &mut Self, other_bits: &Self); 226994f6b1SLoGin fn invert(bits: &mut Self); 236994f6b1SLoGin fn make_mask(shift: usize) -> Self; 246994f6b1SLoGin fn bit_size() -> usize; 256994f6b1SLoGin fn zero() -> Self; 266994f6b1SLoGin fn max() -> Self; 276994f6b1SLoGin } 286994f6b1SLoGin 296994f6b1SLoGin macro_rules! bitops_for { 306994f6b1SLoGin ($target:ty) => { 316994f6b1SLoGin impl BitOps for $target { 326994f6b1SLoGin #[inline] 336994f6b1SLoGin fn get(bits: &Self, index: usize) -> bool { 346994f6b1SLoGin bits & (1 << index) != 0 356994f6b1SLoGin } 366994f6b1SLoGin 376994f6b1SLoGin #[inline] 386994f6b1SLoGin fn set(bits: &mut Self, index: usize, value: bool) -> bool { 396994f6b1SLoGin let mask = 1 << index; 406994f6b1SLoGin let prev = *bits & mask; 416994f6b1SLoGin if value { 426994f6b1SLoGin *bits |= mask; 436994f6b1SLoGin } else { 446994f6b1SLoGin *bits &= !mask; 456994f6b1SLoGin } 466994f6b1SLoGin prev != 0 476994f6b1SLoGin } 486994f6b1SLoGin 496994f6b1SLoGin #[inline] 506994f6b1SLoGin fn set_value(bits: &mut Self, value: Self) { 516994f6b1SLoGin *bits = value; 526994f6b1SLoGin } 536994f6b1SLoGin 546994f6b1SLoGin #[inline] 556994f6b1SLoGin fn len(bits: &Self) -> usize { 566994f6b1SLoGin bits.count_ones() as usize 576994f6b1SLoGin } 586994f6b1SLoGin 596994f6b1SLoGin #[inline] 606994f6b1SLoGin fn first_index(bits: &Self) -> Option<usize> { 616994f6b1SLoGin if *bits == 0 { 626994f6b1SLoGin None 636994f6b1SLoGin } else { 646994f6b1SLoGin Some(bits.trailing_zeros() as usize) 656994f6b1SLoGin } 666994f6b1SLoGin } 676994f6b1SLoGin 686994f6b1SLoGin #[inline] 696994f6b1SLoGin fn first_false_index(bits: &Self) -> Option<usize> { 706994f6b1SLoGin if *bits == <$target>::MAX { 716994f6b1SLoGin None 726994f6b1SLoGin } else { 736994f6b1SLoGin Some(bits.trailing_ones() as usize) 746994f6b1SLoGin } 756994f6b1SLoGin } 766994f6b1SLoGin 776994f6b1SLoGin #[inline] 786994f6b1SLoGin fn last_index(bits: &Self) -> Option<usize> { 796994f6b1SLoGin if *bits == 0 { 806994f6b1SLoGin None 816994f6b1SLoGin } else { 826994f6b1SLoGin Some(<$target>::BITS as usize - 1 - (bits.leading_zeros() as usize)) 836994f6b1SLoGin } 846994f6b1SLoGin } 856994f6b1SLoGin 866994f6b1SLoGin #[inline] 876994f6b1SLoGin fn last_false_index(bits: &Self) -> Option<usize> { 886994f6b1SLoGin if *bits == <$target>::MAX { 896994f6b1SLoGin None 906994f6b1SLoGin } else { 916994f6b1SLoGin Some(<$target>::BITS as usize - 1 - bits.leading_ones() as usize) 926994f6b1SLoGin } 936994f6b1SLoGin } 946994f6b1SLoGin 956994f6b1SLoGin #[inline] 966994f6b1SLoGin fn next_index(bits: &Self, index: usize) -> Option<usize> { 976994f6b1SLoGin if *bits == 0 || index >= <$target>::BITS as usize - 1 { 986994f6b1SLoGin None 996994f6b1SLoGin } else { 1006994f6b1SLoGin let intermediate = 1016994f6b1SLoGin (*bits & (<$target>::MAX.overflowing_shl(1 + index as u32).0)); 1026994f6b1SLoGin 1036994f6b1SLoGin if intermediate == 0 { 1046994f6b1SLoGin None 1056994f6b1SLoGin } else { 1066994f6b1SLoGin Some(intermediate.trailing_zeros() as usize) 1076994f6b1SLoGin } 1086994f6b1SLoGin } 1096994f6b1SLoGin } 1106994f6b1SLoGin 1116994f6b1SLoGin #[inline] 1126994f6b1SLoGin fn next_false_index(bits: &Self, index: usize) -> Option<usize> { 1136994f6b1SLoGin if *bits == <$target>::MAX || index >= <$target>::BITS as usize - 1 { 1146994f6b1SLoGin None 1156994f6b1SLoGin } else { 1166994f6b1SLoGin let intermediate = (*bits | ((1 << (index + 1)) - 1)); 1176994f6b1SLoGin 1186994f6b1SLoGin if intermediate == <$target>::MAX { 1196994f6b1SLoGin None 1206994f6b1SLoGin } else { 1216994f6b1SLoGin Some(intermediate.trailing_ones() as usize) 1226994f6b1SLoGin } 1236994f6b1SLoGin } 1246994f6b1SLoGin } 1256994f6b1SLoGin 1266994f6b1SLoGin #[inline] 1276994f6b1SLoGin fn prev_index(bits: &Self, index: usize) -> Option<usize> { 1286994f6b1SLoGin if *bits == 0 || index == 0 { 1296994f6b1SLoGin None 1306994f6b1SLoGin } else { 1316994f6b1SLoGin let intermediate = bits & ((1 << index) - 1); 1326994f6b1SLoGin 1336994f6b1SLoGin if intermediate == 0 { 1346994f6b1SLoGin None 1356994f6b1SLoGin } else { 1366994f6b1SLoGin Some(<$target>::BITS as usize - 1 - (intermediate.leading_zeros() as usize)) 1376994f6b1SLoGin } 1386994f6b1SLoGin } 1396994f6b1SLoGin } 1406994f6b1SLoGin 1416994f6b1SLoGin #[inline] 1426994f6b1SLoGin fn prev_false_index(bits: &Self, index: usize) -> Option<usize> { 1436994f6b1SLoGin if *bits == <$target>::MAX || index == 0 { 1446994f6b1SLoGin None 1456994f6b1SLoGin } else { 1466994f6b1SLoGin let intermediate = bits | (<$target>::MAX.overflowing_shl(index as u32).0); 1476994f6b1SLoGin 1486994f6b1SLoGin if intermediate == <$target>::MAX { 1496994f6b1SLoGin None 1506994f6b1SLoGin } else { 1516994f6b1SLoGin Some(<$target>::BITS as usize - 1 - (intermediate.leading_ones() as usize)) 1526994f6b1SLoGin } 1536994f6b1SLoGin } 1546994f6b1SLoGin } 1556994f6b1SLoGin 1566994f6b1SLoGin #[inline] 1576994f6b1SLoGin fn bit_and(bits: &mut Self, other_bits: &Self) { 1586994f6b1SLoGin *bits &= *other_bits; 1596994f6b1SLoGin } 1606994f6b1SLoGin 1616994f6b1SLoGin #[inline] 1626994f6b1SLoGin fn bit_or(bits: &mut Self, other_bits: &Self) { 1636994f6b1SLoGin *bits |= *other_bits; 1646994f6b1SLoGin } 1656994f6b1SLoGin 1666994f6b1SLoGin #[inline] 1676994f6b1SLoGin fn bit_xor(bits: &mut Self, other_bits: &Self) { 1686994f6b1SLoGin *bits ^= *other_bits; 1696994f6b1SLoGin } 1706994f6b1SLoGin 1716994f6b1SLoGin #[inline] 1726994f6b1SLoGin fn invert(bits: &mut Self) { 1736994f6b1SLoGin *bits = !*bits; 1746994f6b1SLoGin } 1756994f6b1SLoGin 1766994f6b1SLoGin #[inline] 1776994f6b1SLoGin fn make_mask(shift: usize) -> Self { 1786994f6b1SLoGin if shift == <$target>::BITS as usize { 1796994f6b1SLoGin <$target>::MAX 1806994f6b1SLoGin } else { 1816994f6b1SLoGin (1 << shift) - 1 1826994f6b1SLoGin } 1836994f6b1SLoGin } 1846994f6b1SLoGin 1856994f6b1SLoGin #[cfg(feature = "std")] 1866994f6b1SLoGin fn to_hex(bits: &Self) -> String { 1876994f6b1SLoGin format!("{:x}", bits) 1886994f6b1SLoGin } 1896994f6b1SLoGin 1906994f6b1SLoGin #[inline] 1916994f6b1SLoGin fn bit_size() -> usize { 1926994f6b1SLoGin <$target>::BITS as usize 1936994f6b1SLoGin } 1946994f6b1SLoGin 1956994f6b1SLoGin #[inline] 1966994f6b1SLoGin fn zero() -> Self { 1976994f6b1SLoGin 0 1986994f6b1SLoGin } 1996994f6b1SLoGin 2006994f6b1SLoGin #[inline] 2016994f6b1SLoGin fn max() -> Self { 2026994f6b1SLoGin <$target>::MAX 2036994f6b1SLoGin } 2046994f6b1SLoGin } 2056994f6b1SLoGin }; 2066994f6b1SLoGin } 2076994f6b1SLoGin 2086994f6b1SLoGin // 为 `u8` 、 `u16` 、 `u32` 和 `u64` 实现 `BitOps` trait 2096994f6b1SLoGin bitops_for!(u8); 2106994f6b1SLoGin bitops_for!(u16); 2116994f6b1SLoGin bitops_for!(u32); 2126994f6b1SLoGin bitops_for!(u64); 2136994f6b1SLoGin bitops_for!(usize); 2146994f6b1SLoGin 2156994f6b1SLoGin /// Bitmap应当实现的trait 2166994f6b1SLoGin pub trait BitMapOps<T: BitOps> { 2176994f6b1SLoGin /// 获取指定index的位 2186994f6b1SLoGin /// 2196994f6b1SLoGin /// ## 返回 2206994f6b1SLoGin /// 2216994f6b1SLoGin /// - `Some(true)` - 该位为1 2226994f6b1SLoGin /// - `Some(false)` - 该位为0 2236994f6b1SLoGin /// - `None` - index超出范围 2246994f6b1SLoGin fn get(&self, index: usize) -> Option<bool>; 2256994f6b1SLoGin 2266994f6b1SLoGin /// 设置指定index的位,并返回该位之前的值 2276994f6b1SLoGin /// 2286994f6b1SLoGin /// ## 参数 2296994f6b1SLoGin /// 2306994f6b1SLoGin /// - `index` - 位的index 2316994f6b1SLoGin /// - `value` - 位的新值 2326994f6b1SLoGin /// 2336994f6b1SLoGin /// ## 返回 2346994f6b1SLoGin /// 2356994f6b1SLoGin /// - `Some(true)` - 该位之前为1 2366994f6b1SLoGin /// - `Some(false)` - 该位之前为0 2376994f6b1SLoGin /// - `None` - index超出范围 2386994f6b1SLoGin fn set(&mut self, index: usize, value: bool) -> Option<bool>; 2396994f6b1SLoGin 2406994f6b1SLoGin /// 将所有位设置为指定值 2416994f6b1SLoGin fn set_all(&mut self, value: bool); 2426994f6b1SLoGin 2436994f6b1SLoGin /// 获取bitmap的长度(以位为单位) 2446994f6b1SLoGin /// 2456994f6b1SLoGin /// ## Example 2466994f6b1SLoGin /// 2476994f6b1SLoGin /// ``` 2486994f6b1SLoGin /// use bitmap::StaticBitmap; 2496994f6b1SLoGin /// use bitmap::traits::BitMapOps; 2506994f6b1SLoGin /// 2516994f6b1SLoGin /// let mut bitmap = StaticBitmap::<34>::new(); 2526994f6b1SLoGin /// assert_eq!(bitmap.len(), 34); 2536994f6b1SLoGin /// ``` 2546994f6b1SLoGin /// 2556994f6b1SLoGin fn len(&self) -> usize; 2566994f6b1SLoGin /// 获取bitmap的大小(以字节为单位) 2576994f6b1SLoGin fn size(&self) -> usize; 2586994f6b1SLoGin 2596994f6b1SLoGin /// 获取第一个为1的位的index 2606994f6b1SLoGin /// 2616994f6b1SLoGin /// ## 返回 2626994f6b1SLoGin /// 2636994f6b1SLoGin /// - `Some(index)` - 第一个为1的位的index 2646994f6b1SLoGin /// - `None` - 不存在为1的位 2656994f6b1SLoGin fn first_index(&self) -> Option<usize>; 2666994f6b1SLoGin 2676994f6b1SLoGin /// 获取第一个为0的位的index 2686994f6b1SLoGin /// 2696994f6b1SLoGin /// ## 返回 2706994f6b1SLoGin /// 2716994f6b1SLoGin /// - `Some(index)` - 第一个为0的位的index 2726994f6b1SLoGin /// - `None` - 不存在为0的位 2736994f6b1SLoGin fn first_false_index(&self) -> Option<usize>; 2746994f6b1SLoGin 2756994f6b1SLoGin /// 获取最后一个为1的位的index 2766994f6b1SLoGin /// 2776994f6b1SLoGin /// ## 返回 2786994f6b1SLoGin /// 2796994f6b1SLoGin /// - `Some(index)` - 最后一个为1的位的index 2806994f6b1SLoGin /// - `None` - 不存在为1的位 2816994f6b1SLoGin fn last_index(&self) -> Option<usize>; 2826994f6b1SLoGin 2836994f6b1SLoGin /// 获取最后一个为0的位的index 2846994f6b1SLoGin /// 2856994f6b1SLoGin /// ## 返回 2866994f6b1SLoGin /// 2876994f6b1SLoGin /// - `Some(index)` - 最后一个为0的位的index 2886994f6b1SLoGin /// - `None` - 不存在为0的位 2896994f6b1SLoGin fn last_false_index(&self) -> Option<usize>; 2906994f6b1SLoGin 2916994f6b1SLoGin /// 获取指定index之后第一个为1的位的index 2926994f6b1SLoGin fn next_index(&self, index: usize) -> Option<usize>; 2936994f6b1SLoGin 2946994f6b1SLoGin /// 获取指定index之后第一个为0的位的index 2956994f6b1SLoGin fn next_false_index(&self, index: usize) -> Option<usize>; 2966994f6b1SLoGin 2976994f6b1SLoGin /// 获取指定index之前第一个为1的位的index 2986994f6b1SLoGin fn prev_index(&self, index: usize) -> Option<usize>; 2996994f6b1SLoGin 3006994f6b1SLoGin /// 获取指定index之前第一个为0的位的index 3016994f6b1SLoGin fn prev_false_index(&self, index: usize) -> Option<usize>; 3026994f6b1SLoGin 3036994f6b1SLoGin /// 反转bitmap 3046994f6b1SLoGin fn invert(&mut self); 3056994f6b1SLoGin 3066994f6b1SLoGin /// 判断bitmap是否满了 3076994f6b1SLoGin fn is_full(&self) -> bool; 3086994f6b1SLoGin 3096994f6b1SLoGin /// 判断bitmap是否为空 3106994f6b1SLoGin fn is_empty(&self) -> bool; 3116994f6b1SLoGin 312*b5b571e0SLoGin /// # Safety 313*b5b571e0SLoGin /// *不应直接修改字节数组* 314*b5b571e0SLoGin /// 3156994f6b1SLoGin /// 将bitmap转换为字节数组 3166994f6b1SLoGin unsafe fn as_bytes(&self) -> &[u8]; 3176994f6b1SLoGin } 318