#include <iostream.h>
#include "PrBitmap.h"
#include "PrLinkedList.h"
#include "PrHashTable.h"
#include "PrMatlab.h"

void LinkedList();
void SortedList();
void HashTable();

#define ROW 10
#define COL 5

int main()
{
  PrMatlab *matlab = new PrMatlab();
  float data[ROW][COL];
  for(int i=0;i<ROW;i++)
    for(int j=0;j<COL;j++)
      data[i][j] = 1.0*i*j;

  matlab->start("out.mat");
  matlab->save("A",PR_MATLAB_FLOAT,(void *)data,ROW,COL);
  matlab->end();
  delete matlab;

  /*
    PrBitmap *map = new PrBitmap(200);
    map->selfTest();
    delete map;

    LinkedList();
    SortedList();
    HashTable();
  */
    return (1);
}

void LinkedList()
{
    int i;
    PrLinkedList<int> list(100);
    int a[100];
    for(i=0;i<100;i++)
      {
	  a[i]= i;
	  list.newItem(a+i);
      }
  
    int *item = list.firstItem();
    while(item != 0)
      {
	  cout<< *item<<"->";
	  item = list.nextItem();
      }
    cout<<"->null:("<<list.items()<<")\n";

    for(i=0;i<50;i++)
      {
	  list.deleteItem(a+i);
      }

    item = list.firstItem();
    while(item != 0)
      {
	  cout<< *item<<"->";
	  item = list.nextItem();
      }
    cout<<"->null:("<<list.items()<<")\n";
  
    for(i=0;i<100;i++)
      {
	  if (list.item(a+i))
	    list.deleteItem(a+i);
      }

    item = list.firstItem();
    while(item != 0)
      {
	  cout<< *item<<"->";
	  item = list.nextItem();
      }
    cout<<"->null:("<<list.items()<<")\n";
}

int CompareDec(int *x,int *y)
{
    if (*x>*y) return 1;
    else if (*x<*y) return -1;
    else return 0;
}

void SortedList()
{
    int i;
    PrLinkedList<int> list(200,CompareDec);
    int a[100];
    for(i=99;i>=0;i--)
      {
	  a[i]= i;
	  list.newItem(a+i);
      }
  
    int *item = list.firstItem();
    while(item != 0)
      {
	  cout<< *item<<"->";
	  item = list.nextItem();
      }
    cout<<"->null:("<<list.items()<<")\n";

    for(i=0;i<50;i++)
      {
	  list.deleteItem(a+i);
      }

    item = list.firstItem();
    while(item != 0)
      {
	  cout<< *item<<"->";
	  item = list.nextItem();
      }
    cout<<"->null:("<<list.items()<<")\n";
  
    for(i=0;i<100;i++)
      {
	  if (list.item(a+i))
	    list.deleteItem(a+i);
      }

    item = list.firstItem();
    while(item != 0)
      {
	  cout<< *item<<"->";
	  item = list.nextItem();
      }
    cout<<"->null:("<<list.items()<<")\n";
}

int Equal(int *key1,int *key2)
{
    return (key1 == key2);
}

int Hash(int *key)
{
    return (*key%PR_HASHTABLE_SIZE_DEFAULT);
}

void HashTable()
{
    int i;
  PrHashTable<int,int> list(Equal,Hash,4000);

  int a[3000];
  for(i=0;i<3000;i++)
    {
      a[i]= i;
    }
  for(i=0;i<3000;i++)
    {
      list.newItem(a+i,a+i);
    }
  int *item = (int *)list.firstItem();
  while(item != 0)
    {
      cout<< *item<<"->";
      item = (int *)list.nextItem();
    }

  cout<<"->null:("<<list.items()<<")\n";

  for(i=0;i<50;i++)
    {
      list.deleteItem(a+i);
    }

  item = (int *)list.firstItem();
  while(item != 0)
    {
      cout<< *item<<"->";
      item = (int *)list.nextItem();
    }
  cout<<"->null:("<<list.items()<<")\n";
  
  for(i=0;i<300;i++)
    {
      if (list.item(a+i))
	list.deleteItem(a+i);
    }

  item = (int *)list.firstItem();
  while(item != 0)
    {
      cout<< *item<<"->";
      item = (int *)list.nextItem();
    }
  cout<<"->null:("<<list.items()<<")\n";

}

/*
int strEqual(char *key1, char *key2)
{
  return (strcmp(key1,key2) == 0);
}

int strHash(char *s)
{
  const long Multiplier=(-1664117991); 
  long hashcode = 0;

  for (char *cp=s; *cp; cp++) 
    hashcode = hashcode * Multiplier + *cp;
  return ((int) ((hashcode & INT_MAX) % tableSize()));
}
*/

  /*
  SymbolTable<int> table;
  char *s = "hello";
  int a[300];
  for(i=0;i<300;i++)
    {
      a[i]= i;
      table.newItem(a+i,s);
    }
  int *item = (int *)table.firstItem();
  while(item != 0)
    {
      cout<< *item<<"->";
      item = (int *)table.nextItem();
    }

  cout<<"->null:("<<table.items()<<")\n";
  */
