本文整理汇总了C++中HCTree::build方法的典型用法代码示例。如果您正苦于以下问题:C++ HCTree::build方法的具体用法?C++ HCTree::build怎么用?C++ HCTree::build使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HCTree
的用法示例。
在下文中一共展示了HCTree::build方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main(int argc, char* argv[])
{
// read the file
std::ifstream ifs(argv[1], std::ios::binary);
std::string buffer((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
if (!buffer.size()) {
std::ofstream ofs(argv[2], std::ios::binary);
exit(0);
}
// make Stream
std::stringstream ss(buffer);
BitInputStream bis(ss);
// build tree
HCTree hct;
hct.build(bis.frequences);
bis.size = getSize(hct, bis.frequences);
std::ofstream ofs(argv[2], std::ios::binary);
// decode
int c = hct.decode(bis);
int i = 0;
while (c >= 0) {
ofs << (byte)c;
c = hct.decode(bis);
}
return 0;
}
示例2: main
int main(int argc, char* argv[])
{
// read the file into a buffer
std::ifstream ifs(argv[1], std::ios::binary);
std::string buffer((std::istreambuf_iterator<char>(ifs)),
(std::istreambuf_iterator<char>()));
// create a new file if we're trying to comrpess an empty file
if (!buffer.size()) {
std::ofstream ofs(argv[2], std::ios::binary);
exit(0);
}
// count the occurences of each char
std::vector<int> freqs(256, 0);
for (size_t i = 0; i < buffer.size(); ++i) {
byte b = buffer[i];
++freqs[b];
}
// build the HCTree
HCTree hct;
hct.build(freqs);
// output the compressed file
std::ofstream ofs(argv[2], std::ios::binary);
BitOutputStream bos(ofs);
makeHeader(ofs, freqs);
for (size_t i = 0; i < buffer.size(); ++i) {
hct.encode(buffer[i], bos);
}
return 0;
}
示例3: main
int main(int argc, char* argv[])
{
// read the file
std::ifstream read(argv[1], std::ios::binary);
std::string buffer((std::istreambuf_iterator<char>(read)),
(std::istreambuf_iterator<char>()));
// Make empty file if original was empty
if (!buffer.size()) {
std::ofstream ofs(argv[2], std::ios::binary);
exit(0);
}
// Get frequences
std::vector<int> frequences(256, 0);
for (size_t i = 0; i < buffer.size(); ++i) {
byte b = buffer[i];
++frequences[b];
}
// build the HCTree
HCTree hct;
hct.build(frequences);
// output the compressed file
std::ofstream ofs(argv[2], std::ios::binary);
BitOutputStream bos(ofs);
Header(ofs, frequences);
for (size_t i = 0; i < buffer.size(); ++i) {
hct.encode(buffer[i], bos);
}
return 0;
}
示例4: main
int main( int argc, char* argv[] )
{
if( argc != 3 )
{
cerr << "Usage: " << argv[0] << " infile outfile." << endl;
return -1;
}
errno = 0;
ifstream in;
in.open( argv[1], ios::binary );
if( errno )
{
cerr << "Error in " << argv[1] << ": " << strerror( errno ) << endl;
return -1;
}
ofstream out;
out.open( argv[2], ios::binary );
if( errno )
{
cerr << "Error in " << argv[2] << ": " << strerror( errno ) << endl;
in.close();
return -1;
}
vector<int>* freqs = new vector<int>( 256, 0 );
BitInputStream* input = new BitInputStream( in );
BitOutputStream* output = new BitOutputStream( out );
int msgCnt = input->readInt();
int symCnt = input->readInt();
for( int i = 1; i <= symCnt; i++ )
{
int c = input->readByte();
int cCnt = input->readInt();
freqs->at( c ) = cCnt;
}
HCTree* decodeTree = new HCTree();
decodeTree->build( *freqs );
input->fill();
for( int i = 0; i < msgCnt; i++ )
{
output->writeByte( decodeTree->decode( *input ) );
}
in.close();
out.close();
delete output;
delete decodeTree;
delete input;
delete freqs;
return 0;
}
示例5: main
int main(int argc, char* argv[]){
if (argc != 3) {
std::cout << argv[0] << " called with incorrect arguments. "<< std::endl;
std::cout << "Usage:" << std::endl;
std::cout << argv[0] << " infile outfile" << std::endl;
exit(-1);
}
infile.open(argv[1], ios::binary); // Open input file
outfile.open(argv[2], ios::binary); // Open output file
BitInputStream* input = new BitInputStream(infile);
if(infile.is_open()) {
// Read the number of unique bytes
unsigned int tlength = input -> readInt();
// Read the length of the original text
int textbytes = input -> readInt();
// Read the frequencies of each byte from the header
unsigned int i = 0; // Start past the two nums, at the actual header
while( i < tlength) {
int d = input -> readInt();
int s = input -> readByte();
freq[s] = d;
i++;
}
// Build the tree from the frequencies
HCTree* myTree = new HCTree();
myTree->build(freq);
//Decode individual bytes
int y = 0;
while (infile.good() && y < textbytes) {
int decoded = myTree -> decode(*input);
outfile.put((unsigned char)decoded);
y++;
}
outfile.flush();
}
else {
std::cout << argv[0] << " called with incorrect arguments. "<< std::endl;
std::cout << "Usage:" << std::endl;
std::cout << argv[0] << " infile outfile" << std::endl;
exit(-1);
}
infile.close();
outfile.close();
}
示例6: main
int main(int argc, char**argv){
ofstream output;
ifstream input;
int size = 0;
int b = 0;
int symbols = 0;
vector<int> freqs(256, 0);
//checks if there are 3 arguments
if (argc != 3) {
cerr << "Incorrect # of arguments" << endl;
return -1;
}
input.open(argv[1], ios::binary);
//checks if there is a problem with the input file
if(!input) {
cerr << "File cannot be opened" << endl;
return -1;
}
//recreate frequency vector from header
for (size_t j = 0; j < freqs.size(); j++){
input.read(((char*)(&freqs[j])), sizeof(int));
size += freqs[j];
}
HCTree tree;
tree.build(freqs);
output.open(argv[2], ios::binary);
BitOutputStream out(output);
BitInputStream in(input);
//seek to beginning of file past the header
input.seekg(ios::beg + (256*sizeof(int)));
//decode the file
for (int x = 0; x < size; x++){
b = tree.decode(in);
output.write((char*)&b, 1);
}
output.close();
input.close();
return 0;
}
示例7: main
int main( int argc, char *argv[] ) {
if( argc != 3 ) {
cerr << "Invalid number of arguments.\n";
return EXIT_FAILURE;
}
ifstream fin;
ofstream fout;
fin.open( argv[1], ios::binary );
/* Check to see if the input file opened correctly. */
if( !fin.is_open() ) {
return EXIT_FAILURE;
}
vector<int> freqs( 256, 0 );
int fsize = 0;
HCTree hufftree;
fin.clear();
/* Read in the header. */
for( size_t i = 0; i < freqs.size(); ++i ) {
fin.read( (char *) &freqs[i], sizeof( int ) );
fsize += freqs[i];
}
/* Build the Huffman tree. */
if( fsize != 0 ) {
hufftree.build( freqs );
}
fout.open( argv[2], ios::binary );
BitInputStream bis( fin );
byte tmp = 0;
for( int i = 0; i < fsize; ++i ) {
tmp = hufftree.decode( bis );
fout.write( (char *) &tmp, 1);
}
fin.close();
fout.close();
return EXIT_SUCCESS;
}
示例8: main
int main(int argc, char** argv) {
string tempString;
//create io files
ifstream inFile;
ofstream outFile;
vector<int> freqs(256,0);
int sum = 0;
int nextChar;
//read header, count frequency
inFile.open(argv[1],ifstream::binary);
BitInputStream bitInFile = BitInputStream(inFile);
int unique = bitInFile.readByte();
if (inFile.good()) {
//for each symbol in the header
for (int i=0; i < unique; i++) {
//read the index of unique chars
int index = bitInFile.readByte();
//read its frequency
nextChar = bitInFile.readInt();
//set frequency at specific index of freqs
freqs[index] = nextChar;
sum += nextChar;
}
}
//build tree /
HCTree huffmanTree;
huffmanTree.build(freqs);
//call decode on each encoded symbol
outFile.open(argv[2], ofstream::binary); //changed to binary
//decode each binary input
for (int i=0; i<sum; i++) {
outFile << (char)huffmanTree.decode(bitInFile);
}
//close files
inFile.close();
outFile.close();
return 0;
}
示例9: main
/** Count and output the number of times char occurs in
* a file named by the first command line argument. */
int main(int argc, char** argv) {
//open input file for reading
ifstream in;
in.open(argv[1], ios::binary);
unsigned char buffer;
int recount = 0;
int totalsymbol = 0;
//---------------------------------------------------------------------------------//
//---------------------------------READ HEADER ------------------------------------//
//---------------------------------------------------------------------------------//
vector<int> frequency(256, 0);
for (int i = 0; i < 256 ; i++) {
recount = 0;
for (int j = 0; j < 4; j++) {
buffer = in.get();
recount = recount | (buffer << (24 - 8*j));
//cout << j << recount << endl;
}
frequency[i] = recount;
totalsymbol = totalsymbol + recount;
//cout << i << " " << recount<< " " << totalsymbol<< endl;
}
HCTree hct;
hct.build(frequency);
//---------------------------------------------------------------------------------//
//--------------------------------DECODE FILE--------------------------------------//
//---------------------------------------------------------------------------------//
//open output file for writing and decode
BitInputStream bitin(in);
ofstream out;
out.open(argv[2], ios::binary);
BitOutputStream bitout(out);
for (totalsymbol; totalsymbol > 0 ; totalsymbol--) {
bitout.writeByte(hct.decode(bitin));
}
//close input and output files
in.close();
out.close();
return 0;
// hct.decode(bitin);
}
示例10: main
int main(int argc, char** argv){
if(argc!=3) return -1;
ifstream in;
ofstream out;
vector<int> freq(256);
HCTree huffmanTree;
int count;
in.open(argv[1],ios::binary);
if(!in.is_open()) return -1;
BitInputStream bitIn=BitInputStream(in);
int len=0;
for(int i=0;i<256;i++){
int temp;
//temp=bitIn.readInt();
in >> temp;
freq[i]=temp;
len +=freq[i];
}
out.open(argv[2],ios::binary);
huffmanTree.build(freq);
if(!out.is_open()) return -1;
//read bit right after the header file
// no need to open it again
while(len>0){
char c = huffmanTree.decode(bitIn);
out << c;
len--;
}
out.close();
in.close();
}
示例11: main
int main(int argc,char **argv)
{
//Check for arguments
if(argc != 3) {
cout << "Invalid number of arguments.\n"
<< "Usage: ./compress <input filename> <output filename>.\n";
return -1;
}
//Check for file names
if(!strcmp(argv[1],argv[2])) {
cerr << "Error: Same name for input and output files.\n";
return -1;
}
/** 1. Open the input file for reading. */
ifstream in;
in.open(argv[1],ios::binary);
/**
* 2. Read bytes from the file, counting the number of occurrences of
* each byte value; then close the file.
*/
vector<int> count(256,0);
int ch;
int isempty = 1;
while(1) {
ch = in.get();
if(!in.good()) break; // failure
count[ch]++; // read a char, count it
isempty = 0;
}
if(isempty) { // empty file
ofstream out;
out.open(argv[2],ios::binary);
out.close();
return 0;
}
if(!in.eof()) { // loop stopped for some bad reason...
cerr << "There was a problem, sorry." << endl;
return -1;
}
in.close();
/**
* 3. Use these byte counts to construct a Huffman coding tree. Each
* unique byte with a non-zero count will be a leaf node in the Huffman tree.
*/
HCTree Huffman;
Huffman.build(count);
/** 4. Open the output file for writing. */
ofstream out;
out.open(argv[2],ios::binary);
/**
* 5. Write enough information to the output file to enable the coding
* tree to be reconstructed when the file is read by your uncompress program.
*/
BitOutputStream os = BitOutputStream(out);
int realcount = 0;
for(int i=0;i<count.size();i++) {
if (count[i]) realcount++; //count for asciis that appear
}
os.writeInt(realcount); //print the number of asciis that appear
os.writeByte('\n');
for(int i=0;i<count.size();i++) {
if (count[i]) {
os.writeInt(i);
os.writeByte(' ');
os.writeInt(count[i]);
os.writeByte('\n');
}
}
/** 6. Open input file for reading again. */
in.open(argv[1],ios::binary);
/**
* 7. Using the Huffman coding tree, translate each byte from the input
* file into its code, and append these codes as a sequence of bits to
* the output file, after the header.
*/
char symbol;
while(1) {
symbol = in.get();
if(!in.good()) break;
Huffman.encode(symbol,os);
}
os.flush();
/** 8. Close the input and output files. */
in.close();
out.close();
return 0;
}
示例12: main
int main (int argc, char *argv[]) {
if(argc != 3) {
cerr << USAGE << endl;
return -1;
}
if(argv[1] == argv[2]){
cerr << "Inputfile and outfile cannot have the same name";
return -1;
}
ifstream infile;
ofstream outfile;
infile.open ( argv[1], ios::binary );
if(!infile.is_open ()) {
cerr<< "Error opening the input file." << endl;
return -1;
}
vector<int> freqs( 256, 0 );
size_t filesize = 0;
size_t infilesize = 0;
unsigned int numsymbols = 0;
int nextchar;
//to calculate the size of infile
infile.clear();
while (1) {
infile.get();
if (infile.eof()) break;
infilesize++; //num of bytes in the file
}
infile.clear();
infile.close();
//reopen infile to start uncompression process
infile.open ( argv[1], ios::binary );
printf("Reading header from file \"%s\"... ", argv[1]);
for (size_t i = 0; i < freqs.size(); ++i) {
infile >> nextchar;
infile.get();
if (nextchar != 0) {
freqs[i] = nextchar;
numsymbols++;
}
filesize += freqs[i];
}
cout << "done." <<endl;
printf("Uncompressed file will have %d unique symbols and size %lu bytes.\n",
numsymbols, filesize);
printf("Building Huffman code tree... ");
HCTree hctree;
if (filesize != 0) hctree.build ( freqs );
cout << "done." << endl;
BitInputStream bitin = BitInputStream (infile);
outfile.open( argv[2], ios::binary );
if (!outfile.is_open()){
cerr<<"Error opening the output file"<<endl;
return -1;
}
printf("Writing to file \"%s\"... ", argv[2]);
// decode and write to the outfile
int b;
for( int i = 0; i < filesize; ++i ) {
b = hctree.decode(bitin);
outfile << (char) b;
}
cout << "done." <<endl;
//int infile_size = infile.tellp();
float ratio = (float)filesize/ (float)infilesize;
printf("Uncompression ratio: %f\n",ratio);
outfile.flush();
infile.close();
outfile.close();
return 0;
} //end main
示例13: main
/**
*Compress a file using Huffman code
*The first argument: infile name
*The second argument: outfile name
**/
int main(int argc, char *argv[])
{
HCTree hTree;
//variable for number of characters
int numChars;
if(argc < 2)
{
std::cout << "Error: Too Few Arguments" << std::endl;
return 1;
}
//store the contents of the first file into inFile
std::string inFile = argv[1];
//store the contents of the second file into outFile
std::string outFile = argv[2];
//std::cout << "Section1" << std::endl;
//create instream
std::ifstream in;
//create outstream
std::ofstream out;
//open infile
in.open(inFile, ios::binary);
long sizeOfFile;
in.seekg(0, ios::end);
sizeOfFile = in.tellg();
in.seekg(0, ios::beg);
if(sizeOfFile == 0)
{
std::cout<<"blank file...try again!!!"<<endl;
std::ofstream empty;
empty.open(outFile, ios::binary);
empty.close();
return 0;
}
//open outfile
out.open(outFile, ios::binary);
//put the instream into bitinputstream
BitInputStream bitInStream(in);
BitOutputStream bitOutStream(out);
//std::cout << "Section2" << std::endl;
int total, count, index;
//variable to hold frequency
std::vector<int> freqs(256,0);
int symb = bitInStream.readByte();
while(symb != -1)
{
//std::cout << (char) symb << std::endl;
freqs[symb]++;
symb = bitInStream.readByte();
}
//std::cout << "Section3" << std::endl;
/**for(int i = 0; i < freqs.size(); i++)
{
if(freqs[i])
std::cout << freqs[i] <<std::endl;
}**/
hTree.build(freqs);
//std::cout << "Writing Header" << std::endl;
write_header(freqs, bitOutStream);
//std::cout << "Files closed" << std::endl;
in.close();
in.open(inFile, ios::binary);
//std::cout << "Section4" << std::endl;
/**HCNode node1(1, 'r', nullptr, nullptr, nullptr);
HCNode node2(1, 'r', nullptr, nullptr, nullptr);
HCNode node3(2, 'a', nullptr, nullptr, nullptr);
HCNode node4(3, 'a', nullptr, nullptr, nullptr);**/
//std::cout << "node1 == node2: " << (comp(&node1,&node2)) << std::endl;
//std::cout << "node1 == node1: " << (comp(&node1,&node1)) << std::endl;
//std::cout << "node2 == node3: " << (comp(&node2,&node3)) << std::endl;
//.........这里部分代码省略.........
示例14: main
int main(int argc, char** argv){
if(argc != 3){
cerr << "One file doesn't exist, sorry." << endl;
return -1;
}
if(strcmp(argv[1],argv[2])==0){
cerr << "Output file has the same name as input file, sorry." << endl;
return -1;
}
int valid = 0;
int onlychar;
int total = 0;
int isEmpty = 1;
ifstream in;
in.open(argv[1], ios::binary);
ofstream out;
out.open(argv[2], ios::binary);
if(!in.good()){
cerr << "The input file can't be opened or read from, sorry." << endl;
return -1;
}
if(!out.good()){
cerr << "The output file can't be written, sorry." << endl;
return -1;
}
vector<int> count = vector<int>(256,0);
unsigned char ch;
while(1){
ch = in.get();
if(!in.good()) break;
isEmpty = 0;
count[ch]++;
total++;
}
if(!in.eof()){
cerr << "There was a problem, sorry." << endl;
return -1;
}
if(isEmpty == 1){
cerr << "The file is empty, sorry." << endl;
return -1;
}
in.close();
// ofstream out;
// out.open(argv[2], ios::binary);
BitOutputStream* a = new BitOutputStream(out);
a->writeInt(total);
for(int i=0;i<256;i++){
if(count[i]){
valid++;
onlychar = i;
}
}
if(valid==1){
a->writeByte(1);
a->writeByte(onlychar);
out.close();
return 0;
}else{
a->writeByte(0);
}
// for(int i=0;i<256;i++){
// cout << i << " " << count[i] << endl;
// }
HCTree* tree = new HCTree();
tree->build(count);
for(int i=0;i<256;i++){
a->writeByte(tree->leaf[i]);
}
for(int i=0;i<255;i++){
a->writeByte(tree->intnode[i]);
}
for(int i=0;i<32;i++){
a->writeByte(tree->leafchildbit[i]);
}
for(int i=0;i<32;i++){
a->writeByte(tree->intnodechildbit[i]);
}
in.open(argv[1], ios::binary);
//cout << "test" << endl;
while(1){
ch = in.get();
if(!in.good()) break;
tree->encode(ch, *a);
}
if(!in.eof()){
cerr << "There was a problem, sorry." << endl;
return -1;
}
if(a->get_buf_index()!=0){
a->flush();
}
in.close();
out.close();
return 0;
}
示例15: main
int main(int argc, char * argv[]){
//get the filename from argv[]
ifstream in (argv[1]);
//Create a vector to hold the character frequencies
vector<int> frequency (256, 0);
//Create a char variable to hold the charater from the stream
unsigned char ch;
// Number of unique symbols
int uniqueS = 0;
//create an integer to hold frequency of parsed string
int freqNumber = 0;
//Check that the file exists
if(in.is_open())
{
//Declare a char for the integers only
unsigned char numChar = '\0';
//Create string to get full frequency in char form
unsigned int indexCounter = 0;
//Create an array to hold the integer frequency
char numArray[10] = {};
//Get the character
ch = in.get();
if(in.gcount() < 1)
{
cout << "Uncompressible file" << endl;
cout << "Nothing written to file " << argv[2] << endl;
ofstream out (argv[2]);
in.close();
out.close();
return 0;
}
cout << "Reading header from file " << argv[1] << "... ";
//Loop to get the header
while(in.good() && (unsigned int)ch >= 0)
{
//The header is terminated with the null
//character, so break when this happens
if(ch == '\0')
{
break;
}
else
{
//Ignore the space
in.ignore();
//Parse the string until we hit a space
while((numChar = in.get()) != ' ')
{
numArray[indexCounter] = numChar;
indexCounter++;
}
//Convert string to integer
freqNumber = atoi(numArray);
frequency[(unsigned int) ch] = freqNumber;
//Reset the array to null values
//not efficient...
for(unsigned int j = 0; j < 10; j++)
numArray[j] = '\0';
//Reset the index counter
indexCounter = 0;
}
//Get the character
ch = in.get();
uniqueS++;
}
cout << "done." << endl;
}
else
{
//Specified file was not found
cout << "No readable file was found" << endl;
in.close();
return 0;
}
cout << "Uncompressed file will have " << uniqueS << " unique symbols" << endl;
cout << "Building Huffman coding tree... ";
//Build the Huffman tree based on the frequencies
HCTree* huffTree = new HCTree();
huffTree->build(frequency);
cout << "done." << endl;
///////////////////////////////////////////////
//open the output file to scan and read from///
///////////////////////////////////////////////
//.........这里部分代码省略.........