All pastes #759953 Raw Edit

Miscellany

public text v1 · immutable
·published 2007-11-03 17:31 UTC
rendered paste body
#include <list>
#include <iostream>

class Event
{
	public:
		int awesomeness_;
		int time_;
		
	public:
		Event(int awesomeness, int time) : awesomeness_(awesomeness), time_(time) { }
};

struct CompareByTime
{
	static bool compare(const Event& lhs, const Event& rhs)
	{
		return lhs.time_ < rhs.time_;
	}
};

struct CompareByAwesomeness
{
	static bool compare(const Event& lhs, const Event& rhs)
	{
		return lhs.awesomeness_ < rhs.awesomeness_;
	}
};

template<typename T>
struct CompareFunctor
{
	bool operator()(const Event& x, const Event& y) { return T::compare(x, y); }
};

template<typename T>
class PriorityQueue
{
	private:
		std::list<Event> elements;
		
	public:
		void add(const Event& element) { elements.push_back(element); elements.sort(CompareFunctor<T>());  }
		
		void print() const
		{
			for (std::list<Event>::const_iterator Iter = elements.begin(); Iter != elements.end(); Iter++)
			{
				std::cout << "( " << Iter->awesomeness_ << ", " << Iter->time_ << " )" << std::endl;
			}
		}
};

int main()
{
	PriorityQueue<CompareByTime>  p1;
	PriorityQueue<CompareByAwesomeness>  p2;
	
	Event e1(3, 4);
	Event e2(2, 5);
	
	p1.add(e1);
	p2.add(e1);
	
	p1.add(e2);
	p2.add(e2);
	
	p1.print();
	
	std::cout << "--------------------------" << std::endl;
	
	p2.print();
}