Cleanup cell id v0 logic

This commit is contained in:
Austen Adler 2023-02-24 00:47:33 -05:00
parent 1527e2a0b5
commit 879ee19a69
2 changed files with 64 additions and 16 deletions

View File

@ -10,6 +10,7 @@ use std::{
ops::{Add, AddAssign}, ops::{Add, AddAssign},
str::FromStr, str::FromStr,
}; };
use v0::UnpackedCellID;
mod conversions; mod conversions;
use thiserror::Error; use thiserror::Error;
@ -172,25 +173,26 @@ impl Address<'_> {
pub fn as_cell_id(&self) -> CellID { pub fn as_cell_id(&self) -> CellID {
match self.version { match self.version {
Version::V0 => { Version::V0 => {
let ten_bits = 0b1111_1111_11; UnpackedCellID::from(self).into()
let thirteen_bits = 0b1111_1111_1111_1; // let ten_bits = 0b1111_1111_11;
let mut ret = 0b0; // let thirteen_bits = 0b1111_1111_1111_1;
// let mut ret = 0b0;
// Add words in reverse order // // Add words in reverse order
for word in self.words.iter().rev() { // for word in self.words.iter().rev() {
ret = (ret << 13) | (word.number as u64 & thirteen_bits); // ret = (ret << 13) | (word.number as u64 & thirteen_bits);
} // }
// Add the number // // Add the number
ret = (ret << 10) | (self.number as u64 & ten_bits); // ret = (ret << 10) | (self.number as u64 & ten_bits);
// Add the final bit // // Add the final bit
ret = (ret << 1) | 0b1; // ret = (ret << 1) | 0b1;
// Shift the whole id left by the number of unused levels * 2 // // Shift the whole id left by the number of unused levels * 2
ret = ret << ((s2::cellid::MAX_LEVEL - CELLID_LEVEL as u64) * 2); // ret = ret << ((s2::cellid::MAX_LEVEL - CELLID_LEVEL as u64) * 2);
CellID(ret) // CellID(ret)
} }
} }
} }
@ -199,6 +201,7 @@ impl Address<'_> {
/// Extracts a version number from a number /// Extracts a version number from a number
/// ///
/// The version number is set by the two bits 11 and 12 /// The version number is set by the two bits 11 and 12
// TODO: impl TryFrom ?
fn extract_version(number: Number) -> Result<Version, Error> { fn extract_version(number: Number) -> Result<Version, Error> {
match ((number >> 10) & 0b11) as u8 { match ((number >> 10) & 0b11) as u8 {
0 => Ok(Version::V0), 0 => Ok(Version::V0),

View File

@ -1,5 +1,5 @@
use crate::{conversions, Address, Version}; use crate::{conversions, Address, Version, CELLID_LEVEL};
use std::ops::RangeInclusive; use std::ops::{Add, RangeInclusive};
use words::NUMBER_TO_WORDS; use words::NUMBER_TO_WORDS;
use s2::{cell::Cell, cellid::CellID}; use s2::{cell::Cell, cellid::CellID};
@ -43,6 +43,51 @@ impl From<UnpackedCellID> for Address<'_> {
} }
} }
impl From<&Address<'_>> for UnpackedCellID {
fn from(value: &Address) -> Self {
Self {
number_bits: value.number as u16,
word0_bits: value.words[0].number as u16,
word1_bits: value.words[1].number as u16,
word2_bits: value.words[2].number as u16,
}
}
}
impl From<UnpackedCellID> for u64 {
fn from(value: UnpackedCellID) -> Self {
let ten_bits = 0b1111_1111_11;
let thirteen_bits = 0b1111_1111_1111_1;
let mut ret = 0b0;
// Add words in reverse order
ret = (ret << 13) | (value.word2_bits as u64 & thirteen_bits);
ret = (ret << 13) | (value.word1_bits as u64 & thirteen_bits);
ret = (ret << 13) | (value.word0_bits as u64 & thirteen_bits);
// for word in self.words.iter().rev() {
// ret = (ret << 13) | (word.number as u64 & thirteen_bits);
// }
// Add the number
ret = (ret << 10) | (value.number_bits as u64 & ten_bits);
// Add the final bit
ret = (ret << 1) | 0b1;
// Shift the whole id left by the number of unused levels * 2
ret = ret << ((s2::cellid::MAX_LEVEL - CELLID_LEVEL as u64) * 2);
ret
}
}
impl From<UnpackedCellID> for CellID {
fn from(value: UnpackedCellID) -> Self {
CellID(value.into())
}
}
// impl<A: AsRef<Address<'a>>> From<A> for UnpackedCellID { // impl<A: AsRef<Address<'a>>> From<A> for UnpackedCellID {
// fn from(addr: A) -> Self { // fn from(addr: A) -> Self {
// let number_bits = addr. // let number_bits = addr.