Skip to content

Commit

Permalink
Re-organize into multiple crates
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Nov 13, 2019
1 parent 56065d5 commit f56039d
Show file tree
Hide file tree
Showing 28 changed files with 167 additions and 669 deletions.
18 changes: 16 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 6 additions & 14 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
[package]
name = "divsufsort"
version = "0.1.0"
authors = ["Amos Wenger <[email protected]>"]
edition = "2018"
[workspace]
members = [
"crates/divsufsort",
"crates/cdivsufsort",
"crates/divsuftest",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
better-panic = "0.2.0"
once_cell = "1.2.0"
suffix_array = "0.4.0"

[build-dependencies]
cc = "1.0"
10 changes: 10 additions & 0 deletions crates/cdivsufsort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "cdivsufsort"
version = "0.1.0"
authors = ["Amos Wenger <[email protected]>"]
edition = "2018"

[dependencies]

[build-dependencies]
cc = "1.0.47"
8 changes: 4 additions & 4 deletions build.rs → crates/cdivsufsort/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ fn main() {
};

build
.file("original/divsufsort.c")
.file("original/sssort.c")
.file("original/trsort.c")
.file("original/utils.c");
.file("c-sources/divsufsort.c")
.file("c-sources/sssort.c")
.file("c-sources/trsort.c")
.file("c-sources/utils.c");

build.compile("libdivsufsort.a");
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions crates/cdivsufsort/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
extern "C" {
pub fn divsufsort(T: *const u8, SA: *mut i32, n: i32) -> i32;
pub fn dss_flush();
}
11 changes: 11 additions & 0 deletions crates/divsufsort/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "divsufsort"
version = "0.1.0"
authors = ["Amos Wenger <[email protected]>"]
edition = "2018"

[features]
crosscheck = ["once_cell"]

[dependencies]
once_cell = { version = "1.2.0", optional = true }
File renamed without changes.
26 changes: 15 additions & 11 deletions src/crosscheck.rs → crates/divsufsort/src/crosscheck.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
use crate::common::{ABucket, BMixBucket, Idx, SuffixArray, ALPHABET_SIZE};
#[cfg(feature = "crosscheck")]
use once_cell::sync::Lazy;

use crate::common::{ABucket, BMixBucket, Idx, SuffixArray, ALPHABET_SIZE};
use std::{
fs::File,
io::{BufWriter, Write},
sync::Mutex,
};

pub static CROSSCHECK_FILE: Lazy<Mutex<BufWriter<File>>> =
Lazy::new(|| Mutex::new(BufWriter::new(File::create("crosscheck/rust").unwrap())));
#[cfg(feature = "crosscheck")]
pub static CROSSCHECK_FILE: Lazy<Mutex<BufWriter<File>>> = Lazy::new(|| {
std::fs::create_dir_all("crosscheck").unwrap();
Mutex::new(BufWriter::new(File::create("crosscheck/rust").unwrap()))
});

#[macro_export]
macro_rules! crosscheck {
($($arg: expr),*) => {
#[cfg(debug_assertions)]
#[cfg(feature = "crosscheck")]
{
use std::io::Write;
let mut f = crate::crosscheck::CROSSCHECK_FILE.lock().unwrap();
Expand All @@ -22,41 +27,40 @@ macro_rules! crosscheck {
}

pub fn flush() {
#[cfg(debug_assertions)]
#[cfg(feature = "crosscheck")]
{
let mut f = crate::crosscheck::CROSSCHECK_FILE.lock().unwrap();
f.flush().unwrap();
}
}

pub fn SA_dump(SA: &SuffixArray, label: &str) {
#[cfg(debug_assertions)]
#[cfg(feature = "crosscheck")]
{
use std::io::Write;
let mut f = crate::crosscheck::CROSSCHECK_FILE.lock().unwrap();

writeln!(f, ":: {}", label).unwrap();
// crosscheck!("SA = {:?}", SA.0);
for i in 0..SA.0.len() {
write!(f, "{} ", SA.0[i]).unwrap();
if (i + 1) % 25 == 0 {
writeln!(f, "").unwrap();
writeln!(f).unwrap();
}
}
writeln!(f, "").unwrap();
writeln!(f).unwrap();
}
}

pub fn A_dump(A: &ABucket, label: &str) {
#[cfg(debug_assertions)]
#[cfg(feature = "crosscheck")]
{
crosscheck!(":: {}", label);
crosscheck!("A = {:?}", A.0);
}
}

pub fn BSTAR_dump(B: &mut BMixBucket, label: &str) {
#[cfg(debug_assertions)]
#[cfg(feature = "crosscheck")]
{
use std::io::Write;
let mut f = crate::crosscheck::CROSSCHECK_FILE.lock().unwrap();
Expand Down
File renamed without changes.
15 changes: 15 additions & 0 deletions crates/divsufsort/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![allow(nonstandard_style)]
#![allow(unused_variables)]
#![allow(unused_parens)]
#![allow(unused_mut)]
#![allow(unused_imports)]
#![allow(dead_code)]

mod common;
pub mod crosscheck;
mod divsufsort;
mod sssort;
mod trsort;

pub use common::Idx;
pub use divsufsort::divsufsort;
File renamed without changes.
File renamed without changes.
11 changes: 11 additions & 0 deletions crates/divsuftest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "divsuftest"
version = "0.1.0"
authors = ["Amos Wenger <[email protected]>"]
edition = "2018"

[dependencies]
divsufsort = { path = "../divsufsort" }
cdivsufsort = { path = "../cdivsufsort" }
suffix_array = "0.4.0"
better-panic = "0.2.0"
75 changes: 75 additions & 0 deletions crates/divsuftest/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::time::Instant;

fn main() {
better_panic::install();

let first_arg = std::env::args().nth(1).unwrap_or_else(|| {
std::path::PathBuf::from("testdata")
.join("input.txt")
.to_string_lossy()
.into()
});
let input = std::fs::read(first_arg).unwrap();

#[cfg(debug_assertions)]
println!("{:>20} Running", "C");

let mut sa_c = vec![0 as divsufsort::Idx; input.len()];
let before_c = Instant::now();
let c_duration;

unsafe {
cdivsufsort::divsufsort(input.as_ptr(), sa_c.as_mut_ptr(), input.len() as i32);
c_duration = before_c.elapsed();
cdivsufsort::dss_flush();
}

for i in 0..(sa_c.len() - 1) {
assert!(
input[sa_c[i] as usize..] < input[sa_c[i + 1] as usize..],
"suffixes should be ordered"
);
}

#[cfg(debug_assertions)]
println!("{:>20} Running...", "Rust");

{
let res = std::panic::catch_unwind(|| {
let mut sa_rust = vec![0 as divsufsort::Idx; input.len()];
let before_rust = Instant::now();

std::thread::spawn(|| loop {
std::thread::sleep(std::time::Duration::from_millis(500));
divsufsort::crosscheck::flush();
});

divsufsort::divsufsort(&input[..], &mut sa_rust[..]);
let rust_duration = before_rust.elapsed();
assert!(sa_c == sa_rust, "c & rust divsufsort SAs should be equal");

#[cfg(debug_assertions)]
println!("{:>20} Running...", "huc");

let huc_duration = {
let before_huc = Instant::now();
let sa = suffix_array::SuffixArray::new(&input[..]);
let (_, sa) = sa.into_parts();
let sa = &sa[1..];

for i in 0..sa_c.len() {
assert_eq!(sa_c[i], sa[i] as i32);
}
before_huc.elapsed()
};

let s1 = format!("c {:?}", c_duration);
let s2 = format!("rust {:?}", rust_duration);
let s3 = format!("rust-ref {:?}", huc_duration);
println!("{:30} {:30} {:30}", s1, s2, s3);
});
divsufsort::crosscheck::flush();
res.unwrap()
};
}

21 changes: 0 additions & 21 deletions fuzz.sh

This file was deleted.

4 changes: 0 additions & 4 deletions fuzz/.gitignore

This file was deleted.

Loading

0 comments on commit f56039d

Please sign in to comment.