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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
use std::ops::Add;

use geo::bounding_rect::BoundingRect;
use geo::concave_hull::ConcaveHull;
use geo_types::{Coordinate, MultiPoint, MultiPolygon, Point, Polygon, Rect};
use num_traits::Zero;
use rayon::prelude::*;
use serde::{Deserialize, Serialize};
use smallvec::{smallvec, SmallVec};

use h3ron::collections::compressed::Decompressor;
use h3ron::collections::hashbrown::hash_map::Entry;
use h3ron::collections::{H3Treemap, HashMap};
use h3ron::iter::H3DirectedEdgesBuilder;
use h3ron::{H3Cell, H3DirectedEdge, HasH3Resolution, ToCoordinate};

use crate::algorithm::covered_area::{cells_covered_area, CoveredArea};
use crate::error::Error;
use crate::graph::longedge::LongEdge;
use crate::graph::node::NodeType;
use crate::graph::{
    EdgeWeight, GetCellEdges, GetCellNode, GetStats, GraphStats, H3EdgeGraph, IterateCellNodes,
};

#[derive(Serialize, Deserialize, Clone)]
struct OwnedEdgeValue<W> {
    pub weight: W,

    /// the longedge is a shortcut which includes many consequent edges while
    /// allowing to visit each of then individually.
    ///
    /// The Box takes care of allocating the LongEdge on the heap. That reduces
    /// the footprint of the OwnedEdgeValue - when W = f32 to nearly 10% compared to
    /// allocating the LongEdge on the stack.
    pub longedge: Option<Box<(LongEdge, W)>>,
}

impl<'a, W> From<&'a OwnedEdgeValue<W>> for EdgeWeight<'a, W>
where
    W: Copy,
{
    fn from(owned_edge_value: &'a OwnedEdgeValue<W>) -> Self {
        EdgeWeight {
            weight: owned_edge_value.weight,
            longedge: owned_edge_value
                .longedge
                .as_ref()
                .map(|boxed| (&boxed.0, boxed.1)),
        }
    }
}

type OwnedEdgeTuple<W> = (H3DirectedEdge, OwnedEdgeValue<W>);

/// A smallvec with an array length of 2 allows storing the - probably - most common
/// number of edges on the heap
type OwnedEdgeTupleList<W> = SmallVec<[OwnedEdgeTuple<W>; 2]>;

/// A prepared graph which can be used with a few algorithms.
///
/// Consequent H3DirectedEdges without forks get extended by a `LongEdge` to allow
/// skipping the individual `H3DirectedEdge` values for a more efficient graph
/// traversal.
///
/// <p>
#[doc=include_str!("../../doc/images/edges-and-longedges.svg")]
/// </p>
///
/// <p>
#[doc=include_str!("../../doc/images/prepared_h3_edge_graph.svg")]
/// </p>
///
#[derive(Serialize, Deserialize, Clone)]
pub struct PreparedH3EdgeGraph<W> {
    outgoing_edges: HashMap<H3Cell, OwnedEdgeTupleList<W>>,
    h3_resolution: u8,
    graph_nodes: HashMap<H3Cell, NodeType>,
}

unsafe impl<W> Sync for PreparedH3EdgeGraph<W> where W: Sync {}

impl<W> PreparedH3EdgeGraph<W> {
    /// count the number of edges in the graph
    ///
    /// The returned tuple is (`num_edges`, `num_long_edges`)
    pub fn count_edges(&self) -> (usize, usize) {
        let mut num_edges = 0usize;
        let mut num_long_edges = 0usize;

        for (_cell, oevs) in self.outgoing_edges.iter() {
            num_edges += oevs.len();
            num_long_edges += oevs
                .iter()
                .filter(|(_, oev)| oev.longedge.is_some())
                .count();
        }
        (num_edges, num_long_edges)
    }
}

