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
use crate::util::drain_h3indexes_to_indexes;
use crate::Error;
use h3ron_h3_sys::H3Index;
use std::os::raw::c_int;
pub trait Index: Sized + PartialEq {
fn h3index(&self) -> H3Index;
fn new(h3index: H3Index) -> Self;
fn validate(&self) -> Result<(), Error>;
fn resolution(&self) -> u8 {
(unsafe { h3ron_h3_sys::h3GetResolution(self.h3index()) }) as u8
}
fn is_valid(&self) -> bool {
self.validate().is_ok()
}
fn is_parent_of(&self, other: &Self) -> bool {
*self == other.get_parent_unchecked(self.resolution())
}
fn is_child_of(&self, other: &Self) -> bool {
other.is_parent_of(self)
}
fn contains(&self, other: &Self) -> bool {
self.is_parent_of(other)
}
fn get_parent(&self, parent_resolution: u8) -> Result<Self, Error> {
let res = self.get_parent_unchecked(parent_resolution);
res.validate()?;
Ok(res)
}
fn get_parent_unchecked(&self, parent_resolution: u8) -> Self {
Self::new(unsafe { h3ron_h3_sys::h3ToParent(self.h3index(), parent_resolution as c_int) })
}
fn get_children(&self, child_resolution: u8) -> Vec<Self> {
let max_size =
unsafe { h3ron_h3_sys::maxH3ToChildrenSize(self.h3index(), child_resolution as c_int) };
let mut h3_indexes_out: Vec<h3ron_h3_sys::H3Index> = vec![0; max_size as usize];
unsafe {
h3ron_h3_sys::h3ToChildren(
self.h3index(),
child_resolution as c_int,
h3_indexes_out.as_mut_ptr(),
);
}
drain_h3indexes_to_indexes(h3_indexes_out)
}
}