coveo::linq
Implementation of .NET-like LINQ operators in C++
Functions
to / to_vector / to_associative / to_map

Converts a sequence into another container type. More...

Functions

template<typename Container >
auto coveo::linq::to () -> detail::to_impl< Container >
 Converts sequence into another container. More...
 
template<typename = void>
auto coveo::linq::to_vector () -> detail::to_vector_impl<>
 Converts sequence into std::vector. More...
 
template<typename Container , typename KeySelector >
auto coveo::linq::to_associative (const KeySelector &key_sel) -> detail::to_associative_impl_1< Container, KeySelector >
 Converts sequence into associative container using key selector. More...
 
template<typename Container , typename KeySelector , typename ElementSelector >
auto coveo::linq::to_associative (const KeySelector &key_sel, const ElementSelector &elem_sel) -> detail::to_associative_impl_2< Container, KeySelector, ElementSelector >
 Converts sequence into associative container using key and element selector. More...
 
template<typename KeySelector >
auto coveo::linq::to_map (const KeySelector &key_sel) -> detail::to_map_impl_1< KeySelector >
 Converts sequence into std::map using key selector. More...
 
template<typename KeySelector , typename ElementSelector >
auto coveo::linq::to_map (const KeySelector &key_sel, const ElementSelector &elem_sel) -> detail::to_map_impl_2< KeySelector, ElementSelector >
 Converts sequence into std::map using key and element selector. More...
 

Detailed Description

The to operator (and its siblings) converts a sequence into another type of container.

This is a terminal operator if the resulting container is not in itself a valid sequence.

.NET equivalent: ToArray / ToDictionary / ToList / ToLookup

Function Documentation

◆ to()

template<typename Container >
auto coveo::linq::to ( ) -> detail::to_impl<Container>

Given a source sequence, returns a new container of the provided type that contains the source sequence's elements.

Use like this:

const int NUMS = { 42, 23, 66, 11, 7 };
using namespace coveo::linq;
auto lst = from(NUMS)
| to<std::list<int>>();
// lst == std::list<int>{ 42, 23, 66, 11, 7 }
Template Parameters
ContainerType of container to convert to.
Returns
(Once applied) Object of type Container storing the same elements as source sequence.

◆ to_vector()

template<typename = void>
auto coveo::linq::to_vector ( ) -> detail::to_vector_impl<>

Given a source sequence, returns a new std::vector that contains the source sequence's elements. The vector's element type is auto-detected from the source sequence.

Use like this:

const int NUMS = { 42, 23, 66, 11, 7 };
using namespace coveo::linq;
auto vec = from(NUMS)
| to_vector();
// vec == std::vector<int>{ 42, 23, 66, 11, 7 }
Returns
(Once applied) std::vector storing the same elements as source sequence.

◆ to_associative() [1/2]

template<typename Container , typename KeySelector >
auto coveo::linq::to_associative ( const KeySelector &  key_sel) -> detail::to_associative_impl_1<Container, KeySelector>

Given a source sequence, returns a new associative container of the provided type that maps each element's key, as extracted by the provided key selector, to the corresponding element.

Use like this:

using our_pair = std::pair<int, std::string>;
using our_mm = std::multimap<int, our_pair>;
const std::vector<our_pair> = {
{ 42, "Life" },
{ 23, "Hangar" },
{ 66, "Route" },
};
using namespace coveo::linq;
auto omp = from(NUMS)
| to_associative<our_mm>([](our_pair p) { return p.first; });
// omp == our_mm{
// 23 => { 23, "Hangar" },
// 42 => { 42, "Life" },
// 66 => { 66, "Route" }
// }
Template Parameters
ContainerType of associative container to convert to.
Parameters
key_selSelector used to extract keys for sequence elements.
Returns
(Once applied) Object of type Container mapping the source sequence's elements as specified.

◆ to_associative() [2/2]

template<typename Container , typename KeySelector , typename ElementSelector >
auto coveo::linq::to_associative ( const KeySelector &  key_sel,
const ElementSelector &  elem_sel 
) -> detail::to_associative_impl_2<Container, KeySelector, ElementSelector>

Given a source sequence, returns a new associative container of the provided type that maps each element's key, as extracted by the provided key selector, to the corresponding mapped value, as extracted by the provided element selector.

Use like this:

using our_pair = std::pair<int, std::string>;
using our_mm = std::multimap<int, std::string>;
const std::vector<our_pair> = {
{ 42, "Life" },
{ 23, "Hangar" },
{ 66, "Route" },
};
using namespace coveo::linq;
auto omp = from(NUMS)
| to_associative<our_mm>([](our_pair p) { return p.first; },
[](our_pair p) { return p.second; });
// omp == our_mm{
// 23 => "Hangar",
// 42 => "Life",
// 66 => "Route"
// }
Template Parameters
ContainerType of associative container to convert to.
Parameters
key_selSelector used to extract keys for sequence elements.
elem_selSelector used to extract mapped values for sequence elements.
Returns
(Once applied) Object of type Container mapping the source sequence's elements as specified.

◆ to_map() [1/2]

template<typename KeySelector >
auto coveo::linq::to_map ( const KeySelector &  key_sel) -> detail::to_map_impl_1<KeySelector>

Given a source sequence, returns a new std::map that maps each element's key, as extracted by the provided key selector, to the corresponding element. The map's type is auto-detected from the source sequence and selector.

Use like this:

using our_pair = std::pair<int, std::string>;
const std::vector<our_pair> = {
{ 42, "Life" },
{ 23, "Hangar" },
{ 66, "Route" },
};
using namespace coveo::linq;
auto omp = from(NUMS)
| to_map([](our_pair p) { return p.first; });
// omp == std::map<int, our_pair>{
// 23 => { 23, "Hangar" },
// 42 => { 42, "Life" },
// 66 => { 66, "Route" }
// }
Parameters
key_selSelector used to extract keys for sequence elements.
Returns
(Once applied) std::map mapping the source sequence's elements as specified.

◆ to_map() [2/2]

template<typename KeySelector , typename ElementSelector >
auto coveo::linq::to_map ( const KeySelector &  key_sel,
const ElementSelector &  elem_sel 
) -> detail::to_map_impl_2<KeySelector, ElementSelector>

Given a source sequence, returns a new std::map that maps each element's key, as extracted by the provided key selector, to the corresponding mapped value, as extracted by the provided element selector. The map's type is auto-detected from the source sequence and selectors.

Use like this:

using our_pair = std::pair<int, std::string>;
const std::vector<our_pair> = {
{ 42, "Life" },
{ 23, "Hangar" },
{ 66, "Route" },
};
using namespace coveo::linq;
auto omp = from(NUMS)
| to_map([](our_pair p) { return p.first; },
[](our_pair p) { return p.second; });
// omp == std::map<int, std::string>{
// 23 => "Hangar",
// 42 => "Life",
// 66 => "Route"
// }
Parameters
key_selSelector used to extract keys for sequence elements.
elem_selSelector used to extract mapped values for sequence elements.
Returns
(Once applied) std::map mapping the source sequence's elements as specified.