coveo::linq
Implementation of .NET-like LINQ operators in C++
Functions
order_by / order_by_descending / then_by / then_by_descending

Orders elements in a sequence. More...

Functions

template<typename KeySelector >
auto coveo::linq::order_by (KeySelector &&key_sel) -> detail::order_by_impl< detail::order_by_comparator< KeySelector, detail::less<>, false >>
 Orders elements in sequence by ascending key. More...
 
template<typename KeySelector , typename Pred >
auto coveo::linq::order_by (KeySelector &&key_sel, Pred &&pred) -> detail::order_by_impl< detail::order_by_comparator< KeySelector, Pred, false >>
 Orders elements in sequence by ascending key using predicate. More...
 
template<typename KeySelector >
auto coveo::linq::order_by_descending (KeySelector &&key_sel) -> detail::order_by_impl< detail::order_by_comparator< KeySelector, detail::less<>, true >>
 Orders elements in sequence by descending key. More...
 
template<typename KeySelector , typename Pred >
auto coveo::linq::order_by_descending (KeySelector &&key_sel, Pred &&pred) -> detail::order_by_impl< detail::order_by_comparator< KeySelector, Pred, true >>
 Orders elements in sequence by descending key using predicate. More...
 
template<typename KeySelector >
auto coveo::linq::then_by (KeySelector &&key_sel) -> decltype(order_by(std::forward< KeySelector >(key_sel)))
 Further orders elements in sequence by ascending key. More...
 
template<typename KeySelector , typename Pred >
auto coveo::linq::then_by (KeySelector &&key_sel, Pred &&pred) -> decltype(order_by(std::forward< KeySelector >(key_sel), std::forward< Pred >(pred)))
 Further orders elements in sequence by ascending key using predicate. More...
 
template<typename KeySelector >
auto coveo::linq::then_by_descending (KeySelector &&key_sel) -> decltype(order_by_descending(std::forward< KeySelector >(key_sel)))
 Further orders elements in sequence by descending key. More...
 
template<typename KeySelector , typename Pred >
auto coveo::linq::then_by_descending (KeySelector &&key_sel, Pred &&pred) -> decltype(order_by_descending(std::forward< KeySelector >(key_sel), std::forward< Pred >(pred)))
 Further orders elements in sequence by descending key using predicate. More...
 

Detailed Description

The order_by operator (and its siblings) extract keys from each element in a sequence, then orders those elements according to those keys. Depending on the operator used, the ordering will be ascending or descending. If multiple operators are chained (using coveo::linq::then_by()), elements will be ordered according to the first key extracted and elements with equivalent keys will be further ordered by the other keys.

.NET equivalent: OrderBy / OrderByDescending / ThenBy / ThenByDescending

Function Documentation

◆ order_by() [1/2]

template<typename KeySelector >
auto coveo::linq::order_by ( KeySelector &&  key_sel) -> detail::order_by_impl<detail::order_by_comparator<KeySelector, detail::less<>, false>>

For each element in the input sequence, extracts a key using the provided key selector. Then, returns a sequence containing the same elements, ordered according to the keys in ascending order.

Keys are compared using operator<().

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 10, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 14, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by([](const coord& c) { return c.x; });
// seq == {
// { -1, 1, 12 },
// { 2, 20, 8 },
// { 3, 3, 3 },
// { 10, 2, -4 },
// { 14, 29, 1 }
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
Returns
(Once applied) Sequence of elements ordered by ascending key.

◆ order_by() [2/2]

template<typename KeySelector , typename Pred >
auto coveo::linq::order_by ( KeySelector &&  key_sel,
Pred &&  pred 
) -> detail::order_by_impl<detail::order_by_comparator<KeySelector, Pred, false>>

For each element in the input sequence, extracts a key using the provided key selector. Then, returns a sequence containing the same elements, ordered according to the keys in ascending order.

Keys are compared using the provided predicate. The predicate must provide a strict ordering of the keys, like std::less.

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 10, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 14, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by([](const coord& c) { return c.x; },
[](int i, int j) { return i < j; });
// seq == {
// { -1, 1, 12 },
// { 2, 20, 8 },
// { 3, 3, 3 },
// { 10, 2, -4 },
// { 14, 29, 1 }
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
predPredicate used to compare the keys.
Returns
(Once applied) Sequence of elements ordered by ascending key.

◆ order_by_descending() [1/2]

template<typename KeySelector >
auto coveo::linq::order_by_descending ( KeySelector &&  key_sel) -> detail::order_by_impl<detail::order_by_comparator<KeySelector, detail::less<>, true>>

For each element in the input sequence, extracts a key using the provided key selector. Then, returns a sequence containing the same elements, ordered according to the keys in descending order.

Keys are compared using operator<(), but the result is reversed to produce descending order.

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 10, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 14, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by_descending([](const coord& c) { return c.x; });
// seq == {
// { 14, 29, 1 }
// { 10, 2, -4 },
// { 3, 3, 3 },
// { 2, 20, 8 },
// { -1, 1, 12 },
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
Returns
(Once applied) Sequence of elements ordered by descending key.

