跳过已排序快照的重复排序

This commit is contained in:
boris
2026-08-28 15:49:02 +08:00
parent c1e7fc91e4
commit e261d93ce5
+21 -8
View File
@@ -1386,7 +1386,12 @@ impl DataSet {
mut bundles: Vec<DailySnapshotBundle>,
execution_quotes: Vec<IntradayExecutionQuote>,
) -> Result<Self, DataSetError> {
bundles.sort_by_key(|bundle| bundle.date);
if !bundles
.windows(2)
.all(|window| window[0].date <= window[1].date)
{
bundles.sort_by_key(|bundle| bundle.date);
}
if let Some(pair) = bundles.windows(2).find(|pair| pair[0].date == pair[1].date) {
return Err(DataSetError::DuplicateDailyBundle { date: pair[1].date });
}
@@ -1435,14 +1440,10 @@ impl DataSet {
|row| row.date,
|row| row.symbol.as_str(),
)?;
bundle.market.sort_by(|left, right| left.symbol.cmp(&right.symbol));
sort_rows_by_symbol_if_needed(&mut bundle.market, |row| row.symbol.as_str());
bundle.factors = normalize_factor_snapshots(bundle.factors);
bundle
.factors
.sort_by(|left, right| left.symbol.cmp(&right.symbol));
bundle
.candidates
.sort_by(|left, right| left.symbol.cmp(&right.symbol));
sort_rows_by_symbol_if_needed(&mut bundle.factors, |row| row.symbol.as_str());
sort_rows_by_symbol_if_needed(&mut bundle.candidates, |row| row.symbol.as_str());
if !bundle.market.is_empty() {
grouped.market_by_date.insert(date, bundle.market);
}
@@ -3844,6 +3845,18 @@ where
}
}
fn sort_rows_by_symbol_if_needed<T, F>(rows: &mut Vec<T>, symbol_of: F)
where
F: Fn(&T) -> &str + Copy,
{
if !rows
.windows(2)
.all(|window| symbol_of(&window[0]) <= symbol_of(&window[1]))
{
rows.sort_by(|left, right| symbol_of(left).cmp(symbol_of(right)));
}
}
fn build_symbol_id_index(
instruments: &HashMap<String, Instrument>,
market_by_date: &BTreeMap<NaiveDate, Vec<DailyMarketSnapshot>>,