[Debtags-commits] [svn] r1728 - in tagcoll/2.0: . tagcoll

Enrico Zini enrico at costa.debian.org
Tue May 9 00:23:57 UTC 2006


Author: enrico
Date: Tue May  9 00:23:55 2006
New Revision: 1728

Modified:
   tagcoll/2.0/   (props changed)
   tagcoll/2.0/tagcoll/InputMerger.h
   tagcoll/2.0/tagcoll/PatchCollection.cc
   tagcoll/2.0/tagcoll/PatchCollection.h
   tagcoll/2.0/tagcoll/TDBIndexer.h
   tagcoll/2.0/tagcoll/test-utils.tcc
Log:
 r2615 at viaza:  enrico | 2006-05-08 12:24:18 -0500
 PatchCollection iteration does not crash anymore
 Enforced that collections don't store items with no tags


Modified: tagcoll/2.0/tagcoll/InputMerger.h
==============================================================================
--- tagcoll/2.0/tagcoll/InputMerger.h	(original)
+++ tagcoll/2.0/tagcoll/InputMerger.h	Tue May  9 00:23:55 2006
@@ -70,6 +70,7 @@
 public:
 	typedef typename std::map< ITEM, std::set<TAG> >::const_iterator const_iterator;
 	typedef typename std::map< ITEM, std::set<TAG> >::iterator iterator;
+	typedef typename std::map< ITEM, std::set<TAG> >::value_type value_type;
 
 	const_iterator begin() const { return coll.begin(); }
 	const_iterator end() const { return coll.end(); }
