l.cpp
上传用户:fxdzsc
上传日期:2021-02-18
资源大小:1k
文件大小:2k
- #include <codecogs/io/compression/lzw.h>
- #include <iostream>
-
- // the number of characters to generate in the sample array
- #define N 10000
-
- using namespace IO::Compression;
-
- int main()
- {
- // initialize random seed
- srand(time(0));
-
- // generate an array of N random letters
- std::vector<unsigned char> sample;
- for (int i = 0; i < N; ++i)
- sample.push_back('A' + rand() % ('Z' - 'A' + 1));
-
- // compress the sample array
- std::vector<unsigned char> compressed = LZW::compress(sample);
-
- // decompress the compressed array
- std::vector<unsigned char> uncompressed = LZW::decompress(compressed);
-
- // compare the sizes of the compressed and uncompressed arrays
- std::cout << " Size of the sample array: " << N << std::endl;
- std::cout << " Size of the compressed array: " << compressed.size() << std::endl;
- std::cout << "Size of the uncompressed array: " << uncompressed.size() << std::endl;
-
- std::cout << std::endl;
-
- // test if the sample and the uncompressed arrays are identical
- // this proves that the LZW compression algorithm does not affect the initial data
- bool identical = (N == uncompressed.size());
- for (size_t i = 0; identical && i < uncompressed.size(); ++i)
- if (sample[i] != uncompressed[i])
- identical = false;
-
- if (identical)
- std::cout << "The sample and uncompressed arrays are identical." << std::endl;
- else
- std::cout << "Error! The sample and uncompressed arrays are NOT identical." << std::endl;
-
- return 0;
- }