/*
 *  Timeline.cpp
 *  BrainSim
 *
 *  Created by Will on 10/30/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

//#define MAX_DELTA_T 120


#include "Timeline.h"

Timeline::Timeline()
{
}

Timeline::~Timeline()
{
	for (int i = 0; i< MAX_DELTA_T; i++)
	{
		delete [] messagesForCells[i];
	}
	
	delete [] messagesForCells;
}

void Timeline::tick(int current_tick)
{
	//c++ is being weird with modulus involving negative number
	//ie. -1%120 = 0, rather than 119, so this is a work around
	//that should do what we want
	int location = current_tick%MAX_DELTA_T;

	for(int i = 0;i < number_of_cells;i++)
	{
		messagesForCells[location][i]=NULL;
	}
}

void Timeline::build (int no_cells, int maxDelta)
{

	int i;
	
	MAX_DELTA_T = maxDelta+1;
	number_of_cells = no_cells;

	messagesForCells = new Message** [MAX_DELTA_T];
	
	for (i=0; i < MAX_DELTA_T; i++)
	{
		messagesForCells[i] = new Message* [no_cells];
		for(int j = 0; j < no_cells; j++)
		{
			messagesForCells[i][j] = NULL;
		}
	}
}

Message* Timeline::getMailbox(int current_tick,int cell_no)
{
	return messagesForCells[current_tick%MAX_DELTA_T][cell_no];
}

void Timeline::queueMessage(int current_tick, int delta_t, int cell, Message *mess)
{
	int time_index = (current_tick+delta_t)%MAX_DELTA_T;
	Message *tmp = messagesForCells[time_index][cell];

	mess->Next = tmp;
	messagesForCells[time_index][cell] = mess;
}