◆ order_by_descending() [2/2]

template<typename KeySelector , typename Pred >
auto coveo::linq::order_by_descending ( KeySelector &&  key_sel,
Pred &&  pred 
) -> detail::order_by_impl<detail::order_by_comparator<KeySelector, Pred, true>>

For each element in the input sequence, extracts a key using the provided key selector. Then, returns a sequence containing the same elements, ordered according to the keys in descending order.

Keys are compared using the provided predicate, but the result is reversed to produce descending order. The predicate must provide a strict ordering of the keys, like std::less.

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 10, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 14, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by_descending([](const coord& c) { return c.x; },
[](int i, int j) { return i < j; });
// seq == {
// { 14, 29, 1 }
// { 10, 2, -4 },
// { 3, 3, 3 },
// { 2, 20, 8 },
// { -1, 1, 12 },
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
predPredicate used to compare the keys.
Returns
(Once applied) Sequence of elements ordered by descending key.

◆ then_by() [1/2]

template<typename KeySelector >
auto coveo::linq::then_by ( KeySelector &&  key_sel) -> decltype(order_by(std::forward<KeySelector>(key_sel)))

Further orders a sequence previously ordered via coveo::linq::order_by() or similar operator. Elements that were previously considered equivalent will be further ordered by ascending order of keys as extracted by the provided key selector.

Keys are compared using operator<().

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 2, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 3, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by([](const coord& c) { return c.x; })
| then_by([](const coord& c) { return c.y; });
// seq == {
// { -1, 1, 12 },
// { 2, 2, -4 },
// { 2, 20, 8 },
// { 3, 3, 3 },
// { 3, 29, 1 }
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
Returns
(Once applied) Sequence of elements further ordered by ascending key.

◆ then_by() [2/2]

template<typename KeySelector , typename Pred >
auto coveo::linq::then_by ( KeySelector &&  key_sel,
Pred &&  pred 
) -> decltype(order_by(std::forward<KeySelector>(key_sel), std::forward<Pred>(pred)))

Further orders a sequence previously ordered via coveo::linq::order_by() or similar operator. Elements that were previously considered equivalent will be further ordered by ascending order of keys as extracted by the provided key selector.

Keys are compared using the provided predicate. The predicate must provide a strict ordering of the keys, like std::less.

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 2, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 3, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by([](const coord& c) { return c.x; },
[](int i, int j) { return i < j; })
| then_by([](const coord& c) { return c.y; },
[](int i, int j) { return i < j; });
// seq == {
// { -1, 1, 12 },
// { 2, 2, -4 },
// { 2, 20, 8 },
// { 3, 3, 3 },
// { 3, 29, 1 }
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
predPredicate used to compare the keys.
Returns
(Once applied) Sequence of elements further ordered by ascending key.

◆ then_by_descending() [1/2]

template<typename KeySelector >
auto coveo::linq::then_by_descending ( KeySelector &&  key_sel) -> decltype(order_by_descending(std::forward<KeySelector>(key_sel)))

Further orders a sequence previously ordered via coveo::linq::order_by() or similar operator. Elements that were previously considered equivalent will be further ordered by descending order of keys as extracted by the provided key selector.

Keys are compared using operator<(), but the result is reversed to produce descending order.

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 2, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 3, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by([](const coord& c) { return c.x; })
| then_by_descending([](const coord& c) { return c.y; });
// seq == {
// { -1, 1, 12 },
// { 2, 20, 8 },
// { 2, 2, -4 },
// { 3, 29, 1 }
// { 3, 3, 3 },
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
Returns
(Once applied) Sequence of elements further ordered by descending key.

◆ then_by_descending() [2/2]

template<typename KeySelector , typename Pred >
auto coveo::linq::then_by_descending ( KeySelector &&  key_sel,
Pred &&  pred 
) -> decltype(order_by_descending(std::forward<KeySelector>(key_sel), std::forward<Pred>(pred)))

Further orders a sequence previously ordered via coveo::linq::order_by() or similar operator. Elements that were previously considered equivalent will be further ordered by descending order of keys as extracted by the provided key selector.

Keys are compared using the provided predicate, but the result is reversed to produce descending order. The predicate must provide a strict ordering of the keys, like std::less.

Use like this:

using coord = std::tuple<int, int, int>;
const std::vector<coord> COORDS = {
{ 2, 2, -4 },
{ 2, 20, 8 },
{ -1, 1, 12 },
{ 3, 29, 1 },
{ 3, 3, 3 },
};
using namespace coveo::linq;
auto seq = from(COORDS)
| order_by([](const coord& c) { return c.x; },
[](int i, int j) { return i < j; })
| then_by_descending([](const coord& c) { return c.y; },
[](int i, int j) { return i < j; });
// seq == {
// { -1, 1, 12 },
// { 2, 20, 8 },
// { 2, 2, -4 },
// { 3, 29, 1 }
// { 3, 3, 3 },
// }
Parameters
key_selKey selector, used to extract a key for a sequence element.
predPredicate used to compare the keys.
Returns
(Once applied) Sequence of elements further ordered by descending key.