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

Enrico Zini enrico at costa.debian.org
Sat Sep 30 11:06:07 UTC 2006


Author: enrico
Date: Sat Sep 30 11:06:07 2006
New Revision: 1972

Modified:
   tagcoll/2.0/   (props changed)
   tagcoll/2.0/tagcoll/Patches-tut.cc
   tagcoll/2.0/tagcoll/Patches.h
   tagcoll/2.0/tagcoll/Patches.tcc
   tagcoll/2.0/tagcoll/TextFormat.cc
Log:
 r3449 at viaza:  enrico | 2006-09-30 12:41:14 +0200
 Started little Patch refactoring


Modified: tagcoll/2.0/tagcoll/Patches-tut.cc
==============================================================================
--- tagcoll/2.0/tagcoll/Patches-tut.cc	(original)
+++ tagcoll/2.0/tagcoll/Patches-tut.cc	Sat Sep 30 11:06:07 2006
@@ -28,7 +28,7 @@
 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& out, const std::set<TAG>& tags)
 {
 	for (typename std::set<TAG>::const_iterator i = tags.begin();
-			i != tags.end(); i++)
+			i != tags.end(); ++i)
 		if (i == tags.begin())
 			out << *i;
 		else
@@ -36,6 +36,33 @@
 	return out;
 }
 
