Skip to content

Commit 4dee54a

Browse files
Use ArgParser API, add CsvWriter, simplify solver
Refactor CLI option handling to use the fluent lunar::ArgParser API (methods now return ArgParser& for chaining) and replace ad-hoc OptMap/apply_opt usage across many parse functions (cli and query). Added parse_one/parse_all usage and included the new header where needed. Introduced CSV helpers: csv_num and a CsvWriter class (with header checks and consistent numeric formatting) in src/common/output.hpp. Simplified root solving and removed the threaded worker machinery: eliminated RootCtx, run_wkr, and related threading/atomic includes so run_roots performs serial solves. In spc_ephem, replaced std::once_flag with a simple bool kern_loaded and removed unused mutex include. Also updated includes (added <vector>, <stdexcept> where used) and small API cleanups in include headers. Files touched include: include/lunar/arg_parser.hpp, include/lunar/rt_solver.hpp, include/lunar/spc_ephem.hpp, src/calendar.cpp, src/cli/*, src/query/*, src/common/output.hpp and others to wire the changes.
1 parent 99a6eef commit 4dee54a

21 files changed

+719
-887
lines changed

include/lunar/arg_parser.hpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ class ArgParser{
1414
using Action=std::function<void(const std::vector<std::string>&,
1515
std::size_t&,const std::string&)>;
1616

17-
void add(const std::string&opt,Action action){
17+
ArgParser&add(const std::string&opt,Action action){
1818
actions_[opt]=std::move(action);
19+
return *this;
1920
}
2021

21-
void add_flag(const std::string&opt,std::function<void()> action){
22-
add(opt,[fn=std::move(action)](const std::vector<std::string>&,
23-
std::size_t&,const std::string&){ fn(); });
22+
ArgParser&add_flag(const std::string&opt,std::function<void()> action){
23+
return add(opt,[fn=std::move(action)](const std::vector<std::string>&,
24+
std::size_t&,const std::string&){
25+
fn();
26+
});
2427
}
2528

26-
void add_value(const std::string&opt,
27-
std::function<void(const std::string&)> action){
28-
add(opt,[fn=std::move(action)](const std::vector<std::string>&args,
29-
std::size_t&idx,const std::string&name){
29+
ArgParser&add_value(const std::string&opt,
30+
std::function<void(const std::string&)> action){
31+
return add(opt,[fn=std::move(action)](const std::vector<std::string>&args,
32+
std::size_t&idx,
33+
const std::string&name){
3034
fn(require_value(args,idx,name));
3135
});
3236
}

include/lunar/rt_solver.hpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
#pragma once
22

3-
#include<atomic>
4-
#include<cstddef>
53
#include<string>
6-
#include<vector>
7-
8-
struct SolLunCal;
94

105
struct RootTask{
116
std::string kind;
@@ -14,13 +9,3 @@ struct RootTask{
149
double eps_days;
1510
int max_iter;
1611
};
17-
18-
struct RootCtx{
19-
SolLunCal*self;
20-
const std::vector<RootTask>*tasks;
21-
std::vector<double>*results;
22-
std::vector<std::string>*errors;
23-
std::atomic<std::size_t>*cursor;
24-
};
25-
26-
void run_wkr(RootCtx*ctx);

include/lunar/spc_ephem.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include<map>
44
#include<memory>
5-
#include<mutex>
65
#include<string>
76
#include<utility>
87
#include<vector>
@@ -38,7 +37,7 @@ struct EphRead{
3837
int MOON;
3938
std::map<int,std::string> id_name;
4039
std::shared_ptr<SpkFile> kern;
41-
std::once_flag kern_flag;
40+
bool kern_loaded=false;
4241
bool use_series=false;
4342

4443
explicit EphRead(const std::string&path);

src/calendar.cpp

Lines changed: 7 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,11 @@
66
#include<iomanip>
77
#include<iostream>
88
#include<limits>
9-
#include<memory>
109
#include<stdexcept>
11-
#include<thread>
1210
#include<vector>
1311

1412
namespace{
1513

16-
constexpr std::size_t kMaxWork=8;
17-
1814
std::string clean_txt(std::string text){
1915
for(char&c : text){
2016
if(c=='\t'||c=='\r'||c=='\n'){
@@ -306,65 +302,16 @@ SolLunCal::run_roots(const std::vector<RootTask>&tasks){
306302
return {results,errors};
307303
}
308304

309-
auto run_serial=[&](){
310-
for(std::size_t idx=0;idx<tasks.size();++idx){
311-
const auto&task=tasks[idx];
312-
try{
313-
results[idx]=newton(task.kind,task.jd_initial,task.target,
314-
task.eps_days,task.max_iter);
315-
}catch(const std::exception&ex){
316-
errors[idx]=ex.what();
317-
}catch(...){
318-
errors[idx]="unknown error";
319-
}
320-
}
321-
};
322-
323-
if(tasks.size()==1){
324-
run_serial();
325-
return {results,errors};
326-
}
327-
328-
unsigned int hc=std::thread::hardware_concurrency();
329-
std::size_t wk_count=hc==0?4:static_cast<std::size_t>(hc);
330-
wk_count=std::min<std::size_t>(wk_count,tasks.size());
331-
wk_count=std::min<std::size_t>(wk_count,kMaxWork);
332-
if(wk_count<=1){
333-
run_serial();
334-
return {results,errors};
335-
}
336-
337-
try{
338-
std::vector<std::unique_ptr<SolLunCal>> workers;
339-
workers.reserve(wk_count);
340-
for(std::size_t i=0;i<wk_count;++i){
341-
workers.push_back(std::make_unique<SolLunCal>(eph));
342-
}
343-
344-
std::atomic<std::size_t> cursor{0};
345-
std::vector<RootCtx> ctxs(wk_count);
346-
std::vector<std::thread> threads;
347-
threads.reserve(wk_count);
348-
305+
for(std::size_t idx=0;idx<tasks.size();++idx){
306+
const auto&task=tasks[idx];
349307
try{
350-
for(std::size_t i=0;i<wk_count;++i){
351-
ctxs[i]={workers[i].get(),&tasks,&results,&errors,&cursor};
352-
threads.emplace_back(run_wkr,&ctxs[i]);
353-
}
308+
results[idx]=newton(task.kind,task.jd_initial,task.target,
309+
task.eps_days,task.max_iter);
310+
}catch(const std::exception&ex){
311+
errors[idx]=ex.what();
354312
}catch(...){
355-
for(auto&th : threads){
356-
if(th.joinable()){
357-
th.join();
358-
}
359-
}
360-
throw;
313+
errors[idx]="unknown error";
361314
}
362-
363-
for(auto&th : threads){
364-
th.join();
365-
}
366-
}catch(...){
367-
run_serial();
368315
}
369316

370317
return {results,errors};

0 commit comments

Comments
 (0)