Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use qsort to sort short ByteString #267

Merged
merged 8 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Data/ByteString/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ module Data.ByteString.Internal (
memset, -- :: Ptr Word8 -> Word8 -> CSize -> IO (Ptr Word8)

-- * cbits functions
c_reverse, -- :: Ptr Word8 -> Ptr Word8 -> CInt -> IO ()
c_intersperse, -- :: Ptr Word8 -> Ptr Word8 -> CInt -> Word8 -> IO ()
c_maximum, -- :: Ptr Word8 -> CInt -> IO Word8
c_minimum, -- :: Ptr Word8 -> CInt -> IO Word8
c_count, -- :: Ptr Word8 -> CInt -> Word8 -> IO CInt
c_sort, -- :: Ptr Word8 -> CInt -> IO ()
c_reverse, -- :: Ptr Word8 -> Ptr Word8 -> CSize -> IO ()
c_intersperse, -- :: Ptr Word8 -> Ptr Word8 -> CSize -> Word8 -> IO ()
c_maximum, -- :: Ptr Word8 -> CSize -> IO Word8
c_minimum, -- :: Ptr Word8 -> CSize -> IO Word8
c_count, -- :: Ptr Word8 -> CSize -> Word8 -> IO CInt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
c_count, -- :: Ptr Word8 -> CSize -> Word8 -> IO CInt
c_count, -- :: Ptr Word8 -> CSize -> Word8 -> IO CSize

This could affect all sites that don't use fromIntegral, but that's probably already handled, because CInt isn't a priori any particular one of the normal Haskell integral types, so the fromIntegral should already be there.

c_sort, -- :: Ptr Word8 -> CSize -> IO ()

-- * Chars
w2c, c2w, isSpaceWord8, isSpaceChar8,
Expand Down Expand Up @@ -746,19 +746,19 @@ memset p w s = c_memset p (fromIntegral w) s
--

foreign import ccall unsafe "static fpstring.h fps_reverse" c_reverse
:: Ptr Word8 -> Ptr Word8 -> CULong -> IO ()
:: Ptr Word8 -> Ptr Word8 -> CSize -> IO ()

foreign import ccall unsafe "static fpstring.h fps_intersperse" c_intersperse
:: Ptr Word8 -> Ptr Word8 -> CULong -> Word8 -> IO ()
:: Ptr Word8 -> Ptr Word8 -> CSize -> Word8 -> IO ()

foreign import ccall unsafe "static fpstring.h fps_maximum" c_maximum
:: Ptr Word8 -> CULong -> IO Word8
:: Ptr Word8 -> CSize -> IO Word8

foreign import ccall unsafe "static fpstring.h fps_minimum" c_minimum
:: Ptr Word8 -> CULong -> IO Word8
:: Ptr Word8 -> CSize -> IO Word8

foreign import ccall unsafe "static fpstring.h fps_count" c_count
:: Ptr Word8 -> CULong -> Word8 -> IO CULong
:: Ptr Word8 -> CSize -> Word8 -> IO CULong
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also return a CSize

Suggested change
:: Ptr Word8 -> CSize -> Word8 -> IO CULong
:: Ptr Word8 -> CSize -> Word8 -> IO CSize


foreign import ccall unsafe "static fpstring.h fps_sort" c_sort
:: Ptr Word8 -> CULong -> IO ()
:: Ptr Word8 -> CSize -> IO ()
12 changes: 6 additions & 6 deletions cbits/fpstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include "fpstring.h"

/* copy a string in reverse */
void fps_reverse(unsigned char *q, unsigned char *p, unsigned long n) {
void fps_reverse(unsigned char *q, unsigned char *p, size_t n) {
p += n-1;
while (n-- != 0)
*q++ = *p--;
Expand All @@ -42,7 +42,7 @@ void fps_reverse(unsigned char *q, unsigned char *p, unsigned long n) {
of the duplicated string */
void fps_intersperse(unsigned char *q,
unsigned char *p,
unsigned long n,
size_t n,
unsigned char c) {

while (n > 1) {
Expand All @@ -55,7 +55,7 @@ void fps_intersperse(unsigned char *q,
}

/* find maximum char in a packed string */
unsigned char fps_maximum(unsigned char *p, unsigned long len) {
unsigned char fps_maximum(unsigned char *p, size_t len) {
unsigned char *q, c = *p;
for (q = p; q < p + len; q++)
if (*q > c)
Expand All @@ -64,7 +64,7 @@ unsigned char fps_maximum(unsigned char *p, unsigned long len) {
}

/* find minimum char in a packed string */
unsigned char fps_minimum(unsigned char *p, unsigned long len) {
unsigned char fps_minimum(unsigned char *p, size_t len) {
unsigned char *q, c = *p;
for (q = p; q < p + len; q++)
if (*q < c)
Expand All @@ -73,7 +73,7 @@ unsigned char fps_minimum(unsigned char *p, unsigned long len) {
}

/* count the number of occurences of a char in a string */
unsigned long fps_count(unsigned char *p, unsigned long len, unsigned char w) {
unsigned long fps_count(unsigned char *p, size_t len, unsigned char w) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unsigned long fps_count(unsigned char *p, size_t len, unsigned char w) {
size_t fps_count(unsigned char *p, size_t len, unsigned char w) {

And also in the declartion of c below.

unsigned long c;
for (c = 0; len-- != 0; ++p)
if (*p == w)
Expand All @@ -93,6 +93,6 @@ int fps_compare(const void *a, const void *b) {
return (int)*(unsigned char*)a - (int)*(unsigned char*)b;
}

void fps_sort(unsigned char *p, unsigned long len) {
void fps_sort(unsigned char *p, size_t len) {
return qsort(p, len, 1, fps_compare);
}
14 changes: 6 additions & 8 deletions include/fpstring.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@

#include <string.h>
#include <stdlib.h>

void fps_reverse(unsigned char *dest, unsigned char *from, unsigned long len);
void fps_intersperse(unsigned char *dest, unsigned char *from, unsigned long len, unsigned char c);
unsigned char fps_maximum(unsigned char *p, unsigned long len);
unsigned char fps_minimum(unsigned char *p, unsigned long len);
unsigned long fps_count(unsigned char *p, unsigned long len, unsigned char w);
void fps_sort(unsigned char *p, unsigned long len);

void fps_reverse(unsigned char *dest, unsigned char *from, size_t len);
void fps_intersperse(unsigned char *dest, unsigned char *from, size_t len, unsigned char c);
unsigned char fps_maximum(unsigned char *p, size_t len);
unsigned char fps_minimum(unsigned char *p, size_t len);
unsigned long fps_count(unsigned char *p, size_t len, unsigned char w);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
unsigned long fps_count(unsigned char *p, size_t len, unsigned char w);
size_t fps_count(unsigned char *p, size_t len, unsigned char w);

void fps_sort(unsigned char *p, size_t len);