h3o-ice 0.1.9

Frozen{Map,Set} for H3 cells, based on finite state transducers.
Documentation
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use crate::{BuildError, Key};
use either::Either;
use fst::{IntoStreamer, Set, SetBuilder, Streamer, set::Stream};
use h3o::CellIndex;
use std::{
    io,
    ops::{Bound, RangeBounds},
};

/// A read-only set of H3 cell indexes.
pub struct FrozenSet<D>(Set<D>);

impl<D: AsRef<[u8]>> FrozenSet<D> {
    /// Creates a set from its representation as a raw byte sequence.
    ///
    /// This accepts anything that can be cheaply converted to a `&[u8]`. The
    /// caller is responsible for guaranteeing that the given bytes refer to
    /// a valid set. While memory safety will not be violated by invalid input,
    /// a panic could occur while reading the set at any point.
    ///
    /// # Errors
    ///
    /// The set must have been written with a compatible builder. If the format
    /// is invalid or if there is a mismatch between the API version of this
    /// library and the set, then an error is returned.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use h3o_ice::FrozenSet;
    /// use std::fs;
    ///
    /// # let file_path = "";
    /// let bytes = fs::read_to_string(file_path)?;
    /// let set = FrozenSet::new(bytes);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn new(data: D) -> Result<Self, BuildError> {
        Ok(Set::new(data).map(Self)?)
    }

    /// Returns the number of elements in this set.
    ///
    /// # Examples
    ///
    /// ```
    /// use h3o::CellIndex;
    /// use h3o_ice::FrozenSet;
    ///
    /// let index = CellIndex::try_from(0x8a1fb46622dffff)?;
    /// let set = FrozenSet::try_from_iter(std::iter::once(index))?;
    /// assert_eq!(set.len(), 1);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[inline]
    pub fn len(&self) -> usize {
        self.0.len()
    }

    /// Returns true if and only if this set is empty.
    ///
    /// # Examples
    ///
    /// ```
    /// use h3o::CellIndex;
    /// use h3o_ice::FrozenSet;
    ///
    /// let set = FrozenSet::try_from_iter(std::iter::empty())?;
    /// assert!(set.is_empty());
    ///
    /// let index = CellIndex::try_from(0x8a1fb46622dffff)?;
    /// let set = FrozenSet::try_from_iter(std::iter::once(index))?;
    /// assert!(!set.is_empty());
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Tests the membership of a single H3 cell index.
    ///
    /// Returns true if the cell index or one of its ancestor is present.
    ///
    /// # Examples
    ///
    /// ```
    /// use h3o::CellIndex;
    /// use h3o_ice::FrozenSet;
    ///
    /// let cell = CellIndex::try_from(0x8a1fb46622dffff)?;
    /// let set = FrozenSet::try_from_iter(std::iter::once(cell))?;
    ///
    /// // Exact membership works.
    /// assert_eq!(set.contains(cell), Some(cell));
    ///
    /// // Child membership works too.
    /// let child = CellIndex::try_from(0x8b1fb46622d8fff)?;
    /// assert_eq!(set.contains(child), Some(cell));
    ///
    /// // Even through multiple levels.
    /// let descendant = CellIndex::try_from(0x8d1fb46622d85bf)?;
    /// assert_eq!(set.contains(descendant), Some(cell));
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn contains(&self, index: CellIndex) -> Option<CellIndex> {
        let fst = self.0.as_fst();
        let key = Key::from(index);

        let mut node = fst.root();
        for (i, b) in key.as_ref().iter().enumerate() {
            let idx = node.find_input(*b)?;
            node = fst.node(node.transition_addr(idx));
            if node.is_final() {
                return Some(Key::from(&key.as_ref()[..=i]).into());
            }
        }
        None
    }

    /// Return a lexicographically ordered stream of every descendant (present
    /// in the set) of the given cell index.
    ///
    /// # Examples
    ///
    /// ```
    /// use h3o::{CellIndex, Resolution};
    /// use h3o_ice::FrozenSet;
    ///
    /// let index = CellIndex::try_from(0x85318d83fffffff)?;
    /// let set = FrozenSet::try_from_iter(index.children(Resolution::Six))?;
    ///
    /// for cell in set.descendants(index) {
    ///     println!("{cell}");
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[expect(
        clippy::missing_panics_doc,
        reason = "expect don't need to be documented"
    )]
    pub fn descendants(
        &self,
        index: CellIndex,
    ) -> impl Iterator<Item = CellIndex> + '_ {
        index.resolution().succ().map_or_else(
            // If there is no lower resolution there can't be any descendants.
            || Either::Left(std::iter::empty()),
            |resolution| {
                let mut children = index.children(resolution);
                let start = children.next().expect("first child");
                let end = children.last().expect("last child");
                Either::Right(
                    self.range((Bound::Included(start), Bound::Included(end))),
                )
            },
        )
    }

    /// Return a lexicographically ordered stream of all cells in this set.
    ///
    /// # Examples
    ///
    /// ```
    /// use h3o::{CellIndex, Resolution};
    /// use h3o_ice::FrozenSet;
    ///
    /// let index = CellIndex::try_from(0x85318d83fffffff)?;
    /// let set = FrozenSet::try_from_iter(index.children(Resolution::Six))?;
    ///
    /// for cell in set.iter() {
    ///     println!("{cell}");
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[must_use]
    pub fn iter(&self) -> FrozenSetIterator<'_> {
        FrozenSetIterator::new(self)
    }

    /// Return a lexicographically ordered stream over the subset of keys the
    /// specified range.
    ///
    /// # Examples
    ///
    /// ```
    /// use h3o::{CellIndex, Resolution};
    /// use h3o_ice::FrozenSet;
    /// use std::ops::Bound;
    ///
    /// let index = CellIndex::try_from(0x85318d83fffffff)?;
    /// let set = FrozenSet::try_from_iter(index.children(Resolution::Six))?;
    ///
    /// let start = Bound::Included(CellIndex::try_from(0x86318d817ffffff)?);
    /// let end = Bound::Excluded(CellIndex::try_from(0x86318d827ffffff)?);
    ///
    /// for cell in set.range((start, end)) {
    ///     println!("{cell}");
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn range(
        &self,
        range: impl RangeBounds<CellIndex>,
    ) -> impl Iterator<Item = CellIndex> + '_ {
        let (start, end) = (range.start_bound(), range.end_bound());

        if matches!((start, end), (Bound::Unbounded, Bound::Unbounded)) {
            return Either::Left(self.iter());
        }
        let builder = self.0.range();
        let builder = match start {
            Bound::Included(lower) => builder.ge(Key::from(*lower)),
            Bound::Excluded(lower) => builder.gt(Key::from(*lower)),
            Bound::Unbounded => builder,
        };
        let builder = match end {
            Bound::Included(upper) => builder.le(Key::from(*upper)),
            Bound::Excluded(upper) => builder.lt(Key::from(*upper)),
            Bound::Unbounded => builder,
        };
        Either::Right(FrozenSetRangeIterator::new(builder.into_stream()))
    }
}

