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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
use std::{ fmt::Debug, ops::{BitAnd, BitOr, BitXor, Shl, Shr}, }; #[cfg(feature = "serialize")] use serde::{Deserialize, Serialize}; #[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))] #[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Debug, Hash)] pub struct LocCode<T>(pub T) where T: Copy + Debug; impl<T> From<u8> for LocCode<T> where T: From<u8> + Copy + Debug, { fn from(val: u8) -> Self { Self(val.into()) } } impl<T> From<u16> for LocCode<T> where T: From<u16> + Copy + Debug, { fn from(val: u16) -> Self { Self(val.into()) } } impl<T> From<u32> for LocCode<T> where T: From<u32> + Copy + Debug, { fn from(val: u32) -> Self { Self(val.into()) } } impl<T> From<u64> for LocCode<T> where T: From<u64> + Copy + Debug, { fn from(val: u64) -> Self { Self(val.into()) } } impl<T, D> Shl<D> for LocCode<T> where T: Shl<Output = T> + From<D> + Copy + Debug, { type Output = Self; fn shl(self, rhs: D) -> Self::Output { let LocCode(lhs) = self; LocCode(lhs << rhs.into()) } } impl<T, D> Shr<D> for LocCode<T> where T: Shr<Output = T> + From<D> + Copy + Debug, { type Output = Self; fn shr(self, rhs: D) -> Self::Output { let LocCode(lhs) = self; LocCode(lhs >> rhs.into()) } } impl<T, D> BitXor<D> for LocCode<T> where T: BitXor<Output = T> + From<D> + Copy + Debug, { type Output = Self; fn bitxor(self, rhs: D) -> Self::Output { let LocCode(lhs) = self; LocCode(lhs ^ rhs.into()) } } impl<T, D> BitAnd<D> for LocCode<T> where T: BitAnd<Output = T> + From<D> + Copy + Debug, { type Output = Self; fn bitand(self, rhs: D) -> Self::Output { let LocCode(lhs) = self; LocCode(lhs & rhs.into()) } } impl<T, D> BitOr<D> for LocCode<T> where T: BitOr<Output = T> + From<D> + Copy + Debug, { type Output = Self; fn bitor(self, rhs: D) -> Self::Output { let LocCode(lhs) = self; LocCode(lhs | rhs.into()) } } impl<T> LocCode<T> where T: Eq + From<u8> + Shr<Output = T> + Copy + Debug, { pub fn get_level(&self) -> u32 { let mut temp = *self; let mut level = 1; while temp != 1.into() { temp = temp >> 3; level += 1; } level } } impl<T> LocCode<T> where T: Eq + From<u8> + From<u64> + Shr<Output = T> + Shl<Output = T> + BitXor<Output = T> + BitAnd<Output = T> + Copy + Debug, { pub fn get_center_u32(self) -> (u32, u32, u32) { if self == LocCode(1u8.into()) { return ((2 as u32).pow(31), (2 as u32).pow(31), (2 as u32).pow(31)); } let offset = (2 as u32).pow(32 - self.get_level()); let mask = u64::max_value() ^ 7u64; let center = (self >> 3u8).get_center_u32(); let value = (self ^ mask) & !mask; if value == 0u8.into() { (center.0 - offset, center.1 + offset, center.2 - offset) } else if value == 1u8.into() { (center.0 - offset, center.1 + offset, center.2 + offset) } else if value == 2u8.into() { (center.0 - offset, center.1 - offset, center.2 + offset) } else if value == 3u8.into() { (center.0 - offset, center.1 - offset, center.2 - offset) } else if value == 4u8.into() { (center.0 + offset, center.1 - offset, center.2 - offset) } else if value == 5u8.into() { (center.0 + offset, center.1 - offset, center.2 + offset) } else if value == 6u8.into() { (center.0 + offset, center.1 + offset, center.2 + offset) } else { (center.0 + offset, center.1 + offset, center.2 - offset) } } }