Skip to content

Commit

Permalink
Fix: Syntax issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Mar 2, 2024
1 parent 6998bcf commit 34997a3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
10 changes: 6 additions & 4 deletions python/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ static sz_string_view_t temporary_memory = {NULL, 0};
* native `mmap` module, as it exposes the address of the mapping in memory.
*/
typedef struct {
PyObject_HEAD;
PyObject ob_base;

#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
HANDLE file_handle;
HANDLE mapping_handle;
Expand All @@ -80,7 +81,8 @@ typedef struct {
* - Str(File("some-path.txt"), from=0, to=sys.maxint)
*/
typedef struct {
PyObject_HEAD;
PyObject ob_base;

PyObject *parent;
sz_cptr_t start;
sz_size_t length;
Expand All @@ -93,7 +95,7 @@ typedef struct {
* which might be more memory-friendly, than greedily invoking `str.split`.
*/
typedef struct {
PyObject_HEAD;
PyObject ob_base;

PyObject *text_object; //< For reference counting
PyObject *separator_object; //< For reference counting
Expand Down Expand Up @@ -125,7 +127,7 @@ typedef struct {
* for faster sorting, shuffling, joins, and lookups.
*/
typedef struct {
PyObject_HEAD;
PyObject ob_base;

enum {
STRS_CONSECUTIVE_32,
Expand Down
45 changes: 41 additions & 4 deletions rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,19 @@ pub mod sz {
}
}

/// The default substitution matrix for the Needleman-Wunsch alignment algorithm,
/// which will produce distances equal to the negative Levenshtein edit distance.
/// Generates a default substitution matrix for use with the Needleman-Wunsch
/// alignment algorithm. This matrix is initialized such that diagonal entries
/// (representing matching characters) are zero, and off-diagonal entries
/// (representing mismatches) are -1. This setup effectively produces distances
/// equal to the negative Levenshtein edit distance, suitable for basic sequence
/// alignment tasks where all mismatches are penalized equally and there are no
/// rewards for matches.
///
/// # Returns
///
/// A 256x256 array of `i8`, where each element represents the substitution cost
/// between two characters (byte values). Matching characters are assigned a cost
/// of 0, and non-matching characters are assigned a cost of -1.
pub fn unary_substitution_costs() -> [[i8; 256]; 256] {
let mut result = [[0; 256]; 256];

Expand All @@ -639,10 +650,36 @@ pub mod sz {
result
}

/// Randomizes the contents of a given byte slice `text` using characters from
/// a specified `alphabet`. This function mutates `text` in place, replacing each
/// byte with a random one from `alphabet`. It is designed for situations where
/// you need to generate random strings or data sequences based on a specific set
/// of characters, such as generating random DNA sequences or testing inputs.
///
/// # Type Parameters
///
/// * `T`: The type of the text to be randomized. Must be mutable and convertible to a byte slice.
/// * `A`: The type of the alphabet. Must be convertible to a byte slice.
///
/// # Arguments
///
/// * `text`: A mutable reference to the data to randomize. This data will be mutated in place.
/// * `alphabet`: A reference to the byte slice representing the alphabet to use for randomization.
///
/// # Examples
///
/// ```
/// use stringzilla::sz;
/// let mut my_text = vec![0; 10]; // A buffer to randomize
/// let alphabet = b"ACTG"; // Using a DNA alphabet
/// sz::randomize(&mut my_text, &alphabet);
/// ```
///
/// After than, `my_text` is filled with random 'A', 'C', 'T', or 'G' values.
pub fn randomize<T, A>(text: &mut T, alphabet: &A)
where
T: AsMut<[u8]> + ?Sized, // Relaxing Sized restriction for T
A: AsRef<[u8]> + ?Sized, // Relaxing Sized restriction for A
T: AsMut<[u8]> + ?Sized, // Allows for mutable references to dynamically sized types.
A: AsRef<[u8]> + ?Sized, // Allows for references to dynamically sized types.
{
let text_slice = text.as_mut();
let alphabet_slice = alphabet.as_ref();
Expand Down

0 comments on commit 34997a3

Please sign in to comment.