
#include <cstdio>
#include <ctime>
#include <list>
#include <iostream>

using namespace std;

// class for controlling all timers
class cListOfTimers {
		list <time_t> timers;
	public:
		void insert(time_t abstime);		
		void sort();
		time_t getearliest();
		void clean(time_t now);
		void print();
		
};

void cListOfTimers::print() {
	for(list<time_t>::iterator i = timers.begin(); i!=timers.end(); i++) {
		cout << *i << endl;
	}
}

void cListOfTimers::insert(time_t abstime) {		
	timers.push_back(abstime);
}

void cListOfTimers::sort() {
	timers.sort();
}

time_t cListOfTimers::getearliest() {
	sort();
	return timers.front();
}

void cListOfTimers::clean(time_t now) {
	time_t tmp;
	int count = 0;

	sort();

	for(list<time_t>::iterator i = timers.begin(); i != timers.end(); i++) {
		if ( *i >= now ) {
			break;
		}
	//	timers.pop_front();
		count++;
	}
		
	for(int i=0; i<count; i++) {
		timers.pop_front();
	}
}

void test() {
	cListOfTimers mylist;
	time_t now;

	//initialization
	now = time(NULL);
	mylist.insert(now);
	mylist.insert(1234123);
	cout << "earliest: " << mylist.getearliest() << endl;
	mylist.insert(99999999);
	mylist.sort();
	sleep(1);
	mylist.print();
	mylist.clean(time(NULL));
	mylist.print();
}

int main(void) {
	test();
}