impl<W> PreparedH3EdgeGraph<W>
where
    W: Copy,
{
    /// iterate over all edges of the graph
    pub fn iter_edges(&self) -> impl Iterator<Item = (H3DirectedEdge, EdgeWeight<W>)> {
        self.outgoing_edges
            .iter()
            .flat_map(|(_, oevs)| oevs.iter().map(|(edge, oev)| (*edge, oev.into())))
    }

    /// iterate over all edges of the graph, while skipping simple `H3DirectedEdge`
    /// which are already covered in other `LongEdge` instances of the graph.
    ///
    /// This function iterates the graph twice - the first time to collect
    /// all edges which are part of long-edges.
    pub fn iter_edges_non_overlapping(
        &self,
    ) -> Result<impl Iterator<Item = (H3DirectedEdge, EdgeWeight<W>)>, Error> {
        let mut covered_edges = H3Treemap::<H3DirectedEdge>::default();
        let mut decompressor = Decompressor::default();
        for (_, owned_edge_values) in self.outgoing_edges.iter() {
            for (_, owned_edge_value) in owned_edge_values.iter() {
                if let Some(boxed_longedge) = owned_edge_value.longedge.as_ref() {
                    for edge in decompressor
                        .decompress_block(&boxed_longedge.0.edge_path)?
                        .skip(1)
                    {
                        covered_edges.insert(edge);
                    }
                }
            }
        }
        Ok(self.iter_edges().filter_map(move |(edge, weight)| {
            if covered_edges.contains(&edge) {
                None
            } else {
                Some((edge, weight))
            }
        }))
    }
}

impl<W> HasH3Resolution for PreparedH3EdgeGraph<W> {
    fn h3_resolution(&self) -> u8 {
        self.h3_resolution
    }
}

impl<W> GetStats for PreparedH3EdgeGraph<W> {
    fn get_stats(&self) -> Result<GraphStats, Error> {
        Ok(GraphStats {
            h3_resolution: self.h3_resolution,
            num_nodes: self.graph_nodes.len(),
            num_edges: self.count_edges().0,
        })
    }
}

impl<W> GetCellNode for PreparedH3EdgeGraph<W> {
    fn get_cell_node(&self, cell: &H3Cell) -> Option<NodeType> {
        self.graph_nodes.get(cell).copied()
    }
}

impl<W: Copy> GetCellEdges for PreparedH3EdgeGraph<W> {
    type EdgeWeightType = W;

    fn get_edges_originating_from(
        &self,
        cell: &H3Cell,
    ) -> Result<Vec<(H3DirectedEdge, EdgeWeight<Self::EdgeWeightType>)>, Error> {
        let mut out_vec = Vec::with_capacity(7);
        if let Some(edges_with_weights) = self.outgoing_edges.get(cell) {
            out_vec.extend(
                edges_with_weights
                    .iter()
                    .map(|(edge, owv)| (*edge, owv.into())),
            );
        }
        Ok(out_vec)
    }
}

const MIN_LONGEDGE_LENGTH: usize = 3;

fn to_longedge_edges<W>(
    input_graph: H3EdgeGraph<W>,
    min_longedge_length: usize,
) -> Result<HashMap<H3Cell, OwnedEdgeTupleList<W>>, Error>
where
    W: PartialOrd + PartialEq + Add<Output = W> + Copy + Send + Sync,
{
    if min_longedge_length < MIN_LONGEDGE_LENGTH {
        return Err(Error::Other(format!(
            "minimum longedge length must be >= {}",
            MIN_LONGEDGE_LENGTH
        )));
    }

    let outgoing_edge_vecs = input_graph
        .edges
        .par_iter()
        .try_fold(
            || (Vec::new(), H3DirectedEdgesBuilder::new()),
            |(mut output_vec, mut edge_builder), (edge, weight)| {
                assemble_edge_with_longedge(
                    &input_graph.edges,
                    min_longedge_length,
                    edge,
                    weight,
                    &mut edge_builder,
                )
                .map(|cell_edge| {
                    output_vec.push(cell_edge);
                    (output_vec, edge_builder)
                })
            },
        )
        .collect::<Result<Vec<_>, _>>()?;

    let mut outgoing_edges: HashMap<H3Cell, OwnedEdgeTupleList<W>> = Default::default();
    for (outgoing_edge_vec, _) in outgoing_edge_vecs.into_iter() {
        for (cell, edge_with_weight) in outgoing_edge_vec.into_iter() {
            match outgoing_edges.entry(cell) {
                Entry::Occupied(mut occ) => occ.get_mut().push(edge_with_weight),
                Entry::Vacant(vac) => {
                    vac.insert(smallvec![edge_with_weight]);
                }
            }
        }
    }

    // remove duplicates if there are any. Ignores any differences in weights
    outgoing_edges
        .par_iter_mut()
        .for_each(|(_cell, edges_with_weights)| {
            edges_with_weights.sort_unstable_by_key(|eww| eww.0);
            edges_with_weights.dedup_by(|a, b| a.0 == b.0);
        });

    Ok(outgoing_edges)
}

