Skip to content

Commit

Permalink
impl TryFrom<i64> for Cell
Browse files Browse the repository at this point in the history
JayKickliter committed Apr 22, 2024

Verified

This commit was signed with the committer’s verified signature.
JayKickliter Jay Kickliter
1 parent e4c40f8 commit 784e7f1
Showing 3 changed files with 35 additions and 29 deletions.
10 changes: 9 additions & 1 deletion src/cell.rs
Original file line number Diff line number Diff line change
@@ -225,6 +225,14 @@ impl TryFrom<u64> for Cell {
}
}

impl TryFrom<i64> for Cell {
type Error = Error;

fn try_from(raw: i64) -> Result<Cell> {
Cell::from_raw(raw as u64)
}
}

/// A type for building up Cells in an iterative matter when
/// tree-walking.
pub(crate) struct CellStack(Option<Cell>);
@@ -351,7 +359,7 @@ mod tests {

#[test]
fn test_cell_to_parent() {
let cell = Cell::try_from(0x85283473fffffff).unwrap();
let cell = Cell::from_raw(0x85283473fffffff).unwrap();
let parent = cell.to_parent(cell.res()).unwrap();
assert_eq!(cell, parent);
let parent = cell.to_parent(4).unwrap();
37 changes: 18 additions & 19 deletions src/iteration.rs
Original file line number Diff line number Diff line change
@@ -210,19 +210,18 @@ mod tests {
geom::{ContainmentMode, PolyfillConfig, Polygon, ToCells},
CellIndex, Resolution,
};
use std::convert::TryFrom;

#[test]
fn test_visit() {
let parent = Cell::try_from(0x825997fffffffff).unwrap();
let parent = Cell::from_raw(0x825997fffffffff).unwrap();
let children = [
Cell::try_from(0x835990fffffffff).unwrap(),
Cell::try_from(0x835991fffffffff).unwrap(),
Cell::try_from(0x835992fffffffff).unwrap(),
Cell::try_from(0x835993fffffffff).unwrap(),
Cell::try_from(0x835994fffffffff).unwrap(),
Cell::try_from(0x835995fffffffff).unwrap(),
Cell::try_from(0x835996fffffffff).unwrap(),
Cell::from_raw(0x835990fffffffff).unwrap(),
Cell::from_raw(0x835991fffffffff).unwrap(),
Cell::from_raw(0x835992fffffffff).unwrap(),
Cell::from_raw(0x835993fffffffff).unwrap(),
Cell::from_raw(0x835994fffffffff).unwrap(),
Cell::from_raw(0x835995fffffffff).unwrap(),
Cell::from_raw(0x835996fffffffff).unwrap(),
];

let hexmap: HexTreeMap<Cell> = children.iter().map(|cell| (cell, cell)).collect();
@@ -243,7 +242,7 @@ mod tests {
let mut map = HexTreeMap::new();
for cell in COMPACT_US915_INDICES
.iter()
.map(|&idx| Cell::try_from(idx).unwrap())
.map(|&idx| Cell::from_raw(idx).unwrap())
{
map.insert(cell, cell);
}
@@ -262,7 +261,7 @@ mod tests {
let mut map = HexTreeMap::new();
for cell in COMPACT_US915_INDICES
.iter()
.map(|&idx| Cell::try_from(idx).unwrap())
.map(|&idx| Cell::from_raw(idx).unwrap())
{
map.insert(cell, cell);
}
@@ -283,7 +282,7 @@ mod tests {
let mut cell_value_pairs: Vec<(Cell, i32)> = Vec::new();
let mut count = 0;
while let Ok(idx) = rdr.read_u64::<LE>() {
cell_value_pairs.push((Cell::try_from(idx).unwrap(), count));
cell_value_pairs.push((Cell::from_raw(idx).unwrap(), count));
count += 1;
}
cell_value_pairs
@@ -367,24 +366,24 @@ mod tests {
eiffel_tower_cells.dedup();
eiffel_tower_cells
.into_iter()
.map(|cell| Cell::try_from(u64::from(cell)).unwrap())
.map(|cell| Cell::from_raw(u64::from(cell)).unwrap())
.collect::<Vec<Cell>>()
};
let mut hex_map: HexTreeMap<i32> = eiffel_tower_cells
.iter()
.enumerate()
.map(|(i, &cell)| (cell, i as i32))
.collect();
let eiffel_tower_res1_parent = Cell::try_from(0x811fbffffffffff).unwrap();
let eiffel_tower_res1_parent = Cell::from_raw(0x811fbffffffffff).unwrap();
let value_sum: i32 = hex_map
.subtree_iter(eiffel_tower_res1_parent)
.map(|(_cell, val)| val)
.sum();
// Establish the sum of map values in the eiffel tower block.
assert_eq!(value_sum, 22578);

let west_mask_res9 = Cell::try_from(0x891fb46741bffff).unwrap();
let east_mask_res9 = Cell::try_from(0x891fb467413ffff).unwrap();
let west_mask_res9 = Cell::from_raw(0x891fb46741bffff).unwrap();
let east_mask_res9 = Cell::from_raw(0x891fb467413ffff).unwrap();
let west_value_sum: i32 = hex_map
.subtree_iter(west_mask_res9)
.map(|(_cell, val)| val)
@@ -421,18 +420,18 @@ mod tests {

// https://wolf-h3-viewer.glitch.me/?h3=863969a47ffffff
let monaco_res6_cellidx = CellIndex::try_from(0x863969a47ffffff).unwrap();
let monaco_res6_cell = Cell::try_from(u64::from(monaco_res6_cellidx)).unwrap();
let monaco_res6_cell = Cell::from_raw(u64::from(monaco_res6_cellidx)).unwrap();
// https://wolf-h3-viewer.glitch.me/?h3=863969a6fffffff
let not_monaco_res6_cellidx = CellIndex::try_from(0x863969a6fffffff).unwrap();

let monaco_res10_cells = monaco_res6_cellidx
.children(Resolution::Ten)
.map(|ci| Cell::try_from(u64::from(ci)).unwrap())
.map(|ci| Cell::from_raw(u64::from(ci)).unwrap())
.collect::<Vec<_>>();

let not_monaco_res10_cells = not_monaco_res6_cellidx
.children(Resolution::Ten)
.map(|ci| Cell::try_from(u64::from(ci)).unwrap())
.map(|ci| Cell::from_raw(u64::from(ci)).unwrap())
.collect::<Vec<_>>();

let monaco_hextree: HexTreeMap<(), NullCompactor> = monaco_res10_cells
17 changes: 8 additions & 9 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -2,7 +2,6 @@ use geo::coord;
use h3_lorawan_regions as regions;
use h3ron::H3Cell;
use hextree::{compaction::EqCompactor, Cell, HexTreeMap, HexTreeSet};
use std::convert::TryFrom;

/// Perform a linear search of `region` for `target` cell.
fn naive_contains(region: &[Cell], target: Cell) -> bool {
@@ -26,7 +25,7 @@ fn naive_contains(region: &[Cell], target: Cell) -> bool {
fn from_indicies(indicies: &[u64]) -> (HexTreeSet, Vec<Cell>) {
let cells: Vec<Cell> = indicies
.iter()
.map(|&idx| Cell::try_from(idx).unwrap())
.map(|&idx| Cell::from_raw(idx).unwrap())
.collect();
let set: HexTreeSet = cells.iter().collect();
(set, cells)
@@ -40,9 +39,9 @@ fn all_up() {
let tarpon_springs = H3Cell::from_coordinate(coord! {x: -82.753822, y: 28.15215}, 12).unwrap();
let gulf_of_mexico = H3Cell::from_coordinate(coord! {x: -83.101920, y: 28.128096}, 0).unwrap();
let paris = H3Cell::from_coordinate(coord! {x: 2.340340, y: 48.868680}, 12).unwrap();
let tarpon_springs = Cell::try_from(*tarpon_springs).unwrap();
let gulf_of_mexico = Cell::try_from(*gulf_of_mexico).unwrap();
let paris = Cell::try_from(*paris).unwrap();
let tarpon_springs = Cell::from_raw(*tarpon_springs).unwrap();
let gulf_of_mexico = Cell::from_raw(*gulf_of_mexico).unwrap();
let paris = Cell::from_raw(*paris).unwrap();

assert!(us915_tree.contains(tarpon_springs));
assert!(naive_contains(&us915_cells, tarpon_springs));
@@ -73,7 +72,7 @@ fn all_up() {
}

// https://wolf-h3-viewer.glitch.me/?h3=812a3ffffffffff
let northeast_res1 = Cell::try_from(0x812a3ffffffffff).unwrap();
let northeast_res1 = Cell::from_raw(0x812a3ffffffffff).unwrap();

// Lets get rid of all raw cells not under northeast_res1.
let expected_north_cells = {
@@ -116,12 +115,12 @@ fn mono_map() {

for (name, cells) in regions.iter() {
for cell in cells.iter() {
monomap.insert(Cell::try_from(*cell).unwrap(), name);
monomap.insert(Cell::from_raw(*cell).unwrap(), name);
}
}

for (name, cells) in regions.iter() {
assert!(cells.iter().map(|c| Cell::try_from(*c).unwrap()).all(|c| {
assert!(cells.iter().map(|c| Cell::from_raw(*c).unwrap()).all(|c| {
if let Some((cell, val)) = monomap.get(c) {
c.to_parent(cell.res()) == Some(cell) && val == &name
} else {
@@ -137,7 +136,7 @@ fn test_compaction() {
let (mut us915_nocompact_tree, us915_nocompact_cells) =
from_indicies(regions::nocompact::US915);
let gulf_of_mexico = H3Cell::from_coordinate(coord! {x: -83.101920, y: 28.128096}, 0).unwrap();
let gulf_of_mexico = Cell::try_from(*gulf_of_mexico).unwrap();
let gulf_of_mexico = Cell::from_raw(*gulf_of_mexico).unwrap();
assert_eq!(us915_tree.len(), us915_nocompact_tree.len());
assert!(us915_tree == us915_nocompact_tree);
assert!(us915_nocompact_tree.len() < us915_nocompact_cells.len());

0 comments on commit 784e7f1

Please sign in to comment.