Index: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h
===================================================================
--- GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1789)
+++ GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.h	(revision 1790)
@@ -206,5 +206,7 @@
 		@returns iterator on the sample.
 	*/
-	typename vector<PvsEntry<T, S> >::iterator Find(T sample, const bool checkDirty = true);
+	bool Find(T sample,
+			  typename vector<PvsEntry<T, S> >::iterator &it,
+			  const bool checkDirty = true);
 
 	bool GetSampleContribution(T sample, const float pdf, float &contribution);
@@ -391,7 +393,6 @@
 		float aSumPdf = 0.0f;
 
-		vector<PvsEntry<T, S> >::iterator oit = Find((*it).mObject);		
-
-		const bool entryFound = (it != mEntries.end()) && ((*it).mObject == (*oit).mObject);
+		vector<PvsEntry<T, S> >::iterator oit;
+		const bool entryFound = Find((*it).mObject, oit);		
 
 		if (entryFound)
@@ -427,8 +428,8 @@
 			(*it).mData.mSumPdf = -aSumPdf;
 		} else {
-			vector<PvsEntry<T, S> >::iterator oit = b.Find((*it).mObject);
-
-			const bool entryFound = (it != mEntries.end()) && ((*it).mObject == (*oit).mObject);
-			
+			vector<PvsEntry<T, S> >::iterator oit;
+		
+			const bool entryFound = b.Find((*it).mObject, oit);
+						
 			if (entryFound) {
 				bSumPdf = (*oit).mData.mSumPdf;
@@ -461,6 +462,8 @@
 	for (it = b.mEntries.begin(); it != b.mEntries.end(); ++ it)
 	{
-		std::vector<PvsEntry<T, S> >::const_iterator bit = Find((*it).first);		
-		if (bit == mEntries.end()) ++ dif;
+		vector<PvsEntry<T, S> >::iterator bit;
+		const bool entryFound = Find((*it).first, bit);
+
+		if (!entryFound) ++ dif;
 	}
 
@@ -586,6 +589,10 @@
 
 template <typename T, typename S> 
-typename std::vector<PvsEntry<T, S> >::iterator Pvs<T, S>::Find(T sample, const bool checkDirty)
-{
+bool Pvs<T, S>::Find(T sample,
+					 typename vector<PvsEntry<T, S> >::iterator &it,
+					 const bool checkDirty)
+{
+	bool found = false;
+
 	PvsEntry<T, S> dummy(sample, PvsData());
 
@@ -593,18 +600,23 @@
 	//mLastSorted = 0;
 	vector<PvsEntry<T, S> >::iterator sorted_end = mEntries.begin() + mLastSorted;
-if (sorted_end != mEntries.end())
-cout << "not entries end!! " << endl;
+
 	// binary search
-	vector<PvsEntry<T, S> >::iterator it = lower_bound(mEntries.begin(), sorted_end, dummy);
+	it = lower_bound(mEntries.begin(), sorted_end, dummy);
+
+	if ((it != mEntries.end()) && ((*it).mObject == sample))
+		found = true;
 
 	// sample not found yet => search further in the unsorted part
-	if (checkDirty &&
-		((it == mEntries.end()) || ((*it).mObject != sample)))
-	{
-		vector<PvsEntry<T, S> >::const_iterator it_end = mEntries.end();
-		for (it = sorted_end; (it != it_end) && ((*it).mObject != sample); ++ it);
-	}
-	else cout << "f ";
-	return it;
+	if (!found && checkDirty)
+	{
+		vector<PvsEntry<T, S> >::const_iterator dit, dit_end = mEntries.end();
+
+		for (dit = sorted_end; (dit != dit_end) && ((*dit).mObject != sample); ++ dit);
+
+		if ((dit != mEntries.end()) && ((*dit).mObject == sample))
+			found = true;
+	}
+	
+	return found;
 }
 
@@ -625,9 +637,10 @@
 {
 	++ mSamples;
-	cout << "! ";
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
-	{	cout << "g";
+	
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);		
+
+	if (entryFound)
+	{	
 		S &data = (*it).mData;
 		data.mSumPdf += pdf;
@@ -635,5 +648,5 @@
 	}
 	else 
-	{cout << "t";
+	{
 		PvsEntry<T, S> entry(sample, pdf);
 		mEntries.insert(it, entry);
@@ -641,5 +654,4 @@
 		return pdf;
 	}
-	cout << " $";
 }
 
@@ -654,11 +666,13 @@
 
 template <typename T, typename S>
-typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample, const float pdf)
+typename vector< PvsEntry<T, S> >::iterator Pvs<T, S>::AddSample2(T sample, 
+																  const float pdf)
 {
 	++ mSamples;
 	
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound == Find(sample, it);
+
+	if (entryFound)
 	{
 		S &data = (*it).second;
@@ -674,35 +688,4 @@
 	return it;
 }
-
-
-/*template <typename T, typename S> 
-bool Pvs<T, S>::AddSample(T sample,
-						  const float pdf,
-						  float &contribution)
-{
-	++ mSamples;
-
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
-	{
-		S &data = (*it).mData;
-
-		data.mSumPdf += pdf;
-		contribution = pdf / data.mSumPdf;
-
-		return false;
-	}
-	else 
-	{
-		PvsEntry<T, S> entry(sample, pdf);
-
-		mEntries.insert(it, entry);
-		contribution = 1.0f;
-		++ mLastSorted;
-
-		return true;
-	}
-}*/
 
 
@@ -717,7 +700,8 @@
 	++ mSamples;
 
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if ((it != mEntries.end()) && ((*it).mObject == sample))
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);
+
+	if (entryFound)
 	{
 		S &data = (*it).mData;
@@ -743,7 +727,8 @@
 									  float &contribution) 
 {
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if (it != mEntries.end() && ((*it).mObject == sample))  
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);
+
+	if (entryFound)  
 	{
 		S &data = (*it).mData;
@@ -764,7 +749,8 @@
 	-- mSamples;
 	
-	std::vector<PvsEntry<T, S> >::iterator it = Find(sample);
-
-	if (it == mEntries.end())
+	vector<PvsEntry<T, S> >::iterator it;
+	const bool entryFound = Find(sample, it);
+
+	if (!entryFound)
 		return false;
 