impl FrozenSet<Vec<u8>> {
    /// Create a `FrozenSet` from an iterator of ordered H3 cell indexes.
    ///
    /// Note that this is a convenience function to build a set in memory.
    /// To build a set that streams to an arbitrary `io::Write`, use
    /// `FrozenSetBuilder`.
    ///
    /// # Errors
    ///
    /// If the iterator does not yield values in lexicographic order, then an
    /// error is returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use h3o::{CellIndex, Resolution};
    /// use h3o_ice::FrozenSet;
    ///
    /// let index = CellIndex::try_from(0x85318d83fffffff)?;
    /// let set = FrozenSet::try_from_iter(index.children(Resolution::Six))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn try_from_iter(
        iter: impl IntoIterator<Item = CellIndex>,
    ) -> Result<Self, BuildError> {
        let mut builder = FrozenSetBuilder::memory();
        builder.extend_iter(iter)?;
        Self::new(builder.into_inner()?)
    }

    /// Returns the binary contents of this set.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use h3o::{CellIndex, Resolution};
    /// use h3o_ice::FrozenSet;
    ///
    /// let index = CellIndex::try_from(0x85318d83fffffff)?;
    /// let set = FrozenSet::try_from_iter(index.children(Resolution::Six))?;
    ///
    /// # let file_path = "";
    /// std::fs::write(file_path, set.as_bytes())?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        self.0.as_fst().as_bytes()
    }
}

