Boost.Hana  1.0.2
Your standard library for metaprogramming
boost::hana::set< implementation_defined > Struct Template Reference

Description

template<typename implementation_defined>
struct boost::hana::set< implementation_defined >

Basic unordered container requiring unique, Comparable and Hashable keys.

A set is an unordered container that can hold heterogeneous keys. A set requires (and ensures) that no duplicates are present when inserting new keys.

Note
The actual representation of a hana::set is implementation-defined. In particular, one should not take for granted the order of the template parameters and the presence of any additional constructors or assignment operators than what is documented. The canonical way of creating a hana::set is through hana::make_set.

Modeled concepts

  1. Comparable
    Two sets are equal iff they contain the same elements, regardless of the order.
    // Copyright Louis Dionne 2013-2016
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    namespace hana = boost::hana;
    int main() {
    hana::make_set(hana::int_c<0>, hana::type_c<char>, hana::int_c<1>)
    ==
    hana::make_set(hana::int_c<1>, hana::int_c<0>, hana::type_c<char>)
    );
    hana::make_set(hana::int_c<0>, hana::type_c<char>)
    !=
    hana::make_set(hana::int_c<1>)
    );
    }
  2. Foldable
    Folding a set is equivalent to folding the sequence of its values. However, note that the values are not required to be in any specific order, so using the folds provided here with an operation that is not both commutative and associative will yield non-deterministic behavior.
    // Copyright Louis Dionne 2013-2016
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    namespace hana = boost::hana;
    int main() {
    constexpr auto xs = hana::make_set(hana::int_c<0>, hana::int_c<1>, hana::int_c<2>);
    static_assert(hana::minimum(xs) == hana::int_c<0>, "");
    static_assert(hana::maximum(xs) == hana::int_c<2>, "");
    static_assert(hana::sum<>(xs) == hana::int_c<3>, "");
    }
  3. Searchable
    The elements in a set act as both its keys and its values. Since the elements of a set are unique, searching for an element will return either the only element which is equal to the searched value, or nothing. Also note that operator[] can be used instead of the at_key function.
    // Copyright Louis Dionne 2013-2016
    // Distributed under the Boost Software License, Version 1.0.
    // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
    namespace hana = boost::hana;
    int main() {
    constexpr auto xs = hana::make_set(hana::int_c<0>, hana::int_c<1>, hana::int_c<2>);
    BOOST_HANA_CONSTANT_CHECK(hana::find(xs, hana::int_c<0>) == hana::just(hana::int_c<0>));
    BOOST_HANA_CONSTANT_CHECK(hana::find(xs, hana::int_c<3>) == hana::nothing);
    // operator[] is equivalent to at_key
    BOOST_HANA_CONSTANT_CHECK(xs[hana::int_c<2>] == hana::int_c<2>);
    // long_c<0> == int_<0>, and therefore int_<0> is found
    BOOST_HANA_CONSTANT_CHECK(xs[hana::long_c<0>] == hana::int_c<0>);
    }

Conversion from any Foldable

Any Foldable structure can be converted into a hana::set with to<set_tag>. The elements of the structure must all be compile-time Comparable. If the structure contains duplicate elements, only the first occurence will appear in the resulting set. More specifically, conversion from a Foldable is equivalent to

to<set_tag>(xs) == fold_left(xs, make_set(), insert)

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
int main() {
constexpr auto xs = hana::make_tuple(
hana::int_c<1>,
hana::int_c<3>,
hana::type_c<int>,
hana::long_c<1>
);
hana::to<hana::set_tag>(xs)
==
hana::make_set(hana::int_c<1>, hana::int_c<3>, hana::type_c<int>)
);
}

Synopsis of associated functions

constexpr auto difference
 Returns the set-theoretic difference of two sets. More...
 
constexpr auto intersection
 Returns the intersection of two sets. More...
 
template<>
constexpr auto make< set_tag >
 Function object for creating a hana::set. More...
 
constexpr auto make_set = make<set_tag>
 Equivalent to make<set_tag>; provided for convenience. More...
 
constexpr auto insert
 Insert an element in a hana::set. More...
 
