Introduced preferences struct

This commit is contained in:
Nahuel Lofeudo 2026-03-28 10:24:51 +00:00
parent a22e1ee3fa
commit 34dad5e000
4 changed files with 37 additions and 10 deletions

View File

@ -129,7 +129,7 @@ fn load_vector_map<'a, K, V, IndexFn, FilterT>(
} }
pub fn load_gtfs(gtfs: &mut Gtfs, zip_reader: &mut ZipArchive<File>, route_numbers: HashSet<String>, stop_codes: HashSet<String>) { pub fn load_gtfs(gtfs: &mut Gtfs, zip_reader: &mut ZipArchive<File>, route_numbers: &HashSet<String>, stop_codes: &HashSet<String>) {
// Agencies // Agencies
load_vector(&mut gtfs.agencies, zip_reader, "agency.txt"); load_vector(&mut gtfs.agencies, zip_reader, "agency.txt");

View File

@ -1,15 +1,23 @@
mod loader; mod loader;
mod utils; mod utils;
mod structs; pub mod structs;
use std::{ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
fs::File, fs::File,
}; };
use crate::gtfs::{loader::load_gtfs, structs::Gtfs}; use crate::gtfs::{loader::load_gtfs, structs::{Arrival, Gtfs, Preferences}};
pub fn get_next_stops(gtfs: &Gtfs) -> Vec<Arrival> {
let arrivals = Vec::<Arrival>::new();
return arrivals;
}
/// Load a GTFS structure from a zip file /// Load a GTFS structure from a zip file
pub fn load(src_file: &str, route_numbers: HashSet<String>, stop_codes: HashSet<String>) -> Gtfs { pub fn load(src_file: &str, prefs: &Preferences) -> Gtfs {
// Open zip file // Open zip file
let mut zip_reader = zip::ZipArchive::new(File::open(src_file).unwrap()).unwrap(); let mut zip_reader = zip::ZipArchive::new(File::open(src_file).unwrap()).unwrap();
@ -24,7 +32,7 @@ pub fn load(src_file: &str, route_numbers: HashSet<String>, stop_codes: HashSet<
}; };
load_gtfs(&mut gtfs, &mut zip_reader, route_numbers, stop_codes); load_gtfs(&mut gtfs, &mut zip_reader, &prefs.route_numbers, &prefs.stop_codes);
return gtfs; return gtfs;
} }

View File

@ -1,6 +1,13 @@
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop, TimepointType};
// This is to store the preferences for the GTFS(-R) side of the code.
pub struct Preferences {
pub route_numbers: HashSet<String>,
pub stop_codes: HashSet<String>,
}
use gtfs_structures::{Agency, Calendar, CalendarDate, RawStopTime, RawTrip, Route, Stop};
// The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything // The main GTFS struct. This is similar to (but not exactly) gtfs-structures::Gtfs because we don't need everything
#[derive(Debug)] #[derive(Debug)]
@ -20,3 +27,9 @@ pub struct Gtfs {
/// Stop times for the chosen stops and the chosen routes /// Stop times for the chosen stops and the chosen routes
pub stop_times: HashMap<(String, u32), RawStopTime>, pub stop_times: HashMap<(String, u32), RawStopTime>,
} }
pub struct Arrival<'a> {
pub route: &'a Route,
pub stop: &'a Stop,
pub timepoint: &'a TimepointType,
}

View File

@ -1,16 +1,22 @@
mod gtfs; mod gtfs;
use std::{collections::HashSet, time::Instant}; use std::{collections::HashSet, time::Instant};
use crate::gtfs::structs;
const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip"; const SRC_FILE: &str = "/home/nahuel/Downloads/GTFS_Realtime.zip";
fn main() { fn main() {
let gtfs_prefs = structs::Preferences {
route_numbers: HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]),
stop_codes: HashSet::from([String::from("1117")])
};
// Init GTFS static info // Init GTFS static info
for _ in 0..1000 { for _ in 0..1000 {
let start_gtfs = Instant::now(); let start_gtfs = Instant::now();
println!("Loading GTFS data..."); println!("Loading GTFS data...");
let gtfs = gtfs::load(SRC_FILE, let gtfs = gtfs::load(SRC_FILE, &gtfs_prefs);
HashSet::from([String::from("15A"), String::from("F1"), String::from("F2"), String::from("F3")]),
HashSet::from([String::from("1117")]));
println!("Loaded records in {:#?}. Data size: {:#?}", start_gtfs.elapsed(), ::std::mem::size_of_val(&gtfs)) println!("Loaded records in {:#?}. Data size: {:#?}", start_gtfs.elapsed(), ::std::mem::size_of_val(&gtfs))
} }