当前位置: 首页>>代码示例>>C++>>正文


C++ HCTree类代码示例

本文整理汇总了C++中HCTree的典型用法代码示例。如果您正苦于以下问题:C++ HCTree类的具体用法?C++ HCTree怎么用?C++ HCTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了HCTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: 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;
}
开发者ID:hongmincschen,项目名称:CSE_100,代码行数:34,代码来源:compress.cpp

示例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;
}
开发者ID:ElliotY,项目名称:cse100,代码行数:34,代码来源:compress.cpp

示例3: 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;
}
开发者ID:hongmincschen,项目名称:CSE_100,代码行数:33,代码来源:uncompress.cpp

示例4: main

int main(int argc,  char* argv[]){
 
  //error checking 
  if(argc != 3){
    cerr << "The wrong number of arguments were entered.\n";
    return 0;
  }
  ifstream inStream;		// input filestream
  ofstream outStream;		// output filestream	
  HCTree Tree;			// instance of HCTree class
  byte symbol;		// will store current symbol
  
  inStream.open(argv[1],ios::binary);		// open the input file
  // error checking
  if(inStream.fail()){
    cerr << "Had trouble opening input file.\n";
  }
  outStream.open(argv[2]);		        // open the output file
  BitInputStream bis(inStream);			// call to constructor
  

  // handle header processing
  int charcount = bis.readInt();	// total number of chars 
  cout << endl;
  int diffsymbols = bis.readInt(); 	// number of distinct symbols
  
  if(charcount == 0){
    return 0;
  }
  vector<byte>symbols(diffsymbols, 0);		// vector of levels
  vector<int>levels(diffsymbols, 0);		// vector of symbols 
  
  // this is for the clever header
  for(int i = 0; i < diffsymbols; i++){
    symbols[i] = bis.readByte();
    levels[i] = bis.readByte();		

  }
  
  // this is for the optimized header
  // the leaves and (corresponding levels) are stored in pre-order
  // traversal order
  Tree.rebuild(symbols, levels);
  BitOutputStream bos(outStream);
  
  // read remaining bits, to find the original value
  symbol = Tree.decode(bis);
  outStream.put(symbol);
  int j = 1;
  
  while(j < charcount){
    symbol = Tree.decode(bis);
    outStream.put(symbol);
    j = j+1;
  }
  cout << endl;
  return 0;
}
开发者ID:ShaunakDas88,项目名称:Huffman-Compression,代码行数:58,代码来源:uncompress.cpp

示例5: 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;
}
开发者ID:HasanAbuAmara,项目名称:CSE100-PA3,代码行数:55,代码来源:uncompress.cpp

示例6: 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();
}
开发者ID:iphone5sbetter,项目名称:CS100Assignments,代码行数:53,代码来源:uncompress.cpp

示例7: 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;
}
开发者ID:dskuang,项目名称:Huffman-Coding-Tree,代码行数:52,代码来源:uncompress.cpp

示例8: 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;
}
开发者ID:danielcristiancazares,项目名称:Huffman-Compression-Tree,代码行数:50,代码来源:uncompress.cpp

示例9: 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;
}
开发者ID:madhsia,项目名称:Compress,代码行数:49,代码来源:uncompress.cpp

示例10: 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);
	

}
开发者ID:zhaoweigg,项目名称:Huffman-Compressor,代码行数:49,代码来源:uncompress.cpp

示例11: 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();
    
}
开发者ID:wbangbang,项目名称:CSE-100,代码行数:44,代码来源:uncompress.cpp

示例12: getSize

// get the size of the compressed file in bits
int getSize(HCTree &hct, std::vector<int> frequences)
{
	int TheSize = 0;
	for (size_t i = 0; i < frequences.size(); ++i) {
		if (frequences[i]) {
			TheSize += frequences[i] * hct.fetchCode(i).size(); 
		}
	}
	return TheSize;
}
开发者ID:hongmincschen,项目名称:CSE_100,代码行数:11,代码来源:uncompress.cpp

示例13: main