constexpr auto erase_key
 Remove an element from a hana::set. More...
 
constexpr auto to_set = to<set_tag>
 Equivalent to to<set_tag>; provided for convenience. More...
 
constexpr auto symmetric_difference
 Returns the symmetric set-theoretic difference of two sets. More...
 
constexpr auto union_
 Returns the union of two sets. More...
 

Friends

template<typename X , typename Y >
constexpr auto operator== (X &&x, Y &&y)
 Equivalent to hana::equal
 
template<typename X , typename Y >
constexpr auto operator!= (X &&x, Y &&y)
 Equivalent to hana::not_equal
 

Public Member Functions

constexpr set (set const &other)=default
 Copy-construct a set from another set. This constructor only exists when all the elements of the set are copy-constructible.
 
constexpr set (set &&other)=default
 Move-construct a set from another set. This constructor only exists when all the elements of the set are move-constructible.
 
template<typename Key >
decltype(auto) constexpr operator[] (Key &&key)
 Equivalent to hana::at_key
 

Associated functions

template<typename implementation_defined >
constexpr auto difference
related
Initial value:
= [](auto&& xs, auto&& ys) {
return tag-dispatched;
}

Returns the set-theoretic difference of two sets.

Given two sets xs and ys, difference(xs, ys) is a new set containing all the elements of xs that are not contained in ys. For any object x, the following holds:

x ^in^ difference(xs, ys) if and only if x ^in^ xs && !(x ^in^ ys)
Note
This operation is not commutative, i.e. difference(xs, ys) is not necessarily the same as difference(ys, xs). Indeed, consider the case where xs is empty and ys isn't. Then, difference(xs, ys) is empty but difference(ys, xs) is equal to ys. For the symmetric version of this operation, see symmetric_difference.
Parameters
xsA set to remove values from.
ysThe set whose values are removed from xs.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto xs = hana::make_set(hana::int_c<1>, hana::int_c<2>, hana::type_c<int>, hana::int_c<3>);
constexpr auto ys = hana::make_set(hana::int_c<3>, hana::type_c<void>, hana::type_c<int>);
BOOST_HANA_CONSTANT_CHECK(hana::difference(xs, ys) == hana::make_set(hana::int_c<1>, hana::int_c<2>));
BOOST_HANA_CONSTANT_CHECK(hana::difference(ys, xs) == hana::make_set(hana::type_c<void>));
int main() { }
template<typename implementation_defined >
constexpr auto intersection
related
Initial value:
= [](auto&& xs, auto&& ys) {
return tag-dispatched;
}

Returns the intersection of two sets.

Given two sets xs and ys, intersection(xs, ys) is a new set containing exactly those elements that are present both in xs and in ys. In other words, the following holds for any object x:

x ^in^ intersection(xs, ys) if and only if x ^in^ xs && x ^in^ ys
Parameters
xs,ysTwo sets to intersect.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto xs = hana::make_set(hana::int_c<1>, hana::type_c<void>, hana::int_c<2>);
constexpr auto ys = hana::make_set(hana::int_c<2>, hana::type_c<int>, hana::int_c<3>);
BOOST_HANA_CONSTANT_CHECK(hana::intersection(xs, ys) == hana::make_set(hana::int_c<2>));
int main() { }
template<typename implementation_defined >
template<>
constexpr auto make< set_tag >
related
Initial value:
= [](auto&& ...xs) {
return set<implementation_defined>{forwarded(xs)...};
}

Function object for creating a hana::set.

Given zero or more values xs..., make<set_tag> returns a set containing those values. The values must all be compile-time Comparable, and no duplicate values may be provided. To create a set from a sequence with possible duplicates, use to<set_tag> instead.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto xs = hana::make_set(hana::int_c<1>, hana::type_c<void>);
BOOST_HANA_CONSTANT_CHECK(xs == hana::make<hana::set_tag>(hana::int_c<1>, hana::type_c<void>));
int main() { }
template<typename implementation_defined >
constexpr auto make_set = make<set_tag>
related

