Skip to content
Browse files

File reading implemented. Frequencies are obtained.

  • Loading branch information...
1 parent ee5cac2 commit 2289c5a1ad90fcf9769b95fdac33b4194396a968 @Baha committed Apr 2, 2012
Showing with 138 additions and 4 deletions.
  1. +34 −0 hufftree.cpp
  2. +11 −2 hufftree.h
  3. +1 −1 main1.cpp
  4. +41 −1 source.cpp
  5. +4 −0 source.h
  6. +34 −0 symbol.cpp
  7. +13 −0 symbol.h
View
34 hufftree.cpp
@@ -1,3 +1,37 @@
#include "hufftree.h"
+bool HuffmanNode::isLeaf()
+{
+ return false;
+}
+bool HuffmanLeaf::isLeaf()
+{
+ return true;
+}
+
+void HuffmanTree::buildHuffmanTree(Source source)
+{
+ symbolList(source.getSymbolList);
+ rootNode = new HuffmanNode();
+
+ rootNode->getCodification(symbolList);
+}
+
+/*{
+ float minProb = 100.0f;
+ std::list <Symbol>::iterator it;
+ std::list <Symbol>::iterator minIt;
+
+ for (it = symbolList.begin(); it != symbolList.end(); it++)
+ {
+ if (minProb > (*it).getProbability())
+ minProb = (*it).getProbability();
+ minIt = (*it);
+ }
+ return minSymbol;
+}*/
+
+void HuffmanNode::getCodification(std::list <Symbol> symbolList)
+{
+}
View
13 hufftree.h
@@ -5,16 +5,25 @@
class HuffmanNode
{
- Symbol *symbols;
- float probability;
+ Symbol *symbol;
+ HuffmanNode *leftNode, *rightNode;
+ float probability;
+public:
+ void getCodification(std::list <Symbol> symbolList)
+ virtual bool isLeaf();
};
class HuffmanLeaf : public HuffmanNode
{
+public:
+ bool isLeaf();
};
class HuffmanTree
{
+ HuffmanNode *rootNode;
+ std::list <Symbol> symbolList;
+ void buildHuffmanTree(Source source);
};
#endif
View
2 main1.cpp
@@ -7,7 +7,7 @@ int main(int argc, char* argv[])
FILE *input;
Source source;
- if (argc != 1)
+ if (argc != 2)
{
printf("Uso: ./<programa> <fichero_entrada>\n");
return(-1);
View
42 source.cpp
@@ -7,21 +7,40 @@ void Source::getFrequencies(FILE *input)
std::map<char, unsigned int>::iterator it;
symbolCounter.clear();
+ totalSymbols = 0;
cur_c = fgetc(in);
while (cur_c != EOF)
{
- if (symbolIsEncodable(cur_c))
+ if (Symbol::symbolIsEncodable(cur_c))
{
it = symbolCounter.find(cur_c);
+ totalSymbols++;
+ if (it == symbolCounter.end())
+ symbolCounter[cur_c] = 1;
+ else
+ symbolCounter[cur_c]++;
}
+ cur_c = fgetc(in);
}
}
void Source::getProbabilities()
{
+ std::map<char, unsigned int>::iterator it;
+
+ for (it = symbolCounter.begin(); it != symbolCounter.end();it++)
+ {
+ float lab = (*it).first;
+ float prob = (float)((*it).second) / (float)totalSymbols;
+ Symbol newSymbol;
+
+ newSymbol.setLabel(lab);
+ newSymbol.setProbability(prob);
+ symbolList.push_back(newSymbol);
+ }
}
void Source::getProperties(FILE *input)
@@ -30,7 +49,28 @@ void Source::getProperties(FILE *input)
this->getProbabilities();
}
+std::list <Symbol> Source::getSymbolList()
+{
+ return this->symbolList;
+}
+
void Source::showProperties()
{
+ //debug version
+ std::map<char, unsigned int>::iterator it;
+ std::list <Symbol>::iterator it2;
+
+ printf("Total number of symbols read: %d\n", totalSymbols);
+ printf("The frequencies for this file are...\n");
+
+ for (it = symbolCounter.begin(); it != symbolCounter.end();it++)
+ printf("%c => %d\n", (*it).first, (*it).second);
+
+ printf("\n");
+ printf("The probabilities for this file are...\n");
+
+ for (it2 = symbolList.begin(); it2 != symbolList.end(); it2++)
+ printf("%c => %f\n", it2->getLabel(), it2->getProbability());
+ printf("\n");
}
View
4 source.h
@@ -3,16 +3,20 @@
#include <cstdio>
#include <map>
+#include <list>
#include "symbol.h"
class Source
{
Symbol *symbols;
std::map <char, unsigned int> symbolCounter;
+ std::list <Symbol> symbolList;
+ unsigned int totalSymbols;
public:
void getFrequencies(FILE *input);
void getProbabilities();
void getProperties(FILE *input);
+ std::list <Symbol> getSymbolList();
void showProperties();
};
View
34 symbol.cpp
@@ -1,2 +1,36 @@
#include "symbol.h"
+bool Symbol::symbolIsEncodable(char symbol)
+{
+ return (symbol >= 32 && symbol <= 126); // might need some changes
+}
+
+void Symbol::setLabel(char label)
+{
+ this->label = label;
+}
+
+char Symbol::getLabel()
+{
+ return this->label;
+}
+
+void Symbol::setProbability(float probability)
+{
+ this->probability = probability;
+}
+
+float Symbol::getProbability()
+{
+ return this->probability;
+}
+
+bool Symbol::isCombined()
+{
+ return false;
+}
+
+bool CombinedSymbol::isCombined()
+{
+ return true;
+}
View
13 symbol.h
@@ -5,6 +5,19 @@ class Symbol
{
char label;
float probability;
+public:
+ static bool symbolIsEncodable(char symbol);
+ void setLabel(char label);
+ char getLabel();
+ void setProbability(float probability);
+ float getProbability();
+ virtual bool isCombined();
+};
+
+class CombinedSymbol : public Symbol
+{
+ Symbol *symbol1, *symbol2;
+ bool isCombined();
};
#endif

0 comments on commit 2289c5a

Please sign in to comment.
Something went wrong with that request. Please try again.