Skip to content

Commit

Permalink
run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
BramDevlaminck committed May 14, 2024
1 parent 0e7a03a commit fe0bb8f
Show file tree
Hide file tree
Showing 25 changed files with 242 additions and 260 deletions.
9 changes: 3 additions & 6 deletions fa-compression/benches/algorithm1/decode.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use criterion::black_box;
use fa_compression::algorithm1::{
decode,
encode
};
use fa_compression::algorithm1::{decode, encode};

use super::util::generate_annotation;

Expand All @@ -11,7 +8,7 @@ fn generate_encoded_annotations(count: usize) -> Vec<u8> {
let mut random = rand::thread_rng();

let mut annotations = String::new();
for _ in 0 .. count {
for _ in 0..count {
annotations.push_str(&generate_annotation(&mut random));
annotations.push(';');
}
Expand All @@ -25,7 +22,7 @@ pub fn decode_benchmark(c: &mut criterion::Criterion) {
b.iter_batched(
|| generate_encoded_annotations(100),
|annotations| black_box(decode(annotations.as_slice())),
criterion::BatchSize::SmallInput
criterion::BatchSize::SmallInput,
)
});
}
4 changes: 2 additions & 2 deletions fa-compression/benches/algorithm1/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn generate_decoded_annotations(count: usize) -> String {
let mut random = rand::thread_rng();

let mut annotations = String::new();
for _ in 0 .. count {
for _ in 0..count {
annotations.push_str(&generate_annotation(&mut random));
annotations.push(';');
}
Expand All @@ -22,7 +22,7 @@ pub fn encode_benchmark(c: &mut criterion::Criterion) {
b.iter_batched(
|| generate_decoded_annotations(100),
|annotations| black_box(encode(annotations.as_str())),
criterion::BatchSize::SmallInput
criterion::BatchSize::SmallInput,
)
});
}
10 changes: 3 additions & 7 deletions fa-compression/benches/algorithm2/decode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use criterion::black_box;
use fa_compression::algorithm2::{
decode,
encode,
CompressionTable
};
use fa_compression::algorithm2::{decode, encode, CompressionTable};

use super::util::generate_annotation;