Equivalent to make<set_tag>; provided for convenience.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto xs = hana::make_set(hana::int_c<1>, hana::type_c<void>);
BOOST_HANA_CONSTANT_CHECK(xs == hana::make<hana::set_tag>(hana::int_c<1>, hana::type_c<void>));
int main() { }
template<typename implementation_defined >
constexpr auto insert
related
Initial value:
= [](auto&& set, auto&& element) {
return tag-dispatched;
}

Insert an element in a hana::set.

If the set already contains an element that compares equal, then nothing is done and the set is returned as is.

Parameters
setThe set in which to insert a value.
elementThe value to insert. It must be compile-time Comparable.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
int main() {
constexpr auto xs = hana::make_set(hana::int_c<0>, hana::type_c<int>);
hana::insert(xs, BOOST_HANA_STRING("abc")) ==
hana::make_set(hana::int_c<0>, hana::type_c<int>, BOOST_HANA_STRING("abc"))
);
hana::insert(xs, hana::int_c<0>) == hana::make_set(hana::int_c<0>, hana::type_c<int>)
);
}
template<typename implementation_defined >
constexpr auto erase_key
related
Initial value:
= [](auto&& set, auto&& element) {
return tag-dispatched;
}

Remove an element from a hana::set.

Returns a new set containing all the elements of the original, except the one comparing equal to the given element. If the set does not contain such an element, a new set equal to the original set is returned.

Parameters
setThe set in which to remove a value.
elementThe value to remove. It must be compile-time Comparable.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
int main() {
constexpr auto xs = hana::make_set(hana::int_c<0>, hana::type_c<int>, hana::type_c<void>);
BOOST_HANA_CONSTANT_CHECK(hana::erase_key(xs, hana::type_c<int>) == hana::make_set(hana::int_c<0>, hana::type_c<void>));
BOOST_HANA_CONSTANT_CHECK(hana::erase_key(xs, hana::type_c<char>) == xs);
}
template<typename implementation_defined >
constexpr auto to_set = to<set_tag>
related

Equivalent to to<set_tag>; provided for convenience.

template<typename implementation_defined >
constexpr auto symmetric_difference
related
Initial value:
= [](auto&& xs, auto&& ys) {
return tag-dispatched;
}

Returns the symmetric set-theoretic difference of two sets.

Given two sets xs and ys, symmetric_difference(xs, ys) is a new set containing all the elements of xs that are not contained in ys, and all the elements of ys that are not contained in xs. The symmetric difference of two sets satisfies the following:

symmetric_difference(xs, ys) == union_(difference(xs, ys), difference(ys, xs))
Parameters
xs,ysTwo sets to compute the symmetric difference of.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto xs = hana::make_set(hana::int_c<1>, hana::int_c<2>, hana::type_c<int>, hana::int_c<3>);
constexpr auto ys = hana::make_set(hana::int_c<3>, hana::type_c<void>, hana::type_c<int>);
hana::symmetric_difference(xs, ys) == hana::make_set(hana::int_c<1>, hana::int_c<2>, hana::type_c<void>)
);
int main() { }
template<typename implementation_defined >
constexpr auto union_
related
Initial value:
= [](auto&& xs, auto&& ys) {
return tag-dispatched;
}

Returns the union of two sets.

Given two sets xs and ys, union_(xs, ys) is a new set containing all the elements of xs and all the elements of ys, without duplicates. For any object x, the following holds:

x ^in^ union_(xs, ys) if and only if x ^in^ xs || x ^in^ ys
Parameters
xs,ysTwo sets to compute the union of.

Example

// Copyright Louis Dionne 2013-2016
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
namespace hana = boost::hana;
constexpr auto xs = hana::make_set(hana::int_c<1>, hana::type_c<void>, hana::int_c<2>);
constexpr auto ys = hana::make_set(hana::int_c<2>, hana::type_c<int>, hana::int_c<3>);
BOOST_HANA_CONSTANT_CHECK(hana::union_(xs, ys) == hana::make_set(
hana::int_c<1>, hana::int_c<2>, hana::int_c<3>, hana::type_c<void>, hana::type_c<int>
));
int main() { }