fn assemble_edge_with_longedge<W>(
    input_edges: &HashMap<H3DirectedEdge, W>,
    min_longedge_length: usize,
    edge: &H3DirectedEdge,
    weight: &W,
    edge_builder: &mut H3DirectedEdgesBuilder,
) -> Result<(H3Cell, OwnedEdgeTuple<W>), Error>
where
    W: PartialOrd + PartialEq + Add<Output = W> + Copy,
{
    let mut graph_entry = OwnedEdgeValue {
        weight: *weight,
        longedge: None,
    };

    let origin_cell = edge.origin_cell()?;

    // number of upstream edges leading to this one
    let num_edges_leading_to_this_one = edge_builder
        .from_origin_cell(&origin_cell)?
        .filter(|new_edge| new_edge != edge) // ignore the backwards edge
        .filter(|new_edge| {
            new_edge
                .reversed()
                .ok()
                .map(|rev_edge| input_edges.get(&rev_edge).is_some())
                .unwrap_or(false)
        })
        .count();

    // attempt to build a longedge when this edge is either the end of a path, or a path
    // starting after a conjunction of multiple edges
    if num_edges_leading_to_this_one != 1 {
        let mut edge_path = vec![*edge];
        let mut longedge_weight = *weight;

        let mut last_edge = *edge;
        loop {
            let last_edge_reverse = last_edge.reversed()?;
            // follow the edges until the end or a conjunction is reached
            let following_edges: Vec<_> = edge_builder
                .from_origin_cell(&last_edge.destination_cell()?)?
                .filter_map(|this_edge| {
                    if this_edge != last_edge_reverse {
                        input_edges.get_key_value(&this_edge)
                    } else {
                        None
                    }
                })
                .collect();

            // found no further continuing edge or conjunction
            if following_edges.len() != 1 {
                break;
            }
            let following_edge = *(following_edges[0].0);

            // stop when encountering circles
            if edge_path.contains(&following_edge) {
                break;
            }

            edge_path.push(following_edge);
            longedge_weight = *(following_edges[0].1) + longedge_weight;
            // find the next following edge in the next iteration of the loop
            last_edge = following_edge;
        }

        if edge_path.len() >= min_longedge_length {
            graph_entry.longedge =
                Some(Box::new((LongEdge::try_from(edge_path)?, longedge_weight)));
        }
    }
    Ok((origin_cell, (*edge, graph_entry)))
}

impl<W> PreparedH3EdgeGraph<W>
where
    W: PartialOrd + PartialEq + Add + Copy + Ord + Zero + Send + Sync,
{
    pub fn from_h3edge_graph(
        graph: H3EdgeGraph<W>,
        min_longedge_length: usize,
    ) -> Result<Self, Error> {
        let h3_resolution = graph.h3_resolution();
        let graph_nodes = graph.nodes()?;
        let outgoing_edges = to_longedge_edges(graph, min_longedge_length)?;
        Ok(Self {
            graph_nodes,
            h3_resolution,
            outgoing_edges,
        })
    }
}

impl<W> TryFrom<H3EdgeGraph<W>> for PreparedH3EdgeGraph<W>
where
    W: PartialOrd + PartialEq + Add + Copy + Ord + Zero + Send + Sync,
{
    type Error = Error;

    fn try_from(graph: H3EdgeGraph<W>) -> Result<Self, Self::Error> {
        Self::from_h3edge_graph(graph, 4)
    }
}

