Esempio n. 1
0
void test_destroy(FrequencyTable mytable)
{
  cout << mytable.size() << endl;

  mytable.destroy();

  cout << mytable.size() << endl;
}
Esempio n. 2
0
/******************************************************************************
purpose: prints a Frequency Table built from the maketable() function, this
          function first prints out the frequency of each word in the list,
          followed by the word in alphabetical order.
input: a table of class FrequencyTable
returns: n/a
*****************************************************************************/
void print(FrequencyTable table)
{
  int size = table.size();
  string word;
  int freq;

  for(int i = 0; i < size; i++){
    table.get(i, &word, &freq);//gets pointers to the Nodes
    cout << freq << " " << word << endl;
  }
}
Esempio n. 3
0
void print_words(FrequencyTable mytable)
{
  for (int n = 0; n < mytable.size(); n++) {
    int *p_freq = new int;
    string *p_word = new string;
    mytable.get(n, p_word, p_freq);
    cout << *p_freq << " " << *p_word << endl;
    delete p_freq;
    delete p_word;
  }
}
Esempio n. 4
0
int main()
{
  FrequencyTable table;

  table = maketable(table);

  print(table);

  table.destroy();

  return 0;
}
Esempio n. 5
0
int main()
{
  string holder;
  FrequencyTable mytable;

  while (cin >> holder) {
    mytable = insert_word(mytable, holder);
  }

  print_words(mytable);
  
  mytable.destroy();

  return 0;
}
Esempio n. 6
0
void test_in(FrequencyTable mytable)
{
  if ( mytable.in("the")) {
    cout << "Word is in the table" << endl;
  } else {
    cout << "Word is not in the table" << endl;
  }
}
Esempio n. 7
0
FrequencyTable insert_word(FrequencyTable mytable, string holder)
{
  if (isalpha(holder[0])) {
    for (int i = 0; i < (int) holder.length(); i++) {
      holder[i] =  tolower(holder[i]);
    }
    mytable.insert(holder);
  }
  return mytable;
}
FrequencyTable ParsedEncodedFile::createFrequencyTable(const vector<string>& frequenciesAsStrings) const
{
	FrequencyTable table;

	for (unsigned int i = 0; i < frequenciesAsStrings.size(); i++)
	{
		uint16_t frequency = stoi(frequenciesAsStrings.at(i));

		if (frequency == 0)
			continue;

		uint8_t asciiChar = i;
		FVPair fv;
		fv.frequency = frequency;
		fv.value = asciiChar;
		table.push_back(fv);
	}
	return table;
}
Esempio n. 9
0
bool huffmanPrintCode(Stream& in, Stream& out, bool verbose)
{
    bool bOk = true;
    // check for the binnary header
    if(in.mode() == StreamRead && out.mode() == StreamWrite)
    {
        if(isHuffmanFile(in))
        {   // An Encoded file
            if(verbose)
            {
                fprintf(stdout, "huffmanPrintCode: encoded file\n");
            }
            uint32_t dataBytes;
            HuffmanNode* root = readCodeTable(in, dataBytes);
            assert(root);
            SymbolEncoder SE;
            SE.buildSymbolEncoder(root);
            SE.printCodeTable(out);
            bOk = true;
        }
        else
        {
            // A natural File
            if(verbose)
            {
                fprintf(stdout, "huffmanPrintCode: natural file\n");
            }
            FrequencyTable freqTable;
            SymbolEncoder  SE;

            freqTable.updateFreqTable(in);
            calculateHuffmanCode(freqTable, SE);
            SE.printCodeTable(out);
            bOk = true;
        }
    }
    else
    {
        bOk = false;
    }
    return bOk;
}
Esempio n. 10
0
bool huffmanGetFreqencyTable(Stream& in, Stream& out, bool verbose)
{
    bool bOk = true;
    if(verbose)
    {
        fprintf(stdout, "huffmanGetFreqencyTable\n");
    }
    if(in.mode() == StreamRead && out.mode() == StreamWrite)
    {
        FrequencyTable freqTable;
        freqTable.updateFreqTable(in);
        freqTable.sort();
        freqTable.printFrequencyTable(out);
    }
    else
    {
        bOk = false;
    }
    return bOk;
}
Esempio n. 11
0
 void FreqTable(FrequencyTable& table) const override
 {
   Parameters::StringType newName;
   if (Params->FindValue(Parameters::ZXTune::Core::AYM::TABLE, newName))
   {
     GetFreqTable(newName, table);
   }
   else
   {
     Parameters::DataType newData;
     if (Params->FindValue(Parameters::ZXTune::Core::AYM::TABLE, newData))
     {
       // as dump
       if (newData.size() != table.size() * sizeof(table.front()))
       {
         throw MakeFormattedError(THIS_LINE,
           translate("Invalid frequency table size (%1%)."), newData.size());
       }
       std::memcpy(&table.front(), &newData.front(), newData.size());
     }
   }
 }
