-
Notifications
You must be signed in to change notification settings - Fork 1
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
37b23aa
commit fe5baea
Showing
5 changed files
with
230 additions
and
151 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* srx: The fast Symbol Ranking based compressor. | ||
* Copyright (C) 2023 Mai Thanh Minh (a.k.a. thanhminhmr) | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation, either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
use crate::primary_context::matched::ByteMatched; | ||
use crate::primary_context::history::ByteHistory; | ||
|
||
// ----------------------------------------------- | ||
|
||
pub struct PrimaryContext<const SIZE: usize> { | ||
last_byte: u8, | ||
hash_value: usize, | ||
contexts: Box<[ByteHistory; SIZE]>, | ||
} | ||
|
||
impl<const SIZE: usize> PrimaryContext<SIZE> { | ||
// assert that SIZE is power of 2 | ||
const _SIZE_CHECK: () = assert!(SIZE != 0 && (SIZE & (SIZE - 1)) == 0); | ||
|
||
pub fn new() -> Self { | ||
Self { | ||
last_byte: 0, | ||
hash_value: 0, | ||
contexts: Box::new([ByteHistory::new(); SIZE]), | ||
} | ||
} | ||
|
||
pub fn get(&self) -> (u8, u8, u8, u8, usize, usize) { | ||
let (first_byte, second_byte, third_byte, count): (u8, u8, u8, usize) = | ||
self.contexts[self.hash_value].get(); | ||
return ( | ||
self.last_byte, | ||
first_byte, | ||
second_byte, | ||
third_byte, | ||
count, | ||
self.hash_value, | ||
); | ||
} | ||
|
||
fn update(&mut self, next_byte: u8) { | ||
self.last_byte = next_byte; | ||
self.hash_value = (self.hash_value * (5 << 5) + next_byte as usize + 1) % SIZE; | ||
debug_assert!(self.hash_value < SIZE); | ||
} | ||
|
||
pub fn matching(&mut self, next_byte: u8) -> ByteMatched { | ||
let matching_byte: ByteMatched = self.contexts[self.hash_value].matching(next_byte); | ||
self.update(next_byte); | ||
return matching_byte; | ||
} | ||
|
||
pub fn matched(&mut self, next_byte: u8, matched: ByteMatched) { | ||
self.contexts[self.hash_value].matched(next_byte, matched); | ||
self.update(next_byte); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* srx: The fast Symbol Ranking based compressor. | ||
* Copyright (C) 2023 Mai Thanh Minh (a.k.a. thanhminhmr) | ||
* | ||
* This program is free software: you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation, either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
* details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
use crate::primary_context::matched::ByteMatched; | ||
|
||
// ----------------------------------------------- | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct ByteHistory(u32); | ||
|
||
impl ByteHistory { | ||
pub fn new() -> Self { | ||
Self(0) | ||
} | ||
|
||
pub fn get(&self) -> (u8, u8, u8, usize) { | ||
( | ||
self.0 as u8, // first byte | ||
(self.0 >> 8) as u8, // second byte | ||
(self.0 >> 16) as u8, // third byte | ||
(self.0 >> 24) as usize, // count TODO would a state machine here be better? | ||
) | ||
} | ||
|
||
pub fn matching(&mut self, next_byte: u8) -> ByteMatched { | ||
let mask: u32 = self.0 ^ (0x10101 * next_byte as u32); | ||
return if (mask & 0x0000FF) == 0 { | ||
// mask for the first byte | ||
// increase count by 1, capped at 255 | ||
self.0 += if self.0 < 0xFF000000 { 0x01000000 } else { 0 }; | ||
|
||
ByteMatched::FIRST | ||
} else if (mask & 0x00FF00) == 0 { | ||
// mask for the second byte | ||
self.0 = (self.0 & 0xFF0000) // keep the third byte | ||
| ((self.0 << 8) & 0xFF00) // bring the old first byte to second place | ||
| next_byte as u32 // set the first byte | ||
| 0x1000000; // set count to 1 | ||
|
||
ByteMatched::SECOND | ||
} else if (mask & 0xFF0000) == 0 { | ||
// mask for the third byte | ||
self.0 = ((self.0 << 8) & 0xFFFF00) // move old first/second to second/third | ||
| next_byte as u32 // set the first byte | ||
| 0x1000000; // set count to 1 | ||
|
||
ByteMatched::THIRD | ||
} else { | ||
// not match | ||
self.0 = ((self.0 << 8) & 0xFFFF00) // move old first/second to second/third | ||
| next_byte as u32; // set the first byte | ||
|
||
ByteMatched::NONE | ||
}; | ||
} | ||
|
||
pub fn matched(&mut self, next_byte: u8, matched: ByteMatched) { | ||
match matched { | ||
ByteMatched::FIRST => { | ||
// first byte | ||
// increase count by 1, capped at 255 | ||
self.0 += if self.0 < 0xFF000000 { 0x01000000 } else { 0 }; | ||
} | ||
ByteMatched::SECOND => { | ||
// second byte | ||
self.0 = (self.0 & 0xFF0000) // keep the third byte | ||
| ((self.0 << 8) & 0xFF00) // bring the old first byte to second place | ||
| next_byte as u32 // set the first byte | ||
| 0x1000000; // set count to 1 | ||
} | ||
ByteMatched::THIRD => { | ||
// third byte | ||
self.0 = ((self.0 << 8) & 0xFFFF00) // move old first/second to second/third | ||
| next_byte as u32 // set the first byte | ||
| 0x1000000; // set count to 1 | ||
} | ||
ByteMatched::NONE => { | ||
// not match | ||
self.0 = ((self.0 << 8) & 0xFFFF00) // move old first/second to second/third | ||
| next_byte as u32; // set the first byte | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.