From a21a891c1c676f6e81b8e9d8d1c75bba63fb5763 Mon Sep 17 00:00:00 2001 From: seeleseelesee <2448828513@qq.com> Date: Wed, 21 Jan 2026 18:17:04 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E5=88=9D=E6=AD=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/memory/working_memory/sliding_window.rs | 85 +++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 src/memory/working_memory/sliding_window.rs diff --git a/src/memory/working_memory/sliding_window.rs b/src/memory/working_memory/sliding_window.rs new file mode 100644 index 0000000..6b79d99 --- /dev/null +++ b/src/memory/working_memory/sliding_window.rs @@ -0,0 +1,85 @@ +use std::collections::VecDeque + +//滑动窗口(容器、容量、标记窗口)通过标记窗口中对应索引是否为true来判断是否被标记 +pub struct SlidingWindow { + window: VecDeque, + capacity: usize, + tag_window: VecDeque, + tag_count: usize +} + +impl SlidingWindow { + //新建 + pub fn new(capacity: usize) -> Self { + Self { + window: VecDeque::with_capacity(capacity), + capacity, + tag_window: VecDeque::with_capacity(capacity), + tag_count: 0 + } + } + //信息滑入,对应标记窗口滑入false,返回被弹出的信息,若未满或弹出未标记的信息则返回None,若弹出被标记信息则返回Some(该信息) + pub fn push(&mut self, value: T) -> Option { + let target = None; + if self.window.len() == self.capacity { + target = self.pop(); + } + self.capacity += 1; + self.window.push_back(value); + self.auto_tag(); + target + } + //信息滑出,返回被弹出的信息,若未满或弹出未标记的信息则返回None,若弹出被标记信息则返回Some(该信息) + pub fn pop(&mut self) -> Option { + if self.tag_window.get(0).copied() == Some(true) { + let target = Some(self.dwindow.pop_front()); + self.capacity -= 1; + self.tag_window.pop_front(); + target + } else { + self.window.pop_front(); + self.capacity -= 1; + self.tag_window.pop_front(); + None + } + } + //获取窗口大小 + pub fn len(&self) -> usize { + self.window.len() + } + //获取窗口容量 + pub fn get_capacity(&self) -> usize { + self.capacity + } + //获取窗口容量(可变) + pub fn get_mut_capacity(&mut self) -> &mut usize { + &mut self.capacity + } + //判断窗口是否为空 + pub fn is_empty(&self) -> bool { + self.window.is_empty() + } + //标记用 + pub fn tag_information(&mut self, index: usize) { + if index < self.capacity { + self.tag_window[index] = true; + } + } + //取消标记用 + pub fn untag_information(&mut self, index: usize) { + if index < self.capacity { + self.tag_window[index] = false; + } + } + //每滑出capacity次信息时进行一次标记 + fn auto_tag(&mut self) { + self.tag_count += 1; + if self.tag_count == self.capacity { + self.tag_window.push_back(true); + self.tag_count = 0; + } + else{ + self.tag_window.push_back(false); + } + } +} From a6b15e190ebd82160e8af1adbca2bae9677b9824 Mon Sep 17 00:00:00 2001 From: seeleseelesee <2448828513@qq.com> Date: Wed, 21 Jan 2026 22:00:14 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=94=B91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/memory/working_memory/sliding_window.rs | 102 +++++++++++++------- 1 file changed, 68 insertions(+), 34 deletions(-) diff --git a/src/memory/working_memory/sliding_window.rs b/src/memory/working_memory/sliding_window.rs index 6b79d99..eb4d723 100644 --- a/src/memory/working_memory/sliding_window.rs +++ b/src/memory/working_memory/sliding_window.rs @@ -1,47 +1,44 @@ use std::collections::VecDeque -//滑动窗口(容器、容量、标记窗口)通过标记窗口中对应索引是否为true来判断是否被标记 -pub struct SlidingWindow { - window: VecDeque, +//滑动窗口(容器、容量、标记计数、摘要用临时储存) +pub struct SlidingWindow { + window: VecDeque, capacity: usize, - tag_window: VecDeque, - tag_count: usize + tag_count: usize, + summary: Vec } -impl SlidingWindow { +impl SlidingWindow { //新建 pub fn new(capacity: usize) -> Self { Self { window: VecDeque::with_capacity(capacity), - capacity, - tag_window: VecDeque::with_capacity(capacity), - tag_count: 0 + capacity: 0, + tag_count: 0, + summary: Vec::with_capacity(capacity) } } - //信息滑入,对应标记窗口滑入false,返回被弹出的信息,若未满或弹出未标记的信息则返回None,若弹出被标记信息则返回Some(该信息) - pub fn push(&mut self, value: T) -> Option { - let target = None; + //信息滑入,若滑出时信息有标记则发送摘要用片段 + pub fn push(&mut self, value: information) -> Option> { + value = self.auto_tag(value); + let is_tagged: bool = false; + let target: Option = None; if self.window.len() == self.capacity { - target = self.pop(); + is_tagged = self.pop(); + } + if is_tagged { + target = self.summarize(); } self.capacity += 1; self.window.push_back(value); - self.auto_tag(); target } - //信息滑出,返回被弹出的信息,若未满或弹出未标记的信息则返回None,若弹出被标记信息则返回Some(该信息) - pub fn pop(&mut self) -> Option { - if self.tag_window.get(0).copied() == Some(true) { - let target = Some(self.dwindow.pop_front()); - self.capacity -= 1; - self.tag_window.pop_front(); - target - } else { - self.window.pop_front(); - self.capacity -= 1; - self.tag_window.pop_front(); - None - } + //信息滑出,返回弹出信息是否被标记 + pub fn pop(&mut self) -> bool { + let target = self.window.pop_front(); + self.capacity -= 1; + self.summary.push(target.clone()); + self.get_tagged(target) } //获取窗口大小 pub fn len(&self) -> usize { @@ -59,27 +56,64 @@ impl SlidingWindow { pub fn is_empty(&self) -> bool { self.window.is_empty() } + //清空窗口 + pub fn clear(&mut self) { + self.window.clear(); + self.capacity = 0; + self.tag_count = 0; + self.summary.clear(); + } //标记用 pub fn tag_information(&mut self, index: usize) { if index < self.capacity { - self.tag_window[index] = true; + self.window[index].tag_information(); } } //取消标记用 pub fn untag_information(&mut self, index: usize) { if index < self.capacity { - self.tag_window[index] = false; + self.window[index].untag_information(); } } //每滑出capacity次信息时进行一次标记 - fn auto_tag(&mut self) { + fn auto_tag(&mut self, value: information) -> information { self.tag_count += 1; if self.tag_count == self.capacity { - self.tag_window.push_back(true); + value.tag_information(); self.tag_count = 0; } - else{ - self.tag_window.push_back(false); - } + value + } + //给出摘要 + pub fn summarize(&mut self) -> Vec { + self.summary.drain(..).collect() + } + //检测是否存在标记信息 + pub fn is_tagged(&mut self, value: information) -> bool { + value.is_tagged() + } + +} + +pub struct information { + pub text: String, + pub tag: bool, +} + +impl information { + pub fn new(text: String) -> Self { + Self { text, false } + } + pub fn tag_information(&mut self) { + self.tag = true; + } + pub fn untag_information(&mut self) { + self.tag = false; + } + pub fn is_tagged(&self) -> bool { + self.tag + } + pub fn get_mut_capacity(&mut self) -> &mut usize { + &mut self.capacity } } From 6252d7d599ae965e6a2d2d1548b9918cece7b32c Mon Sep 17 00:00:00 2001 From: seeleseelesee <2448828513@qq.com> Date: Wed, 21 Jan 2026 22:06:35 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=BB=91=E5=8A=A8=E7=AA=97=E5=8F=A3?= =?UTF-8?q?=E5=88=9D=E6=AD=A5=E6=94=B91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/memory/working_memory/sliding_window.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/memory/working_memory/sliding_window.rs b/src/memory/working_memory/sliding_window.rs index eb4d723..b19e3f3 100644 --- a/src/memory/working_memory/sliding_window.rs +++ b/src/memory/working_memory/sliding_window.rs @@ -75,7 +75,7 @@ impl SlidingWindow { self.window[index].untag_information(); } } - //每滑出capacity次信息时进行一次标记 + //每滑入capacity次信息时进行一次标记 fn auto_tag(&mut self, value: information) -> information { self.tag_count += 1; if self.tag_count == self.capacity {