Esempio n. 12
0
///////////////////////////////////////////////////////////////////////////////
// calculateHuffmanCode turns freqTable into an array with a single entry that
// is the root of the huffman tree. The return value is a SymbolEncoder,
// which is an array of huffman codes index by symbol value.
bool calculateHuffmanCode(FrequencyTable& freqTable, SymbolEncoder& SE)
{
    bool bOk = true;
    freqTable.sort( );
    size_t n = freqTable.numberOfSymbols();
    if(n>0)
    {
        // Construct a Huffman tree. This code is based on the algorithm given in
        //  Managing Gigabytes by Ian Witten et al, 2nd edition, page 34.
        // Note that this implementation uses a simple count instead of probability.
        for(size_t i=0; i<n-1; ++i)
        {
            // Set m1 and m2 to the two subSets of least probability.
            HuffmanNode* m1 = freqTable[0];
            HuffmanNode* m2 = freqTable[1];

            // Replace m1 and m2 with a Set {m1, m2} whose probability
            // is the sum of that of m1 and m2.
            HuffmanNode* newNode = new HuffmanNode(m1,  // zero tree
                                                  m2); // one  tree

            freqTable.setNodeAtBegin(newNode);

            // Put new set into the correct count position in freqTable.
            freqTable.sort();
        }
        // Build the SymbolEncoder array from the tree.
        SE.buildSymbolEncoder(freqTable[0]);
    }
    else
    {
        fprintf(stdout, "Error: cannot compute code. Frequency table is empty.\n");
        bOk = false;
    }
    return bOk;
}
Esempio n. 13
0
/******************************************************************************
purpose: reads in data from a file
input: a class of type FrequencyTable, includes all the private and public
        variables and functions
returns: a class of type FrequencyTable
*****************************************************************************/
FrequencyTable maketable(FrequencyTable table)
{
  string word;
  int size;

  while(cin >> word){//reads in strings until there is no more data
    size = word.size();

      for(int i = 0; i < size; i++){
	word[i] = tolower(word[i]);//lowercases the word
      }

      if(isalpha(word[0])){
        table.insert(word);
      }
  }

  return table;
}
Esempio n. 14
0
bool huffmanProcess(Stream& in, Stream& out, HuffmanDirection direction, bool verbose)
{
    bool bOk = true;

    if(direction == HuffmanEncode)
    {
        FrequencyTable  freqTable;
        SymbolEncoder   SE;

        if(verbose) fprintf(stdout, "Encode\n");
        {
            // Get the frequency of each symbol in the input file.
            freqTable.updateFreqTable(in);

            if(verbose) fprintf(stdout, "Input Size(%ld bytes)\n", freqTable.totalCount());
            if(verbose) fprintf(stdout, "Nb symbols: %ld\n",       freqTable.numberOfSymbols()); 

            // Build an optimal table from the symbolCount.
            bOk = calculateHuffmanCode(freqTable, SE);

            double bitSize = 0.0;
            double entropy = 0.0;
            if(bOk) 
            {
                computeAverageBitSize(SE, freqTable.totalCount(), bitSize, entropy, verbose);
                // Scan the file again and, using the table
                // previously built, encode it into the output file.
                in.rewind( );
            }
            if(verbose) fprintf(stdout, "Writing code table (%lu symbols, %lu worlds)\n", freqTable.size(), freqTable.totalCount());
            bOk = bOk ? SE.writeCodeTable(out, freqTable.totalCount()) : false;
            if(bOk)
            {
                if(verbose) fprintf(stdout, "Encoding\n");
                bOk = SE.encode(in, out, verbose);
            }
            // Print info
            fprintf(stdout, "Encoded: nbSymbol: %ld, bitSize: %.2g, entropy: %.2g, nbBytes: %ld\n",
                        freqTable.numberOfSymbols(), bitSize, entropy, freqTable.totalCount());
        }
    }
    else
    {
        if(verbose) fprintf(stdout, "Decode File");
        unsigned int totalNbBytes = 0;

        // Read the Huffman code table.
        fprintf(stdout, "# Reading code table");

        std::auto_ptr<const HuffmanNode> root(readCodeTable(in, totalNbBytes));
        if(!root.get())
        {
            fprintf(stdout, "Error: cannot read code table\n");
            bOk = false;
        }
        else
        {
            if(verbose) fprintf(stdout, "# Decoding (size: %u bytes)", totalNbBytes);
            // Decode the file.
            const HuffmanNode*  p = root.get();
            unsigned int remainingNbBytes = totalNbBytes;
            unsigned int totalCountOfSymbols = 0;
            while(remainingNbBytes>0 && in.isOpen())
            {
                const Byte byte = in.readByte();
                Byte       mask = 1;
                while(remainingNbBytes>0 && mask)
                {
                    p = (byte & mask) ? p->one() : p->zero();
                    mask <<= 1;
                    assert(p);
                    if(p->isLeaf( ))
                    {
                        out.writeSymbol(p->symbol());
                        totalCountOfSymbols++;
                        p = root.get();
                        --remainingNbBytes;
                    }
                }
            }

            // this is not needed for the decode - Just for the info section
            SymbolEncoder SE;
            SE.buildSymbolEncoder(root.get());
            double bitSize = 0.0;
            double entropy = 0.0;
            computeAverageBitSize(SE, totalCountOfSymbols, bitSize, entropy, verbose);
            fprintf(stdout, "Decoded: nbSymbol: %ld, bitSize: %.2g, entropy: %.2g, nbBytes: %u\n",
                        SE.nbSymbol(), bitSize, entropy, totalNbBytes);
        }
    }
    if(verbose) fprintf(stdout, "done (%d).\n", bOk);
    return bOk;
}
Esempio n. 15
0
void test_frequency(FrequencyTable mytable)
{
  cout << mytable.frequency("the") << endl;
}