-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathushuffle.pyx
45 lines (37 loc) · 1.24 KB
/
ushuffle.pyx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
cimport ushuffle_mod
from libc.stdlib cimport malloc, free, rand, srand
from libc.string cimport strlen
def shuffle(char *sequence, int let_size):
"""Shuffle the sequence while preserving k-let size."""
cdef int length = len(sequence)
cdef char *t = <char *>malloc((len(sequence)) * sizeof(char))
ushuffle_mod.shuffle(sequence, t, length, let_size)
try:
py_t = t[:length]
finally:
free(t)
return py_t
def set_seed(int seed):
srand(seed)
ushuffle_mod.set_randfunc(<ushuffle_mod.randfunc_t> rand)
cdef class Shuffler:
"""A sequence shuffler with k-let size preservation."""
cdef char *s
cdef char *t
cdef readonly int length
cdef readonly int let_size
def __cinit__(self, char *sequence, int let_size):
self.s = sequence
self.let_size = let_size
self.length = strlen(sequence)
self.t = <char *>malloc(self.length * sizeof(char))
ushuffle_mod.shuffle1(self.s, self.length, self.let_size)
def shuffle(self):
ushuffle_mod.shuffle2(self.t)
py_t = self.t[:self.length]
return py_t
property sequence:
def __get__(self):
return self.s[:self.length]
def __dealloc__(self):
free(<void *>self.t)