Author: Andreas Tille <tille@debian.org>
Description: Use Debian packaged streamvbyte library
Reviewed-By: Étienne Mollier <emollier@debian.org>
Last-Update: 2024-07-24
Forwarded: not-needed

--- libslow5lib.orig/CMakeLists.txt
+++ libslow5lib/CMakeLists.txt
@@ -38,7 +38,6 @@
 # include_directories(${PROJECT_SOURCE_DIR}/src)
 # include_directories(${PROJECT_SOURCE_DIR}/test)
 # include_directories(${PROJECT_SOURCE_DIR}/include)
-# include_directories(${PROJECT_SOURCE_DIR}/thirdparty/streamvbyte/include)
 
 set(slow5_ src/slow5.c)
 set(slow5_idx src/slow5_idx.c)
@@ -54,7 +53,6 @@
 # set(openmp_get_reads examples/random_read_openmp.c)
 # add_executable(slow5exmp ${openmp_get_reads})
 
-add_subdirectory(thirdparty/streamvbyte)
 
 option(SLOW5_LINK_STATIC "libslow5 will create a static lib" OFF) #OFF by default
 if(SLOW5_LINK_STATIC)
@@ -67,7 +65,7 @@
 unset(SLOW5_LINK_STATIC CACHE)
 
 target_include_directories(slow5 PUBLIC ${PROJECT_SOURCE_DIR}/include)
-target_link_libraries(slow5 streamvbyte_slow5)
+target_link_libraries(slow5)
 
 # Build a static lib
 #add_library(slow5 STATIC ${slow5_} ${slow5_idx} ${slow5_misc} ${slow5_press})
--- libslow5lib.orig/test/Makefile
+++ libslow5lib/test/Makefile
@@ -4,7 +4,7 @@
 CPPFLAGS	+= -I ../include/ -I $(SRC)/
 #CFLAGS		+= -g -Wall -Werror -Wpedantic -std=c99
 CFLAGS		+= -g -Wall -Werror -std=gnu99
-LDFLAGS		+= $(LIB)/libslow5.a -lm -lz
+LDFLAGS		+= $(LIB)/libslow5.a -lm -lz -lstreamvbyte
 ifeq ($(zstd),1)
 CFLAGS		+= -DSLOW5_USE_ZSTD
 LDFLAGS		+= -lzstd
--- libslow5lib.orig/src/slow5_press.c
+++ libslow5lib/src/slow5_press.c
@@ -1062,7 +1062,7 @@
 static uint8_t *ptr_compress_svb(const uint32_t *ptr, size_t count, size_t *n) {
     uint32_t length = count / sizeof *ptr;
 
-    size_t max_n = __slow5_streamvbyte_max_compressedbytes(length);
+    size_t max_n = streamvbyte_max_compressedbytes(length);
     uint8_t *out = (uint8_t *) malloc(max_n + sizeof length);
     if (!out) {
         SLOW5_MALLOC_ERROR();
@@ -1070,7 +1070,7 @@
         return NULL;
     }
 
-    *n = __slow5_streamvbyte_encode(ptr, length, out + sizeof length);
+    *n = streamvbyte_encode(ptr, length, out + sizeof length);
     memcpy(out, &length, sizeof length); /* copy original length of ptr (needed for depress) */
     *n = *n + sizeof length;
     SLOW5_LOG_DEBUG("max svb bytes=%zu\nsvb bytes=%zu\n",
@@ -1103,7 +1103,7 @@
         slow5_errno = SLOW5_ERR_MEM;
         return NULL;
     }
-    __slow5_zigzag_delta_encode(in, diff, length, 0);
+    zigzag_delta_encode(in, diff, length, 0);
 
 
     SLOW5_LOG_DEBUG("orig bytes=%zu\n", count); /* TESTING */
@@ -1127,7 +1127,7 @@
     }
 
     size_t bytes_read;
-    if ((bytes_read = __slow5_streamvbyte_decode(ptr + sizeof length, out, length)) != count - sizeof length) {
+    if ((bytes_read = streamvbyte_decode(ptr + sizeof length, out, length)) != count - sizeof length) {
         SLOW5_ERROR("Expected streamvbyte_decode to read '%zu' bytes, instead read '%zu' bytes.",
                 count - sizeof length, bytes_read);
         slow5_errno = SLOW5_ERR_PRESS;
@@ -1152,14 +1152,14 @@
     }
     uint32_t length = *n / sizeof *diff;
 
-    int16_t *orig = (int16_t *) malloc(length * sizeof *orig);
+    int32_t *orig = (int32_t *) malloc(length * sizeof *orig);
     if (!orig) {
         SLOW5_MALLOC_ERROR();
         free(diff);
         slow5_errno = SLOW5_ERR_MEM;
         return NULL;
     }
-    __slow5_zigzag_delta_decode(diff, orig, length, 0);
+    zigzag_delta_decode(diff, orig, length, 0);
 
     // int16_t *orig = (int16_t *) malloc(length * sizeof *orig);
     // for (int64_t i = 0; i < length; ++ i) {
@@ -1169,7 +1169,7 @@
     *n = length * sizeof *orig;
     free(diff);
     //free(out);
-    return orig;
+    return (int16_t*)orig;  /* /o\ */
 }
 
 
