graphed for real
This commit is contained in:
parent
c613abc83a
commit
bc2f900f37
3 changed files with 46 additions and 40 deletions
10
src/airport.rs
Normal file
10
src/airport.rs
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub(crate) struct Airport {
|
||||||
|
pub(crate) id: usize,
|
||||||
|
pub(crate) code: String,
|
||||||
|
pub(crate) name: String,
|
||||||
|
pub(crate) city: String,
|
||||||
|
pub(crate) country: String,
|
||||||
|
pub(crate) lat: f64,
|
||||||
|
pub(crate) lon: f64,
|
||||||
|
}
|
71
src/main.rs
71
src/main.rs
|
@ -1,67 +1,60 @@
|
||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
|
|
||||||
|
mod airport;
|
||||||
mod distance;
|
mod distance;
|
||||||
mod parse;
|
mod parse;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
cell::RefCell,
|
||||||
collections::{hash_map::Entry, HashMap},
|
collections::{hash_map::Entry, HashMap},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
use parse::Record;
|
use parse::Record;
|
||||||
|
|
||||||
type Graph = HashMap<String, Node>;
|
type Graph = HashMap<usize, Rc<RefCell<Node>>>;
|
||||||
type Airports = HashMap<usize, Rc<Airport>>;
|
type Airports = HashMap<usize, Rc<airport::Airport>>;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Node {
|
struct Node {
|
||||||
origin: Rc<Airport>,
|
origin: Rc<airport::Airport>,
|
||||||
destinations: Vec<Destination>,
|
destinations: Vec<Destination>,
|
||||||
|
source_distance: f64,
|
||||||
visited: bool,
|
visited: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Node {
|
impl Node {
|
||||||
fn new(origin: Rc<Airport>, destinations: Vec<Destination>) -> Self {
|
fn new(origin: Rc<airport::Airport>, destinations: Vec<Destination>) -> Self {
|
||||||
Node {
|
Node {
|
||||||
destinations,
|
destinations,
|
||||||
origin,
|
origin,
|
||||||
|
source_distance: 0.0,
|
||||||
visited: false,
|
visited: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
|
||||||
struct Airport {
|
|
||||||
id: usize,
|
|
||||||
code: String,
|
|
||||||
name: String,
|
|
||||||
city: String,
|
|
||||||
country: String,
|
|
||||||
lat: f64,
|
|
||||||
lon: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct Destination {
|
struct Destination {
|
||||||
airport: Rc<Airport>,
|
airport: Rc<RefCell<Node>>,
|
||||||
distance: f64,
|
distance: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Destination {
|
impl Destination {
|
||||||
fn new(source: &Airport, destination: Rc<Airport>) -> Self {
|
fn new(source: &airport::Airport, destination: Rc<airport::Airport>, graph: &Graph) -> Self {
|
||||||
Destination {
|
Destination {
|
||||||
distance: distance::haversine(
|
distance: distance::haversine(
|
||||||
(source.lat, source.lon),
|
(source.lat, source.lon),
|
||||||
(destination.lat, destination.lon),
|
(destination.lat, destination.lon),
|
||||||
),
|
),
|
||||||
airport: destination,
|
airport: graph[&destination.id].clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_airports_from_record(airports: &mut Airports, record: &Record) {
|
fn insert_airports_from_record(airports: &mut Airports, record: &Record) {
|
||||||
if let Entry::Vacant(entry) = airports.entry(record.origin_airport_id) {
|
if let Entry::Vacant(entry) = airports.entry(record.origin_airport_id) {
|
||||||
entry.insert(Rc::new(Airport {
|
entry.insert(Rc::new(airport::Airport {
|
||||||
id: record.origin_airport_id,
|
id: record.origin_airport_id,
|
||||||
code: record.origin_airport_code.clone(),
|
code: record.origin_airport_code.clone(),
|
||||||
name: record.origin_airport.clone(),
|
name: record.origin_airport.clone(),
|
||||||
|
@ -73,7 +66,7 @@ fn insert_airports_from_record(airports: &mut Airports, record: &Record) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if let Entry::Vacant(entry) = airports.entry(record.destination_airport_id) {
|
if let Entry::Vacant(entry) = airports.entry(record.destination_airport_id) {
|
||||||
entry.insert(Rc::new(Airport {
|
entry.insert(Rc::new(airport::Airport {
|
||||||
id: record.destination_airport_id,
|
id: record.destination_airport_id,
|
||||||
code: record.destination_airport_code.clone(),
|
code: record.destination_airport_code.clone(),
|
||||||
name: record.destination_airport.clone(),
|
name: record.destination_airport.clone(),
|
||||||
|
@ -98,27 +91,29 @@ fn main() {
|
||||||
insert_airports_from_record(&mut airports, record);
|
insert_airports_from_record(&mut airports, record);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let code_to_id: HashMap<String, usize> = HashMap::from_iter(
|
||||||
|
airports
|
||||||
|
.iter()
|
||||||
|
.map(|(id, airport)| (airport.code.clone(), *id)),
|
||||||
|
);
|
||||||
|
|
||||||
let mut graph: Graph = HashMap::new();
|
let mut graph: Graph = HashMap::new();
|
||||||
|
|
||||||
for record in data {
|
// Populate Nodes
|
||||||
match graph.entry(record.origin_airport_code.clone()) {
|
for (id, airport) in &mut airports {
|
||||||
Entry::Occupied(mut entry) => entry
|
graph.insert(
|
||||||
.get_mut()
|
*id,
|
||||||
.destinations
|
Rc::new(RefCell::new(Node::new(airport.clone(), Vec::new()))),
|
||||||
.push(record.get_destination(&airports)),
|
);
|
||||||
Entry::Vacant(entry) => {
|
|
||||||
entry.insert(Node::new(
|
|
||||||
airports[&record.origin_airport_id].clone(),
|
|
||||||
vec![record.get_destination(&airports)],
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("{graph:#?}");
|
// Populate Edges
|
||||||
|
for record in data {
|
||||||
|
let destination = record.get_destination(&airports, &graph);
|
||||||
|
if let Entry::Occupied(mut entry) = graph.entry(record.origin_airport_id) {
|
||||||
|
let mut entry = entry.get_mut().borrow_mut();
|
||||||
|
|
||||||
println!(
|
entry.destinations.push(destination);
|
||||||
"{}",
|
};
|
||||||
distance::haversine((19.10317, -165.54224), (73.85371, 55.85692))
|
}
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::{Airports, Destination};
|
use crate::{Airports, Destination, Graph};
|
||||||
|
|
||||||
#[derive(Debug, Clone, serde::Deserialize)]
|
#[derive(Debug, Clone, serde::Deserialize)]
|
||||||
pub struct Record {
|
pub struct Record {
|
||||||
|
@ -19,10 +19,11 @@ pub struct Record {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Record {
|
impl Record {
|
||||||
pub fn get_destination(&self, airports: &Airports) -> Destination {
|
pub fn get_destination(&self, airports: &Airports, graph: &Graph) -> Destination {
|
||||||
Destination::new(
|
Destination::new(
|
||||||
&airports[&self.origin_airport_id],
|
&airports[&self.origin_airport_id],
|
||||||
airports[&self.destination_airport_id].clone(),
|
airports[&self.destination_airport_id].clone(),
|
||||||
|
graph,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue