From fc92660c5877151f80c4a034036c413bb7fea98e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adam=20Wardy=C5=84ski?= <no@email>
Date: Tue, 9 Nov 2010 16:13:14 +0100
Subject: [PATCH] equals, is_subset_of and intersects for StrSet, changing its
 to_raw_string to match spec in parser

---
 libwccl/values/strset.cpp | 43 +++++++++++++++++++++++++++++++++------
 libwccl/values/strset.h   | 15 ++++----------
 2 files changed, 41 insertions(+), 17 deletions(-)

diff --git a/libwccl/values/strset.cpp b/libwccl/values/strset.cpp
index 4823999..ed6b4bc 100644
--- a/libwccl/values/strset.cpp
+++ b/libwccl/values/strset.cpp
@@ -11,15 +11,46 @@ const char* StrSet::type_name = "StrSet";
 std::string StrSet::to_raw_string() const
 {
 	std::stringstream ss;
-	ss << "{";
-	bool comma = false;
-	foreach (const UnicodeString& u, set_) {
-		if (comma) {
-			ss << ",";
+	ss << "[";
+	set_t::const_iterator it = set_.begin();
+	while(it != set_.end()) {
+		ss << '\"' << PwrNlp::to_utf8(*it) << '\"'; //TODO escaping
+		if(++it != set_.end()) {
+			ss << ", ";
 		}
-		ss << '\"' << PwrNlp::to_utf8(u) << '\"'; //TODO escaping
 	}
+	ss << "]";
 	return ss.str();
 }
 
+bool StrSet::intersects(const StrSet &other) const {
+	if(empty() || other.empty()) {
+		return false;
+	}
+	//we just want to check if there is an intersection, no
+	//need to actually compute it to check if it's empty.
+	//doing it like below sounds faster than, say, sorting
+	//the sets and using set_intersection
+	const set_t& smaller = size() < other.size() ? set_ : other.set_;
+	const set_t& bigger = size() < other.size() ? other.set_ : set_;
+	foreach (const UnicodeString& u, smaller) {
+		if(bigger.find(u) != bigger.end()) {
+			return true;
+		}
+	}
+	return false;
+}
+
+bool StrSet::is_subset_of(const StrSet &other) const {
+	if(empty() || size() > other.size()) {
+		return false;
+	}
+	foreach (const UnicodeString& u, set_) {
+		if(other.set_.find(u) == other.set_.end()) {
+			return false;
+		}
+	}
+	return true;
+}
+
 } /* end ns Wccl */
diff --git a/libwccl/values/strset.h b/libwccl/values/strset.h
index ec6b48f..e984bd0 100644
--- a/libwccl/values/strset.h
+++ b/libwccl/values/strset.h
@@ -56,19 +56,12 @@ public:
 		return set_.empty();
 	}
 
-	bool is_subset_of(const StrSet& /*set*/) const {
-		//TODO: implement this
-		return false;
-	}
+	bool is_subset_of(const StrSet& other) const;
 
-	bool intersects(const StrSet& /*set*/) const {
-		//TODO: implement this
-		return false;
-	}
+	bool intersects(const StrSet& other) const;
 
-	bool equals(const StrSet& /*set*/) const {
-		//TODO: implement this
-		return false;
+	bool equals(const StrSet& other) const {
+		return set_ == other.set_;
 	}
 
 	/// Value override
-- 
GitLab