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

Enrico Zini enrico at costa.debian.org
Tue May 9 00:20:26 UTC 2006


Author: enrico
Date: Tue May  9 00:20:25 2006
New Revision: 1725

Modified:
   tagcoll/2.0/   (props changed)
   tagcoll/2.0/tagcoll/PatchCollection.cc
   tagcoll/2.0/tagcoll/PatchCollection.h
   tagcoll/2.0/tagcoll/ReadonlyCollection.h
   tagcoll/2.0/tagcoll/TDBIndexer.cc
   tagcoll/2.0/tagcoll/TDBIndexer.h
Log:
 r2612 at viaza:  enrico | 2006-05-07 18:26:32 -0500
 Refactored PatchCollection and TDBIndexer
 Readded an output method to ReadonlyCollection


Modified: tagcoll/2.0/tagcoll/PatchCollection.cc
==============================================================================
--- tagcoll/2.0/tagcoll/PatchCollection.cc	(original)
+++ tagcoll/2.0/tagcoll/PatchCollection.cc	Tue May  9 00:20:25 2006
@@ -30,28 +30,29 @@
 namespace tagcoll
 {
 
+#if 0
 template<class ITEM, class TAG>
 void PatchCollection<ITEM, TAG>::consumeItem(const ITEM& item, const std::set<TAG>& tags)
 {
 	if (!tags.empty())
 		changes.addPatch(Patch<ITEM, TAG>(item, tags, std::set<TAG>()));
 }
+#endif
 
-template<class ITEM, class TAG>
-std::set<ITEM> PatchCollection<ITEM, TAG>::getItemsHavingTag(const TAG& tag) const
+template<typename ROCOLL>
+typename coll_traits<ROCOLL>::itemset_type PatchCollection<ROCOLL>::getItems(const Tag& tag) const
 {
-	std::set<ITEM> items(coll.getItems(tag));
-	std::set<ITEM> res;
+	ItemSet items(coll.getItems(tag));
+	ItemSet res;
 
 	// Check items in coll first
-	for (typename std::set<ITEM>::const_iterator i = items.begin();
-			i != items.end(); i++)
+	for (typename ItemSet::const_iterator i = items.begin(); i != items.end(); ++i)
 		// If they are unmodified, then we can trust coll.getItems
 		if (changes.find(*i) == changes.end())
 			res |= *i;
 
 	// Then check items in the patch
-	for (typename PatchList<ITEM, TAG>::const_iterator i = changes.begin();
+	for (typename Patches::const_iterator i = changes.begin();
 			i != changes.end(); i++)
 		if (set_contains(changes.patch(i->first, coll.getTags(i->first)), tag))
 			res |= i->first;
@@ -59,26 +60,19 @@
 	return res;
 }
 
-template<class ITEM, class TAG>
-std::set<TAG> PatchCollection<ITEM, TAG>::getTagsOfItem(const ITEM& item) const
-{
-	return changes.patch(item, coll.getTags(item));
-}
-
-template<class ITEM, class TAG>
-void PatchCollection<ITEM, TAG>::setChanges(const PatchList<ITEM, TAG>& changes)
+template<typename ROCOLL>
+void PatchCollection<ROCOLL>::setChanges(const Patches& changes)
 {
-	this->changes.clear();
+	this->m_changes.clear();
 	
 	// Simplify the patch against the contents of `coll' before adding it.
-	for (typename PatchList<ITEM, TAG>::const_iterator i = changes.begin();
-			i != changes.end(); i++)
+	for (typename Patches::const_iterator i = changes.begin(); i != changes.end(); i++)
 		// Consider only valid items
-		if (i->first != ITEM())
+		if (i->first != Item())
 		{
-			Patch<ITEM, TAG> newChange(i->second);
+			Patch newChange(i->second);
 
-			std::set<TAG> tags(coll.getTags(i->first));
+			TagSet tags(coll.getTags(i->first));
 			newChange.removeRedundant(tags);
 
 			// Empty patches are filtered out by PatchList so we don't need to do
@@ -87,30 +81,30 @@
 		}
 }
 
-template<class ITEM, class TAG>
-void PatchCollection<ITEM, TAG>::addChanges(const PatchList<ITEM, TAG>& changes)
+template<typename ROCOLL>
+void PatchCollection<ROCOLL>::addChanges(const Patches& changes)
 {
-	PatchList<ITEM, TAG> ch(this->changes);
+	Patches ch(m_changes);
 
 	ch.addPatch(changes);
 
 	setChanges(ch);
 }
 
-template<class ITEM, class TAG>
-bool PatchCollection<ITEM, TAG>::hasTag(const TAG& tag) const
+template<typename ROCOLL>
+bool PatchCollection<ROCOLL>::hasTag(const Tag& tag) const
 {
-	std::set<ITEM> items(coll.getItems(tag));
+	ItemSet items(coll.getItems(tag));
 
 	// Check items in coll first
-	for (typename std::set<ITEM>::const_iterator i = items.begin();
+	for (typename ItemSet::const_iterator i = items.begin();
 			i != items.end(); i++)
 		// If they are unmodified, then we can trust coll.getItems
 		if (changes.find(*i) == changes.end())
 			return true;
 
 	// Then check items in the patch
-	for (typename PatchList<ITEM, TAG>::const_iterator i = changes.begin();
+	for (typename Patches::const_iterator i = changes.begin();
 			i != changes.end(); i++)
 		if (set_contains(i->second.getAdded(), tag))
 			return true;
@@ -118,43 +112,27 @@
 	return false;
 }
 
-template<class ITEM, class TAG>
-std::set<ITEM> PatchCollection<ITEM, TAG>::getTaggedItems() const
+template<typename ROCOLL>
+typename coll_traits<ROCOLL>::itemset_type PatchCollection<ROCOLL>::getTaggedItems() const
 {
-	std::set<ITEM> res(coll.getTaggedItems());
-	for (typename PatchList<ITEM, TAG>::const_iterator i = changes.begin();
+	ItemSet res(coll.getTaggedItems());
+	for (typename Patches::const_iterator i = changes.begin();
 			i != changes.end(); i++)
 		res |= i->first;
 	return res;
 }
 
-template<typename ITEM, typename TAG>
-class TagCollector : public Consumer<ITEM, TAG>, public std::set<TAG>
-{
-protected:
-	virtual void consumeItemUntagged(const ITEM&) {}
-	virtual void consumeItemsUntagged(const std::set<ITEM>&) {}
-
-	virtual void consumeItem(const ITEM&, const std::set<TAG>& tags)
-	{
-		*this |= tags;
-	}
-	virtual void consumeItems(const std::set<ITEM>&, const std::set<TAG>& tags)
-	{
-		*this |= tags;
-	}
-};
-
-template<class ITEM, class TAG>
-std::set<TAG> PatchCollection<ITEM, TAG>::getAllTags() const
+template<typename ROCOLL>
+typename coll_traits<ROCOLL>::tagset_type PatchCollection<ROCOLL>::getAllTags() const
 {
-	TagCollector<ITEM, TAG> res;
-
-	output(res);
-
+	TagSet res;
+	for (typename ROCOLL::const_iterator i = coll.begin();
+			i != coll.end(); ++i)
+		res |= i->second;
 	return res;
 }
 
+#if 0
 template<typename ITEM, typename TAG, typename OUT>
 class UnpatchedOnly : public wibble::mixin::OutputIterator< UnpatchedOnly<ITEM, TAG, OUT> >
 {
@@ -199,25 +177,26 @@
 		cons.consume(i->first,
 				changes.patch(i->first, coll.getTags(i->first)));
 }
+#endif
 
-template<class ITEM, class TAG>
-void PatchCollection<ITEM, TAG>::applyChange(const PatchList<ITEM, TAG>& change)
+template<typename ROCOLL>
+void PatchCollection<ROCOLL>::applyChange(const Patches& change)
 {
-	for (typename PatchList<ITEM, TAG>::const_iterator i = change.begin();
+	for (typename Patches::const_iterator i = change.begin();
 			i != change.end(); i++)
 	{
-		Patch<ITEM, TAG> newChange(i->second);
+		Patches newChange(i->second);
 		newChange.removeRedundant(getTags(i->first));
 		changes.addPatch(newChange);
 	}
 }
 
-template<class ITEM, class TAG>
-int PatchCollection<ITEM, TAG>::getCardinality(const TAG& tag) const
+template<typename ROCOLL>
+int PatchCollection<ROCOLL>::getCardinality(const Tag& tag) const
 {
 	int card = coll.getCardinality(tag);
 
-	for (typename PatchList<ITEM, TAG>::const_iterator i = changes.begin();
+	for (typename Patches::const_iterator i = changes.begin();
 			i != changes.end(); i++)
 	{
 		if (set_contains(i->second.getAdded(), tag))
@@ -232,14 +211,6 @@
 
 }
 
-#ifndef INSTANTIATING_TEMPLATES
-#include <string>
-
-namespace tagcoll {
-template class PatchCollection<std::string, std::string>;
-}
-#endif
-
 #ifdef COMPILE_TESTSUITE
 
 #include <tests/test-utils.h>
@@ -259,7 +230,7 @@
 	// Use an InputMerger as the embedded collection
 	InputMerger<string, string> startcoll;
 
-	PatchCollection<string, string> coll(startcoll);
+	PatchCollection< InputMerger<string, string> > coll(startcoll);
 
 	output_test_collection(coll);
 	test_readonly_collection(coll);
@@ -271,7 +242,7 @@
 	// Use an InputMerger as the embedded collection
 	InputMerger<string, string> startcoll;
 
-	PatchCollection<string, string> coll(startcoll);
+	PatchCollection< InputMerger<string, string> > coll(startcoll);
 
 	test_collection(coll);
 }

Modified: tagcoll/2.0/tagcoll/PatchCollection.h
==============================================================================
--- tagcoll/2.0/tagcoll/PatchCollection.h	(original)
+++ tagcoll/2.0/tagcoll/PatchCollection.h	Tue May  9 00:20:25 2006
@@ -29,59 +29,108 @@
 namespace tagcoll
 {
 
+	/**
+	 * TODO: iterator is implementable if we consider that both the underlying
+	 * collection and the PatchList iterate items in sorted order: the iterator
+	 * can keep two iterators inside and present items as merges of sorted
+	 * sequences
+	 */
+
 /**
  * Wraps a collection by intercepting all changes to it and preserving them as
  * a PatchList.
  */
-template<class ITEM, class TAG>
-class PatchCollection : public Collection<ITEM, TAG>
+template<typename ROCOLL>
+class PatchCollection
 {
+public:
+	typedef Patch<
+		typename coll_traits<ROCOLL>::item_type,
+		typename coll_traits<ROCOLL>::tag_type> Patch;
+	typedef PatchList<
+		typename coll_traits<ROCOLL>::item_type,
+		typename coll_traits<ROCOLL>::tag_type> Patches;
+
 protected:
-	const ReadonlyCollection<ITEM, TAG>& coll;
-	PatchList<ITEM, TAG> changes;
+	typedef typename coll_traits<ROCOLL>::item_type Item;
+	typedef typename coll_traits<ROCOLL>::tag_type Tag;
+	typedef typename coll_traits<ROCOLL>::itemset_type ItemSet;
+	typedef typename coll_traits<ROCOLL>::tagset_type TagSet;
 
+	const ROCOLL& coll;
+	Patches m_changes;
+
+#if 0
 	virtual void consumeItem(const ITEM& item, const std::set<TAG>& tags);
 
 	virtual std::set<ITEM> getItemsHavingTag(const TAG& tag) const;
 	virtual std::set<TAG> getTagsOfItem(const ITEM& item) const;
+#endif
 
 public:
-	PatchCollection(const ReadonlyCollection<ITEM, TAG>& coll) : coll(coll) {}
-	virtual ~PatchCollection() {}
+	PatchCollection(const ROCOLL& coll) : coll(coll) {}
 
 	/**
 	 * Get the changes that have been applied to this collection
 	 */
-	const PatchList<ITEM, TAG>& getChanges() const { return changes; }
+	const Patches& changes() const { return m_changes; }
 
 	/**
 	 * Throw away all changes previously applied to this collection
 	 */
-	void resetChanges() { changes.clear(); }
+	void resetChanges() { m_changes.clear(); }
 
 	/**
 	 * Set the changes list to a specific patch list
 	 */
-	void setChanges(const PatchList<ITEM, TAG>& changes);
+	void setChanges(const Patches& changes);
 
 	/**
 	 * Add a specific patch list to the changes list
 	 */
-	void addChanges(const PatchList<ITEM, TAG>& changes);
+	void addChanges(const Patches& changes);
+
+    bool hasTag(const Tag& tag) const;
 
-    virtual bool hasTag(const TAG& tag) const;
+	TagSet getTags(const Item& item) const
+	{
+		return changes.patch(item, coll.getTags(item));
+	}
+	template<typename ITEMS>
+	TagSet getTags(const Item& items) const
+	{
+		using namespace wibble::operators;
+		TagSet res;
+		for (typename ITEMS::const_iterator i = items.begin();
+				i != items.end(); i++)
+			res |= getTags(*i);
+		return res;
+	}
+	ItemSet getItems(const typename coll_traits<ROCOLL>::tag_type& tag) const;
+	template<typename TAGS>
+	ItemSet getItems(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);
+		return res;
+	}
 
-	virtual std::set<ITEM> getTaggedItems() const;
-	virtual std::set<TAG> getAllTags() const;
+	ItemSet getTaggedItems() const;
+	TagSet getAllTags() const;
 
-	virtual int getCardinality(const TAG& tag) const;
+	int getCardinality(const Tag& tag) const;
 
-	virtual void applyChange(const PatchList<ITEM, TAG>& change);
+	void applyChange(const Patches& change);
 
+#if 0
 	virtual void output(Consumer<ITEM, TAG>& consumer) const;
+#endif
 };
 
-};
+}
 
 // vim:set ts=4 sw=4:
 #endif

Modified: tagcoll/2.0/tagcoll/ReadonlyCollection.h
==============================================================================
--- tagcoll/2.0/tagcoll/ReadonlyCollection.h	(original)
+++ tagcoll/2.0/tagcoll/ReadonlyCollection.h	Tue May  9 00:20:25 2006
@@ -135,7 +135,7 @@
 		typename coll_traits<Self>::tagset_type res;
 		for (typename ITEMS::const_iterator i = items.begin();
 				i != items.end(); i++)
-			res |= getTagsOfItem(*i);
+			res |= getTags(*i);
 		return res;
 	}
 
@@ -262,16 +262,16 @@
 	/**
 	 * Output all the contents of the collection to a Consumer
 	 */
-	//virtual void output(Consumer<ITEM, TAG>& consumer) const = 0;
-
-#if 0
 	template<typename OUT>
-	void outputToIterator(const OUT& cons) const
+	void output(OUT out) const
 	{
-		Forwarder<OUT> forwarder(cons);
-		output(forwarder);
+		for (typename Self::const_iterator i = this->begin();
+				i != this->end(); ++i)
+		{
+			*out = *i;
+			++out;
+		}
 	}
-#endif
 
 	/**
 	 * Send to a consumer all the items which are tagged with at least the

Modified: tagcoll/2.0/tagcoll/TDBIndexer.cc
==============================================================================
--- tagcoll/2.0/tagcoll/TDBIndexer.cc	(original)
+++ tagcoll/2.0/tagcoll/TDBIndexer.cc	Tue May  9 00:20:25 2006
@@ -29,7 +29,7 @@
 
 
 template<class ITEM, class TAG>
-std::set<ITEM> TDBIndexer<ITEM, TAG>::getItemsHavingTag(const TAG& tag) const
+std::set<ITEM> TDBIndexer<ITEM, TAG>::getItems(const TAG& tag) const
 {
 	typename map<TAG, std::set<ITEM> >::const_iterator i = tags.find(tag);
 	if (i != tags.end())
@@ -39,7 +39,7 @@
 }
 
 template<class ITEM, class TAG>
-std::set<TAG> TDBIndexer<ITEM, TAG>::getTagsOfItem(const ITEM& item) const
+std::set<TAG> TDBIndexer<ITEM, TAG>::getTags(const ITEM& item) const
 {
 	typename map<ITEM, std::set<TAG> >::const_iterator i = items.find(item);
 	if (i != items.end())
@@ -68,6 +68,7 @@
 	return res;
 }
 
+#if 0
 template<class ITEM, class TAG>
 void TDBIndexer<ITEM, TAG>::output(Consumer<ITEM, TAG>& consumer) const
 {
@@ -75,6 +76,7 @@
 			i != items.end(); i++)
 		consumer.consume(i->first, i->second);
 }
+#endif
 
 template<class ITEM, class TAG>
 void TDBIndexer<ITEM, TAG>::applyChange(const PatchList<ITEM, TAG>& change)
@@ -104,7 +106,7 @@
 	}
 }
 
-
+#if 0
 template<class ITEM, class TAG>
 void TDBIndexer<ITEM, TAG>::consumeItem(const ITEM& item, const std::set<TAG>& tags)
 {
@@ -127,6 +129,7 @@
 		// Add the items to the tag
 		this->tags[*i] |= items;
 }
+#endif
 
 #ifndef INSTANTIATING_TEMPLATES
 #include <string>

Modified: tagcoll/2.0/tagcoll/TDBIndexer.h
==============================================================================
--- tagcoll/2.0/tagcoll/TDBIndexer.h	(original)
+++ tagcoll/2.0/tagcoll/TDBIndexer.h	Tue May  9 00:20:25 2006
@@ -28,6 +28,19 @@
 
 namespace tagcoll
 {
+template<typename T1, typename T2> class PatchList;
+
+template<typename ITEM, typename TAG>
+class TDBIndexer;
+
+template<typename ITEM, typename TAG>
+struct coll_traits< TDBIndexer<ITEM, TAG> >
+{
+	typedef ITEM item_type;
+	typedef TAG tag_type;
+	typedef std::set<ITEM> tagset_type;
+	typedef std::set<TAG> itemset_type;
+};
 
 /**
  * In-memory collection whose representation is organised similarly to the
@@ -40,29 +53,42 @@
  * run and tend to be more compact than the ones created by TDBDiskIndex.
  */
 template<class ITEM, class TAG>
-class TDBIndexer : public Collection<ITEM, TAG>
+class TDBIndexer : public Collection< TDBIndexer<ITEM, TAG> >
 {
 protected:
 	std::map<ITEM, std::set<TAG> > items;
 	std::map<TAG, std::set<ITEM> > tags;
 
+#if 0
 	virtual void consumeItem(const ITEM& item, const std::set<TAG>& tags);
 	virtual void consumeItems(const std::set<ITEM>& items, const std::set<TAG>& tags);
 
 	virtual std::set<ITEM> getItemsHavingTag(const TAG& tag) const;
 	virtual std::set<TAG> getTagsOfItem(const ITEM& item) const;
+#endif
 
 public:
-	virtual ~TDBIndexer() {}
-
-	virtual bool empty() const { return items.empty(); }
+	typedef typename std::map< ITEM, std::set<TAG> >::const_iterator const_iterator;
+	typedef typename std::map< ITEM, std::set<TAG> >::iterator iterator;
 
-    virtual bool hasItem(const ITEM& item) const { return items.find(item) != items.end(); }
-    virtual bool hasTag(const TAG& tag) const { return tags.find(tag) != tags.end(); }
-	virtual std::set<ITEM> getTaggedItems() const;
-	virtual std::set<TAG> getAllTags() const;
-	virtual void output(Consumer<ITEM, TAG>& consumer) const;
-	virtual void applyChange(const PatchList<ITEM, TAG>& change);
+	const_iterator begin() const { return items.begin(); }
+	const_iterator end() const { return items.end(); }
+	iterator begin() { return items.begin(); }
+	iterator end() { return items.end(); }
+
+	std::set<TAG> getTags(const ITEM& item) const;
+	std::set<ITEM> getItems(const TAG& tag) const;
+
+	bool empty() const { return items.empty(); }
+
+    bool hasItem(const ITEM& item) const { return items.find(item) != items.end(); }
+    bool hasTag(const TAG& tag) const { return tags.find(tag) != tags.end(); }
+	std::set<ITEM> getTaggedItems() const;
+	std::set<TAG> getAllTags() const;
+#if 0
+	void output(Consumer<ITEM, TAG>& consumer) const;
+#endif
+	void applyChange(const PatchList<ITEM, TAG>& change);
 };
 
 };



More information about the Debtags-commits mailing list