impl<W> From<PreparedH3EdgeGraph<W>> for H3EdgeGraph<W>
where
    W: PartialOrd + PartialEq + Add + Copy + Ord + Zero,
{
    fn from(prepared_graph: PreparedH3EdgeGraph<W>) -> Self {
        Self {
            edges: prepared_graph
                .iter_edges()
                .map(|(edge, edge_value)| (edge, edge_value.weight))
                .collect(),
            h3_resolution: prepared_graph.h3_resolution,
        }
    }
}

impl<W> CoveredArea for PreparedH3EdgeGraph<W> {
    type Error = Error;

    fn covered_area(&self, reduce_resolution_by: u8) -> Result<MultiPolygon<f64>, Self::Error> {
        cells_covered_area(
            self.graph_nodes.iter().map(|(cell, _)| cell),
            self.h3_resolution(),
            reduce_resolution_by,
        )
    }
}

impl<'a, W> IterateCellNodes<'a> for PreparedH3EdgeGraph<W> {
    type CellNodeIterator = h3ron::collections::hashbrown::hash_map::Iter<'a, H3Cell, NodeType>;

    fn iter_cell_nodes(&'a self) -> Self::CellNodeIterator {
        self.graph_nodes.iter()
    }
}

impl<W> ConcaveHull for PreparedH3EdgeGraph<W> {
    type Scalar = f64;

    /// concave hull - this implementation leaves out invalid cells
    fn concave_hull(&self, concavity: Self::Scalar) -> Polygon<Self::Scalar> {
        let mpoint = MultiPoint::from(
            self.iter_cell_nodes()
                .filter_map(|(cell, _)| cell.to_coordinate().ok().map(Point::from))
                .collect::<Vec<_>>(),
        );
        mpoint.concave_hull(concavity)
    }
}

impl<W> BoundingRect<f64> for PreparedH3EdgeGraph<W> {
    type Output = Option<Rect<f64>>;

    fn bounding_rect(&self) -> Self::Output {
        let mut iter = self.iter_cell_nodes();
        let mut rect = {
            // consume until encountering the first valid cell
            if let Some(coord) = iter.find_map(|(cell, _)| cell.to_coordinate().ok()) {
                Point::from(coord).bounding_rect()
            } else {
                return None;
            }
        };

        for (cell, _) in iter {
            if let Ok(coord) = cell.to_coordinate() {
                rect = Rect::new(
                    Coordinate {
                        x: if coord.x < rect.min().x {
                            coord.x
                        } else {
                            rect.min().x
                        },
                        y: if coord.y < rect.min().y {
                            coord.y
                        } else {
                            rect.min().y
                        },
                    },
                    Coordinate {
                        x: if coord.x > rect.max().x {
                            coord.x
                        } else {
                            rect.max().x
                        },
                        y: if coord.y > rect.max().y {
                            coord.y
                        } else {
                            rect.max().y
                        },
                    },
                );
            }
        }
        Some(rect)
    }
}

#[cfg(test)]
mod tests {
    use std::convert::TryInto;

    use geo_types::{Coordinate, LineString};

    use crate::graph::{H3EdgeGraph, PreparedH3EdgeGraph};

    fn build_line_prepared_graph() -> PreparedH3EdgeGraph<u32> {
        let full_h3_res = 8;
        let cells: Vec<_> = h3ron::line(
            &LineString::from(vec![
                Coordinate::from((23.3, 12.3)),
                Coordinate::from((24.2, 12.2)),
            ]),
            full_h3_res,
        )
        .unwrap()
        .into();
        assert!(cells.len() > 100);

        let mut graph = H3EdgeGraph::new(full_h3_res);
        for w in cells.windows(2) {
            graph.add_edge_using_cells(w[0], w[1], 20u32).unwrap();
        }
        assert!(graph.num_edges() > 50);
        let prep_graph: PreparedH3EdgeGraph<_> = graph.try_into().unwrap();
        assert_eq!(prep_graph.count_edges().1, 1);
        prep_graph
    }

    #[test]
    fn test_iter_edges() {
        let graph = build_line_prepared_graph();
        assert!(graph.iter_edges().count() > 50);
    }

    #[test]
    fn test_iter_non_overlapping_edges() {
        let graph = build_line_prepared_graph();
        assert_eq!(graph.iter_edges_non_overlapping().unwrap().count(), 1);
    }
}