The Searchable
concept represents structures that can be searched.
Intuitively, a Searchable
is any structure, finite or infinite, containing elements that can be searched using a predicate. Sometimes, Searchable
s will associate keys to values; one can search for a key with a predicate, and the value associated to it is returned. This gives rise to map-like data structures. Other times, the elements of the structure that are searched (i.e. those to which the predicate is applied) are the same that are returned, which gives rise to set-like data structures. In general, we will refer to the keys of a Searchable
structure as those elements that are used for searching, and to the values of a Searchable
as those elements that are returned when a search is successful. As was explained, there is no requirement that both notions differ, and it is often useful to have keys and values coincide (think about std::set
).
Some methods like any_of
, all_of
and none_of
allow simple queries to be performed on the keys of the structure, while other methods like find
and find_if
make it possible to find the value associated to a key. The most specific method should always be used if one cares about performance, because it is usually the case that heavy optimizations can be performed in more specific methods. For example, an associative data structure implemented as a hash table will be much faster to access using find
than find_if
, because in the second case it will have to do a linear search through all the entries. Similarly, using contains
will likely be much faster than any_of
with an equivalent predicate.
Insight
In a lazy evaluation context, anyFoldable
can also become a model ofSearchable
because we can search lazily through the structure withfold_right
. However, in the context of C++, someSearchable
s can not be folded; think for example of an infinite set.
find_if
and any_of
When find_if
and any_of
are provided, the other functions are implemented according to the laws explained below.
any_of(xs, pred)
by checking whether find_if(xs, pred)
is an empty optional
or not, and then reduce the minimal complete definition to find_if
. However, this is not done because that implementation requires the predicate of any_of
to return a compile-time Logical
, which is more restrictive than what we have right now.In order for the semantics of the methods to be consistent, some properties must be satisfied by any model of the Searchable
concept. Rigorously, for any Searchable
s xs
and ys
and any predicate p
, the following laws should be satisfied:
Additionally, if all the keys of the Searchable
are Logical
s, the following laws should be satisfied:
hana::map
, hana::optional
, hana::range
, hana::set
, hana::string
, hana::tuple
Builtin arrays whose size is known can be searched as-if they were homogeneous tuples. However, since arrays can only hold objects of a single type and the predicate to find_if
must return a compile-time Logical
, the find_if
method is fairly useless. For similar reasons, the find
method is also fairly useless. This model is provided mainly because of the any_of
method & friends, which are both useful and compile-time efficient.
Given two Searchables
S1
and S2
, a function \( f : S_1(X) \to S_2(X) \) is said to preserve the Searchable
structure if for all xs
of data type S1(X)
and predicates \( \mathtt{pred} : X \to Bool \) (for a Logical
Bool
),
This is really just a generalization of the following, more intuitive requirements. For all xs
of data type S1(X)
and x
of data type X
,
These requirements can be understood as saying that f
does not change the content of xs
, although it may reorder elements. As usual, such a structure-preserving transformation is said to be an embedding if it is also injective, i.e. if it is a lossless transformation.
Variables | |
constexpr auto | boost::hana::all |
Returns whether all the keys of the structure are true-valued.The keys of the structure must be Logical s. If the structure is not finite, a false-valued key must appear at a finite "index" in order for this method to finish. More... | |
constexpr auto | boost::hana::all_of |
Returns whether all the keys of the structure satisfy the predicate .If the structure is not finite, predicate has to return a false- valued Logical after looking at a finite number of keys for this method to finish. More... | |
constexpr auto | boost::hana::any |
Returns whether any key of the structure is true-valued.The keys of the structure must be Logical s. If the structure is not finite, a true-valued key must appear at a finite "index" in order for this method to finish. More... | |
constexpr auto | boost::hana::any_of |
Returns whether any key of the structure satisfies the predicate .If the structure is not finite, predicate has to be satisfied after looking at a finite number of keys for this method to finish. More... | |
constexpr auto | boost::hana::at_key |
Returns the value associated to the given key in a structure, or fail.Given a key and a Searchable structure, at_key returns the first value whose key is equal to the given key , and fails at compile-time if no such key exists. This requires the key to be compile-time Comparable , exactly like for find . at_key satisfies the following: More... | |
constexpr auto | boost::hana::contains |
Returns whether the key occurs in the structure.Given a Searchable structure xs and a key , contains returns whether any of the keys of the structure is equal to the given key . If the structure is not finite, an equal key has to appear at a finite position in the structure for this method to finish. For convenience, contains can also be applied in infix notation. More... | |
constexpr auto | boost::hana::in = hana::infix(hana::flip(hana::contains)) |
Return whether the key occurs in the structure.Specifically, this is equivalent to contains , except in takes its arguments in reverse order. Like contains , in can also be applied in infix notation for increased expressiveness. This function is not a method that can be overriden; it is just a convenience function provided with the concept. More... | |
constexpr auto | boost::hana::find |
Finds the value associated to the given key in a structure.Given a key and a Searchable structure, find returns the just the first value whose key is equal to the given key , or nothing if there is no such key. Comparison is done with equal . find satisfies the following: More... | |
constexpr auto | boost::hana::find_if |
Finds the value associated to the first key satisfying a predicate.Given a Searchable structure xs and a predicate pred , find_if(xs, pred) returns just the first element whose key satisfies the predicate, or nothing if there is no such element. More... | |
constexpr auto | boost::hana::is_disjoint |
Returns whether two Searchable s are disjoint.Given two Searchable s xs and ys , is_disjoint returns a Logical representing whether the keys in xs are disjoint from the keys in ys , i.e. whether both structures have no keys in common. More... | |
constexpr auto | boost::hana::is_subset |
Returns whether a structure contains a subset of the keys of another structure.Given two Searchable s xs and ys , is_subset returns a Logical representing whether xs is a subset of ys . In other words, it returns whether all the keys of xs are also present in ys . This method does not return whether xs is a strict subset of ys ; if xs and ys are equal, all the keys of xs are also present in ys , and is_subset returns true. More... | |
constexpr auto | boost::hana::none |
Returns whether all of the keys of the structure are false-valued.The keys of the structure must be Logical s. If the structure is not finite, a true-valued key must appear at a finite "index" in order for this method to finish. More... | |
constexpr auto | boost::hana::none_of |
Returns whether none of the keys of the structure satisfy the predicate .If the structure is not finite, predicate has to return a true- valued Logical after looking at a finite number of keys for this method to finish. More... | |
constexpr auto boost::hana::all |
#include <boost/hana/fwd/all.hpp>
Returns whether all the keys of the structure are true-valued.The keys of the structure must be Logical
s. If the structure is not finite, a false-valued key must appear at a finite "index" in order for this method to finish.
constexpr auto boost::hana::all_of |
#include <boost/hana/fwd/all_of.hpp>
Returns whether all the keys of the structure satisfy the predicate
.If the structure is not finite, predicate
has to return a false- valued Logical
after looking at a finite number of keys for this method to finish.
xs | The structure to search. |
predicate | A function called as predicate(k) , where k is a key of the structure, and returning a Logical . |
constexpr auto boost::hana::any |
#include <boost/hana/fwd/any.hpp>
Returns whether any key of the structure is true-valued.The keys of the structure must be Logical
s. If the structure is not finite, a true-valued key must appear at a finite "index" in order for this method to finish.
constexpr auto boost::hana::any_of |
#include <boost/hana/fwd/any_of.hpp>
Returns whether any key of the structure satisfies the predicate
.If the structure is not finite, predicate
has to be satisfied after looking at a finite number of keys for this method to finish.
xs | The structure to search. |
predicate | A function called as predicate(k) , where k is a key of the structure, and returning a Logical . |
constexpr auto boost::hana::at_key |
#include <boost/hana/fwd/at_key.hpp>
Returns the value associated to the given key in a structure, or fail.Given a key
and a Searchable
structure, at_key
returns the first value whose key is equal to the given key
, and fails at compile-time if no such key exists. This requires the key
to be compile-time Comparable
, exactly like for find
. at_key
satisfies the following:
If the Searchable
actually stores the elements it contains, at_key
is required to return a lvalue reference, a lvalue reference to const or a rvalue reference to the found value, where the type of reference must match that of the structure passed to at_key
. If the Searchable
does not store the elements it contains (i.e. it generates them on demand), this requirement is dropped.
xs | The structure to be searched. |
key | A key to be searched for in the structure. The key has to be Comparable with the other keys of the structure. In the current version of the library, the comparison of key with any other key of the structure must return a compile-time Logical . |
constexpr auto boost::hana::contains |
#include <boost/hana/fwd/contains.hpp>
Returns whether the key occurs in the structure.Given a Searchable
structure xs
and a key
, contains
returns whether any of the keys of the structure is equal to the given key
. If the structure is not finite, an equal key has to appear at a finite position in the structure for this method to finish. For convenience, contains
can also be applied in infix notation.
xs | The structure to search. |
key | A key to be searched for in the structure. The key has to be Comparable with the other keys of the structure. |
constexpr auto boost::hana::in = hana::infix(hana::flip(hana::contains)) |
#include <boost/hana/fwd/contains.hpp>
Return whether the key occurs in the structure.Specifically, this is equivalent to contains
, except in
takes its arguments in reverse order. Like contains
, in
can also be applied in infix notation for increased expressiveness. This function is not a method that can be overriden; it is just a convenience function provided with the concept.
constexpr auto boost::hana::find |
#include <boost/hana/fwd/find.hpp>
Finds the value associated to the given key in a structure.Given a key
and a Searchable
structure, find
returns the just
the first value whose key is equal to the given key
, or nothing
if there is no such key. Comparison is done with equal
. find
satisfies the following:
xs | The structure to be searched. |
key | A key to be searched for in the structure. The key has to be Comparable with the other keys of the structure. In the current version of the library, the comparison of key with any other key of the structure must return a compile-time Logical . |
constexpr auto boost::hana::find_if |
#include <boost/hana/fwd/find_if.hpp>
Finds the value associated to the first key satisfying a predicate.Given a Searchable
structure xs
and a predicate pred
, find_if(xs, pred)
returns just
the first element whose key satisfies the predicate, or nothing
if there is no such element.
xs | The structure to be searched. |
predicate | A function called as predicate(k) , where k is a key of the structure, and returning whether k is the key of the element being searched for. In the current version of the library, the predicate has to return an IntegralConstant holding a value that can be converted to bool . |
constexpr auto boost::hana::is_disjoint |
#include <boost/hana/fwd/is_disjoint.hpp>
Returns whether two Searchable
s are disjoint.Given two Searchable
s xs
and ys
, is_disjoint
returns a Logical
representing whether the keys in xs
are disjoint from the keys in ys
, i.e. whether both structures have no keys in common.
xs,ys | Two Searchable s to test for disjointness. |
constexpr auto boost::hana::is_subset |
#include <boost/hana/fwd/is_subset.hpp>
Returns whether a structure contains a subset of the keys of another structure.Given two Searchable
s xs
and ys
, is_subset
returns a Logical
representing whether xs
is a subset of ys
. In other words, it returns whether all the keys of xs
are also present in ys
. This method does not return whether xs
is a strict subset of ys
; if xs
and ys
are equal, all the keys of xs
are also present in ys
, and is_subset
returns true.
is_subset
can also be applied in infix notation.This method is tag-dispatched using the tags of both arguments. It can be called with any two Searchable
s sharing a common Searchable
embedding, as defined in the main documentation of the Searchable
concept. When Searchable
s with two different tags but sharing a common embedding are sent to is_subset
, they are first converted to this common Searchable
and the is_subset
method of the common embedding is then used. Of course, the method can be overriden for custom Searchable
s for efficieny.
is_subset
is supported, it is not currently used by the library because there are no models of Searchable
with a common embedding.xs | The structure to check whether it is a subset of ys . |
ys | The structure to check whether it is a superset of xs . |
constexpr auto boost::hana::none |
#include <boost/hana/fwd/none.hpp>
Returns whether all of the keys of the structure are false-valued.The keys of the structure must be Logical
s. If the structure is not finite, a true-valued key must appear at a finite "index" in order for this method to finish.
constexpr auto boost::hana::none_of |
#include <boost/hana/fwd/none_of.hpp>
Returns whether none of the keys of the structure satisfy the predicate
.If the structure is not finite, predicate
has to return a true- valued Logical
after looking at a finite number of keys for this method to finish.
xs | The structure to search. |
predicate | A function called as predicate(k) , where k is a key of the structure, and returning a Logical . |