Skip to content
Snippets Groups Projects
strset.h 1.92 KiB
Newer Older
ilor's avatar
ilor committed
#ifndef LIBWCCL_VALUES_STRSET_H
#define LIBWCCL_VALUES_STRSET_H

#include <libwccl/values/value.h>
#include <boost/unordered_set.hpp>
#include <libcorpus2/lexeme.h> // for unicodestring hash

namespace Wccl {

/**
 * A Value subtype representing a set of strings.
 *
 * No guarantees for the order of elements are given at this time.
 *
 * By default the set is empty.
 */
ilor's avatar
ilor committed
class StrSet : public Value
{
public:
	WCCL_VALUE_PREAMBLE

	typedef boost::unordered_set<UnicodeString> value_type;
ilor's avatar
ilor committed

ilor's avatar
ilor committed
	StrSet()
		: set_()
	{
	}

	explicit StrSet(const value_type& s)
ilor's avatar
ilor committed
		: set_(s)
	{
	}

	const value_type& get_value() const {
ilor's avatar
ilor committed
		return set_;
	}

	void set_value(const value_type& set) {
ilor's avatar
ilor committed
		set_ = set;
	}

	/**
	 * get_value() alias.
	 */
	const value_type& contents() const {
		return set_;
	/**
	 * Nonconst variant of get_value()
	 */
	value_type& contents() {
		return set_;
ilor's avatar
ilor committed
	void swap(StrSet& ss) {
		ss.set_.swap(set_);
	}

	/// Convenience function to add a new UnicodeString to the set
ilor's avatar
ilor committed
	void insert(const UnicodeString& u) {
		set_.insert(u);
	}

	/// Convenience function to add a new string to the set, treated as UTF-8
ilor's avatar
ilor committed
	void insert_utf8(const std::string& u) {
		insert(UnicodeString::fromUTF8(u));
	}

	/// Convenience size accesor
ilor's avatar
ilor committed
	int size() const {
		return set_.size();
	}

	/// Convenience empty checker
	/**
	 * @return true if each string from this set exists in the other set
	 *         (note that an empty set is a subset of anything)
	 */
	/**
	 * @return true if there is at least one common string between this set and
	 *         the other set (an empty set intersects with nothing)
	 */
	bool equals(const StrSet& other) const {
		return set_ == other.set_;
ilor's avatar
ilor committed
	/// Value override
	std::string to_raw_string() const;

private:
ilor's avatar
ilor committed
};

} /* end ns Wccl */

#endif // LIBWCCL_VALUES_STRSET_H