swh:1:snp:818279ffd0c3c25a68d33cc2b97d007e1bdc7ed6
Raw File
Tip revision: 39b13612ebd645a65eda854771b517371f2f858a authored by ennetws on 13 March 2015, 18:17:18 UTC
Create README.md
Tip revision: 39b1361
MyPriorityQueue.h
#pragma once
#include <set>
#include "CurveskelHelper.h"

namespace CurveskelTypes{

	class MyPriorityQueue
	{
	public:
		class EdgeCompareFunctor{
		public:
			CurveskelTypes::ScalarEdgeProperty elengths;

			EdgeCompareFunctor(CurveskelModel* skel){
				elengths = skel->edge_property<CurveskelTypes::Scalar>("e:length");
			}

			bool operator() (const CurveskelTypes::Edge& lhs, const CurveskelTypes::Edge& rhs) const{
				CurveskelTypes::Scalar p0 = elengths[lhs];
				CurveskelTypes::Scalar p1 = elengths[rhs];
				return (p0 == p1) ? (lhs.idx()<rhs.idx()) : (p0 < p1);
			}
		};   
		EdgeCompareFunctor compareFunctor;
		std::set<CurveskelTypes::Edge, EdgeCompareFunctor> set;

		MyPriorityQueue(CurveskelModel* skel) : compareFunctor(skel), set(compareFunctor){}

		void insert(CurveskelTypes::Edge edge, CurveskelTypes::Scalar length){
			compareFunctor.elengths[edge] = length;
			set.insert(edge);
		}

		CurveskelTypes::Edge pop(){
			CurveskelTypes::Edge e = *(set.begin());
			set.erase(e);
			compareFunctor.elengths[e] = -1.0;
			return e;
		}

		bool update(CurveskelTypes::Edge edge, CurveskelTypes::Scalar /*length*/){
			// erase the edge
			int nerased = set.erase(edge);

			if( nerased != 1 ){
				qDebug() << "I was supposed to erase one element, but I erased: " << nerased; 
				return false;  
			}

			// re-insert it
			set.insert(edge);

			return true;
		}

		bool empty(){ return set.empty(); }

		bool has(CurveskelTypes::Edge edge){ return set.find(edge) != set.end(); }
	};

}
back to top