void NodeList::createListFromFile(const std::string &filename) { std::ifstream file ; file.open(filename.c_str(),std::ios::in); if (!file.good()) { file.close(); throw ExceptionHuffman("Can't open the source file."); } else { std::bitset<8> readC; while(file.read((char *)(&readC), sizeof(char))) { Node* node=new Node(readC, sizeof(char)); NodeList::iterator it = begin(); while(it!=end() && (*it)->c!=node->c) ++it; if(it != end()) { (*it)->freq++; delete node; } else push_front(node); } sortNode(); file.close(); } }
void sortNode(struct Node *p, int l, int r) { int i = l; int j = r; int mid = p[(l + r) / 2].rank; while (i <= j) { while (p[i].rank < mid) i++; while (p[j].rank > mid) j--; if (i <= j) { struct Node tmp = p[i]; p[i] = p[j]; p[j] = tmp; i++; j--; } } if (i < r) sortNode(p, i, r); if (l < j) sortNode(p, l, j); }
void NodeList::createListFromHuff(const std::string &filename) { std::ifstream file; file.open(filename.c_str(),std::ios::in | std::ios::binary); if (!file.good()) { file.close(); throw ExceptionHuffman("Can't open the source file."); } else { std::bitset<8> car; int nbElement=0; int nb_car=0; char c=0; while(c!=' ') { file.read((char*)(&c),sizeof(char)); } file.read((char*)(&nb_car),sizeof(int)); file.read((char*)(&car),sizeof(char)); file.read((char*)(&nbElement),sizeof(int)); while(nbElement!=0) { int nb_occ; file.read((char*)(&car),sizeof(char)); file.read((char*)(&nb_occ),sizeof(int)); Node* n=new Node(car,nb_occ); push_back(n); nbElement--; } sortNode(); file.close(); } }