Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Refactor: Use a btreemap for hash storage #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 8 additions & 45 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ unsafe fn kmerminhash_get_mins(ptr: *mut KmerMinHash) -> Result<*const u64> {
assert!(!ptr.is_null());
&mut *ptr
};
let output = mh.mins.clone();

Ok(Box::into_raw(output.into_boxed_slice()) as *const u64)
Ok(Box::into_raw(mh.mins().into_boxed_slice()) as *const u64)
}
}

Expand All @@ -107,25 +106,14 @@ unsafe fn kmerminhash_get_abunds(ptr: *mut KmerMinHash) -> Result<*const u64> {
assert!(!ptr.is_null());
&mut *ptr
};
if let Some(ref abunds) = mh.abunds {
let output = abunds.clone();
Ok(Box::into_raw(output.into_boxed_slice()) as *const u64)
if let Some(abunds) = mh.abunds() {
Ok(Box::into_raw(abunds.into_boxed_slice()) as *const u64)
} else {
Ok(ptr::null())
}
}
}

ffi_fn! {
unsafe fn kmerminhash_get_min_idx(ptr: *mut KmerMinHash, idx: u64) -> Result<u64> {
let mh = {
assert!(!ptr.is_null());
&mut *ptr
};
Ok(mh.mins[idx as usize])
}
}

#[no_mangle]
pub extern "C" fn kmerminhash_get_mins_size(ptr: *mut KmerMinHash) -> usize {
let mh = unsafe {
Expand All @@ -136,26 +124,12 @@ pub extern "C" fn kmerminhash_get_mins_size(ptr: *mut KmerMinHash) -> usize {
}

#[no_mangle]
pub extern "C" fn kmerminhash_mins_push(ptr: *mut KmerMinHash, val: u64) {
pub extern "C" fn kmerminhash_push(ptr: *mut KmerMinHash, key: u64, val: u64) {
let mh = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
mh.mins.push(val)
}

ffi_fn! {
unsafe fn kmerminhash_get_abund_idx(ptr: *mut KmerMinHash, idx: u64) -> Result<u64> {
let mh = {
assert!(!ptr.is_null());
&mut *ptr
};
if let Some(ref mut abunds) = mh.abunds {
Ok(abunds[idx as usize])
} else {
Ok(0)
}
}
mh.mins.insert(key, val);
}

#[no_mangle]
Expand All @@ -164,24 +138,13 @@ pub extern "C" fn kmerminhash_get_abunds_size(ptr: *mut KmerMinHash) -> usize {
assert!(!ptr.is_null());
&mut *ptr
};
if let Some(ref mut abunds) = mh.abunds {
abunds.len()
if mh.track_abundance {
mh.mins.len()
} else {
0
}
}

#[no_mangle]
pub extern "C" fn kmerminhash_abunds_push(ptr: *mut KmerMinHash, val: u64) {
let mh = unsafe {
assert!(!ptr.is_null());
&mut *ptr
};
if let Some(ref mut abunds) = mh.abunds {
abunds.push(val)
}
}

#[no_mangle]
pub extern "C" fn kmerminhash_is_protein(ptr: *mut KmerMinHash) -> bool {
let mh = unsafe {
Expand All @@ -206,7 +169,7 @@ pub extern "C" fn kmerminhash_track_abundance(ptr: *mut KmerMinHash) -> bool {
assert!(!ptr.is_null());
&mut *ptr
};
mh.abunds.is_some()
mh.track_abundance
}

#[no_mangle]
Expand Down
Loading