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


C++ HCTree::decode方法代码示例

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


在下文中一共展示了HCTree::decode方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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;
}
开发者ID:hongmincschen,项目名称:CSE_100,代码行数:33,代码来源:uncompress.cpp

示例2: 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

示例3: 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

示例4: 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

示例5: 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

示例6: 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

示例7: 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

示例8: 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

示例9: 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

示例10: main


//.........这里部分代码省略.........
    out.open(argv[2], ios::binary);
  }

  /** Bit Input Stream (for compress input) */
  BitInputStream bitin = BitInputStream(in);

  vector<int> counts(ASCII); /** Vector that will store all ASCII counts */
  HCTree tree = HCTree();    /** HCTree that will be doing the decoding */

  unsigned char ch;          /** Temporary char to hold next input */
  int currentVal = 0;        /** Temporary int to hold current vector value */

  int numUnique  = 0;        /** Keep track of # unique symbols */
  int numOutChar = 0;        /** Keep track of # output characters */
  /******************** END VARIABLE INITIALIZE ********************/

  /******************** BEGIN ARGUMENT PARSE ********************/
  /** Ensure 2 arguments */
  if(argc != 3)
  {
    cerr << "Invalid usage: must have 2 arguments." << endl;
    exit(-1);
  }

  /** Check for valid input file */
  if(!in.good())
  {
    cerr << "Invalid input file: " << argv[1] << endl;
    exit(-1);
  }

  /** Check for valid input file */
  if(!out.good())
  {
    cerr << "Invalid output file: " << argv[2] << endl;
    exit(-1);
  }

  /** Check for same file name input and output */
  if(!strcmp(argv[1],argv[2]))
  {
    cerr << "Input and Output file names cannot be the same!" << endl;
    exit(-1);
  }

  /********************* END ARGUMENT PARSE *********************/

  /** Parse through header file to read counts vector */
  cout << "Reading header from file \"" << argv[1] << "\"...";

  /** First byte is numUnique */
  ch = in.get();
  if(!in.good())
  {
    cerr << "Input file is empty!!!" << endl;
    exit(1);
  }

  /** 1st byte is # unique chars. Note that max value of 1 byte = 255, so I
   *  subtract 1 from numUnique in compress (the 0 unique case is the empty
   *  file, which I handle separately), and I add 1 when I read it in
   *  uncompress
   */
  numUnique = ((int)ch + 1);

  /** Read header for each unique symbol */
  for(int i = 0; i < numUnique; i++)
  {
    /** First char is symbol */
    ch = in.get();
    unsigned char symbol = ch;

    /** Second char is count */
    int symCount;
    in.read( (char*)&symCount, sizeof(symCount) );

    /** Update count at symbol with the correct count */
    counts.at((int)((unsigned char)symbol)) = symCount;
    numOutChar += symCount;
  }
  cout << " done." << endl;

  /** Create HCTree and populate its leaves*/
  cout << "Building Huffman code tree...";
  tree.build(counts);
  cout << " done." << endl;

  /** Read rest of file and decode */
  cout << "Writing to file \"" << argv[2] << "\"...";

  int i = 0;
  while(i < numOutChar)
  {
    out << (char)tree.decode(bitin);
    i++;
  }
  cout << " done." << endl;

  return 0;
}
开发者ID:hak053,项目名称:Algorithm-Problem-Solutions,代码行数:101,代码来源:uncompress.cpp

示例11: 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
开发者ID:cluborega,项目名称:Huffman-Coding,代码行数:79,代码来源:uncompress.cpp

示例12: main


//.........这里部分代码省略.........
	    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///
    ///////////////////////////////////////////////

    //Create a stream to write to using the second command
    //line arguement
    ofstream out;
    out.open(argv[2]);
        
    //Create a BitInputStream
    BitInputStream toFile(in);

    //Check that the file exists
    if(in.is_open())
    {
	cout << "Writing to file " << argv[2] << "... ";
	unsigned int decodedAscii;
        decodedAscii = huffTree->decode(toFile);
        //Loop until you have reached the end of the file
        while(decodedAscii >= 0 && in.good() && decodedAscii != 256)
	{
            out.put((unsigned char) decodedAscii);
            decodedAscii = huffTree->decode(toFile);
        }
	
	toFile.finalFillIn();		 
	huffTree->~HCTree();
	cout << "done." << endl;
        //close the file
        in.close();
        out.close();
    }
    else
    {
	//Specified file was not found
        cout << "No readable file was found" << endl;
	in.close();
	out.close();
        return 0;
    }
    return 0;
}
开发者ID:Jesse-Bishop,项目名称:CSE100_Huffman,代码行数:101,代码来源:uncompress.cpp

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