第九版

This commit is contained in:
YuWuKunCheng
2026-06-05 09:02:54 +08:00
parent 58c85dc7cd
commit 823a24d364
49 changed files with 7280 additions and 7442 deletions
+92 -41
View File
@@ -29,14 +29,29 @@ use std::sync::atomic::{AtomicI64, Ordering};
use std::sync::{Arc, RwLock};
/// 中枢 — 三段虚线重叠区间构成的价格中枢
/// 可变字段使用 Cell/RefCell 实现内部可变性,确保 Rc 指针身份一致
///
/// 可变字段使用 AtomicI64 / RwLock 实现内部可变性,确保多 Arc 共享时可修改。
///
/// 字段:
/// - 序号: 中枢序号,同一级别内递增
/// - 标识: 中枢标识,格式如 "笔中枢<0>" 或 "线段中枢<1>"
/// - 级别: 中枢级别(笔中枢=1,线段中枢=2 等)
/// - 基础序列: 构成中枢的虚线序列(至少 3 根,延伸后可多至 9 根甚至更多)
/// - 第三买卖线: 第三类买卖点对应的虚线(离开中枢后不回中枢的虚线)
/// - 本级_第三买卖线: 本级第三类买卖点对应的虚线
#[derive(Debug)]
pub struct {
/// 中枢序号,同一级别内递增
pub : AtomicI64,
/// 中枢标识,格式如 "笔中枢<0>" 或 "线段中枢<1>"
pub : RwLock<String>,
/// 中枢级别(笔中枢=1,线段中枢=2 等)
pub : AtomicI64,
/// 构成中枢的虚线序列(至少 3 根,延伸后可多至 9+ 根)
pub : RwLock<Vec<Arc<线>>>,
/// 第三类买卖点对应的虚线(离开中枢后不回中枢的虚线)
pub 线: RwLock<Option<Arc<线>>>,
/// 本级第三类买卖点对应的虚线
pub _第三买卖线: RwLock<Option<Arc<线>>>,
}
@@ -54,6 +69,7 @@ impl Clone for 中枢 {
}
impl {
/// 创建新中枢(最多取前 3 根虚线作为基础序列)
pub fn new(: i64, : String, : i64, : Vec<Arc<线>>) -> Self {
Self {
: AtomicI64::new(),
@@ -65,12 +81,14 @@ impl 中枢 {
}
}
pub fn 线(&self, 线: Arc<线>) {
/// 向基础序列尾部添加虚线(中枢延伸),并清除第三买卖线
pub fn _添加虚线(&self, 线: Arc<线>) {
self..write().unwrap().push(线);
*self._第三买卖线.write().unwrap() = None;
*self.线.write().unwrap() = None;
}
/// 返回图表标题字符串,格式为 "文.标识:文.周期:中枢标识:序号"
pub fn (&self) -> String {
format!(
"{}:{}:{}:{}",
@@ -81,14 +99,17 @@ impl 中枢 {
)
}
/// 返回基础序列的最后一根虚线(当前离开段)
pub fn (&self) -> Arc<线> {
Arc::clone(&self..read().unwrap()[self..read().unwrap().len() - 1])
}
/// 返回中枢方向(与基础序列第一段方向相反)
pub fn (&self) -> {
self..read().unwrap()[0].().()
}
/// 中枢上沿 = min(前三段的高)
pub fn (&self) -> f64 {
self..read().unwrap()[..3]
.iter()
@@ -97,6 +118,7 @@ impl 中枢 {
.unwrap_or(0.0)
}
/// 中枢下沿 = max(前三段的低)
pub fn (&self) -> f64 {
self..read().unwrap()[..3]
.iter()
@@ -105,6 +127,7 @@ impl 中枢 {
.unwrap_or(0.0)
}
/// 中枢最高点 = max(所有段的高)
pub fn (&self) -> f64 {
self.
.read()
@@ -115,6 +138,7 @@ impl 中枢 {
.unwrap_or(0.0)
}
/// 中枢最低点 = min(所有段的低)
pub fn (&self) -> f64 {
self.
.read()
@@ -125,10 +149,12 @@ impl 中枢 {
.unwrap_or(0.0)
}
/// 返回基础序列第一段的起点分型
pub fn (&self) -> Arc<> {
Arc::clone(&self..read().unwrap()[0].)
}
/// 返回基础序列最后一段的终点分型
pub fn (&self) -> Arc<> {
Arc::clone(
&*self..read().unwrap()[self..read().unwrap().len() - 1]
@@ -138,8 +164,9 @@ impl 中枢 {
)
}
pub fn 线(&self, 线: Arc<线>) {
*self.线.write().unwrap() = Some(线);
/// 设置第三类买卖点对应的虚线
pub fn 线(&self, 线: Option<Arc<线>>) {
*self.线.write().unwrap() = 线;
}
/// 获取序列 — 基础序列 + 第三买卖线(若有)
@@ -151,6 +178,7 @@ impl 中枢 {
}
/// 返回序列化数据文本,用于调试和存储
pub fn (&self) -> String {
let 线_str = match &*self.线.read().unwrap() {
Some(x) => format!("{}", x),
@@ -175,7 +203,7 @@ impl 中枢 {
}
/// 校验中枢合法性
pub fn (&self, : &[Arc<线>]) -> bool {
pub fn _校验合法性(&self, : &[Arc<线>]) -> bool {
let mut = self..read().unwrap().clone();
let mut : Vec<Arc<线>> = Vec::new();
for in self..read().unwrap().iter() {
@@ -198,7 +226,7 @@ impl 中枢 {
}
if .len() < 3 {
*self.线.write().unwrap() = None;
self.线(None);
*self._第三买卖线.write().unwrap() = None;
return false;
}
@@ -248,7 +276,7 @@ impl 中枢 {
if let Some(ref 线) = 线_opt {
if .iter().any(|x| Arc::as_ptr(x) == Arc::as_ptr(线)) {
if !self..read().unwrap().last().unwrap().(线) {
*self.线.write().unwrap() = None;
self.线(None);
} else if !crate::types::::(
self.(),
self.(),
@@ -257,11 +285,11 @@ impl 中枢 {
)
.()
{
self.线(Arc::clone(线));
*self.线.write().unwrap() = None;
self._添加虚线(Arc::clone(线));
self.线(None);
}
} else {
*self.线.write().unwrap() = None;
self.线(None);
}
}
true
@@ -359,6 +387,7 @@ impl 中枢 {
pub fn (
: Arc<线>, : Arc<线>, : Arc<线>, : i64, : &str
) -> Self {
debug_assert!(Self::(&, &, &), "中枢.创建 基础检查失败");
Self::new(
0,
format!("{}中枢<{}>", , ..read().unwrap()),
@@ -367,8 +396,8 @@ impl 中枢 {
)
}
/// 从序列中获取中枢
pub fn (
/// _从序列中获取中枢
pub fn _从序列中获取中枢(
线: &[Arc<线>],
: ,
: &str,
@@ -385,33 +414,38 @@ impl 中枢 {
None
}
/// 向中枢序列尾部添加
pub fn (
/// _向中枢序列尾部添加
pub fn _向中枢序列尾部添加(
: &mut Vec<Arc<>>, : Arc<>
) {
if let Some() = .last() {
.
.store(..load(Ordering::Relaxed) + 1, Ordering::Relaxed);
// 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..load(Ordering::Relaxed) > new_last..load(Ordering::Relaxed)
{
panic!(
"向中枢序列尾部添加 序号错误 前last={} > new_last={}",
_last..load(Ordering::Relaxed),
new_last..load(Ordering::Relaxed)
);
}
let _last_序号 =
.()
.last()
.unwrap()
.
.load(Ordering::Relaxed);
let new_last_序号 =
.()
.last()
.unwrap()
.
.load(Ordering::Relaxed);
if _last_序号 > new_last_序号 {
panic!(
"向中枢序列尾部添加 序号错误 前last={} > new_last={}",
_last_序号, new_last_序号
);
}
}
.push();
}
/// 从中枢序列尾部弹出
pub fn (
pub fn _从中枢序列尾部弹出(
: &mut Vec<Arc<>>,
: &Arc<>,
) -> Option<Arc<>> {
@@ -448,7 +482,7 @@ impl 中枢 {
let = 线
.iter()
.position(|x| Arc::as_ptr(x) == Arc::as_ptr())
.unwrap_or(i - 1);
.expect("中枢.分析: 左元素不在虚线序列中");
if && (..load(Ordering::Relaxed) == 0 || == 0) {
continue;
}
@@ -470,7 +504,7 @@ impl 中枢 {
..load(Ordering::Relaxed),
,
));
Self::(, );
Self::_向中枢序列尾部添加(, );
// Python: return 中枢递归分析(虚线序列, 中枢序列, ...)
Self::(线, , , , _层级);
return;
@@ -483,10 +517,10 @@ impl 中枢 {
let mut _idx = .len() - 1;
// Validate via shared reference (中枢 uses RwLock internally)
let needs_pop = ![_idx].(线);
let needs_pop = ![_idx]._校验合法性(线);
if needs_pop {
let = Arc::clone(&[_idx]);
Self::(, &);
Self::_从中枢序列尾部弹出(, &);
Self::(线, , , , _层级);
return;
}
@@ -527,12 +561,29 @@ impl 中枢 {
.(&线)
};
if needs_三买 {
[_idx].线(线.clone());
[_idx].线(Some(线.clone()));
}
} else {
if .is_empty() {
// 仍在范围内:延伸中枢
[_idx].线(线);
debug_assert!(
[_idx]
.
.read()
.unwrap()
.last()
.unwrap()
.(&线),
"中枢延伸: 不连续 {}, {}",
[_idx]
.
.read()
.unwrap()
.last()
.unwrap(),
线
);
[_idx]._添加虚线(线);
} else {
.push(线);
}
@@ -548,9 +599,9 @@ impl 中枢 {
.unwrap()
.()
.();
match Self::(&, , ) {
match Self::_从序列中获取中枢(&, , ) {
Some() => {
Self::(, );
Self::_向中枢序列尾部添加(, );
// Python: 当前中枢 = 新中枢
_idx = .len() - 1;
= [_idx].();
@@ -732,7 +783,7 @@ mod tests {
assert_eq!(..load(Ordering::Relaxed), 99);
// RefCell 第三买卖线读写
.线(Arc::clone(&1));
.线(Some(Arc::clone(&1)));
assert!(.线.read().unwrap().is_some());
assert_eq!(
Arc::as_ptr(.线.read().unwrap().as_ref().unwrap()),
@@ -764,7 +815,7 @@ mod tests {
);
assert_eq!(..read().unwrap().len(), 3);
.线(Arc::clone(&4));
._添加虚线(Arc::clone(&4));
assert_eq!(..read().unwrap().len(), 4);
assert_eq!(
Arc::as_ptr(&..read().unwrap()[3]),
@@ -785,12 +836,12 @@ mod tests {
1,
vec![Arc::clone(&1), Arc::clone(&2), Arc::clone(&3)],
);
.线(Arc::clone(&1));
.线(Some(Arc::clone(&1)));
*._第三买卖线.write().unwrap() = Some(Arc::clone(&2));
assert!(.线.read().unwrap().is_some());
assert!(._第三买卖线.read().unwrap().is_some());
.线(Arc::clone(&4));
._添加虚线(Arc::clone(&4));
// 添加虚线后第三买卖线被清除
assert!(.线.read().unwrap().is_none());
assert!(._第三买卖线.read().unwrap().is_none());
@@ -812,7 +863,7 @@ mod tests {
1,
vec![Arc::clone(&1), Arc::clone(&2), Arc::clone(&3)],
);
.线(Arc::clone(&1));
.线(Some(Arc::clone(&1)));
let = .clone();
@@ -885,7 +936,7 @@ mod tests {
assert_eq!(2..load(Ordering::Relaxed), 88);
// 通过 rc1 添加虚线
1.线(Arc::clone(&4));
1._线(Arc::clone(&4));
assert_eq!(2..read().unwrap().len(), 4);
// 验证共享的 Arc<虚线> 指针一致