Viewing file: c/huffman1/huffman.h | Back to directory listing
Author: Loren Segal | Last modified: February 20 2006 07:00 pm | Download

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <iomanip>

// the number of bits reserved per bitCode
#define BITSIZE(a)	(8*a)	

struct BitCodeInfo
{
	char charCode;
	char bitCode;
	char bitLength;

	BitCodeInfo();
};

class CompressionSet 
{
public:
	int			bitCompression;
	int			numUniqueChars;
	BitCodeInfo *bitCodeList;

	void makeCharset(char *buffer, int size, int bitComp = 1);
	CompressionSet();

	int getBitCode(int charCode);
	int getBitLength(int charCode);
	int getBitData(int charCode, int *bitCode, int *bitLength);
	int getTotalBitLength(char *buffer, int size);

	void printBitCode(int bitCode, int bitLength);
	void printStringBitCodes(char *buffer, int size);
	void *CompressedString(char *buffer, int size);
	int  makeCharCode(char *buffer);
};

class Node
{

public:
	int addParentNode(Node *pNode);

	Node	*pParent;
	Node	*pLeftChild;
	Node	*pRightChild;

	int		charCode;
	int		freqTotal;		// the total frequency 
							// owned by all of this node's children

	Node(int initialCharCode = 0);
};

class NodeTree
{
public:
	int	listSize;
	CompressionSet *cParent;
	
	void orderNodeTree(Node **nodeList);
	void sortList(Node **nodeList, size_t count);
	void calculateBitCodes(Node *startNode, int isLeftRight = 0, int level = 0, int bitCode = 0);
	NodeTree(CompressionSet *c, char *buffer, int size);

	int addBitCode(int charCode, int bitCode, int bitLength);
};

class jZip
{
public:
	CompressionSet *Sets;
	
	// constructor
	jZip(int numSets);
};