[Debtags-commits] [svn] r1755 - in tagcoll/2.0: . tagcoll tagcoll/coll tools

Enrico Zini enrico at costa.debian.org
Thu May 11 00:41:03 UTC 2006


Author: enrico
Date: Thu May 11 00:40:31 2006
New Revision: 1755

Added:
   tagcoll/2.0/tagcoll/Implications.tcc
Modified:
   tagcoll/2.0/   (props changed)
   tagcoll/2.0/tagcoll/Implications.cc
   tagcoll/2.0/tagcoll/Implications.h
   tagcoll/2.0/tagcoll/Makefile.am
   tagcoll/2.0/tagcoll/coll/base.h
   tagcoll/2.0/tagcoll/test-utils.tcc
   tagcoll/2.0/tools/tagcoll.cc
Log:
 r2690 at viaza:  enrico | 2006-05-10 13:34:38 -0500
 Split Implications in .cc and .tcc
 tagcoll.cc compiles


Modified: tagcoll/2.0/tagcoll/Implications.cc
==============================================================================
--- tagcoll/2.0/tagcoll/Implications.cc	(original)
+++ tagcoll/2.0/tagcoll/Implications.cc	Thu May 11 00:40:31 2006
@@ -1,7 +1,7 @@
 /*
  * Collection of tag implications and a Filter to apply or compress them
  *
- * Copyright (C) 2003,2004,2005  Enrico Zini <enrico at debian.org>
+ * Copyright (C) 2003--2006  Enrico Zini <enrico at debian.org>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -18,113 +18,17 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
  */
 
-#include <tagcoll/utils/set.h>
-#include <tagcoll/Implications.h>
-#include <wibble/operators.h>
-
-#include <algorithm>
-
-using namespace std;
-using namespace tagcoll;
-using namespace wibble::operators;
-
-template<class TAG>
-std::set<TAG> Implications<TAG>::getDestinations(const TAG& tag, const std::set<TAG>& seen) const
-{
-	typename Implications<TAG>::const_iterator i = this->coll.find(tag);
-	if (i == this->coll.end())
-		return std::set<TAG>();
-
-	// res <- the union of all the destinations of the tag
-	std::set<TAG> res;
-	for (typename std::set<TAG>::const_iterator t = i->second.begin();
-			t != i->second.end(); t++)
-	{
-		// If the tag is already in res, then also all his destinations are
-		// already there
-		if (!utils::set_contains(res, *t) && !utils::set_contains(seen, *t))
-		{
-			res |= *t;
-			res |= getDestinations(*t, seen | tag);
-		}
-	}
-
-	return res;
-}
-
-template<class TAG>
-bool Implications<TAG>::reaches(const TAG& tag1, const TAG& tag2, const std::set<TAG>& seen) const
-{
-	// Check if we've reached the target
-	if (tag1 == tag2)
-		return true;
-	
-	// No: see if we have other paths to follow
-	typename Implications<TAG>::const_iterator i = this->coll.find(tag1);
-	if (i == this->coll.end())
-		return false;
-
-	// Try all paths
-	for (typename std::set<TAG>::const_iterator t = i->second.begin();
-			t != i->second.end(); t++)
-		if (!utils::set_contains(seen, *t) && reaches(*t, tag2, seen | tag1))
-			return true;
-
-	// Nothing has been found
-	return false;
-}
-
-// Remove unnecessary arcs from the dag
-template <class TAG>
-void Implications<TAG>::pack()
-{
-	// For every tag
-	for (typename Implications<TAG>::iterator i = this->coll.begin();
-			i != this->coll.end(); i++)
-	{
-		std::set<TAG> redundant;
-
-		// For every couple of parents A and B, if A -> B but not B -> A, then B is redundant
-		// I need to check every combination; however, I can ignore in the
-		// search items that have already been found as redundant
-
-		std::set<TAG> candidates = i->second;
-		while (candidates.size() > 1)
-		{
-			typename std::set<TAG>::const_iterator a = candidates.begin();
-			typename std::set<TAG>::const_iterator b = a;
-			std::set<TAG> got;
-			for (++b; b != candidates.end(); b++)
-			{
-				bool ab = reaches(*a, *b);
-				bool ba = reaches(*b, *a);
-				if (ab && !ba)
-				{
-					got |= *b;
-					break;
-				} else if (ba && !ab) {
-					got |= *a;
-					break;
-				}
-			}
-			candidates -= got;
-			redundant |= got;
-			if (!candidates.empty())
-				candidates.erase(candidates.begin());
-		}
-
-		i->second -= redundant;
-	}
-}
-
 #ifdef COMPILE_TESTSUITE
 
