graphed for real

This commit is contained in:
Zynh0722 2024-04-24 00:02:35 -07:00
parent c613abc83a
commit bc2f900f37
3 changed files with 46 additions and 40 deletions

10
src/airport.rs Normal file
View 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,
}

View file

@ -1,67 +1,60 @@
#![allow(dead_code)]
mod airport;
mod distance;
mod parse;
use std::{
cell::RefCell,
collections::{hash_map::Entry, HashMap},
rc::Rc,
};
use parse::Record;
type Graph = HashMap<String, Node>;
type Airports = HashMap<usize, Rc<Airport>>;
type Graph = HashMap<usize, Rc<RefCell<Node>>>;
type Airports = HashMap<usize, Rc<airport::Airport>>;
#[derive(Debug, Clone)]
struct Node {
origin: Rc<Airport>,
origin: Rc<airport::Airport>,
destinations: Vec<Destination>,
source_distance: f64,
visited: bool,
}
impl Node {
fn new(origin: Rc<Airport>, destinations: Vec<Destination>) -> Self {
fn new(origin: Rc<airport::Airport>, destinations: Vec<Destination>) -> Self {
Node {
destinations,
origin,
source_distance: 0.0,
visited: false,
}
}
}
#[derive(Debug, Clone)]
struct Airport {
id: usize,
code: String,
name: String,
city: String,
country: String,
lat: f64,
lon: f64,
}
#[derive(Debug, Clone)]
struct Destination {
airport: Rc<Airport>,
airport: Rc<RefCell<Node>>,
distance: f64,
}
impl Destination {
fn new(source: &Airport, destination: Rc<Airport>) -> Self {
fn new(source: &airport::Airport, destination: Rc<airport::Airport>, graph: &Graph) -> Self {
Destination {
distance: distance::haversine(
(source.lat, source.lon),
(destination.lat, destination.lon),
),
airport: destination,
airport: graph[&destination.id].clone(),
}
}
}
fn insert_airports_from_record(airports: &mut Airports, record: &Record) {
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,
code: record.origin_airport_code.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) {
entry.insert(Rc::new(Airport {
entry.insert(Rc::new(airport::Airport {
id: record.destination_airport_id,
code: record.destination_airport_code.clone(),
name: record.destination_airport.clone(),
@ -98,27 +91,29 @@ fn main() {
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();
for record in data {
match graph.entry(record.origin_airport_code.clone()) {
Entry::Occupied(mut entry) => entry
.get_mut()
.destinations
.push(record.get_destination(&airports)),
Entry::Vacant(entry) => {
entry.insert(Node::new(
airports[&record.origin_airport_id].clone(),
vec![record.get_destination(&airports)],
));
}
}
// Populate Nodes
for (id, airport) in &mut airports {
graph.insert(
*id,
Rc::new(RefCell::new(Node::new(airport.clone(), Vec::new()))),
);
}
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!(
"{}",
distance::haversine((19.10317, -165.54224), (73.85371, 55.85692))
);
entry.destinations.push(destination);
};
}
}

View file

@ -1,4 +1,4 @@
use crate::{Airports, Destination};
use crate::{Airports, Destination, Graph};
#[derive(Debug, Clone, serde::Deserialize)]
pub struct Record {
@ -19,10 +19,11 @@ pub struct Record {
}
impl Record {
pub fn get_destination(&self, airports: &Airports) -> Destination {
pub fn get_destination(&self, airports: &Airports, graph: &Graph) -> Destination {
Destination::new(
&airports[&self.origin_airport_id],
airports[&self.destination_airport_id].clone(),
graph,
)
}
}