#include <libwccl/values/strset.h>
#include <libpwrutils/foreach.h>
#include <libpwrutils/util.h>
#include <sstream>


namespace Wccl {

const char* StrSet::type_name = "StrSet";

std::string StrSet::to_raw_string() const
{
	std::stringstream 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 << "]";
	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.
	//it's faster to iterate through the smaller set and check in
	//the larger than it is to do the opposite, hence the &?: below
	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 (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 */