Skip to content

Reduce allocations #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/delta_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn delta_time(year: f64) -> f64 {

// From "Delta T: Polynomial Approximation of Time Period 1620-2013"
// By M. Khalid, Mariam Sultana, and Faheem Zaidi (2014)
let terms = vec![
let terms = [
(3.670, 76.541, -253.532, 695.901, -1256.982, 627.152),
(3.120, 10.872, -40.744, 236.890, -351.537, 36.612),
(2.495, 13.480, 13.075, 8.635, -3.307, -128.294),
Expand Down
10 changes: 5 additions & 5 deletions src/earth_orbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ fn get_time_of(event: Event, timestamp: i64) -> i64 {

fn get_jdme(event: Event, y: f64) -> f64 {
// For the years -1000 to +1000
let jdme_terms_before_1000 = vec![
let jdme_terms_before_1000 = [
(1721_139.29189, 365_242.13740, 0.06134, 0.00111, -0.00071), // March Equinoxe
(1721_233.25401, 365_241.72562, -0.05323, 0.00907, -0.00025), // June Solstice
(1721_325.70455, 365_242.49558, -0.11677, -0.00297, 0.00074), // September Equinoxe
(1721_414.39987, 365_242.88257, -0.00769, -0.00933, -0.00006) // December Solstice
];

// For the years +1000 to +3000
let jdme_terms_after_1000 = vec![
let jdme_terms_after_1000 = [
(2451_623.80984, 365_242.37404, 0.05169, -0.00411, -0.00057), // March Equinoxe
(2451_716.56767, 365_241.62603, 0.00325, 0.00888, -0.00030), // June Solstice
(2451_810.21715, 365_242.01767, -0.11575, 0.00337, 0.00078), // September Equinoxe
Expand All @@ -67,7 +67,7 @@ fn get_jdme(event: Event, y: f64) -> f64 {
}

fn get_periodic_terms(t: f64) -> f64 {
let terms = vec![
let terms = [
(485.0, 324.96, 1934.136),
(203.0, 337.23, 32964.467),
(199.0, 342.08, 20.186),
Expand Down Expand Up @@ -169,7 +169,7 @@ mod tests {
let accuracy = 11; // TODO: Improve accuracy
let solstice_december_2012 = parse_time("2012-12-21T11:11:37.00+00:00");

let times = vec![
let times = [
get_previous_december_solstice(parse_time("2014-06-01T00:00:00.00+00:00")),
parse_time("2013-06-01T00:00:00.00+00:00"),
];
Expand All @@ -184,7 +184,7 @@ mod tests {
let accuracy = 13; // TODO: Improve accuracy
let solstice_december_2013 = parse_time("2013-12-21T17:11:00.00+00:00");

let times = vec![
let times = [
get_next_december_solstice(parse_time("2012-06-01T00:00:00.00+00:00")),
parse_time("2013-06-01T00:00:00.00+00:00"),
];
Expand Down
4 changes: 2 additions & 2 deletions src/ephemeris.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn get_ephemeris(timestamp: i64, longitude: f64, latitude: f64) -> BTreeMap<

events.insert(timestamp, "Current".to_string());

let es = vec![
let es = [
("Equinox", get_next_march_equinox(day_begin_at)),
("Equinox", get_next_september_equinox(day_begin_at)),
("Solstice", get_next_december_solstice(day_begin_at)),
Expand All @@ -29,7 +29,7 @@ pub fn get_ephemeris(timestamp: i64, longitude: f64, latitude: f64) -> BTreeMap<
}

let n = get_lunation_number(day_begin_at); // FIXME: Potential bug here
let es = vec![
let es = [
("New Moon", get_new_moon(n)),
("First Quarter Moon", get_first_quarter_moon(n + 0.25)),
("Full Moon", get_full_moon(n + 0.50)),
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn main() {
let args: Vec<String> = env::args().map(|arg|
// Encode float arguments that can be negative to avoid getopts panic
// from unrecognized options.
if let Some(x) = arg.parse::<f64>().ok() {
if let Ok(x) = arg.parse() {
encode_float(x)
} else {
arg
Expand Down Expand Up @@ -77,7 +77,7 @@ fn main() {

// Convert geodate string back into unix timestamp
if matches.free.len() == 4 && matches.free[3].contains(":") {
println!("{}", get_timestamp(format, matches.free[3].clone(), lon));
println!("{}", get_timestamp(&format, &matches.free[3], lon));
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/moon_phase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn get_time_of(phase: MoonPhase, lunation_number: f64) -> i64 {
// Correction to be added to JDE

// [New Moon, First Quarter, Full Moon, Last Quarter]
let num_cors = vec![
let num_cors = [
[-0.40720, -0.62801, -0.40614, -0.62801],
[ 0.17241, 0.17172, 0.17302, 0.17172],
[ 0.01608, -0.01183, 0.01614, -0.01183],
Expand Down Expand Up @@ -104,7 +104,7 @@ fn get_time_of(phase: MoonPhase, lunation_number: f64) -> i64 {

// Multiply each previous terms by E to a given power
// [new moon, first quarter, full moon, last quarter]
let pow_cors = vec![
let pow_cors = [
[0, 0, 0, 0],
[1, 1, 1, 1],
[0, 1, 0, 1],
Expand Down Expand Up @@ -138,7 +138,7 @@ fn get_time_of(phase: MoonPhase, lunation_number: f64) -> i64 {
let terms = [s, m, f, o];

// [new and full moon, first and last quarter]
let mul_cors = vec![
let mul_cors = [
[[ 0.0, 1.0, 0.0, 0.0], [ 0.0, 1.0, 0.0, 0.0]],
[[ 1.0, 0.0, 0.0, 0.0], [ 1.0, 0.0, 0.0, 0.0]],
[[ 0.0, 2.0, 0.0, 0.0], [ 1.0, 1.0, 0.0, 0.0]],
Expand Down
4 changes: 2 additions & 2 deletions src/moon_transit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn get_moon_position(julian_day: f64) -> (f64, f64, f64) {

// Periodic terms for the longitude, distance and latitude of the Moon
// D, M, M', F, sine, cosine, sine
let terms = vec![
let terms = [
(0.0, 0.0, 1.0, 0.0, 6_288_774.0, -20_905_355.0, 0.0),
(2.0, 0.0, -1.0, 0.0, 1_274_027.0, -3_699_111.0, 0.0),
(2.0, 0.0, 0.0, 0.0, 658_314.0, -2_955_968.0, 0.0),
Expand Down Expand Up @@ -376,7 +376,7 @@ mod tests {
#[test]
fn get_moonrise_test() {
let accuracy = 90;
let times = vec![
let times = [
("2000-01-01T01:50:14+00:00", "2000-01-01T12:00:00+00:00", 0.0, 0.0),
("2000-01-01T02:37:20+00:00", "2000-01-01T12:00:00+00:00", 50.0, 0.0),
("2000-01-01T06:06:16+00:00", "2000-01-01T12:00:00+00:00", 50.0, -50.0),
Expand Down
15 changes: 7 additions & 8 deletions src/reverse.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use geodate::*;

use alloc::string::String;
use alloc::vec::Vec;

/// Reverse a geodate into a timestamp
pub fn get_timestamp(format: String, date: String, longitude: f64) -> i64 {
let y = date_year(date.clone());
let n = date_index(date.clone());
pub fn get_timestamp(format: &str, date: &str, longitude: f64) -> i64 {
let y = date_year(date);
let n = date_index(date);

// Approximate timestamps of bounds
let mut min = (y - 2) * 365 * 86400;
Expand All @@ -30,7 +29,7 @@ pub fn get_timestamp(format: String, date: String, longitude: f64) -> i64 {

loop {
let mid = (min + max) / 2;
let i = date_index(get_formatted_date(&format, mid, longitude));
let i = date_index(&get_formatted_date(&format, mid, longitude));
if i == n || mid == min || mid == max {
return mid;
}
Expand All @@ -43,7 +42,7 @@ pub fn get_timestamp(format: String, date: String, longitude: f64) -> i64 {
}

// Extract year from a geodate string
fn date_year(date: String) -> i64 {
fn date_year(date: &str) -> i64 {
let parts: Vec<_> = date.split(":").collect();

let y = match parts.len() {
Expand All @@ -56,8 +55,8 @@ fn date_year(date: String) -> i64 {
}

// Transform a geodate string into an integer for comparison
fn date_index(date: String) -> i64 {
let year = date_year(date.clone());
fn date_index(date: &str) -> i64 {
let year = date_year(date);
let mut index = date.replace(":", "").parse::<i64>().unwrap();
if index < 0 { // Special case for negative years
index = (year + 0) * 100_000_000 - (index % 100_000_000);
Expand Down
10 changes: 5 additions & 5 deletions src/sun_transit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub fn nutation(julian_century: f64) -> (f64, f64) {
let pm = modulo(pm, 360.0);
let fm = modulo(fm, 360.0);

let terms = vec![
let terms = [
// D M M' F Ω sine coef cosine coef
[ 0.0, 0.0, 0.0, 0.0, 1.0, -171_996.0, -174.2, 92_025.0, 8.9],
[-2.0, 0.0, 0.0, 2.0, 2.0, -13_187.0, -1.6, 5736.0, -3.1],
Expand Down Expand Up @@ -296,7 +296,7 @@ mod tests {
get_noon(parse_time("1992-10-13T00:00:00+00:00"), 174.0);

// http://www.esrl.noaa.gov/gmd/grad/solcalc/
let times = vec![
let times = [
("2010-06-21T12:01:46+00:00", "2010-06-21T12:00:00+00:00", 45.0, 0.0),
("2010-09-23T11:52:25+00:00", "2010-09-23T12:00:00+00:00", 45.0, 0.0),
("2010-12-21T11:58:03+00:00", "2010-12-21T12:00:00+00:00", 45.0, 0.0)
Expand All @@ -310,7 +310,7 @@ mod tests {
#[test]
fn get_midnight_test() {
// http://www.esrl.noaa.gov/gmd/grad/solcalc/
let times = vec![
let times = [
("2000-01-01T00:03:18+00:00", "2000-01-01T12:00:00+00:00", 45.0, 0.0),
("1973-03-20T00:07:32+00:00", "1973-03-20T12:00:00+00:00", 45.0, 0.0),
("1973-03-21T00:07:15+00:00", "1973-03-21T12:00:00+00:00", 45.0, 0.0),
Expand All @@ -327,7 +327,7 @@ mod tests {

// TODO: Test at latitudes > 70
// http://www.esrl.noaa.gov/gmd/grad/solcalc/
let times = vec![
let times = [
("2010-06-21T04:13:15+00:00", "2010-06-21T12:00:00+00:00", 45.0, 0.0),
("2010-09-23T05:48:17+00:00", "2010-09-23T12:00:00+00:00", 45.0, 0.0),
("2010-12-21T07:35:09+00:00", "2010-12-21T12:00:00+00:00", 45.0, 0.0),
Expand All @@ -350,7 +350,7 @@ mod tests {

// TODO: Test at latitudes > 70
// http://www.esrl.noaa.gov/gmd/grad/solcalc/
let times = vec![
let times = [
("2010-06-21T19:50:16+00:00", "2010-06-21T12:00:00+00:00", 45.0, 0.0),
("2010-09-23T17:56:34+00:00", "2010-09-23T12:00:00+00:00", 45.0, 0.0),
("2010-12-21T16:20:58+00:00", "2010-12-21T12:00:00+00:00", 45.0, 0.0),
Expand Down
Loading