#include <j2k/Fred/Standard.hpp>
#include <ctype.h>
#include <j2k/DataType/HuffMan/LettFreq.hpp>
#include <j2k/DataType/BinTree.hpp>
#include <j2k/DataType/HuffMan/HuffTree.hpp>
#include <j2k/DataType/Link/SLink.hpp>
#include <j2k/DataType/Link/SList.hpp>
#include <j2k/DataType/Link/SLink.cpp>
#include <j2k/DataType/Link/SList.cpp>
#include <j2k/DataType/HuffMan/HuffBld.hpp>
Go to the source code of this file.
Compounds | |
struct | CodeTableEntry |
Defines | |
#define | CODELEN 20 |
Typedefs | |
typedef LettFreq * | Belem |
typedef HuffTree * | Elem |
Functions | |
void | print (List &in) |
ostream & | operator<< (ostream &s, LettFreq *z) |
ostream & | operator<< (ostream &s, HuffTree *z) |
void | read_freqs (List &hufflist) |
int | getindex (char codeword) |
void | output_tree (BinNode *node, char *prefix, int level) |
void | do_commands (HuffTree *tree) |
int | main (int argc, char **argv) |
Variables | |
double | total = 0 |
CodeTableEntry | CodeTable [100] |
int | CodeCount = 0 |
FILE * | fp |
List | templist |
|
Definition at line 37 of file HuffMan.hpp. |
|
Definition at line 22 of file HuffMan.hpp. |
|
Definition at line 27 of file HuffMan.hpp. |
|
Definition at line 216 of file HuffMan.hpp. Referenced by main().
00217 { 00218 BinNode* temp; 00219 char* currchar; 00220 char buff[80]; 00221 int index; 00222 00223 while( fgets(buff, 99, fp) ) 00224 { 00225 if( !strncmp(buff, "decode", 6) ) 00226 { 00227 for( currchar = buff; *currchar != '"'; currchar++ ); 00228 00229 cout << "Decode " << currchar++; 00230 temp = tree->root(); 00231 00232 while (*currchar != '"') // Traverse the tree 00233 { 00234 if ( *currchar == '0' ) 00235 { 00236 temp = temp->leftchild(); 00237 } 00238 else if ( *currchar == '1' ) 00239 { 00240 temp = temp->rightchild(); 00241 } 00242 else 00243 { 00244 cout << "Bad input! " << *currchar << "\n"; 00245 abort(); 00246 } 00247 00248 if ( temp->isLeaf() ) 00249 { 00250 cout << temp->value()->letter(); 00251 temp = tree->root(); // reset at root 00252 } 00253 00254 currchar++; 00255 00256 } // End of 2nd while() 00257 00258 } 00259 else if( !strncmp(buff, "encode", 6) ) 00260 { 00261 00262 for ( currchar = buff; *currchar != '"'; currchar++); 00263 00264 cout << "Encode " << currchar++; 00265 00266 // Assume codes are characters. Should generalize this. 00267 while( *currchar != '"' ) 00268 { 00269 if (*currchar == ' ') 00270 { 00271 cout << ' '; 00272 } 00273 else 00274 { 00275 index = getindex( *currchar ); 00276 assert( index < CodeCount ); // Must find it 00277 cout << CodeTable[index].code; 00278 } 00279 00280 currchar++; // This is what needs generalized 00281 } 00282 } 00283 cout << "\n"; 00284 00285 } // End of 1st while() 00286 } |
|
Definition at line 173 of file HuffMan.hpp. Referenced by do_commands(), and output_tree().
|
|
Definition at line 289 of file HuffMan.hpp. 00290 { 00291 HuffTree* codes; 00292 char prefix[30]; 00293 00294 if (argc == 1) 00295 { 00296 fp = stdin; 00297 } 00298 else 00299 { 00300 fp = fopen(argv[1], "rt"); 00301 cout << "Read frequencies\n"; 00302 read_freqs(templist); 00303 cout << "Build the tree\n"; 00304 codes = build_tree(templist); 00305 cout << "Output the tree\n"; 00306 output_tree(codes->root(), prefix, 0); 00307 cout << "Average code length is "; 00308 cout << total/(double)codes->weight() << "\n"; 00309 do_commands(codes); 00310 return(FALSE); 00311 } 00312 } |
|
Definition at line 79 of file HuffMan.hpp. |
|
Definition at line 65 of file HuffMan.hpp. |
|
Definition at line 190 of file HuffMan.hpp. Referenced by main().
00191 { 00192 int index; 00193 assert( node != NULL ); // This is a Full Binary Tree so should NOT happen 00194 if ( node->isLeaf() ) 00195 { 00196 cout << node->value()->letter() << "\t" << prefix << "\n"; 00197 index = getindex( node->value()->letter() ); 00198 strcpy( CodeTable[index].code, prefix ); 00199 total += level * node->value()->weight(); 00200 } 00201 else 00202 { 00203 prefix[ level ] = '0'; 00204 prefix[ level + 1 ] = '\0'; 00205 output_tree( node->leftchild(), prefix, level+1 ); 00206 00207 prefix[ level ] = '1'; 00208 prefix[ level + 1 ] = '\0'; 00209 output_tree( node->rightchild(), prefix, level+1 ); 00210 00211 prefix[ level ] = '\0'; 00212 } 00213 } |
|
Definition at line 40 of file HuffMan.hpp. 00041 { 00042 if (in.isEmpty()) 00043 { 00044 cout << "()\n"; 00045 } 00046 else 00047 { 00048 in.setFirst(); 00049 cout << "( " << in.currValue(); 00050 in.next(); 00051 00052 while (in.isInList()) 00053 { 00054 cout << ", " << in.currValue(); 00055 in.next(); 00056 } 00057 00058 cout << " )\n"; 00059 } 00060 } |
|
Definition at line 99 of file HuffMan.hpp. Referenced by main().
00100 { 00101 char buff[100]; 00102 char buff2[100]; 00103 char *ptr; 00104 char *ptr2; 00105 int freq; 00106 HuffTree* temptree; 00107 00108 while (TRUE) 00109 { 00110 if (!fgets(buff, 99, fp)) // read the next entry 00111 { 00112 cout << "No more codes to read -- where is end?\n"; 00113 assert(FALSE); 00114 } 00115 00116 // process the entry, creating a new HuffTree 00117 for(ptr=buff; *ptr==' '; ptr++); 00118 { 00119 if (!strncmp(ptr, "end", 3)) 00120 { 00121 cout<< "Print out the tree:\n"; 00122 print(hufflist); 00123 return; 00124 } 00125 } 00126 00127 if (*ptr != '"') 00128 { 00129 cout << "First char was not a quote mark.\n"; 00130 assert(FALSE); 00131 } 00132 00133 for (ptr2=buff2,ptr++; *ptr!='"'; ptr++) 00134 { 00135 *ptr2++ = *ptr; 00136 } 00137 00138 *ptr2 = '\0'; // End of string 00139 00140 for (ptr++; *ptr==' '; ptr++); 00141 00142 if (!isdigit(*ptr)) assert(FALSE); 00143 00144 freq = atoi(ptr); 00145 00146 CodeTable[CodeCount].entrydata = new LettFreq(freq, buff2[0]); 00147 00148 temptree = new HuffTree(CodeTable[CodeCount++].entrydata); 00149 00150 // put in the list in sorted order 00151 // WARNING: This may be considered buggy. Nodes with equal weight will 00152 // be put in reverse order of appearance, not in alphabetical order. 00153 for (hufflist.setFirst(); hufflist.isInList(); hufflist.next()) 00154 { 00155 if (temptree->weight() <= hufflist.currValue()->weight()) 00156 { 00157 hufflist.insert(temptree); 00158 break; 00159 } 00160 } 00161 00162 if (!hufflist.isInList()) 00163 { 00164 hufflist.append(temptree); 00165 } 00166 00167 } // End of while loop 00168 } |
|
Definition at line 92 of file HuffMan.hpp. |
|
|
|
Definition at line 93 of file HuffMan.hpp. |
|
Definition at line 95 of file HuffMan.hpp. |
|
Definition at line 62 of file HuffMan.hpp. |