Expand All @@ -14,7 +10,7 @@ fn generate_encoded_annotations_and_table(count: usize) -> (Vec<u8>, Compression
let mut compression_table2 = CompressionTable::new();

let mut annotations = String::new();
for _ in 0 .. count {
for _ in 0..count {
let annotation = generate_annotation(&mut random);
annotations.push_str(&annotation);
annotations.push(';');
Expand All @@ -32,7 +28,7 @@ pub fn decode_benchmark(c: &mut criterion::Criterion) {
b.iter_batched(
|| generate_encoded_annotations_and_table(100),
|(annotations, ct)| black_box(decode(annotations.as_slice(), ct)),
criterion::BatchSize::SmallInput
criterion::BatchSize::SmallInput,
)
});
}
9 changes: 3 additions & 6 deletions fa-compression/benches/algorithm2/encode.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use criterion::black_box;
use fa_compression::algorithm2::{
encode,
CompressionTable
};
use fa_compression::algorithm2::{encode, CompressionTable};

use super::util::generate_annotation;

Expand All @@ -12,7 +9,7 @@ fn generate_decoded_annotations_and_table(count: usize) -> (String, CompressionT
let mut compression_table = CompressionTable::new();

let mut annotations = String::new();
for _ in 0 .. count {
for _ in 0..count {
let annotation = generate_annotation(&mut random);
annotations.push_str(&annotation);
annotations.push(';');
Expand All @@ -29,7 +26,7 @@ pub fn encode_benchmark(c: &mut criterion::Criterion) {
b.iter_batched(
|| generate_decoded_annotations_and_table(100),
|(annotations, ct)| black_box(encode(annotations.as_str(), ct)),
criterion::BatchSize::SmallInput
criterion::BatchSize::SmallInput,
)
});
}
21 changes: 9 additions & 12 deletions fa-compression/benches/util.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
use rand::{
rngs::ThreadRng,
Rng
};
use rand::{rngs::ThreadRng, Rng};

/// Generate a random InterPro annotation.
pub fn generate_ipr(random: &mut ThreadRng) -> String {
format!("IPR:IPR{:06}", random.gen_range(0 .. 999999))
format!("IPR:IPR{:06}", random.gen_range(0..999999))
}

/// Generate a random Gene Ontology annotation.
pub fn generate_go(random: &mut ThreadRng) -> String {
format!("GO:{:07}", random.gen_range(0 .. 9999999))
format!("GO:{:07}", random.gen_range(0..9999999))
}

/// Generate a random Enzyme Commission annotation.
pub fn generate_ec(random: &mut ThreadRng) -> String {
format!(
"EC:{}.{}.{}.{}",
random.gen_range(0 .. 8),
random.gen_range(0 .. 30),
random.gen_range(0 .. 30),
random.gen_range(0 .. 200)
random.gen_range(0..8),
random.gen_range(0..30),
random.gen_range(0..30),
random.gen_range(0..200)
)
}

/// Generate a random annotation.
pub fn generate_annotation(random: &mut ThreadRng) -> String {
match random.gen_range(0 .. 3) {
match random.gen_range(0..3) {
0 => generate_ipr(random),
1 => generate_go(random),
2 => generate_ec(random),
_ => unreachable!()
_ => unreachable!(),
}
}
5 changes: 1 addition & 4 deletions fa-compression/src/algorithm1/decode.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
//! This module provides a function to decode a byte array into a string representation of
//! annotations.
use super::{
CharacterSet,
Decode
};
use super::{CharacterSet, Decode};

/// The prefixes for the different types of annotations.
static PREFIXES: [&str; 3] = ["EC:", "GO:", "IPR:IPR"];
Expand Down
11 changes: 4 additions & 7 deletions fa-compression/src/algorithm1/encode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//! This module contains the function to encode the input string into a compressed byte vector.
use super::{
CharacterSet,
Encode
};
use super::{CharacterSet, Encode};

/// Encodes the input string into a compressed byte vector.
///
Expand Down Expand Up @@ -50,11 +47,11 @@ pub fn encode(input: &str) -> Vec<u8> {
// Read the input and split the annotations into the corresponding vectors
for annotation in input.split(';') {
if annotation.starts_with("IPR") {
interpros.push(&annotation[7 ..]);
interpros.push(&annotation[7..]);
} else if annotation.starts_with("GO") {
gos.push(&annotation[3 ..]);
gos.push(&annotation[3..]);
} else if annotation.starts_with("EC") {
ecs.push(&annotation[3 ..]);
ecs.push(&annotation[3..]);
}
}

Expand Down
14 changes: 7 additions & 7 deletions fa-compression/src/algorithm1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ enum CharacterSet {
Comma,

/// Annotation separator
Semicolon
Semicolon,
}

impl Encode for CharacterSet {
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Encode for CharacterSet {
b'.' => CharacterSet::Point,
b',' => CharacterSet::Comma,
b';' => CharacterSet::Semicolon,
_ => panic!("Invalid character")
_ => panic!("Invalid character"),
}
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ impl Decode for CharacterSet {
12 => '.',
13 => ',',
14 => ';',
_ => panic!("Invalid character")
_ => panic!("Invalid character"),
}
}
}
Expand Down Expand Up @@ -185,21 +185,21 @@ mod tests {
CharacterSet::Dash,
CharacterSet::Point,
CharacterSet::Comma,
CharacterSet::Semicolon
CharacterSet::Semicolon,
];

#[test]
fn test_or() {
for i in 0 .. CHARACTERS.len() {
for j in 0 .. CHARACTERS.len() {
for i in 0..CHARACTERS.len() {
for j in 0..CHARACTERS.len() {
assert_eq!(CHARACTER_SETS[i] | CHARACTER_SETS[j], ((i as u8) << 4) | (j as u8));
}
}
}

#[test]
fn test_encode() {
for i in 0 .. CHARACTERS.len() {
for i in 0..CHARACTERS.len() {
assert_eq!(CHARACTER_SETS[i], CharacterSet::encode(CHARACTERS[i]));
}
}
Expand Down
2 changes: 1 addition & 1 deletion fa-compression/src/algorithm2/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn encode(input: &str, compression_table: CompressionTable) -> Vec<u8> {
let mut encoded: Vec<u8> = Vec::with_capacity(input.len() / 3);
for annotation in input.split(';') {
if let Some(index) = compression_table.index_of(annotation) {
encoded.extend_from_slice(&index.to_le_bytes()[0 .. 3])
encoded.extend_from_slice(&index.to_le_bytes()[0..3])
}
}

Expand Down
10 changes: 4 additions & 6 deletions fa-compression/src/algorithm2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ pub use encode::encode;
/// Represents an entry in the compression table.
#[doc(hidden)]
pub struct CompressionTableEntry {
annotation: String
annotation: String,
}

/// Represents a compression table.
pub struct CompressionTable {
/// List of annotations in the compression table.
entries: Vec<CompressionTableEntry>
entries: Vec<CompressionTableEntry>,
}

impl CompressionTable {
Expand All @@ -37,7 +37,7 @@ impl CompressionTable {
/// ```
pub fn new() -> CompressionTable {
CompressionTable {
entries: Vec::new()
entries: Vec::new(),
}
}

Expand All @@ -57,9 +57,7 @@ impl CompressionTable {
/// table.add_entry("IPR:IPR000002".to_string());
/// ```
pub fn add_entry(&mut self, annotation: String) {
self.entries.push(CompressionTableEntry {
annotation
});
self.entries.push(CompressionTableEntry { annotation });
}

/// Returns the index of the given annotation in the compression table, if it exists.
Expand Down
12 changes: 5 additions & 7 deletions libsais64-rs/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl<'a> Error for CompileError<'a> {}
/// # Returns
///
/// Returns () if the exit status was success
///
///
/// # Errors
///
///
/// Returns a CompilationError if the command failed
fn exit_status_to_result(name: &str, exit_status: ExitStatus) -> Result<(), CompileError> {
match exit_status.success() {
Expand All @@ -52,17 +52,15 @@ fn main() -> Result<(), Box<dyn Error>> {
// compile the c library
Command::new("rm")
.args(["libsais/CMakeCache.txt"])
.status().unwrap_or_default(); // if removing fails, it is since the cmake cache did not exist, we just can ignore it
.status()
.unwrap_or_default(); // if removing fails, it is since the cmake cache did not exist, we just can ignore it
exit_status_to_result(
"cmake",
Command::new("cmake")
.args(["-DCMAKE_BUILD_TYPE=\"Release\"", "libsais", "-Blibsais"])
.status()?,
)?;
exit_status_to_result(
"make",
Command::new("make").args(["-C", "libsais"]).status()?,
)?;
exit_status_to_result("make", Command::new("make").args(["-C", "libsais"]).status()?)?;

// link the c libsais library to rust
println!("cargo:rustc-link-search=native=libsais64-rs/libsais");
Expand Down
7 changes: 4 additions & 3 deletions libsais64-rs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));


/// Builds the suffix array over the `text` using the libsais64 algorithm
///
/// # Arguments
Expand All @@ -16,7 +15,9 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
/// Returns None if construction of the suffix array failed
pub fn sais64(text: &[u8]) -> Option<Vec<i64>> {
let mut sa = vec![0; text.len()];
let exit_code = unsafe { libsais64(text.as_ptr(), sa.as_mut_ptr(), text.len() as i64, 0, std::ptr::null_mut()) };
let exit_code = unsafe {
libsais64(text.as_ptr(), sa.as_mut_ptr(), text.len() as i64, 0, std::ptr::null_mut())
};
if exit_code == 0 {
Some(sa)
} else {
Expand All @@ -26,7 +27,7 @@ pub fn sais64(text: &[u8]) -> Option<Vec<i64>> {

#[cfg(test)]
mod tests {
use crate::{sais64};
use crate::sais64;

#[test]
fn check_build_sa_with_libsais64() {
Expand Down
Loading

0 comments on commit fe0bb8f

Please sign in to comment.