1919# include < lz4/lz4.h>
2020#endif
2121
22+ #define USE_RC
23+ #define USE_ADAPTIVE
24+ #define USE_SLZ4
25+ #define USE_ZLIB
26+
27+ // #undef USE_LZ4
28+
2229class Timer
2330{
2431public:
@@ -46,9 +53,11 @@ long long Timer::microseconds() const
4653 return std::chrono::duration_cast<std::chrono::microseconds>(end_ - start_).count ();
4754}
4855
56+ #ifdef USE_SLZ4
4957int def_slz4 (cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcoder::u8 * src)
5058{
51- cpprcoder::s32 result = slz4::compress (outStream.capacity (), reinterpret_cast <slz4::u8 *>(&outStream[0 ]), srcSize, reinterpret_cast <slz4::u8 *>(src));
59+ slz4::SLZ4Context context;
60+ cpprcoder::s32 result = slz4::compress (context, outStream.capacity (), reinterpret_cast <slz4::u8 *>(&outStream[0 ]), srcSize, reinterpret_cast <slz4::u8 *>(src));
5261
5362 if (0 <= result) {
5463 outStream.resize (result);
@@ -64,7 +73,9 @@ int inf_slz4(cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcod
6473 }
6574 return result;
6675}
76+ #endif
6777
78+ #ifdef USE_ZLIB
6879int def_zlib (cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcoder::u8 * src)
6980{
7081 int ret, flush;
@@ -156,6 +167,7 @@ int inf_zlib(cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcod
156167 inflateEnd (&stream);
157168 return ret == Z_STREAM_END ? outCount : Z_DATA_ERROR;
158169}
170+ #endif
159171
160172#ifdef USE_LZ4
161173int def_lz4 (cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcoder::u8 * src)
@@ -177,6 +189,7 @@ int inf_lz4(cpprcoder::MemoryStream& outStream, cpprcoder::u32 srcSize, cpprcode
177189}
178190#endif
179191
192+ #ifdef USE_RC
180193void run_rangecoder (const char * filepath)
181194{
182195 Timer timer;
@@ -223,7 +236,9 @@ void run_rangecoder(const char* filepath)
223236 }
224237 delete[] src;
225238}
239+ #endif
226240
241+ #ifdef USE_ADAPTIVE
227242void run_adaptive (const char * filepath)
228243{
229244 Timer timer;
@@ -281,7 +296,9 @@ void run_adaptive(const char* filepath)
281296 }
282297 delete[] src;
283298}
299+ #endif
284300
301+ #ifdef USE_SLZ4
285302void run_slz4 (const char * filepath)
286303{
287304 Timer timer;
@@ -312,19 +329,27 @@ void run_slz4(const char* filepath)
312329 timer.stop ();
313330 deflateTime = timer.microseconds ();
314331
332+ timer.start ();
315333 if (inf_slz4 (decstream, encstream.size (), &encstream[0 ]) < 0 ) {
316334 delete[] src;
317335 return ;
318336 }
319337 timer.stop ();
320338 inflateTime = timer.microseconds ();
321339
340+ for (size_t i=0 ; i<decstream.size (); ++i){
341+ if (src[i] != decstream[i]){
342+ return ;
343+ }
344+ }
322345 double ratio = (double )encstream.size () / size;
323346 // printf("|%s|%d|%d|%f|%lld|%lld|\n", filepath, size, encstream.size(), ratio, deflateTime, inflateTime);
324347 printf (" |%s|%f|%lld|%lld|\n " , filepath, ratio, deflateTime, inflateTime);
325348 delete[] src;
326349}
350+ #endif
327351
352+ #ifdef USE_ZLIB
328353void run_zlib (const char * filepath)
329354{
330355 Timer timer;
@@ -353,6 +378,7 @@ void run_zlib(const char* filepath)
353378 timer.stop ();
354379 deflateTime = timer.microseconds ();
355380
381+ timer.start ();
356382 if (inf_zlib (decstream, encstream.size (), &encstream[0 ]) < 0 ) {
357383 delete[] src;
358384 return ;
@@ -365,6 +391,7 @@ void run_zlib(const char* filepath)
365391 printf (" |%s|%f|%lld|%lld|\n " , filepath, ratio, deflateTime, inflateTime);
366392 delete[] src;
367393}
394+ #endif
368395
369396#ifdef USE_LZ4
370397void run_lz4 (const char * filepath)
@@ -396,7 +423,8 @@ void run_lz4(const char* filepath)
396423 }
397424 timer.stop ();
398425 deflateTime = timer.microseconds ();
399-
426+
427+ timer.start ();
400428 if (inf_lz4 (decstream, encstream.size (), &encstream[0 ]) < 0 ) {
401429 delete[] src;
402430 return ;
@@ -411,6 +439,7 @@ void run_lz4(const char* filepath)
411439}
412440#endif
413441
442+ #ifdef USE_RC
414443bool test_rangecoder ()
415444{
416445 std::mt19937 mt;
@@ -439,7 +468,9 @@ bool test_rangecoder()
439468 }
440469 return true ;
441470}
471+ #endif
442472
473+ #ifdef USE_ADAPTIVE
443474bool test_adaptive ()
444475{
445476 std::mt19937 mt;
@@ -477,10 +508,11 @@ bool test_adaptive()
477508 }
478509 return true ;
479510}
511+ #endif
480512
481513int main (int /* argc*/ , char ** /* argv*/ )
482514{
483- test_rangecoder ();
515+ // test_rangecoder();
484516 // test_adaptive();
485517
486518 const char * files[] =
@@ -499,25 +531,31 @@ int main(int /*argc*/, char** /*argv*/)
499531 };
500532 static const int numFiles = sizeof (files) / (sizeof (files[0 ]));
501533
534+ #ifdef USE_RC
502535 printf (" Range Coder\n " );
503536 printf (" -------------------------------------------\n " );
504537 for (int i = 0 ; i < numFiles; ++i) {
505538 run_rangecoder (files[i]);
506539 }
540+ #endif
507541
542+ #ifdef USE_ADAPTIVE
508543 printf (" Adaptive Range Coder\n " );
509544 printf (" -------------------------------------------\n " );
510545 for (int i = 0 ; i < numFiles; ++i) {
511546 run_adaptive (files[i]);
512547 }
548+ #endif
513549
550+ #ifdef USE_SLZ4
514551 printf (" SLZ4\n " );
515552 printf (" -------------------------------------------\n " );
516553 for (int i = 0 ; i < numFiles; ++i) {
517554 run_slz4 (files[i]);
518555 }
556+ #endif
519557
520- #if 1
558+ #ifdef USE_ZLIB
521559 printf (" ZLib\n " );
522560 printf (" -------------------------------------------\n " );
523561 for (int i = 0 ; i < numFiles; ++i) {
0 commit comments