
#include <iostream>
#include <list>


#define DISTANCE 10

using namespace std;

class cTimerjob {
	public:
	//	bool operator < (const cTimerjob& t);
		char *channel;
		int prio;
};

bool operator < (const cTimerjob& t1, const cTimerjob& t2) {
	return t1.prio < t2.prio;
}

class cListOfTimerjobs {
		list<cTimerjob> jobs[DISTANCE];
	public:
		void insert(cTimerjob j, int scheduleTime);
		void clean(int index);
		void cleanall();
		void print(int index);
		void printall();
		cTimerjob gethighestprio(int index);
		void sort(int index);
		void sortall();
};


cTimerjob cListOfTimerjobs::gethighestprio(int index) {
	sort(index);
	return jobs[index].front();
}

void cListOfTimerjobs::sort(int index) {
	jobs[index].sort();
}

void cListOfTimerjobs::sortall() {
	for (int i = 0; i < DISTANCE; i++) {
		sort(i);
	}
}

void cListOfTimerjobs::print(int index) {
	cout << "elements for index " << index << ": " << endl;
	for(list<cTimerjob>::iterator i = jobs[index].begin(); i != jobs[index].end(); i++) {
		cout << "\t" << (i)->channel << ", " << (i)->prio << endl;
	}
	cout << "*******************************" << endl;
}

void cListOfTimerjobs::printall() {
	for(int i = 0; i < DISTANCE; i++) {
		print(i);
	}
	cout << "|  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |" << endl;  
}

void cListOfTimerjobs::clean(int index) {
	jobs[index % DISTANCE].clear();	
}

void cListOfTimerjobs::cleanall() {
	for(int i=0; i < DISTANCE; i++) {
		clean(i);
	}
}

void cListOfTimerjobs::insert(cTimerjob j, int scheduleTime) {
	jobs[scheduleTime % DISTANCE].push_back(j);
}

//
// each cTimerjob must have a seperate persistant memory location
//
// change to non pointer list because of sort to function
void test() {
	cListOfTimerjobs mylist;

	cTimerjob job;
	job.channel = "testchannel";
	job.prio = 3;


	job.channel = "name2";
	job.prio = 4;
	mylist.insert(job, 4);

	job.channel = "blablalba";
	job.prio = 1;
	mylist.insert(job, 4);

	mylist.insert(job, 5);

	job.channel = "name2";
	job.prio = 4;

	mylist.insert(job, 5);

	cout << "hele" << endl;
	mylist.clean(14);
	cout << "hele" << endl;
	job.channel = "new";
	job.prio = 3;
	mylist.insert(job, 14);
	
	mylist.printall();
	mylist.sortall();
	mylist.printall();




}

// test globality
	list<cTimerjob> mylist;

void testcTimerjob()  {
//	list<cTimerjob *> mylist;

//	cTimerjob *job = new cTimerjob();
//	job->channel = "aaa";
//	job->prio = 4;
//
//	mylist.push_back(job);
//
//	job = new cTimerjob();
//	job->channel = "bbb";
//	job->prio = 1;
//
//	mylist.push_back(job);
//
//	job = new cTimerjob();
//	job->channel = "ccc";
//	job->prio = 5;
//
//	mylist.push_back(job);
//
//	for (list<cTimerjob *>::iterator i = mylist.begin(); i != mylist.end(); i++) {
//		cout << (*i)->channel << ", " << (*i)->prio << endl;
//	}
//
//	cout << "---------------------------------------------------------------" << endl;
//	mylist.sort();
//
//	for (list<cTimerjob *>::iterator i = mylist.begin(); i != mylist.end(); i++) {
//		cout << (*i)->channel << ", " << (*i)->prio << endl;
//	}

	cTimerjob job;
	job.channel = "aaa";
	job.prio = 4;

	mylist.push_back(job);

	job.channel = "bbb";
	job.prio = 1;

	mylist.push_back(job);

	job.channel = "ccc";
	job.prio = 5;

	mylist.push_back(job);

	for (list<cTimerjob>::iterator i = mylist.begin(); i != mylist.end(); i++) {
		cout << (*i).channel << ", " << (*i).prio << endl;
	}

	cout << "---------------------------------------------------------------" << endl;
	mylist.sort();

	for (list<cTimerjob>::iterator i = mylist.begin(); i != mylist.end(); i++) {
		cout << (*i).channel << ", " << (*i).prio << endl;
	}


}

void testglobality() {
	for (list<cTimerjob>::iterator i = mylist.begin(); i != mylist.end(); i++) {
		cout << (*i).channel << ", " << (*i).prio << endl;
	}
}

int main() {
	test();
//	testcTimerjob();
//	testglobality();
}