int main(int argc, char* argv[]){
  if (argc < 3) {
    printUsage(argv);
    exit(-1);
  }
  
  ifstream is (argv[1], ios::binary);
  BitInputStream in(is);
  ofstream out (argv[2],ios::binary);
  HCTree* hct = new HCTree();
  int numChars = 0; // number of characters need to be decoded

  // read the number of characters at the beginning of compressed file
  is.read((char*)&numChars, sizeof(numChars));
  
  /* check if it is empty */
  if (numChars == 0){
    delete hct;
    exit(-1);
  }
  
  hct->rebuild(is);
  
  /* write the symbols to uncompressed file */
  if (is.is_open()) {
    for (int i=0; i<numChars; i++){
      int result=hct->decode(in);
      out<<(byte) result;
    }
    is.close();
    out.close();
  }
  else{
    cerr << "\tError: Cannot open infile: " << argv[1] << endl;
    exit(-1);
  }
  
  delete hct;
  return 0;
}
开发者ID:ericc572,项目名称:UCSD_CSE100_Winter2015,代码行数:40,代码来源:uncompress.cpp

示例14: main

int main(int argc, char * argv[])
{
    

    ifstream infile;
    ofstream outfile;
    vector<int> freqs(256,0);
    HCTree tree;
    
    infile.open(argv[1],ios_base::binary);
    if(! infile.good() )
        cerr<<"Can't open input file!"<<endl;
    BitInputStream in(infile);
    
    /** First read in the file and count the freq of every byte
     */
    unsigned char temp;
    while(1)
    {
        temp=in.readByte();
        if(in.eof())
            break;
        freqs[temp]++;
    }
    infile.close();

#define testinput
#ifndef testinput
#define testinput
    for (int i=0;i<256;i++)
    {
        cout<<setw(6)<<i<<": "<<setw(6)<<freqs[i];
        if (i%4==3)
            cout<<endl;
    }
#endif
    //build tree;
    tree.build(freqs);
    
    outfile.open(argv[2],ios_base::binary);
    BitOutputStream out(outfile);
    
    if(! outfile.good() )
        cerr<<"Can't open output file!"<<endl;
    
    //write header to target file
    tree.writeHeader(out);
    
    //reopen input file, and encode the byte, write to the target file the encoded bits
    infile.open(argv[1],ios_base::binary);
    while(1)
    {
        temp = in.readByte();
        if(in.eof())
            break;
        tree.encode(temp,out);
    }
    //flush the last bit buffer
    out.flush();
    infile.close();
    outfile.close();
    
    
}
开发者ID:hongmincschen,项目名称:HuffmanCompressing,代码行数:64,代码来源:compress.cpp

示例15: main

int main(int argc, char const *argv[])
{
	vector<int> freqs(256, 0);
	string infile, outfile;
	infile = argv[1];
	outfile = argv[2];
	int sum=0;

	//Read the header from input file 
	ifstream compFile;
	compFile.open(infile,ios::binary);
	if(!compFile){
		cout<<"File open error!\n";
		return 0;
	}
	if(compFile.is_open()){
		string temp;
		getline(compFile,temp);
		if(temp != "h"){
			cout<<"Not a correct compressed file!"<<endl;
			return 0;
		}
		else{
			getline(compFile,temp);
		}
		while(temp != "f"){
			int strSize = temp.size();
			int symbol = 0, count = 0;
			bool flag = false;
			for (int i = 0; i < strSize; ++i)
			{
				if(temp[i] != ' '){
					if(flag == false){
						symbol = symbol*10 + temp[i] -'0';
					}
					else{
						count = count*10 + temp[i] - '0';
					}
				}
				else{
					flag = true;
				}

			}	
			freqs[symbol] = count;
			sum = sum + count;
			getline(compFile,temp);

		}
		
	}

	//Build the Huffman code
    HCTree hctree;
	hctree.build(freqs);
	ofstream uncompFile;
	uncompFile.open(outfile,ios::binary);
	if(!uncompFile){
		cout<<"File open error!\n";
		return 0;
	}

	//Read the file and translate into symbol
	BitInputStream bitIStream(compFile);
	for(int i = 0; i < sum; ++i){
		uncompFile<<(byte)hctree.decode(bitIStream);
		uncompFile.flush();
	}
	uncompFile.close();
    compFile.close();
	



	return 0;
}
开发者ID:goldslime88,项目名称:E3jDUhELkE,代码行数:76,代码来源:uncompress.cpp


注:本文中的HCTree类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。