@@ -79,6 +80,8 @@
 	template<typename ITEMS, typename TAGS>
 	void insert(const ITEMS& items, const TAGS& tags)
 	{
+		if (tags.empty())
+			return;
 		for (typename ITEMS::const_iterator i = items.begin();
 				i != items.end(); ++i)
 		{

Modified: tagcoll/2.0/tagcoll/PatchCollection.cc
==============================================================================
--- tagcoll/2.0/tagcoll/PatchCollection.cc	(original)
+++ tagcoll/2.0/tagcoll/PatchCollection.cc	Tue May  9 00:23:55 2006
@@ -117,8 +117,8 @@
 typename coll_traits<ROCOLL>::tagset_type PatchCollection<ROCOLL>::getAllTags() const
 {
 	TagSet res;
-	for (typename ROCOLL::const_iterator i = coll.begin();
-			i != coll.end(); ++i)
+	for (typename PatchCollection<ROCOLL>::const_iterator i = begin();
+			i != end(); ++i)
 		res |= i->second;
 	return res;
 }

Modified: tagcoll/2.0/tagcoll/PatchCollection.h
==============================================================================
--- tagcoll/2.0/tagcoll/PatchCollection.h	(original)
+++ tagcoll/2.0/tagcoll/PatchCollection.h	Tue May  9 00:23:55 2006
@@ -78,13 +78,16 @@
 
 	class const_iterator
 	{
+		const PatchCollection<ROCOLL>& coll;
 		typename ROCOLL::const_iterator ci;
 		typename Patches::const_iterator pi;
 		mutable const typename PatchCollection<ROCOLL>::value_type* cached_val;
 
 	protected:
-		const_iterator(const typename ROCOLL::const_iterator& ci, const typename Patches::const_iterator& pi)
-			: ci(ci), pi(pi), cached_val(0) {}
+		const_iterator(const PatchCollection<ROCOLL>& coll,
+						const typename ROCOLL::const_iterator& ci,
+						const typename Patches::const_iterator& pi)
+			: coll(coll), ci(ci), pi(pi), cached_val(0) {}
 	
 	public:
 		~const_iterator()
@@ -92,9 +95,18 @@
 			if (cached_val)
 				delete cached_val;
 		}
-		const typename PatchCollection<ROCOLL>::value_type& operator*() const
+		const typename PatchCollection<ROCOLL>::value_type operator*() const
 		{
-			if (ci->first < pi->first)
+			if (cached_val)
+				return *cached_val;
+
+			if (ci == coll.coll.end() && pi == coll.m_changes.end())
+				return *(typename PatchCollection<ROCOLL>::value_type*)0;
+			else if (pi == coll.m_changes.end())
+				return *ci;
+			else if (ci == coll.coll.end())
+				return make_pair(pi->first, pi->second.getAdded());
+			else if (ci->first < pi->first)
 				return *ci;
 			else if (ci->first > pi->first)
 				return make_pair(pi->first, pi->second.getAdded());
@@ -103,19 +115,19 @@
 		}
 		const typename PatchCollection<ROCOLL>::value_type* operator->() const
 		{
-			if (ci->first < pi->first)
-				cached_val = new typename PatchCollection<ROCOLL>::value_type(*ci);
-			else if (ci->first > pi->first)
-				cached_val = new typename PatchCollection<ROCOLL>::value_type(
-						make_pair(pi->first, pi->second.getAdded()));
-			else
-				cached_val = new typename PatchCollection<ROCOLL>::value_type(
-						make_pair(ci->first, pi->second.apply(ci->second)));
-			return cached_val;
+			if (cached_val)
+				return cached_val;
+			return cached_val = new typename PatchCollection<ROCOLL>::value_type(*(*this));
 		}
 		const_iterator& operator++()
 		{
-			if (ci->first < pi->first)
+			if (ci == coll.coll.end() && pi == coll.m_changes.end())
+				;
+			else if (pi == coll.m_changes.end())
+				++ci;
+			else if (ci == coll.coll.end())
+				++pi;
+			else if (ci->first < pi->first)
 				++ci;
 			else if (ci->first > pi->first)
 				++pi;
@@ -142,16 +154,14 @@
 		
 		friend class PatchCollection<ROCOLL>;
 	};
-	const_iterator begin() const { return const_iterator(coll.begin(), m_changes.begin()); }
-	const_iterator end() const { return const_iterator(coll.end(), m_changes.end()); }
+	const_iterator begin() const { return const_iterator(*this, coll.begin(), m_changes.begin()); }
+	const_iterator end() const { return const_iterator(*this, coll.end(), m_changes.end()); }
 
 	PatchCollection(const ROCOLL& coll) : coll(coll) {}
 
 	template<typename ITEMS, typename TAGS>
 	void insert(const ITEMS& items, const TAGS& tags)
 	{
-		if (tags.empty())
-			return;
 		for (typename ITEMS::const_iterator i = items.begin();
 				i != items.end(); ++i)
 			m_changes.addPatch(Patch(*i, tags, TagSet()));
@@ -204,10 +214,10 @@
 	ItemSet getCommonItems(const TAGS& tags) const
 	{
 		using namespace wibble::operators;
-		ItemSet res;
-		for (typename TAGS::const_iterator i = tags.begin();
-				i != tags.end(); i++)
-			res |= getItems(*i);
+		typename TAGS::const_iterator i = tags.begin();
+		ItemSet res = getItems(*i);
+		for ( ; i != tags.end(); ++i)
+			res &= getItems(*i);
 		return res;
 	}
 

Modified: tagcoll/2.0/tagcoll/TDBIndexer.h
==============================================================================
--- tagcoll/2.0/tagcoll/TDBIndexer.h	(original)
+++ tagcoll/2.0/tagcoll/TDBIndexer.h	Tue May  9 00:23:55 2006
@@ -70,6 +70,7 @@
 public:
 	typedef typename std::map< ITEM, std::set<TAG> >::const_iterator const_iterator;
 	typedef typename std::map< ITEM, std::set<TAG> >::iterator iterator;
+	typedef typename std::map< ITEM, std::set<TAG> >::value_type value_type;
 
 	const_iterator begin() const { return items.begin(); }
 	const_iterator end() const { return items.end(); }
@@ -79,6 +80,9 @@
 	template<typename ITEMS, typename TAGS>
 	void insert(const ITEMS& items, const TAGS& tags)
 	{
+		if (tags.empty())
+			return;
+
 		for (typename ITEMS::const_iterator i = items.begin();
 				i != items.end(); ++i)
 		{

Modified: tagcoll/2.0/tagcoll/test-utils.tcc
==============================================================================
--- tagcoll/2.0/tagcoll/test-utils.tcc	(original)
+++ tagcoll/2.0/tagcoll/test-utils.tcc	Tue May  9 00:23:55 2006
@@ -121,6 +121,37 @@
 {
 	std::set<string> s, s1;
 
+	// Try one iteration of the collection
+	s.clear(); s1.clear();
+	for (typename ROCOLL::const_iterator i = tc.begin();
+			i != tc.end(); ++i)
+	{
+		s |= i->first;
+		s1 |= i->second;
+		/*
+		cerr << i->first << ": ";
+		for (typename ROCOLL::value_type::second_type::const_iterator j = i->second.begin(); j != i->second.end(); ++j)
+			cerr << *j << ' ';
+		cerr << endl;
+		cerr << "s: ";
+		for (std::set<string>::const_iterator j = s.begin(); j != s.end(); ++j)
+			cerr << *j << ' ';
+		cerr << endl;
+		*/
+	}
+	inner_ensure_equals(s.size(), 4u);
+	inner_ensure_equals(s1.size(), 5u);
+	inner_ensure_not_contains(s, string("gnocco"));
+	inner_ensure_contains(s, string("margherita"));
+	inner_ensure_contains(s, string("funghi"));
+	inner_ensure_contains(s, string("rosmarino"));
+	inner_ensure_contains(s, string("marinara"));
+	inner_ensure_contains(s1, string("tomato"));
+	inner_ensure_contains(s1, string("mozzarella"));
+	inner_ensure_contains(s1, string("mushrooms"));
+	inner_ensure_contains(s1, string("garlic"));
+	inner_ensure_contains(s1, string("rosemerry"));
+
 	// hasTag
 	inner_ensure(tc.hasTag("tomato"));
 	inner_ensure(tc.hasTag("mozzarella"));



More information about the Debtags-commits mailing list