第二版

This commit is contained in:
YuWuKunCheng
2026-05-25 11:07:08 +08:00
parent f92eef11fb
commit bf96517c9d
21 changed files with 1599 additions and 1002 deletions
+167 -63
View File
@@ -12,7 +12,11 @@ pub struct 笔;
impl {
/// 获取可成笔的缠K数量(考虑弱化模式)
pub fn K数量(K序列: &[Rc<K线>], : &[Rc<线>], : &) -> usize {
pub fn K数量(
K序列: &[Rc<K线>],
: &[Rc<线>],
: &,
) -> usize {
let = K序列.len();
if >= . as usize {
return ;
@@ -31,13 +35,19 @@ impl 笔 {
if !.is_empty() {
// Try both high and low points (Python: 根据缠K找笔(笔序列, 实际高点) or 根据缠K找笔(笔序列, 实际低点))
let = .as_ref()
let =
.as_ref()
.and_then(|h| Self::K找笔(, h, 1))
.or_else(|| .as_ref().and_then(|l| Self::K找笔(, l, 1)));
.or_else(|| {
.as_ref()
.and_then(|l| Self::K找笔(, l, 1))
});
if let Some(ref ) = {
if let (Some(ref _k), Some(ref _k)) = (&, &) {
let = 1 + (_k.K线. - _k.K线.).unsigned_abs() as usize;
let =
1 + (_k.K线. - _k.K线.).unsigned_abs() as usize;
// 向上笔
if .().() && _k. < .() {
if >= ._原始数量 as usize {
@@ -62,15 +72,25 @@ impl 笔 {
if K序列.len() < 2 {
return K序列.first().cloned();
}
let max_高 = K序列.iter().map(|k| k.).fold(f64::NEG_INFINITY, f64::max);
let max_高 = K序列
.iter()
.map(|k| k.)
.fold(f64::NEG_INFINITY, f64::max);
// 排除最高值
let filtered: Vec<&Rc<K线>> = K序列.iter().filter(|k| k. != max_高).collect();
if filtered.is_empty() {
return K序列.first().cloned();
}
// 筛选次高值
let second_高 = filtered.iter().map(|k| k.).fold(f64::NEG_INFINITY, f64::max);
let mut candidates: Vec<&Rc<K线>> = filtered.iter().filter(|k| k. == second_高).copied().collect();
let second_高 = filtered
.iter()
.map(|k| k.)
.fold(f64::NEG_INFINITY, f64::max);
let mut candidates: Vec<&Rc<K线>> = filtered
.iter()
.filter(|k| k. == second_高)
.copied()
.collect();
// 按时间戳排序
candidates.sort_by(|a, b| a..cmp(&b.));
if {
@@ -93,7 +113,11 @@ impl 笔 {
}
// 筛选次低值
let second_低 = filtered.iter().map(|k| k.).fold(f64::INFINITY, f64::min);
let mut candidates: Vec<&Rc<K线>> = filtered.iter().filter(|k| k. == second_低).copied().collect();
let mut candidates: Vec<&Rc<K线>> = filtered
.iter()
.filter(|k| k. == second_低)
.copied()
.collect();
// 按时间戳排序
candidates.sort_by(|a, b| a..cmp(&b.));
if {
@@ -108,7 +132,10 @@ impl 笔 {
if K序列.is_empty() {
return None;
}
let max_高 = K序列.iter().map(|k| k.).fold(f64::NEG_INFINITY, f64::max);
let max_高 = K序列
.iter()
.map(|k| k.)
.fold(f64::NEG_INFINITY, f64::max);
let mut candidates: Vec<&Rc<K线>> = K序列.iter().filter(|k| k. == max_高).collect();
if candidates.is_empty() {
return Some(Rc::clone(&K序列[0]));
@@ -142,30 +169,84 @@ impl 笔 {
}
/// 判断笔的相对关系是否合理
pub fn (: &线, _配置: &) -> bool {
let = &.;
let = &.;
pub fn (: &线, : &) -> bool {
let = &.;
let = &.;
// 向上笔:文(底)低 → 武(顶)高
if .().() {
return . > .;
let = if . {
let _rc = Rc::clone(&.);
let _rc = Rc::clone(&.);
let _元素: [Option<&Rc<K线>>; 3] =
[..as_ref(), Some(&_rc), ..as_ref()];
let : Vec<&Rc<K线>> = _元素.iter().filter_map(|x| *x).collect();
let =
.iter()
.map(|k| k.)
.fold(f64::NEG_INFINITY, f64::max);
let = .iter().map(|k| k.).fold(f64::INFINITY, f64::min);
let _右: Option<&Rc<K线>> = if ._包括右 {
..as_ref()
} else {
None
};
let _元素: [Option<&Rc<K线>>; 3] = [..as_ref(), Some(&_rc), _右];
let : Vec<&Rc<K线>> = _元素.iter().filter_map(|x| *x).collect();
let =
.iter()
.map(|k| k.)
.fold(f64::NEG_INFINITY, f64::max);
let = .iter().map(|k| k.).fold(f64::INFINITY, f64::min);
crate::types::::(, , , )
} else {
let = crate::types::::(
..,
..,
..,
..,
);
if .K线包含整笔 {
let = &..K线;
let = &..K线;
if crate::types::::(., ., ., .)
.()
{
return false;
}
}
};
if .() == crate::types:::: {
return .();
}
// 向下笔:文(顶)高 → 武(底)低
. < .
.()
}
/// 以文会友 — 根据起点分型找笔
pub fn (: &[Rc<线>], : &Rc<>) -> Option<Rc<线>> {
.iter().find(|b| Rc::as_ptr(&b.) == Rc::as_ptr()).cloned()
.iter()
.find(|b| Rc::as_ptr(&b.) == Rc::as_ptr())
.cloned()
}
/// 以武会友 — 根据终点分型找笔
pub fn (: &[Rc<线>], : &Rc<>) -> Option<Rc<线>> {
.iter().find(|b| Rc::as_ptr(&b.) == Rc::as_ptr()).cloned()
.iter()
.rev()
.find(|b| Rc::as_ptr(&b.) == Rc::as_ptr())
.cloned()
}
/// 根据缠K找对应的笔
pub fn K找笔(: &[Rc<线>], K: &Rc<K线>, : i64) -> Option<Rc<线>> {
pub fn K找笔(
: &[Rc<线>],
K: &Rc<K线>,
: i64,
) -> Option<Rc<线>> {
// Python iterates in reverse: for 筆 in 笔序列[::-1]
for b in .iter().rev() {
// Python: 筆.文.中.序号 - 偏移 <= 缠K.序号 <= 筆.武.中.序号
@@ -282,7 +363,8 @@ impl 笔 {
let = Rc::clone(.last().unwrap());
// Python line 2338: 时序检查 — skip out-of-order fractals
if . > . && .. - .. > 1 {
if . > . && .. - .. > 1
{
continue;
}
@@ -310,8 +392,12 @@ impl 笔 {
// Python line 2350: 分型结构相反 → 可能成笔
if . != . {
let _idx = K序列.iter().position(|k| Rc::as_ptr(k) == Rc::as_ptr(&.));
let _idx = K序列.iter().position(|k| Rc::as_ptr(k) == Rc::as_ptr(&.));
let _idx = K序列
.iter()
.position(|k| Rc::as_ptr(k) == Rc::as_ptr(&.));
let _idx = K序列
.iter()
.position(|k| Rc::as_ptr(k) == Rc::as_ptr(&.));
if let (Some(_idx), Some(_idx)) = (_idx, _idx) {
let = &K序列[_idx..=_idx];
@@ -339,7 +425,9 @@ impl 笔 {
// Python line 2369-2372: 武将
let = match . {
:: => Self::(, .),
:: => {
Self::(, .)
}
_ => Self::(, .),
};
@@ -377,7 +465,9 @@ impl 笔 {
} else {
// Python line 2388-2390: 元素不足 → 右元素扩展
if let Some(ref ) = . {
if let Some() = ::K序列中获取分型(K序列, ) {
if let Some() =
::K序列中获取分型(K序列, )
{
.push(::(Rc::new(), + 1));
continue;
}
@@ -398,8 +488,7 @@ impl 笔 {
let = Rc::clone(&);
Self::(, );
if let Some(k线序列) =
K线::(K序列, &., &.)
if let Some(k线序列) = K线::(K序列, &., &.)
{
let = match . {
:: => {
@@ -490,7 +579,8 @@ impl 笔 {
// Python line 2337-2341: 时序检查
let = Rc::clone(.last().unwrap());
if . > . && .. - .. > 1 {
if . > . && .. - .. > 1
{
println!("时序错误-{}, {}, {}", , , );
return ;
}
@@ -509,7 +599,13 @@ impl 笔 {
if {
Self::(, );
return Self::(
, , , K序列, _普K序列, + 1, ,
,
,
,
K序列,
_普K序列,
+ 1,
,
);
}
}
@@ -518,7 +614,8 @@ impl 笔 {
// Python line 2350: 分型结构相反 → 可能成笔
let = Rc::clone(.last().unwrap());
if . != . {
if let Some() = K线::(K序列, &., &.) {
if let Some() = K线::(K序列, &., &.)
{
let = Rc::new(线::(
Rc::clone(&),
Rc::clone(&),
@@ -549,7 +646,11 @@ impl 笔 {
,
);
return Self::(
, , , K序列, _普K序列,
,
,
,
K序列,
_普K序列,
+ 1,
,
);
@@ -559,9 +660,7 @@ impl 笔 {
// Python line 2369-2375: 武将 and笔形成
let = match . {
:: => {
Self::(&, .)
}
:: => Self::(&, .),
_ => Self::(&, .),
};
@@ -578,9 +677,7 @@ impl 笔 {
// Python line 2378-2385: 笔次级成笔
if . {
let = match . {
:: => {
Self::(&, .)
}
:: => Self::(&, .),
_ => Self::(&, .),
};
if let Some(ref _k) = {
@@ -595,8 +692,7 @@ impl 笔 {
} else {
// Python line 2388-2390: 元素不足 → 右元素扩展
if let Some(ref ) = . {
if let Some() =
::K序列中获取分型(K序列, )
if let Some() = ::K序列中获取分型(K序列, )
{
return Self::(
Rc::new(),
@@ -626,13 +722,10 @@ impl 笔 {
let = Rc::clone(&);
Self::(, );
if let Some(k线序列) =
K线::(K序列, &., &.)
if let Some(k线序列) = K线::(K序列, &., &.)
{
let = match . {
:: => {
Self::(&k线序列, .)
}
:: => Self::(&k线序列, .),
_ => Self::(&k线序列, .),
};
@@ -667,10 +760,11 @@ impl 笔 {
|| ck. == Some(::)
{
if let Some() =
::K序列中获取分型(K序列, ck)
::K序列中获取分型(
K序列, ck,
)
{
let _rc =
Rc::new();
let _rc = Rc::new();
= Self::(
Rc::clone(&_rc),
,
@@ -687,7 +781,11 @@ impl 笔 {
}
return Self::(
, , , K序列, _普K序列,
,
,
,
K序列,
_普K序列,
+ 1,
,
);
@@ -700,7 +798,13 @@ impl 笔 {
::(, );
} else {
return Self::(
, , , K序列, _普K序列, + 1, ,
,
,
,
K序列,
_普K序列,
+ 1,
,
);
}
}
@@ -714,20 +818,17 @@ impl 笔 {
: &mut Vec<Rc<>>,
: &mut Vec<Rc<线>>,
: Rc<>,
: Rc<线>,
mut : Rc<线>,
) {
.push();
let = if .is_empty() {
0
} else {
.last().unwrap(). + 1
};
let mut 线 = (*).clone();
线. = ;
if 线...is_none() && 线...is_none() {
线. = false;
if !.is_empty() {
let seg = Rc::make_mut(&mut );
seg. = .last().unwrap(). + 1;
if seg...is_none() && seg...is_none() {
seg. = false;
}
}
.push(Rc::new(线));
.push();
}
/// 自检 — 验证笔的有效性(文为实际高/低点,武为实际低/高点)
@@ -735,13 +836,15 @@ impl 笔 {
let = &.;
let = &.;
let = .K序列(&.K线序列);
if Self::K数量(&, , ) >= . as usize {
if Self::K数量(&, , ) >= . as usize
{
if .() == :: {
if let (Some(), Some()) = (
Self::(&, false),
Self::(&, .),
) {
if Rc::ptr_eq(&.., &) && Rc::ptr_eq(&.., &) {
if Rc::ptr_eq(&.., &) && Rc::ptr_eq(&.., &)
{
return true;
}
}
@@ -751,7 +854,8 @@ impl 笔 {
Self::(&, false),
Self::(&, .),
) {
if Rc::ptr_eq(&.., &) && Rc::ptr_eq(&.., &) {
if Rc::ptr_eq(&.., &) && Rc::ptr_eq(&.., &)
{
return true;
}
}
+117 -69
View File
@@ -1,86 +1,134 @@
use crate::config::;
use crate::kline::bar::K线;
use crate::structure::dash_line::线;
use crate::types::;
use std::rc::Rc;
/// 背驰分析 — 判断进入段和离开段之间是否存在背驰
pub struct ;
impl {
/// MACD背驰 — 离开段MACD柱子面积小于进入段
pub fn MACD背驰(: &线, : &线, K线序列: &[Rc<K线>], : &str) -> bool {
// 获取进入段和离开段对应的K线
let _始 = ... as usize;
let _终 = ... as usize;
let _始 = ... as usize;
let _终 = ... as usize;
/// MACD背驰 — MACD柱状线面积背驰
/// 方式: "总"=阳+|阴|总面积, 其他=按进入段方向选阳或阴
pub fn MACD背驰(
: &线, : &线, K线序列: &[Rc<K线>], : &str
) -> bool {
let MACD =
Self::_获取MACD面积(K线序列, &...K线, &...K线);
let MACD =
Self::_获取MACD面积(K线序列, &...K线, &...K线);
if _终 >= K线序列.len() || _终 >= K线序列.len() {
return false;
}
// 计算面积(绝对值求和)
let = if == "" {
MACD.().abs()
} else if .() == :: {
MACD..abs()
} else {
MACD..abs()
};
let = if == "" {
MACD.().abs()
} else if .() == :: {
MACD..abs()
} else {
MACD..abs()
};
let MACD = Self::(&K线序列[_始..=_终], );
let MACD = Self::(&K线序列[_始..=_终], );
// 背驰条件: 离开段面积绝对值小于进入段
let = MACD. + MACD..abs();
let = MACD. + MACD..abs();
if < f64::EPSILON {
return false;
}
<
<
}
/// 斜率背驰 — 离开段斜率小于进入段
/// 斜率背驰 — 价格斜率背驰
pub fn (: &线, : &线) -> bool {
let = (.. - ..).abs()
/ (.. - ..).abs() as f64;
let = (.. - ..).abs()
/ (.. - ..).abs() as f64;
let dx = (.. - ..) as f64;
let dy = .. - ..;
let = dy / dx;
<
let dx = (.. - ..) as f64;
let dy = .. - ..;
let = dy / dx;
if .() == :: {
.() > .() && .abs() < .abs()
} else {
.() < .() && .abs() < .abs()
}
}
/// 测度背驰 — 价格测度背驰判断
/// 测度背驰 — 价格时间测度背驰
pub fn (: &线, : &线) -> bool {
let = (.. - ..).abs();
let = (.. - ..).abs();
<
let dx = (.. - ..) as f64;
let dy = .. - ..;
let = (dx * dx + dy * dy).sqrt();
let dx = (.. - ..) as f64;
let dy = .. - ..;
let = (dx * dx + dy * dy).sqrt();
if .() == :: {
.() > .() && .abs() < .abs()
} else {
.() < .() && .abs() < .abs()
}
}
/// 全量背驰 — MACD + 斜率 + 测度 三者全满足
pub fn (: &线, : &线, K序列: &[Rc<K线>]) -> bool {
Self::MACD背驰(, , K序列, "")
&& Self::(, )
&& Self::(, )
&& Self::(, )
}
/// 任意背驰 — 任一条件满足即可
pub fn (: &线, : &线, K序列: &[Rc<K线>]) -> bool {
Self::MACD背驰(, , K序列, "")
|| Self::(, )
|| Self::(, )
|| Self::(, )
}
/// 配置背驰 — 根据配置选择判断方式
pub fn (: &线, : &线, K序列: &[Rc<K线>], : &) -> bool {
let mut result = true;
if .线_MACD {
result = result && Self::MACD背驰(, , K序列, "");
pub fn (
: &线,
: &线,
K序列: &[Rc<K线>],
: &,
) -> bool {
match (
.线_MACD,
.线_测度,
.线_斜率,
) {
(true, true, true) => {
Self::MACD背驰(, , K序列, "")
&& Self::(, )
&& Self::(, )
}
(false, false, false) => false,
(true, false, true) => {
Self::MACD背驰(, , K序列, "") && Self::(, )
}
(false, true, false) => Self::(, ),
(true, false, false) => Self::MACD背驰(, , K序列, ""),
(false, true, true) => {
Self::(, ) && Self::(, )
}
(false, false, true) => Self::(, ),
(true, true, false) => {
Self::MACD背驰(, , K序列, "") && Self::(, )
}
}
if .线_斜率 {
result = result && Self::(, );
}
if .线_测度 {
result = result && Self::(, );
}
result
}
/// 任选背驰
/// 任选背驰 — 至少两个条件满足(多数投票)
pub fn (: &线, : &线, K序列: &[Rc<K线>]) -> bool {
Self::(, , K序列)
let = [
Self::MACD背驰(, , K序列, ""),
Self::(, ),
Self::(, ),
];
.iter().filter(|&&x| x).count() >= 2
}
/// 背驰模式 — 根据模式字符串选择判断方式
@@ -95,40 +143,34 @@ impl 背驰分析 {
"全量" => Self::(, , K序列),
"任意" => Self::(, , K序列),
"配置" => Self::(, , K序列, ),
"相对" => Self::(, ),
_ => Self::(, , K序列, ),
"相对" => Self::(, , K序列),
_ => false,
}
}
// ---- 内部辅助 ----
fn (K线序列: &[Rc<K线>], : &str) -> MACD面积 {
fn _获取MACD面积(K线序列: &[Rc<K线>], : &Rc<K线>, : &Rc<K线>) -> MACD面积 {
let _idx = K线序列.iter().position(|k| Rc::as_ptr(k) == Rc::as_ptr());
let _idx = K线序列.iter().position(|k| Rc::as_ptr(k) == Rc::as_ptr());
let mut = 0.0f64;
let mut = 0.0f64;
for k in K线序列 {
if let Some(ref macd) = k.macd {
let hist = macd.MACD柱;
match {
"" => {
if hist > 0.0 {
+= hist
}
}
"" => {
if hist < 0.0 {
+= hist
}
}
"" | _ => {
if hist >= 0.0 {
+= hist
} else {
+= hist
}
if let (Some(), Some()) = (_idx, _idx) {
let (, ) = if <= { (, ) } else { (, ) };
for k in &K线序列[..=] {
if let Some(ref macd) = k.macd {
let hist = macd.MACD柱;
if hist >= 0.0 {
+= hist;
} else {
+= hist;
}
}
}
}
MACD面积 { , }
}
}
@@ -137,3 +179,9 @@ struct MACD面积 {
: f64,
: f64,
}
impl MACD面积 {
fn (&self) -> f64 {
self. + self.
}
}
+240 -63
View File
@@ -35,7 +35,10 @@ impl 中枢 {
pub fn (&self) -> String {
format!(
"{}:{}:{}:{}",
self.().., self.().., self., self.
self.()..,
self.()..,
self.,
self.
)
}
@@ -91,6 +94,15 @@ impl 中枢 {
self.线 = Some(线);
}
/// 获取序列 — 基础序列 + 第三买卖线(若有)
pub fn (&self) -> Vec<Rc<线>> {
let mut : Vec<Rc<线>> = self..clone();
if let Some(ref ) = self.线 {
.push(Rc::clone());
}
}
pub fn (&self) -> String {
let 线_str = match &self.线 {
Some(x) => format!("{}", x),
@@ -115,24 +127,158 @@ impl 中枢 {
}
/// 校验中枢合法性
pub fn (&self, 线: &[Rc<线>], _中枢序列: &[Rc<>]) -> bool {
// 1. 检查基础序列中的元素是否仍在虚线序列中
pub fn (&mut self, : &[Rc<线>]) -> bool {
let mut = self..clone();
let mut : Vec<Rc<线>> = Vec::new();
for in &self. {
if !线.iter().any(|x| Rc::as_ptr(x) == Rc::as_ptr()) {
if !.iter().any(|x| Rc::as_ptr(x) == Rc::as_ptr()) {
.push(Rc::clone());
}
}
if !.is_empty() {
let = &[0];
if let Some(pos) = self
.
.iter()
.position(|x| Rc::as_ptr(x) == Rc::as_ptr())
{
= self.[..pos].to_vec();
}
}
if .len() < 3 {
self.线 = None;
self._第三买卖线 = None;
return false;
}
self. = ;
let = self.();
let = self.();
= Vec::new();
for in &self. {
if crate::types::::(, , .(), .()).()
{
break;
}
.push(Rc::clone());
}
self. = ;
if self..len() < 3 {
return false;
}
for i in 1..self..len() {
let = &self.[i - 1];
let = &self.[i];
if !.() {
return false;
}
}
// 2. 检查前三根重叠是否有效
if self..len() >= 3 {
let = self.();
let = self.();
if <= {
if !crate::types::::(
self.[0].(),
self.[0].(),
self.[2].(),
self.[2].(),
)
.()
{
let = self.();
let = self.();
if > {
return false;
}
}
if let Some(ref 线) = self.线.clone() {
if .iter().any(|x| Rc::as_ptr(x) == Rc::as_ptr(线)) {
if !self..last().unwrap().(线) {
self.线 = None;
} else if !crate::types::::(
self.(),
self.(),
线.(),
线.(),
)
.()
{
self.线(Rc::clone(线));
self.线 = None;
}
} else {
self.线 = None;
}
}
true
}
/// 完整性 — 详见教你炒股票43:有关背驰的补习课
/// 不完整时下一个中枢大概率会与当前中枢发生扩展
pub fn (&self, : &str) -> bool {
if self.[0]. == "" {
return self.线.is_some();
}
let 线 = if == "" {
&self..last().unwrap()._中枢序列
} else {
&self..last().unwrap()._中枢序列
};
for in 线 {
if crate::types::::(
self.(),
self.(),
.(),
.(),
)
.()
{
return true;
}
}
false
}
/// 获取扩展中枢 — 当基础序列 >= 9 时生成扩展中枢
pub fn (
&self, : &mut Vec<Rc<>>, : &crate::config::
) {
if self..len() >= 9 {
let mut 线: Vec<Rc<线>> = Vec::new();
crate::algorithm::segment::线::(&self., &mut 线, );
::(
&线,
,
false,
&format!("{}_扩展中枢_", self.),
0,
);
}
}
/// 当前状态 — 详见教你炒股票49:利润率最大的操作模式
/// 返回当前中枢最后一段所处的位置关系:中枢之中/中枢之上/中枢之下
pub fn (&self) -> &str {
let = self..last().unwrap();
let _中 = if . == "" {
&..
} else {
&..last().unwrap()..
};
let = crate::types::::(self.(), self.(), _中., _中.);
if == crate::types:::: {
"中枢之上"
} else if == crate::types:::: {
"中枢之下"
} else {
"中枢之中"
}
}
// ---- 关联函数 ----
/// 基础检查 — 三根虚线是否能形成中枢
@@ -140,22 +286,27 @@ impl 中枢 {
if !.() || !.() {
return false;
}
let = [.(), .(), .()]
.iter()
.copied()
.min_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0);
let = [.(), .(), .()]
.iter()
.copied()
.max_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
.unwrap_or(0.0);
>
let = crate::types::::(.(), .(), .(), .());
matches!(
,
crate::types::::
| crate::types::::
| crate::types::::
| crate::types::::
| crate::types::::
)
}
/// 创建中枢
pub fn (: Rc<线>, : Rc<线>, : Rc<线>, : i64, : &str) -> Self {
Self::new(0, format!("{}中枢<{}>", , .), , vec![, , ])
pub fn (
: Rc<线>, : Rc<线>, : Rc<线>, : i64, : &str
) -> Self {
Self::new(
0,
format!("{}中枢<{}>", , .),
,
vec![, , ],
)
}
/// 从序列中获取中枢
@@ -177,27 +328,32 @@ impl 中枢 {
}
/// 向中枢序列尾部添加
pub fn (: &mut Vec<Rc<>>, : Rc<>) {
// Dedup
pub fn (
: &mut Vec<Rc<>>, mut : Rc<>
) {
if let Some() = .last() {
if .() == .() && .() == .() {
return;
let = Rc::make_mut(&mut );
. = . + 1;
// Python: assert seq[-1].获取序列()[-1].序号 <= new.获取序列()[-1].序号
let _seq = .();
let new_seq = .();
if let (Some(_last), Some(new_last)) = (_seq.last(), new_seq.last()) {
if _last. > new_last. {
panic!(
"向中枢序列尾部添加 序号错误 前last={} > new_last={}",
_last., new_last.
);
}
}
}
// Set序号: Python — if中枢序列: 待添加中枢.序号 = 中枢序列[-1].序号 + 1 else: stays 0
if let Some() = .last() {
let = . + 1;
// Clone to mutate since Rc is immutable
let mut cloned = (*).clone();
cloned. = ;
.push(Rc::new(cloned));
} else {
.push();
}
.push();
}
/// 从中枢序列尾部弹出
pub fn (: &mut Vec<Rc<>>, : &Rc<>) -> Option<Rc<>> {
pub fn (
: &mut Vec<Rc<>>,
: &Rc<>,
) -> Option<Rc<>> {
if .last().map(|x| Rc::as_ptr(x)) == Some(Rc::as_ptr()) {
.pop()
} else {
@@ -228,13 +384,17 @@ impl 中枢 {
if Self::(, , ) {
// Python: 序号 = 虚线序列.index(左)
let = 线.iter().position(|x| Rc::as_ptr(x) == Rc::as_ptr()).unwrap_or(i - 1);
let = 线
.iter()
.position(|x| Rc::as_ptr(x) == Rc::as_ptr())
.unwrap_or(i - 1);
if && (. == 0 || == 0) {
continue;
}
if >= 2 {
let = &线[ - 2];
let = crate::types::::(.(), .(), .(), .());
let =
crate::types::::(.(), .(), .(), .());
if .() && .().() {
continue;
}
@@ -259,46 +419,58 @@ impl 中枢 {
}
// 增量更新
let _idx = .len() - 1;
let = Rc::clone(&[_idx]);
let mut _idx = .len() - 1;
if !.(线, ) {
// Validate in-place via Rc::make_mut — avoids full中枢 struct clone
let needs_pop = {
let cur = Rc::make_mut(&mut [_idx]);
!cur.(线)
};
if needs_pop {
let = Rc::clone(&[_idx]);
Self::(, &);
Self::(线, , , , );
return;
}
// 找到当前中枢最后一个元素在虚线序列中的位置
let = &.[..len() - 1];
let = match 线
.iter()
.position(|x| Rc::as_ptr(x) == Rc::as_ptr())
{
Some(idx) => idx + 1,
None => return,
let = {
let cur = &[_idx];
let = &cur.[cur..len() - 1];
match 线
.iter()
.position(|x| Rc::as_ptr(x) == Rc::as_ptr())
{
Some(idx) => idx + 1,
None => return,
}
};
let = .();
let = .();
let mut = [_idx].();
let mut = [_idx].();
let mut : Vec<Rc<线>> = Vec::new();
let mut = (*).clone();
for i in ..线.len() {
let 线 = Rc::clone(&线[i]);
// 检查是否超出中枢范围(缺口)
if crate::types::::(, , 线.(), 线.()).() {
if crate::types::::(, , 线.(), 线.()).()
{
.push(线.clone());
// Python: if 当前中枢.基础序列[-1].之后是(当前虚线):
if ..last().unwrap().(&线) {
.线(线.clone());
let needs_三买 = {
let cur = &[_idx];
cur..last().unwrap().(&线)
};
if needs_三买 {
Rc::make_mut(&mut [_idx])
.线(线.clone());
}
} else {
if .is_empty() {
// 仍在范围内:延伸中枢
.线(线);
Rc::make_mut(&mut [_idx]).线(线);
} else {
.push(线);
}
@@ -306,12 +478,20 @@ impl 中枢 {
// 候选序列积满3个:尝试创建新中枢
while .len() >= 3 {
let = ..last().unwrap().().();
let = [_idx]
.
.last()
.unwrap()
.()
.();
match Self::(&, , ) {
Some() => {
[_idx] = Rc::new(.clone());
Self::(, );
return;
// Python: 当前中枢 = 新中枢
_idx = .len() - 1;
= [_idx].();
= [_idx].();
.clear();
}
None => {
.remove(0); // 滑动窗口
@@ -319,9 +499,6 @@ impl 中枢 {
}
}
}
// 更新当前中枢
[_idx] = Rc::new();
}
}
File diff suppressed because it is too large Load Diff