+#include <tagcoll/Implications.h>
 #include <tests/test-utils.h>
 
 #include <tagcoll/coll/simple.h>
 
 namespace wibble {
 namespace tut {
+using namespace std;
+using namespace tagcoll;
 using namespace tagcoll::tests;
 
 struct tagcoll_implications_shar {
@@ -200,6 +104,7 @@
 }
 
 #include <tagcoll/TextFormat.tcc>
+#include <tagcoll/Implications.tcc>
 
 #endif
 

Modified: tagcoll/2.0/tagcoll/Implications.h
==============================================================================
--- tagcoll/2.0/tagcoll/Implications.h	(original)
+++ tagcoll/2.0/tagcoll/Implications.h	Thu May 11 00:40:31 2006
@@ -33,6 +33,8 @@
 
 /**
  * List of explicit implications that can be applied to a tagged collection.
+ *
+ * The underlying Simple collection goes as "{Item} implies {Tags}"
  */
 template <class TAG>
 class Implications : public coll::Simple<TAG, TAG>
@@ -95,33 +97,36 @@
 	// Remove unnecessary arcs from the dag
 	void pack();
 
-#if 0
-	// Output the fully expanded implication dag to a TagcollConsumer
-	void outputFull(Consumer<TAG, TAG>& consumer) const
+	template<typename COLL>
+	void addFrom(const COLL& coll)
 	{
-		for (typename impl_t::const_iterator i = implications.begin();
-				i != implications.end(); i++)
+		std::set<TAG> allTags = coll.getAllTags();
+		for (typename std::set<TAG>::const_iterator t = allTags.begin();
+				t != allTags.end(); t++)
 		{
-			std::set<TAG> destinations = getDestinations(i->first);
-
-			if (destinations.empty())
-				consumer.consume(i->first);
-			else
-				consumer.consume(i->first, destinations);
+			typename std::set<TAG> implying = coll.getTagsImplying(*t);
+			for (typename std::set<TAG>::const_iterator i = implying.begin();
+					i != implying.end(); ++i)
+				insert(wibble::singleton(*i), wibble::singleton(*t));
 		}
 	}
 
-	// Output the implication dag to a TagcollConsumer
-	void output(Consumer<TAG, TAG>& consumer) const
+	// Output the fully expanded implication dag to a TagcollConsumer
+	template<typename OUT>
+	void outputFull(OUT out) const
 	{
-		for (typename impl_t::const_iterator i = implications.begin();
-				i != implications.end(); i++)
-			if (i->second.empty())
-				consumer.consume(i->first);
-			else
-				consumer.consume(i->first, i->second);
+		for (typename coll::Simple<TAG, TAG>::const_iterator i = this->begin();
+				i != this->end(); ++i)
+		{
+			std::set<TAG> destinations = getDestinations(i->first);
+
+			if (!destinations.empty())
+			{
+				*out = make_pair(wibble::singleton(i->first), destinations);
+				++out;
+			}
+		}
 	}
-#endif
 };
 
 /**

Modified: tagcoll/2.0/tagcoll/Makefile.am
==============================================================================
--- tagcoll/2.0/tagcoll/Makefile.am	(original)
+++ tagcoll/2.0/tagcoll/Makefile.am	Thu May 11 00:40:31 2006
@@ -44,8 +44,7 @@
 		BasicStringDiskIndex.h \
 		\
 		Implications.h \
-		Implications.cc \
-		\
+		Implications.tcc \
 		DerivedTags.h \
 		\
 		SmartHierarchy.h \
@@ -83,7 +82,6 @@
 		TextFormat.cc \
 		\
 		Implications.cc \
-		\
 		DerivedTags.cc \
 		\
 		SmartHierarchy.cc

Modified: tagcoll/2.0/tagcoll/coll/base.h
==============================================================================
--- tagcoll/2.0/tagcoll/coll/base.h	(original)
+++ tagcoll/2.0/tagcoll/coll/base.h	Thu May 11 00:40:31 2006
@@ -247,12 +247,12 @@
 
 		// First get a list of packages that have a non-empty intersection with `tags'
 		for (typename TAGS::const_iterator i = tags.begin(); i != tags.end(); i++)
-			packages |= getItemsHavingTag(*i);
+			packages |= static_cast<const Self*>(this)->getItems(*i);
 
 		// Then keep only those within the given distance
 		for (typename coll_traits<Self>::itemset_type::const_iterator i = packages.begin(); i != packages.end(); i++)
 		{
-			int dist = utils::set_distance(tags, getTagsOfItem(*i));
+			int dist = utils::set_distance(tags, static_cast<const Self*>(this)->getTags(*i));
 			if (dist >= 0 && dist <= maxdistance)
 				res |= *i;
 		}

Modified: tagcoll/2.0/tagcoll/test-utils.tcc
==============================================================================
--- tagcoll/2.0/tagcoll/test-utils.tcc	(original)
+++ tagcoll/2.0/tagcoll/test-utils.tcc	Thu May 11 00:40:31 2006
@@ -288,7 +288,18 @@
 	// TODO:    virtual std::set<TAG> getAllTags() const = 0;
 	// TODO:    virtual int getCardinality(const TAG& tag) const
 	// TODO:    virtual std::set<TAG> getCompanionTags(const std::set<TAG>& tags) const
-	// TODO:    virtual std::set<ITEM> getRelatedItems(const std::set<TAG>& tags, int maxdistance = 1) const
+
+	// std::set<ITEM> getRelatedItems(const std::set<TAG>& tags, int maxdistance = 1) const
+	s1.clear();
+	s1.insert("tomato"); s1.insert("mozzarella");
+	s = tc.getRelatedItems(s1, 1);
+	inner_ensure_equals(s.size(), 2u);
+	inner_ensure_not_contains(s, string("gnocco"));
+	inner_ensure_contains(s, string("margherita"));
+	inner_ensure_contains(s, string("funghi"));
+	inner_ensure_not_contains(s, string("rosmarino"));
+	inner_ensure_not_contains(s, string("marinara"));
+
 
 	// void output(Consumer<ITEM, TAG>& consumer) const
 	coll::Simple<string, string> coll1;

Modified: tagcoll/2.0/tools/tagcoll.cc
==============================================================================
--- tagcoll/2.0/tools/tagcoll.cc	(original)
+++ tagcoll/2.0/tools/tagcoll.cc	Thu May 11 00:40:31 2006
@@ -586,14 +586,7 @@
 			Implications<string> newImpls;
 
 			// Find tag implications
-			std::set<string> allTags = coll.getAllTags();
-			for (std::set<string>::const_iterator t = allTags.begin();
-					t != allTags.end(); t++)
-			{
-				std::set<string> implied = coll.getImpliedBy(*t);
-				if (!implied.empty())
-					newImpls.insert(wibble::singleton(*t), implied);
-			}
+			newImpls.addFrom(coll);
 
 			newImpls.pack();
 
@@ -695,15 +688,7 @@
 			printItems(coll.getItemsExactMatch(ts));
 
 			if (maxdist)
-			{
-				// Get the related tagsets
-				list< std::set<string> > rel = coll.getRelatedTagsets(ts, maxdist);
-
-				// Print the output list
-				for (list< std::set<string> >::const_iterator i = rel.begin();
-						i != rel.end(); i++)
-					printItems(coll.getItemsExactMatch(*i));
-			}
+				printItems(coll.getRelatedItems(ts, maxdist));
 		}
 		else if (opts.foundCommand() == opts.reverse)
 		{
@@ -787,4 +772,8 @@
 	}
 }
 
+#include <tagcoll/TextFormat.tcc>
+#include <tagcoll/Patches.tcc>
+#include <tagcoll/Implications.tcc>
+
 // vim:set ts=4 sw=4:



More information about the Debtags-commits mailing list