@@ -1346,7 +1346,7 @@
             return -1;
         }
 
-        nr_press_tmp = __slow5_streamvbyte_max_compressedbytes(nex);
+        nr_press_tmp = streamvbyte_max_compressedbytes(nex);
 		ex_pos_press = (uint8_t *)malloc(nr_press_tmp);
 		if(!ex_pos_press){
             free(ex_pos_delta);
@@ -1357,7 +1357,7 @@
             return -1;
         }
 
-        nr_press_tmp=__slow5_streamvbyte_encode(ex_pos_delta, nex, ex_pos_press);
+        nr_press_tmp=streamvbyte_encode(ex_pos_delta, nex, ex_pos_press);
 		free(ex_pos_delta);
 		nex_pos_press = (uint32_t) nr_press_tmp;
         SLOW5_ASSERT(nex_pos_press > 0);
@@ -1372,7 +1372,7 @@
 		offset += nex_pos_press;
 
         //actual exceptions
-        nr_press_tmp = __slow5_streamvbyte_max_compressedbytes(nex);
+        nr_press_tmp = streamvbyte_max_compressedbytes(nex);
 		ex_press = (uint8_t *)malloc(nr_press_tmp);
         if(!ex_press){
             free(ex_pos);
@@ -1382,7 +1382,7 @@
             return -1;
         }
 
-        nr_press_tmp=__slow5_streamvbyte_encode(ex, nex, ex_press);
+        nr_press_tmp=streamvbyte_encode(ex, nex, ex_press);
 		nex_press = (uint32_t) nr_press_tmp;
         SLOW5_ASSERT(nex_press > 0);
 
@@ -1486,7 +1486,7 @@
 			(void) memcpy(ex_pos_press, in + offset, nex_pos_press);
 			offset += nex_pos_press;
 
-            int ret = __slow5_streamvbyte_decode(ex_pos_press, ex_pos, nex);
+            int ret = streamvbyte_decode(ex_pos_press, ex_pos, nex);
             if (ret !=nex_pos_press){
                 SLOW5_ERROR("Expected streamvbyte_decode to read '%d' bytes, instead read '%d' bytes.",
                         nex_pos_press, ret);
@@ -1516,7 +1516,7 @@
 			offset += nex_press;
 
 
-            ret = __slow5_streamvbyte_decode(ex_press, ex, nex);
+            ret = streamvbyte_decode(ex_press, ex, nex);
             if (ret != nex_press){
                 SLOW5_ERROR("Expected streamvbyte_decode to read '%d' bytes, instead read '%d' bytes.",
                         nex_press, ret);
--- libslow5lib.orig/setup.py
+++ libslow5lib/setup.py
@@ -18,9 +18,9 @@
 # for the install.
 try:
     import numpy as np
-    include_dirs = ['include/', np.get_include(), 'thirdparty/streamvbyte/include']
+    include_dirs = ['include/', np.get_include() ]
 except ImportError:
-    include_dirs = ['include/', 'thirdparty/streamvbyte/include']
+    include_dirs = ['include/']
     def np(*args, ** kwargs ):
         import numpy as np
         return np(*args, ** kwargs)
@@ -38,12 +38,12 @@
 
 sources=['python/pyslow5.pyx', 'src/slow5.c', 'src/slow5_press.c', 'src/slow5_misc.c', 'src/slow5_idx.c',
             'src/slow5_mt.c',
-            'thirdparty/streamvbyte/src/streamvbyte_zigzag.c', 'thirdparty/streamvbyte/src/streamvbyte_decode.c', 'thirdparty/streamvbyte/src/streamvbyte_encode.c']
+            ]
 depends=['python/pyslow5.pxd', 'python/pyslow5.h',
             'slow5/slow5.h', 'slow5/slow5_defs.h', 'slow5/slow5_error.h', 'slow5/slow5_press.h', 'slow5/slow5_lazymt.h',
             'slow5/klib/khash.h', 'slow5/klib/kvec.h',
             'src/slow5_extra.h', 'src/slow5_idx.h', 'src/slow5_misc.h', 'src/klib/ksort.h',
-            'thirdparty/streamvbyte/include/streamvbyte.h', 'thirdparty/streamvbyte/include/streamvbyte_zigzag.h']
+            ]
 extra_compile_args = ['-g', '-Wall', '-O2', '-std=c99', '-DSLOW5_ENABLE_MT=1' ]
 # extra_compile_args = []
 # os.environ["CFLAGS"] = '-g -Wall -O2 -std=c99'
@@ -58,8 +58,8 @@
         extra_compile_args.extend(['-DSTREAMVBYTE_SSSE3=1', '-mssse3'])   # WARNING: ancient x86_64 CPUs don't have SSSE3
 # note that on macos, for now we just let it compile without simd
 
-# include_dirs = ['include/', np.get_include(), 'thirdparty/streamvbyte/include']
-libraries = ['m', 'z']
+# include_dirs = ['include/', np.get_include() ]
+libraries = ['m', 'z', 'streamvbyte']
 library_dirs = ['.']
 
 # a nasty hack to provide option to build with zstd