impl<'a, D: AsRef<[u8]>> IntoIterator for &'a FrozenSet<D> {
    type IntoIter = FrozenSetIterator<'a>;
    type Item = CellIndex;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

// ------------------------------------------------------------------------------

/// A builder for creating a frozen set.
///
/// # Example: build in memory
///
/// ```
/// use h3o::{CellIndex, Resolution};
/// use h3o_ice::FrozenSetBuilder;
///
/// let mut builder = FrozenSetBuilder::memory();
/// builder.insert(CellIndex::try_from(0x85283473fffffff)?)?;
///
/// let index = CellIndex::try_from(0x8a1fb46622dffff)?;
/// builder.extend_iter(index.children(Resolution::Six))?;
///
/// let set = builder.into_set();
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
/// # Example: stream to file
///
/// ```no_run
/// use h3o::{CellIndex, Resolution};
/// use h3o_ice::FrozenSetBuilder;
/// use std::{fs, io};
///
/// # let file_path = "";
/// let mut wtr = io::BufWriter::new(fs::File::create(file_path)?);
/// let mut builder = FrozenSetBuilder::new(wtr)?;
///
/// let index = CellIndex::try_from(0x8a1fb46622dffff)?;
/// builder.extend_iter(index.children(Resolution::Six))?;
///
/// builder.finish()?;
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
pub struct FrozenSetBuilder<W>(SetBuilder<W>);

impl<W: io::Write> FrozenSetBuilder<W> {
    /// Create a builder that builds a set by writing it to `wtr` in a
    /// streaming fashion.
    ///
    /// # Errors
    ///
    /// If there was a problem writing to the underlying writer, an error is
    /// returned.
    pub fn new(wtr: W) -> Result<Self, BuildError> {
        SetBuilder::new(wtr).map(Self).map_err(Into::into)
    }

    /// Insert a new cell index into the set.
    ///
    /// # Errors
    ///
    /// If a cell index is inserted that is less than any previous cell index
    /// added, then an error is returned.
    ///
    /// Similarly, if there was a problem writing to the underlying writer, an
    /// error is returned.
    pub fn insert(&mut self, index: CellIndex) -> Result<(), BuildError> {
        self.0.insert(Key::from(index)).map_err(Into::into)
    }

    /// Calls insert on each cell index in the iterator.
    ///
    /// # Errors
    ///
    /// If an error occurred while adding an element, processing is stopped
    /// and the error is returned.
    pub fn extend_iter(
        &mut self,
        iter: impl IntoIterator<Item = CellIndex>,
    ) -> Result<(), BuildError> {
        self.0
            .extend_iter(iter.into_iter().map(Key::from))
            .map_err(Into::into)
    }

    /// Finishes the construction of the set and flushes the underlying
    /// writer. After completion, the data written to `W` may be read using
    /// one of `FrozenSet`'s constructor methods.
    ///
    /// # Errors
    ///
    /// Returns an error if there was a problem writing to the underlying
    /// writer.
    pub fn finish(self) -> Result<(), BuildError> {
        self.0.finish().map_err(Into::into)
    }

    /// Just like `finish`, except it returns the underlying writer after
    /// flushing it.
    ///
    /// # Errors
    ///
    /// Returns an error if there was a problem writing to the underlying
    /// writer.
    pub fn into_inner(self) -> Result<W, BuildError> {
        self.0.into_inner().map_err(Into::into)
    }
}

impl FrozenSetBuilder<Vec<u8>> {
    /// Create a builder that builds a set in memory.
    #[inline]
    #[must_use]
    pub fn memory() -> Self {
        Self(SetBuilder::memory())
    }

    /// Finishes the construction of the set and returns it.
    #[inline]
    #[must_use]
    pub fn into_set(self) -> FrozenSet<Vec<u8>> {
        FrozenSet(self.0.into_set())
    }
}

// ------------------------------------------------------------------------------

/// An iterator over the every value of the set.
pub struct FrozenSetIterator<'a> {
    stream: Stream<'a>,
    len: usize,
    count: usize,
}

impl<'a> FrozenSetIterator<'a> {
    fn new<D>(set: &'a FrozenSet<D>) -> Self
    where
        D: AsRef<[u8]>,
    {
        Self {
            stream: set.0.stream(),
            len: set.len(),
            count: 0,
        }
    }
}

impl Iterator for FrozenSetIterator<'_> {
    type Item = CellIndex;

    fn next(&mut self) -> Option<Self::Item> {
        self.stream.next().map(|key| {
            self.count += 1;
            Key::from(key).into()
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.len(), Some(self.len()))
    }
}

impl ExactSizeIterator for FrozenSetIterator<'_> {
    // We can easily calculate the remaining number of iterations.
    fn len(&self) -> usize {
        self.len - self.count
    }
}

// ------------------------------------------------------------------------------

/// An iterator over a subset of keys in a specified range.
struct FrozenSetRangeIterator<'a> {
    stream: Stream<'a>,
}

impl<'a> FrozenSetRangeIterator<'a> {
    const fn new(stream: Stream<'a>) -> Self {
        Self { stream }
    }
}

impl Iterator for FrozenSetRangeIterator<'_> {
    type Item = CellIndex;

    fn next(&mut self) -> Option<Self::Item> {
        self.stream.next().map(|key| Key::from(key).into())
    }
}