From a83504d916958bca79ed2cea2ffdb920f4d023c5 Mon Sep 17 00:00:00 2001 From: Ilya Grebnov Date: Mon, 14 Feb 2022 20:06:59 -0800 Subject: [PATCH] bsc_bwt_encode: handle NULL values for 'num_indexes' and 'indexes' parameters. --- libbsc/bwt/bwt.cpp | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/libbsc/bwt/bwt.cpp b/libbsc/bwt/bwt.cpp index a8678eb..94c5640 100644 --- a/libbsc/bwt/bwt.cpp +++ b/libbsc/bwt/bwt.cpp @@ -44,18 +44,39 @@ int bsc_bwt_encode(unsigned char * T, int n, unsigned char * num_indexes, int * { if (int * RESTRICT A = (int *)bsc_malloc(n * sizeof(int))) { - int mod = n / 8; + int index; + + if (num_indexes != NULL && indexes != NULL) { - mod |= mod >> 1; mod |= mod >> 2; - mod |= mod >> 4; mod |= mod >> 8; - mod |= mod >> 16; mod >>= 1; - } + int I[256]; + + int mod = n / 8; + { + mod |= mod >> 1; mod |= mod >> 2; + mod |= mod >> 4; mod |= mod >> 8; + mod |= mod >> 16; mod >>= 1; + } + +#ifdef LIBBSC_OPENMP + index = libsais_bwt_aux_omp(T, T, A, n, 0, NULL, mod + 1, I, (features & LIBBSC_FEATURE_MULTITHREADING) > 0 ? 0 : 1); +#else + index = libsais_bwt_aux(T, T, A, n, 0, NULL, mod + 1, I); +#endif + if (index == 0) + { + num_indexes[0] = (unsigned char)((n - 1) / (mod + 1)); + index = I[0]; for (int t = 0; t < num_indexes[0]; ++t) indexes[t] = I[t + 1] - 1; + } + } + else + { #ifdef LIBBSC_OPENMP - int index = libsais_bwt_aux_omp(T, T, A, n, 0, NULL, mod + 1, indexes, (features & LIBBSC_FEATURE_MULTITHREADING) > 0 ? 0 : 1); + index = libsais_bwt_omp(T, T, A, n, 0, NULL, (features & LIBBSC_FEATURE_MULTITHREADING) > 0 ? 0 : 1); #else - int index = libsais_bwt_aux(T, T, A, n, 0, NULL, mod + 1, indexes); + index = libsais_bwt(T, T, A, n, 0, NULL); #endif + } bsc_free(A); @@ -65,9 +86,6 @@ int bsc_bwt_encode(unsigned char * T, int n, unsigned char * num_indexes, int * case -2 : return LIBBSC_NOT_ENOUGH_MEMORY; } - num_indexes[0] = (unsigned char)((n - 1) / (mod + 1)); - index = indexes[0]; for (int t = 0; t < num_indexes[0]; ++t) indexes[t] = indexes[t + 1] - 1; - return index; } return LIBBSC_NOT_ENOUGH_MEMORY;