[Debtags-commits] [svn] r1854 - in tagcoll/2.0: . tagcoll/stream tools

Enrico Zini enrico at costa.debian.org
Tue Aug 22 17:02:16 UTC 2006


Author: enrico
Date: Tue Aug 22 17:02:15 2006
New Revision: 1854

Modified:
   tagcoll/2.0/   (props changed)
   tagcoll/2.0/tagcoll/stream/filters-tut.cc
   tagcoll/2.0/tagcoll/stream/filters.h
   tagcoll/2.0/tools/TagcollParser.h
   tagcoll/2.0/tools/tagcoll.cc
Log:
 r3188 at viaza:  enrico | 2006-08-22 18:01:55 +0100
 Added new tagCounter stream filter, and used it to implement --count in tagcoll


Modified: tagcoll/2.0/tagcoll/stream/filters-tut.cc
==============================================================================
--- tagcoll/2.0/tagcoll/stream/filters-tut.cc	(original)
+++ tagcoll/2.0/tagcoll/stream/filters-tut.cc	Tue Aug 22 17:02:15 2006
@@ -185,6 +185,30 @@
 	ensure_equals(count2Tags, 4);
 }
 
+template<> template<>
+void to::test<8>()
+{
+	std::string input_coll(
+			"a: a1, a2, a3\n"
+			"b: \n"
+			"c: b1, b2, b3, b4, b5\n"
+			);
+	coll::Simple<string, unsigned int> result;
+	parseCollection(input_coll, tagCounter(inserter(result))); 
+
+	std::set<unsigned int> tagset;
+
+	tagset = result.getTagsOfItem("a");
+	ensure_equals(tagset.size(), 1u);
+	ensure_equals(*tagset.begin(), 3);
+	tagset = result.getTagsOfItem("b");
+	ensure_equals(tagset.size(), 1u);
+	ensure_equals(*tagset.begin(), 0);
+	tagset = result.getTagsOfItem("c");
+	ensure_equals(tagset.size(), 1u);
+	ensure_equals(*tagset.begin(), 5);
+}
+
 }
 
 #include <tagcoll/tests/test-utils.tcc>

Modified: tagcoll/2.0/tagcoll/stream/filters.h
==============================================================================
--- tagcoll/2.0/tagcoll/stream/filters.h	(original)
+++ tagcoll/2.0/tagcoll/stream/filters.h	Tue Aug 22 17:02:15 2006
@@ -212,6 +212,34 @@
 	return UngroupItems<OUT>(out);
 }
 
+
+/**
+ * Replace the tagsets with a wibble::Singleton<unsigned int> that contains
+ * their size
+ */
+template<typename OUT>
+class TagCounter : public wibble::mixin::OutputIterator< TagCounter<OUT> >
+{
+	OUT out;
+
+public:
+	TagCounter(const OUT& out) : out(out) {}
+
+	template<typename ITEMS, typename TAGS>
+	TagCounter<OUT>& operator=(const std::pair<ITEMS, TAGS>& data)
+	{
+		*out = make_pair(data.first, wibble::singleton(data.second.size()));
+		++out;
+		return *this;
+	}
+};
+
+template<typename OUT>
+TagCounter<OUT> tagCounter(const OUT& out)
+{
+	return TagCounter<OUT>(out);
+}
+
 }
 }
 

Modified: tagcoll/2.0/tools/TagcollParser.h
==============================================================================
--- tagcoll/2.0/tools/TagcollParser.h	(original)
+++ tagcoll/2.0/tools/TagcollParser.h	Tue Aug 22 17:02:15 2006
@@ -41,6 +41,7 @@
 	BoolOption* out_group;
 	BoolOption* out_redundant;
 	BoolOption* out_itemsOnly;
+	BoolOption* out_count;
 
 	// Hierarchy
 	IntOption* hie_flatten;
@@ -101,6 +102,8 @@
 						"when implications are provided, expand them explicitly in the output");
 		out_itemsOnly = group->add<BoolOption>("items", 'i', "items", "",
 						"output only the names of the items, without the tags");
+		out_count = group->add<BoolOption>("count", 'c', "count", "",
+						"output the count of tags instead of the tags");
 		return group;
 	}
 

Modified: tagcoll/2.0/tools/tagcoll.cc
==============================================================================
--- tagcoll/2.0/tools/tagcoll.cc	(original)
+++ tagcoll/2.0/tools/tagcoll.cc	Tue Aug 22 17:02:15 2006
@@ -58,6 +58,7 @@
 
 #include <algorithm>
 #include <iostream>
+#include <sstream>
 
 #include "TagcollParser.h"
 
@@ -248,6 +249,45 @@
 	}
 }
 
+
+/**
+ * Serialize tags to strings
+ */
+template<typename OUT>
+class TagFormatter : public wibble::mixin::OutputIterator< TagFormatter<OUT> >
+{
+	OUT out;
+
+	template<typename T>
+	std::string format(const T& value)
+	{
+		std::stringstream str;
+		str << value;
+		return str.str();
+	}
+
+public:
+	TagFormatter(const OUT& out) : out(out) {}
+
+	template<typename ITEMS, typename TAGS>
+	TagFormatter<OUT>& operator=(const std::pair<ITEMS, TAGS>& data)
+	{
+		std::set<std::string> formatted;
+		for (typename TAGS::const_iterator i = data.second.begin();
+				i != data.second.end(); ++i)
+			formatted.insert(format(*i));
+		*out = make_pair(data.first, formatted);
+		++out;
+		return *this;
+	}
+};
+
+template<typename OUT>
+TagFormatter<OUT> tagFormatter(const OUT& out)
+{
+	return TagFormatter<OUT>(out);
+}
+
 // Break filtering into generic steps that are abstracted by a virtual function
 
 class FilterStep
@@ -463,6 +503,11 @@
 				return filterStep(stream::itemsOnly(inserter(grouper)));
 			else
 				return filterStep(stream::itemsOnly(stream::ungroupItems(textformat::StdioWriter(stdout))));
+		else if (opts.out_count->boolValue())
+			if (opts.out_group->boolValue())
+				return filterStep(stream::tagCounter(tagFormatter(inserter(grouper))));
+			else
+				return filterStep(stream::tagCounter(tagFormatter(stream::ungroupItems(textformat::StdioWriter(stdout)))));
 		else
 			if (opts.out_group->boolValue())
 				return filterStep(inserter(grouper));



More information about the Debtags-commits mailing list