-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
468c41a
commit b39ee23
Showing
16 changed files
with
733 additions
and
508 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,68 @@ | ||
use super::{Driver, EncoderConfig}; | ||
use crate::representation::{Codes, KeyMap}; | ||
use std::iter::zip; | ||
use crate::representation::{Codes, Element, KeyMap, Representation}; | ||
|
||
/// 编码是否已被占据 | ||
/// 用一个数组和一个哈希集合来表示,数组用来表示四码以内的编码,哈希集合用来表示四码以上的编码 | ||
pub struct C3 { | ||
pub full_space: Vec<u8>, | ||
pub full_space: Vec<bool>, | ||
pub involved_message: Vec<Vec<usize>>, | ||
} | ||
|
||
impl C3 { | ||
pub fn new(length: usize) -> Self { | ||
Self { | ||
full_space: vec![0; length], | ||
full_space: vec![false; length], | ||
involved_message: vec![], | ||
} | ||
} | ||
|
||
pub fn reset(&mut self) { | ||
self.full_space.iter_mut().for_each(|x| { | ||
*x = 0; | ||
*x = false; | ||
}); | ||
} | ||
} | ||
|
||
impl Driver for C3 { | ||
fn run(&mut self, keymap: &KeyMap, config: &EncoderConfig, buffer: &mut Codes) { | ||
fn init(&mut self, config: &EncoderConfig, _: &Representation) { | ||
for _ in 0..=config.elements_length { | ||
self.involved_message.push(vec![]); | ||
} | ||
for (index, encodable) in config.encodables.iter().enumerate() { | ||
for element in &encodable.sequence { | ||
self.involved_message[*element].push(index); | ||
} | ||
} | ||
} | ||
|
||
fn run( | ||
&mut self, | ||
keymap: &KeyMap, | ||
config: &EncoderConfig, | ||
buffer: &mut Codes, | ||
moved_elements: &[Element], | ||
) { | ||
self.reset(); | ||
// 1. 全码 | ||
for (encodable, pointer) in zip(&config.encodables, buffer.iter_mut()) { | ||
let sequence = &encodable.sequence; | ||
assert!(sequence.len() >= 3); | ||
let code = keymap[sequence[0]] as u64 * config.radix * config.radix | ||
+ keymap[sequence[1]] as u64 * config.radix | ||
+ keymap[sequence[2]] as u64; | ||
pointer.full.actual = code; | ||
|
||
// 部分编码,只计算变化的部分 | ||
for element in moved_elements { | ||
for index in &self.involved_message[*element] { | ||
let pointer = &mut buffer[*index]; | ||
let encodable = &config.encodables[*index]; | ||
let sequence = &encodable.sequence; | ||
let full = &mut pointer.full; | ||
let code = keymap[sequence[0]] as u64 * config.radix * config.radix | ||
+ keymap[sequence[1]] as u64 * config.radix | ||
+ keymap[sequence[2]] as u64; | ||
full.check_actual(code); | ||
} | ||
} | ||
|
||
for pointer in buffer.iter_mut() { | ||
let rank = self.full_space[pointer.full.actual as usize]; | ||
pointer.full.duplicate = rank > 0; | ||
self.full_space[pointer.full.actual as usize] = rank.saturating_add(1); | ||
let full = &mut pointer.full; | ||
let duplicate = self.full_space[full.actual as usize]; | ||
full.check_duplicate(duplicate); | ||
self.full_space[full.actual as usize] = true; | ||
} | ||
} | ||
} |
Oops, something went wrong.