+template<typename ITEM, typename TAG>
+ostream& operator<<(ostream& out, const tagcoll::Patch<ITEM, TAG>& p)
+{
+	out << p.getItem() << ": ";
+	bool first = true;
+	for (typename std::set<TAG>::const_iterator i = p.getAdded().begin();
+			i != p.getAdded().end(); ++i)
+	{
+		if (first)
+			first = false;
+		else
+			out << ", ";
+		out << "+" << *i;
+	}
+	for (typename std::set<TAG>::const_iterator i = p.getRemoved().begin();
+			i != p.getRemoved().end(); ++i)
+	{
+		if (first)
+			first = false;
+		else
+			out << ", ";
+		out << "-" << *i;
+	}
+	return out;
+
+}
+
 }
 
 namespace tut {
@@ -49,43 +76,94 @@
 TESTGRP(tagcoll_patches);
 
 
-
+// Test empty patches
 template<> template<>
 void to::test<1>()
 {
-	// Instantiate an empty patch
-	Patch<string, string> a("foo");
-	ensure_equals(a.getAdded(), std::set<string>());
-	ensure_equals(a.getRemoved(), std::set<string>());
+	Patch<string, int> a("foo");
+	ensure_equals(a.getItem(), "foo");
+	ensure(a.getAdded().empty());
+	ensure(a.getRemoved().empty());
+
+	Patch<string, int> b("foo", std::set<int>(), std::set<int>());
+	ensure_equals(b.getItem(), "foo");
+	ensure(b.getAdded().empty());
+	ensure(b.getRemoved().empty());
+
+	Patch<string, int> c("foo", wibble::Empty<int>(), wibble::Empty<int>());
+	ensure_equals(c.getItem(), "foo");
+	ensure(c.getAdded().empty());
+	ensure(c.getRemoved().empty());
+
+	ensure_equals(a, b);
+	ensure_equals(a, c);
+	ensure_equals(b, c);
+}
 
-	// Instantiate a non-empty patch
-	std::set<string> added;
-	std::set<string> removed;
-	added.insert("a");
-	added.insert("b");
-	removed.insert("c");
-	removed.insert("d");
-	Patch<string, string> b("foo", added, removed);
+// Test the different constructors
+template<> template<>
+void to::test<2>()
+{
+	Patch<string, int> a("foo");
+	a.add(1);
+	a.remove(2);
+	ensure_equals(a.getItem(), "foo");
+	ensure_equals(a.getAdded().size(), 1u);
+	ensure_equals(a.getRemoved().size(), 1u);
+	ensure_equals(*a.getAdded().begin(), 1);
+	ensure_equals(*a.getRemoved().begin(), 2);
+
+	std::set<int> added;
+	std::set<int> removed;
+	added.insert(1);
+	removed.insert(2);
+	Patch<string, int> b("foo", added, removed);
+	ensure_equals(b.getItem(), "foo");
+	ensure_equals(b.getAdded().size(), 1u);
+	ensure_equals(b.getRemoved().size(), 1u);
+	ensure_equals(*b.getAdded().begin(), 1);
+	ensure_equals(*b.getRemoved().begin(), 2);
+
+	Patch<string, int> c("foo", wibble::singleton(1), wibble::singleton(2));
+	ensure_equals(c.getItem(), "foo");
+	ensure_equals(c.getAdded().size(), 1u);
+	ensure_equals(c.getRemoved().size(), 1u);
+	ensure_equals(*c.getAdded().begin(), 1);
+	ensure_equals(*c.getRemoved().begin(), 2);
+
+	ensure_equals(a, b);
+	ensure_equals(a, c);
+	ensure_equals(b, c);
+}
+
+// Test non-empty patches
+template<> template<>
+void to::test<3>()
+{
+	Patch<string, int> p("foo");
+	p.add(1);
+	p.add(2);
+	p.remove(3);
+	p.remove(4);
 
 	// Check that getReverse() actually returns the reverse patch
-	Patch<string, string> rb = b.getReverse();
-	ensure_equals(b.getAdded(), rb.getRemoved());
-	ensure_equals(b.getRemoved(), rb.getAdded());
+	Patch<string, int> rp = p.getReverse();
+	ensure_equals(p.getAdded(), rp.getRemoved());
+	ensure_equals(p.getRemoved(), rp.getAdded());
 
 	// Check removeRedundant()
-	std::set<string> ts;
-	ts.insert("a");
-	ts.insert("c");
-	b.removeRedundant(ts);
-	ensure_not_contains(b.getAdded(), std::string("a"));
-	ensure_contains(b.getAdded(), std::string("b"));
-	ensure_contains(b.getRemoved(), std::string("c"));
-	ensure_not_contains(b.getRemoved(), std::string("d"));
-
+	std::set<int> ts;
+	ts.insert(1);
+	ts.insert(3);
+	p.removeRedundant(ts);
+	ensure_not_contains(p.getAdded(), 1);
+	ensure_contains(p.getAdded(), 2);
+	ensure_contains(p.getRemoved(), 3);
+	ensure_not_contains(p.getRemoved(), 4);
 }
 
 template<> template<>
-void to::test<2>()
+void to::test<4>()
 {
 #if 0
 	string input_coll(
@@ -129,6 +207,33 @@
 #endif
 }
 
+// Check addPatchInverted
+template<> template<>
+void to::test<5>()
+{
+	PatchList<string, string> patches;
+	patches.addPatchInverted(Patch<string, string>(
+				"pizza",
+				wibble::singleton(string("tomato")),
+				wibble::singleton(string("ketchup"))));
+
+	ensure_equals(patches.size(), 2u);
+
+	PatchList<string, string>::const_iterator i = patches.begin();
+
+	ensure_equals(i->first, "ketchup");
+	ensure_equals(i->second.getAdded().size(), 0u);
+	ensure_equals(i->second.getRemoved().size(), 1u);
+	ensure_equals(*i->second.getRemoved().begin(), "pizza");
+
+	++i;
+
+	ensure_equals(i->first, "tomato");
+	ensure_equals(i->second.getAdded().size(), 1u);
+	ensure_equals(i->second.getRemoved().size(), 0u);
+	ensure_equals(*i->second.getAdded().begin(), "pizza");
+}
+
 }
 
 #include <tagcoll/TextFormat.tcc>

Modified: tagcoll/2.0/tagcoll/Patches.h
==============================================================================
--- tagcoll/2.0/tagcoll/Patches.h	(original)
+++ tagcoll/2.0/tagcoll/Patches.h	Sat Sep 30 11:06:07 2006
@@ -24,7 +24,6 @@
  */
 
 #include <wibble/operators.h>
-#include <wibble/singleton.h>
 #include <wibble/mixin.h>
 #include <map>
 #include <string>
@@ -36,17 +35,17 @@
  * Patch for the tagset of a specific item
  */
 template <typename ITEM, typename TAG>
-class Patch
+struct Patch
 {
-protected:
 	ITEM item;
 	std::set<TAG> added;
 	std::set<TAG> removed;
 
-public:
 	Patch(const Patch<ITEM, TAG>& p) : item(p.item), added(p.added), removed(p.removed) {}
 	Patch(const ITEM& item) : item(item) {}
 	Patch(const ITEM& item, const std::set<TAG>& added, const std::set<TAG>& removed);
+	template<typename CONTA, typename CONTB>
+	Patch(const ITEM& item, const CONTA& added, const CONTB& removed);
 	~Patch() {}
 
 	void add(const TAG& tag)
@@ -70,10 +69,6 @@
 		removed |= tags; added -= tags;
 	}
 
-	const ITEM& getItem() const { return item; }
-	const std::set<TAG>& getAdded() const { return added; }
-	const std::set<TAG>& getRemoved() const { return removed; }
-
 	Patch<ITEM, TAG> getReverse() const
 	{
 		return Patch<ITEM, TAG>(item, removed, added);
@@ -110,6 +105,15 @@
 		// Don't remove what does not exist
 		removed -= (removed - ts);
 	}
+
+	bool operator==(const Patch<ITEM, TAG>& p) const
+	{
+		return p.item == item && p.added == added && p.removed == removed;
+	}
+	bool operator!=(const Patch<ITEM, TAG>& p) const
+	{
+		return p.item != item || p.added != added || p.removed != removed;
+	}
 };
 
 /**
@@ -142,6 +146,18 @@
 	void addPatch(const PatchList<ITEM, TAG>& patches);
 
 	/**
+	 * Add 'patch' to this PatchList, as tag: +/- package rather than package
+	 * +/- tag
+	 */
+	void addPatchInverted(const Patch<TAG, ITEM>& patch);
+
+	/**
+	 * Add 'patches' to this PatchList, as tag: +/- package rather than package
+	 * +/- tag
+	 */
+	void addPatchInverted(const PatchList<TAG, ITEM>& patches);
+
+	/**
 	 * Patch a tagged item
 	 *
 	 * @return

Modified: tagcoll/2.0/tagcoll/Patches.tcc
==============================================================================
--- tagcoll/2.0/tagcoll/Patches.tcc	(original)
+++ tagcoll/2.0/tagcoll/Patches.tcc	Sat Sep 30 11:06:07 2006
@@ -22,6 +22,8 @@
 #define TAGCOLL_PATCHES_TCC
 
 #include <tagcoll/Patches.h>
+#include <wibble/singleton.h>
+#include <wibble/empty.h>
 
 using namespace std;
 using namespace wibble::operators;
@@ -34,16 +36,24 @@
 {
 }
 
+template <typename ITEM, typename TAG> template<typename CONTA, typename CONTB>
+Patch<ITEM, TAG>::Patch(const ITEM& item, const CONTA& added, const CONTB& removed)
+	: item(item)
+{
+	std::copy(added.begin(), added.end(), inserter(this->added, this->added.begin()));
+	std::copy(removed.begin(), removed.end(), inserter(this->removed, this->removed.begin()));
+}
+
 template <class ITEM, class TAG>
 void PatchList<ITEM, TAG>::addPatch(const Patch<ITEM, TAG>& patch)
 {
 	// Filter out empty patches
-	if (patch.getAdded().empty() && patch.getRemoved().empty())
+	if (patch.added.empty() && patch.removed.empty())
 		return;
 
-	iterator i = this->find(patch.getItem());
+	iterator i = this->find(patch.item);
 	if (i == this->end())
-		insert(make_pair<ITEM, Patch<ITEM, TAG> >(patch.getItem(), patch));
+		insert(make_pair<ITEM, Patch<ITEM, TAG> >(patch.item, patch));
 	else
 		i->second.mergeWith(patch);
 }
@@ -56,6 +66,30 @@
 		addPatch(i->second);
 }
 
+template <class ITEM, class TAG>
+void PatchList<ITEM, TAG>::addPatchInverted(const Patch<TAG, ITEM>& patch)
+{
+	// Filter out empty patches
+	if (patch.added.empty() && patch.removed.empty())
+		return;
+
+	for (typename std::set<ITEM>::const_iterator i = patch.added.begin();
+			i != patch.added.end(); ++i)
+		addPatch(Patch<ITEM, TAG>(*i, wibble::singleton(patch.item), wibble::Empty<TAG>()));
+	for (typename std::set<ITEM>::const_iterator i = patch.removed.begin();
+			i != patch.removed.end(); ++i)
+		addPatch(Patch<ITEM, TAG>(*i, wibble::Empty<TAG>(), wibble::singleton(patch.item)));
+}
+
+template <class ITEM, class TAG>
+void PatchList<ITEM, TAG>::addPatchInverted(const PatchList<TAG, ITEM>& patches)
+{
+	for (typename PatchList<TAG, ITEM>::const_iterator i = patches.begin();
+			i != patches.end(); i++)
+		addPatchInverted(i->second);
+}
+
+
 template <class ITEM, class TAG> template<typename COLL1, typename COLL2>
 void PatchList<ITEM, TAG>::addPatch(const COLL1& im1, const COLL2& im2)
 {

Modified: tagcoll/2.0/tagcoll/TextFormat.cc
==============================================================================
--- tagcoll/2.0/tagcoll/TextFormat.cc	(original)
+++ tagcoll/2.0/tagcoll/TextFormat.cc	Sat Sep 30 11:06:07 2006
@@ -165,8 +165,8 @@
 		bool start = true;
 
 		std::set<string> stags;
-		for (std::set<std::string>::const_iterator j = i->second.getAdded().begin();
-				j != i->second.getAdded().end(); j++)
+		for (std::set<std::string>::const_iterator j = i->second.added.begin();
+				j != i->second.added.end(); j++)
 		{
 			if (start)
 			{
@@ -177,8 +177,8 @@
 				if (fprintf(out, ", +%s", j->c_str()) < 0)
 					throw wibble::exception::System("writing patched tag");
 		}
-		for (std::set<std::string>::const_iterator j = i->second.getRemoved().begin();
-				j != i->second.getRemoved().end(); j++)
+		for (std::set<std::string>::const_iterator j = i->second.removed.begin();
+				j != i->second.removed.end(); j++)
 		{
 			if (start)
 			{



More information about the Debtags-commits mailing list