![]() |
fluentcpp 0.2
|
Core object used to query items and hold the sequence state. More...
#include <query.h>
Public Types | |
| typedef T | item_type |
| Type of items to query over. Used for type deduction. | |
Public Member Functions | |
| Queryable (std::vector< T > items) | |
| Construct a new Queryable object from a sequence of items. | |
| Queryable ()=delete | |
| Empty constructor. | |
| Queryable (Queryable &&) noexcept=default | |
| Move constructor. | |
| Queryable (const Queryable &)=delete | |
| Copy constructor. | |
| Queryable & | operator= (const Queryable &)=delete |
| Copy assignment operator. | |
| operator std::vector< T > () && | |
| Implicit cast operator to vector from an rvalue. | |
| operator std::vector< T > () const & | |
| Implicit cast operator to vector from an lvalue. | |
| template<typename U, typename AccumulateFn> | |
| U | accumulate (U initial, AccumulateFn accumulate_func) const |
| Sums all the projected values of the sequence into a single value. | |
| template<typename ActionFn> | |
| Queryable< T > | action (ActionFn action_func) |
| Executes a void action to create side effects. | |
| template<typename Predicate> | |
| bool | all (Predicate predicate) const |
| Determines whether all items of a sequence satisfy a condition. | |
| template<typename Predicate> | |
| bool | any (Predicate predicate) const |
| Determines whether a sequence contains any items. | |
| template<typename Predicate> | |
| WhenTrue< T > | branch (Predicate predicate) |
| Branches the sequence into two based on a condition. | |
| Queryable< T > | difference (std::vector< T > rhs_items) |
| Produces the multiset difference of two sequences. | |
| Queryable< T > | distinct () |
| Gets distinct items from a sequence. | |
| bool | empty () const |
| Indicates of the sequence is empty. | |
| template<typename Predicate> | |
| std::optional< T > | first_or_default (Predicate predicate) |
| Gets the first item of a sequence, or a default value if no item is found. | |
| auto | flatten () |
| Projects each item in the sequence-of-sequences and flattens the resulting sequences into one (i.e. vector<vector<T>> -> vector<T>). | |
| template<typename KeySelector> | |
| Queryable< std::vector< T > > | group_by (KeySelector key_selector) |
| Groups the items of a sequence. | |
| Queryable< T > | intersect (std::vector< T > rhs_items) |
| Produces the multiset intersection of two sequences. | |
| template<typename U, typename LhsKeySelector, typename RhsKeySelector> | |
| Queryable< std::tuple< T, U > > | join (std::vector< U > rhs_items, LhsKeySelector lhs_key_selector, RhsKeySelector rhs_key_selector) |
| Correlates the items of two sequences based on matching keys. | |
| template<typename KeySelector> | |
| auto | keyed_group_by (KeySelector key_selector) |
| Groups the items of a sequence by key and produces as a key-group pair sequence. | |
| T | max () |
| Gets the maximum item from the sequence. | |
| template<typename ValueSelector> | |
| T | max (ValueSelector value_selector) |
| Gets the maximum item from the sequence based on a selected value. | |
| T | min () |
| Gets the minimum item from the sequence. | |
| template<typename ValueSelector> | |
| T | min (ValueSelector value_selector) |
| Gets the minimum item from the sequence based on a selected value. | |
| template<typename ValueSelector> | |
| Queryable< T > | order_by (ValueSelector value_selector, bool descending=false) |
| Orders the sequence by the selected value. | |
| Queryable< T > | reverse () |
| Inverts the order of the items in the sequence. | |
| template<typename Selector> | |
| auto | select (Selector selector) |
| Projects each item of a sequence into a new form. | |
| Queryable< T > | shuffle () |
| Randomizes / shuffles all the item's order in the sequence. | |
| size_t | size () const |
| Gets the size of the sequence. | |
| Queryable< T > | skip (size_t value) |
| Bypasses a specified number of items in the sequence and then returns the remaining items. | |
| Queryable< T > | slice (size_t start_index, size_t size, size_t stride=1) |
| Gets a slice of the sequence. | |
| Queryable< T > | sort () |
| Sorts the items in the sequence. | |
| Queryable< T > | take (size_t value) |
| Takes a specified number of contiguous items from the start of a sequence. | |
| Queryable< T > | take_random (size_t value) |
| Takes the specified number of items from the sequence at random places. | |
| Queryable< T > | trim (size_t size) |
| Trim from the back of the sequence. | |
| template<typename KeySelector> | |
| auto | to_multi_value_map (KeySelector key_selector) |
| Groups items by a selected key value and puts them into a hash map. | |
| template<typename KeySelector> | |
| auto | to_single_value_map (KeySelector key_selector) |
| Groups items by selected key value, takes the first item of every group, and puts them into a hash map. | |
| std::set< T > | to_set () |
| Gets the sequence as a set. | |
| std::vector< T > | to_vector () |
| Gets the sequence as a vector. | |
| Queryable< T > | unionize (std::vector< T > rhs_items) |
| Unions the left hand and right hand side sequences. | |
| template<typename Predicate> | |
| Queryable< T > | where (Predicate predicate) |
| Selects items in the sequence that satisfy the predicate / conditional. | |
| template<typename U> | |
| Queryable< std::tuple< T, U > > | zip (std::vector< U > rhs_items, bool truncate=false) |
| Produces a sequence of tuples with items from the two specified sequences. | |
| template<typename U> | |
| Queryable< std::tuple< T, U > > | zip (std::initializer_list< U > rhs_items, bool truncate=false) |
| Produces a sequence of tuples with items from the two specified sequences. | |
Friends | |
| bool | operator== (const Queryable< T > &lhs, const std::vector< T > &rhs) |
| Equals overload operator for Queryable<T> against a std::vector<T>. | |
| bool | operator== (const Queryable< T > &lhs, const Queryable< T > &rhs) |
| Equals overload operator. | |
Core object used to query items and hold the sequence state.
Any operation performed from Queryable<T> will be moved into the new instance of Queryable<T>. This means that any reference to the previous instance of Queryable<T> may have unexpected behavior dependent on the operation performed and memory semantics of the object under query.
The nomenclature used in the documentation is as follows:
In terms of memory semantics, if a signature takes by value and not reference, it is because it makes modifications to the value and doesn't want to cause side effects. To prevent a copy of a single use variable, it must be moved.
Lambdas can be passed with arguments by reference or value depending on the caller's preference. Pass by reference may be the case if the object is large. [](auto x) { return x.y; } // Fundamental type [](auto& x) { return x.y; } // Larger composite type
| T | Type of items to query over. |
|
explicit |
Construct a new Queryable object from a sequence of items.
| items | Sequence of items to query over. |
|
delete |
Empty constructor.
|
defaultnoexcept |
Move constructor.
|
delete |
Copy constructor.
| U fcpp::Queryable< T >::accumulate | ( | U | initial, |
| AccumulateFn | accumulate_func ) const |
Sums all the projected values of the sequence into a single value.
| U | Accumulating value type. |
| AccumulateFn | std::function<U(U, T)> Transform function type. |
| initial | Value to start with before iterating sequence. |
| accumulate_func | Transform function to apply to each item. |
| Queryable< T > fcpp::Queryable< T >::action | ( | ActionFn | action_func | ) |
Executes a void action to create side effects.
| action_func | Void function to apply to each item. |
| bool fcpp::Queryable< T >::all | ( | Predicate | predicate | ) | const |
Determines whether all items of a sequence satisfy a condition.
| predicate | Function to test each item for a condition. |
| bool fcpp::Queryable< T >::any | ( | Predicate | predicate | ) | const |
Determines whether a sequence contains any items.
| predicate | Function to test each item for a condition. |
| WhenTrue< T > fcpp::Queryable< T >::branch | ( | Predicate | predicate | ) |
Branches the sequence into two based on a condition.
| predicate | Function to test each item for a condition. |
| Queryable< T > fcpp::Queryable< T >::difference | ( | std::vector< T > | rhs_items | ) |
Produces the multiset difference of two sequences.
Each right hand side item cancels out at most one equivalent left hand side item. Neither sequence needs to be sorted.
| rhs_items | Right hand side sequence. |
| Queryable< T > fcpp::Queryable< T >::distinct | ( | ) |
Gets distinct items from a sequence.
|
nodiscard |
Indicates of the sequence is empty.
| std::optional< T > fcpp::Queryable< T >::first_or_default | ( | Predicate | predicate | ) |
Gets the first item of a sequence, or a default value if no item is found.
| predicate | Function to test each item for a condition. |
| auto fcpp::Queryable< T >::flatten | ( | ) |
Projects each item in the sequence-of-sequences and flattens the resulting sequences into one (i.e. vector<vector<T>> -> vector<T>).
| Queryable< std::vector< T > > fcpp::Queryable< T >::group_by | ( | KeySelector | key_selector | ) |
Groups the items of a sequence.
| KeySelector | Transform function type. std::function<K(T)> |
| key_selector | Transform to key function to apply to each item. |
| Queryable< T > fcpp::Queryable< T >::intersect | ( | std::vector< T > | rhs_items | ) |
Produces the multiset intersection of two sequences.
Each right hand side item matches at most one equivalent left hand side item. Neither sequence needs to be sorted.
| rhs_items | Right hand side sequence. |
| Queryable< std::tuple< T, U > > fcpp::Queryable< T >::join | ( | std::vector< U > | rhs_items, |
| LhsKeySelector | lhs_key_selector, | ||
| RhsKeySelector | rhs_key_selector ) |
Correlates the items of two sequences based on matching keys.
| U | Type of the right hand side sequence. |
| LhsKeySelector | Transform to key function type. std::function<K(T)> |
| RhsKeySelector | Transform to key function type. std::function<K(U)> |
| rhs_items | The right hand side sequence to join against. |
| lhs_key_selector | Transform to key function to apply to each item in this / left hand side sequence. |
| rhs_key_selector | Transform to key function to apply to each item in the right hand side sequence. |
| auto fcpp::Queryable< T >::keyed_group_by | ( | KeySelector | key_selector | ) |
Groups the items of a sequence by key and produces as a key-group pair sequence.
| KeySelector | Transform to key function type. std::function<K(T)> |
| key_selector | Transform to key function to apply to each item. |
| T fcpp::Queryable< T >::max | ( | ) |
Gets the maximum item from the sequence.
| T fcpp::Queryable< T >::max | ( | ValueSelector | value_selector | ) |
Gets the maximum item from the sequence based on a selected value.
| ValueSelector | Transform to value function type. std::function<V(T)> |
| value_selector | Transform to value function to apply to each item. |
| T fcpp::Queryable< T >::min | ( | ) |
Gets the minimum item from the sequence.
| T fcpp::Queryable< T >::min | ( | ValueSelector | value_selector | ) |
Gets the minimum item from the sequence based on a selected value.
| ValueSelector | Transform to value function type. std::function<V(T)> |
| value_selector | Transform to value function to apply to each item. |
|
inline |
Implicit cast operator to vector from an rvalue.
|
inline |
Implicit cast operator to vector from an lvalue.
|
delete |
Copy assignment operator.
| Queryable< T > fcpp::Queryable< T >::order_by | ( | ValueSelector | value_selector, |
| bool | descending = false ) |
Orders the sequence by the selected value.
The sort is stable and the value selector is called once per item.
| ValueSelector | Transform to value function type. std::function<V(T)> |
| value_selector | Transform to value function to apply to each item. |
| descending | True if to order by greater to smaller values, otherwise smaller to greater. |
| Queryable< T > fcpp::Queryable< T >::reverse | ( | ) |
Inverts the order of the items in the sequence.
| auto fcpp::Queryable< T >::select | ( | Selector | selector | ) |
Projects each item of a sequence into a new form.
| Selector | Transform function type that takes. std::function<V(T)> |
| selector | Transform function to apply to each item. |
| Queryable< T > fcpp::Queryable< T >::shuffle | ( | ) |
Randomizes / shuffles all the item's order in the sequence.
|
nodiscard |
Gets the size of the sequence.
| Queryable< T > fcpp::Queryable< T >::skip | ( | size_t | value | ) |
Bypasses a specified number of items in the sequence and then returns the remaining items.
| value | The number of items from the beginning to skip. |
| Queryable< T > fcpp::Queryable< T >::slice | ( | size_t | start_index, |
| size_t | size, | ||
| size_t | stride = 1 ) |
Gets a slice of the sequence.
| start_index | The initial / starting index into the sequence inclusively. |
| size | The number of the items to retrieve starting at start_index. |
| stride | The length of the skip to the next size items. A stride of zero repeats the item at start_index, which requires T to be copyable. |
| Queryable< T > fcpp::Queryable< T >::sort | ( | ) |
Sorts the items in the sequence.
| Queryable< T > fcpp::Queryable< T >::take | ( | size_t | value | ) |
Takes a specified number of contiguous items from the start of a sequence.
| value | The number of items from the beginning to keep. |
| Queryable< T > fcpp::Queryable< T >::take_random | ( | size_t | value | ) |
Takes the specified number of items from the sequence at random places.
| value | The number of items to take randomly. |
| auto fcpp::Queryable< T >::to_multi_value_map | ( | KeySelector | key_selector | ) |
Groups items by a selected key value and puts them into a hash map.
| KeySelector | Transform to key function type. std::function<K(T)> |
| key_selector | Transform to key function to apply to each item. |
| std::set< T > fcpp::Queryable< T >::to_set | ( | ) |
Gets the sequence as a set.
| auto fcpp::Queryable< T >::to_single_value_map | ( | KeySelector | key_selector | ) |
Groups items by selected key value, takes the first item of every group, and puts them into a hash map.
| KeySelector | Transform to key function type. std::function<K(T)> |
| key_selector | Transform to key function to apply to each item. |
| std::vector< T > fcpp::Queryable< T >::to_vector | ( | ) |
Gets the sequence as a vector.
| Queryable< T > fcpp::Queryable< T >::trim | ( | size_t | size | ) |
Trim from the back of the sequence.
| size | The number of items to trim / erase from the back. |
| Queryable< T > fcpp::Queryable< T >::unionize | ( | std::vector< T > | rhs_items | ) |
Unions the left hand and right hand side sequences.
| rhs_items | The right hand side sequence to union. |
| Queryable< T > fcpp::Queryable< T >::where | ( | Predicate | predicate | ) |
Selects items in the sequence that satisfy the predicate / conditional.
| predicate | Function to test each item for a condition. |
| Queryable< std::tuple< T, U > > fcpp::Queryable< T >::zip | ( | std::initializer_list< U > | rhs_items, |
| bool | truncate = false ) |
Produces a sequence of tuples with items from the two specified sequences.
| U | Type of the right hand side items. |
| rhs_items | Right hand side items of the produced tuple sequence. |
| truncate | Truncate the combined sequence by the minimum size of the two. If false and the sizes differ, the shorter side is padded with default values, which throws std::invalid_argument if that type has no default constructor. |
| Queryable< std::tuple< T, U > > fcpp::Queryable< T >::zip | ( | std::vector< U > | rhs_items, |
| bool | truncate = false ) |
Produces a sequence of tuples with items from the two specified sequences.
| U | Type of the right hand side items. |
| rhs_items | Right hand side items of the produced tuple sequence. |
| truncate | Truncate the combined sequence by the minimum size of the two. If false and the sizes differ, the shorter side is padded with default values, which throws std::invalid_argument if that type has no default constructor. |
|
friend |
Equals overload operator.
| lhs | Left operand Queryable<T> to compare. |
| rhs | Right operand Queryable<T> to compare. |
|
friend |
Equals overload operator for Queryable<T> against a std::vector<T>.
| lhs | Left operand Queryable<T> to compare. |
| rhs | Right operand std::vector<T> to compare. |