Segfault calling C function, any advice?

I think it should be:

k = maximum(in_vector)+1

As the parameter is the alphabet size (which includes missing letters). So essentially, the maximum value in the input array.
Specifically, if input is [1,5,2,2] is should be at least 5+1 = 6 and not 3 which is the length(Set([1,5,2,2])). The +1 on the maximum is because 0 is a an alphabet value too.

As, the maximum value in the example is rather big, it might be productive to first reduce the maximum alphabet value with a lookup table as follows:

v = Int32[2,1,1,1,1458521]
d = Dict(Iterators.map(reverse,pairs(sort(unique(v)))))
newv = Int32.(get.(Ref(d), v, 0))

Now, the maximum(newv)+1 should be minimal for the input vector, and ready to be shipped to libsais_int. Since the ordering of inputs are preserved, the output suffix array is also identical.

1 Like