/*******************************************************************************
* File: mytable.c
* Purpose: To supply the function form_table() to the program.
* Name: 
* Date: 
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "huffcode.h"

extern int VERBOSE;

/*******************************************************************************
* Function: form_table
* Purpose: To calculate the Huffman code for pixel frequencies stored in the
* array of structures named table. The array has a length of num. The value
* and count data fields are already assigned values when the function is called.
* You should write the body of this function. To do so they need to
* implement an algorithm to find a Huffman code table for given frequencies,
* which are stored as the counts fields in the array named table). You
* should set the bits field to be the number of bits of the Huffman code, and
* fill the elements of the unsigned character array "bitstring" with the
* values (unsigned char)0 and (unsigned char)1 to store the Huffman code.
* Please note that the bitstring field is an unsigned char pointer and you
* should allocate memory for the array bitstring for each element of the
* table (use calloc or malloc).
* Name: 
* Date: 
*******************************************************************************/
void form_table(struct HUFFTABLE *table, int num)
{
   int i, p;


   /* Please note that the array named table actually has a
      length of num+1. You can access its elements with
      indices from 0 to num. */


   /****************************************************************************
   * If the Huffman codes were found, this code could print them out to the
   * screen. I have included it to help you understand what the contents of
   * the data structures in the array table should hold. 
   ****************************************************************************/
   /*
   if(VERBOSE){
      for(i=0;i<=num;i++){
         if(table[i].count != 0){
            printf("TABLE[%3d] value=%6d count=%6d bits=%4d bitstring=", i,
               table[i].value, table[i].count, table[i].bits);
            for(p=0;p<table[i].bits;p++) printf("%d", (int)table[i].bitstring[p]);
            printf("\n");
         }
      }
   }
   */
}
