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. More... | |
Queryable ()=delete | |
Empty constructor. More... | |
Queryable (Queryable &&)=default | |
Move assignment operator. More... | |
Queryable (const Queryable &)=delete | |
Copy constructor operator. More... | |
Queryable & | operator= (const Queryable &)=delete |
Copy assignment operator. More... | |
operator const std::vector< T > && () const | |
Implicit cast operator to vector as rvalue reference. More... | |
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. More... | |
template<typename ActionFn > | |
Queryable< T > | action (ActionFn action_func) |
Executes a void action to create side effects. More... | |
template<typename Predicate > | |
bool | all (Predicate predicate) const |
Determines whether all items of a sequence satisfy a condition. More... | |
template<typename Predicate > | |
bool | any (Predicate predicate) const |
Determines whether a sequence contains any items. More... | |
template<typename Predicate > | |
WhenTrue< T > | branch (Predicate predicate) |
Branches the sequence into two based on a condition. More... | |
Queryable< T > | difference (std::vector< T > rhs_items) |
Produces the set difference of two sequences. More... | |
Queryable< T > | distinct () |
Gets distinct items from a sequence. More... | |
bool | empty () const |
Indicates of the sequence is empty. More... | |
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. More... | |
auto | flatten () |
Projects each item in the sequence-of-sequences and flattens the resulting sequences into one (i.e. vector<vector<T>> -> vector<T>). More... | |
template<typename KeySelector > | |
Queryable< std::vector< T > > | group_by (KeySelector key_selector) |
Groups the items of a sequence. More... | |
Queryable< T > | intersect (const std::vector< T > &rhs_items) |
Produces the set intersection of two sequences. More... | |
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. More... | |
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. More... | |
T | max () |
Gets the maximum item from the sequence. More... | |
template<typename ValueSelector > | |
T | max (ValueSelector value_selector) |
Gets the maximum item from the sequence based on a selected value. More... | |
T | min () |
Gets the minimum item from the sequence. More... | |
template<typename ValueSelector > | |
T | min (ValueSelector value_selector) |
Gets the minimum item from the sequence based on a selected value. More... | |
template<typename ValueSelector > | |
Queryable< T > | order_by (ValueSelector value_selector, bool descending=false) |
Orders the sequence by the selected value. More... | |
Queryable< T > | reverse () |
Inverts the order of the items in the sequence. More... | |
template<typename Selector > | |
auto | select (Selector selector) |
Projects each item of a sequence into a new form. More... | |
Queryable< T > | shuffle () |
Randomizes / shuffles all the item's order in the sequence. More... | |
size_t | size () const |
Gets the size of the sequence. More... | |
Queryable< T > | skip (size_t value) |
Bypasses a specified number of items in the sequence and then returns the remaining items. More... | |
Queryable< T > | slice (size_t start_index, size_t size, size_t stride=1) |
Gets a slice of the sequence. More... | |
Queryable< T > | sort () |
Sorts the items in the sequence. More... | |
Queryable< T > | take (size_t value) |
Takes a specified number of contiguous items from the start of a sequence. More... | |
Queryable< T > | take_random (size_t value) |
Takes the specified number of items from the sequence at random places. More... | |
Queryable< T > | trim (size_t size) |
Trim from the back of the sequence. More... | |
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. More... | |
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. More... | |
std::set< T > | to_set () |
Gets the sequence as a set. More... | |
std::vector< T > | to_vector () |
Gets the sequence as a vector. More... | |
Queryable< T > | unionize (std::vector< T > rhs_items) |
Unions the left hand and right hand side sequences. More... | |
template<typename Predicate > | |
Queryable< T > | where (Predicate predicate) |
Selects items in the sequence that satisfy the predicate / conditional. More... | |
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. More... | |
template<typename U > | |
Queryable< std::tuple< T, U > > | zip (std::initializer_list< U > rhs_items, bool truncate) |
Produces a sequence of tuples with items from the two specified sequences. More... | |
Friends | |
bool | operator== (const Queryable< T > &lhs, const std::vector< T > &rhs) |
Equals overload operator for Queryable<T> against a std::vector<T>. More... | |
bool | operator== (const Queryable< T > &lhs, const Queryable< T > &rhs) |
Equals overload operator. More... | |
Detailed Description
template<typename T>
class fcpp::Queryable< T >
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:
- Sequence refers to an ordered collection that can contain duplicates.
- Item refers to an object in a sequence. It doesn't have any inherit properties.
- Key refers to an object that has equality.
- Value refers to an object that has comparison. It the context of hash tables, it refers to the value part of the key-value pair.
- Remarks
- Note that not all methods are type dependent and will not always be valid. For example the max method will not work on items that don't have comparability. This will show as an error at compile time though.
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
- Template Parameters
-
T Type of items to query over.
Constructor & Destructor Documentation
◆ Queryable() [1/4]
|
explicit |
Construct a new Queryable object from a sequence of items.
- Parameters
-
items Sequence of items to query over.
◆ Queryable() [2/4]
|
delete |
Empty constructor.
- Remarks
- Removed since no-op state is unusable.
◆ Queryable() [3/4]
|
default |
Move assignment operator.
- Remarks
- Allowed since state will be stable as it isn't shared amongst multiple copies.
◆ Queryable() [4/4]
|
delete |
Copy constructor operator.
- Remarks
- Not allowed since state will not be stable as multiple Queryables are moving items.
Member Function Documentation
◆ accumulate()
U fcpp::Queryable< T >::accumulate | ( | U | initial, |
AccumulateFn | accumulate_func | ||
) | const |
Sums all the projected values of the sequence into a single value.
- Template Parameters
-
U Accumulating value type. AccumulateFn std::function<U(U, T)> Transform function type.
- Parameters
-
initial Value to start with before iterating sequence. accumulate_func Transform function to apply to each item.
- Returns
- U The final accumulated value.
◆ action()
Queryable< T > fcpp::Queryable< T >::action | ( | ActionFn | action_func | ) |
Executes a void action to create side effects.
- Parameters
-
action_func Void function to apply to each item.
- Returns
- Queryable<T>
◆ all()
bool fcpp::Queryable< T >::all | ( | Predicate | predicate | ) | const |
Determines whether all items of a sequence satisfy a condition.
- Parameters
-
predicate Function to test each item for a condition.
- Returns
- true if all items satisfy the predicate.
- false if one of the items doesn't satisfy the predicate.
◆ any()
bool fcpp::Queryable< T >::any | ( | Predicate | predicate | ) | const |
Determines whether a sequence contains any items.
- Parameters
-
predicate Function to test each item for a condition.
- Returns
- true if all of the items satisfy the predicate.
- false if none of the items satisfy the predicate.
◆ branch()
WhenTrue< T > fcpp::Queryable< T >::branch | ( | Predicate | predicate | ) |
Branches the sequence into two based on a condition.
- Parameters
-
predicate Function to test each item for a condition.
- Returns
- WhenTrue<T> True / if block query for items that evaluated to true.
◆ difference()
Queryable< T > fcpp::Queryable< T >::difference | ( | std::vector< T > | rhs_items | ) |
Produces the set difference of two sequences.
- Parameters
-
rhs_items Right hand side sequence.
- Returns
- Queryable<T> Sequence of items not found in both sequences.
◆ distinct()
Queryable< T > fcpp::Queryable< T >::distinct |
Gets distinct items from a sequence.
- Returns
- Queryable<T>
◆ empty()
bool fcpp::Queryable< T >::empty |
Indicates of the sequence is empty.
- Returns
- true if the sequence is empty.
- false if the sequence is populated.
◆ first_or_default()
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.
- Parameters
-
predicate Function to test each item for a condition.
- Returns
- std::optional<T> Either the satisfying item or the not found item of std::nullopt
◆ flatten()
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>).
- Returns
- Queryable<U = std::vector<T>>
◆ group_by()
Queryable< std::vector< T > > fcpp::Queryable< T >::group_by | ( | KeySelector | key_selector | ) |
Groups the items of a sequence.
- Template Parameters
-
KeySelector Transform function type. std::function<K(T)>
- Parameters
-
key_selector Transform to key function to apply to each item.
- Returns
- Queryable<std::vector<T>>
◆ intersect()
Queryable< T > fcpp::Queryable< T >::intersect | ( | const std::vector< T > & | rhs_items | ) |
Produces the set intersection of two sequences.
- Parameters
-
rhs_items Right hand side sequence.
- Returns
- Queryable<T> The intersected set that share the same items.
◆ join()
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.
- Template Parameters
-
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)>
- Parameters
-
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.
- Returns
- Queryable<std::tuple<T, U>>
◆ keyed_group_by()
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.
- Template Parameters
-
KeySelector Transform to key function type. std::function<K(T)>
- Parameters
-
key_selector Transform to key function to apply to each item.
- Returns
- Queryable<std::pair<K, std::vector<T>>>
◆ max() [1/2]
T fcpp::Queryable< T >::max |
Gets the maximum item from the sequence.
- Returns
- T
◆ max() [2/2]
T fcpp::Queryable< T >::max | ( | ValueSelector | value_selector | ) |
Gets the maximum item from the sequence based on a selected value.
- Template Parameters
-
ValueSelector Transform to value function type. std::function<V(T)>
- Parameters
-
value_selector Transform to value function to apply to each item.
- Returns
- T
◆ min() [1/2]
T fcpp::Queryable< T >::min |
Gets the minimum item from the sequence.
- Returns
- T
◆ min() [2/2]
T fcpp::Queryable< T >::min | ( | ValueSelector | value_selector | ) |
Gets the minimum item from the sequence based on a selected value.
- Template Parameters
-
ValueSelector Transform to value function type. std::function<V(T)>
- Parameters
-
value_selector Transform to value function to apply to each item.
- Returns
- T
◆ operator const std::vector< T > &&()
|
inline |
Implicit cast operator to vector as rvalue reference.
- Remarks
- Object state will be invalid after this call since items have been moved out.
- Returns
- std::vector<T>
◆ operator=()
|
delete |
Copy assignment operator.
- Remarks
- Not allowed since state will not be stable as multiple Queryables are moving items.
- Returns
- Queryable&
◆ order_by()
Queryable< T > fcpp::Queryable< T >::order_by | ( | ValueSelector | value_selector, |
bool | descending = false |
||
) |
Orders the sequence by the selected value.
- Template Parameters
-
ValueSelector Transform to value function type. std::function<V(T)>
- Parameters
-
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.
- Returns
- Queryable<T>
◆ reverse()
Queryable< T > fcpp::Queryable< T >::reverse |
Inverts the order of the items in the sequence.
- Returns
- Queryable<T>
◆ select()
auto fcpp::Queryable< T >::select | ( | Selector | selector | ) |
Projects each item of a sequence into a new form.
- Template Parameters
-
Selector Transform function type that takes. std::function<V(T)>
- Parameters
-
selector Transform function to apply to each item.
- Returns
- Queryable<decltype(selector(T))>
◆ shuffle()
Queryable< T > fcpp::Queryable< T >::shuffle |
Randomizes / shuffles all the item's order in the sequence.
- Returns
- Queryable<T>
◆ size()
size_t fcpp::Queryable< T >::size |
Gets the size of the sequence.
- Returns
- size_t
◆ skip()
Queryable< T > fcpp::Queryable< T >::skip | ( | size_t | value | ) |
Bypasses a specified number of items in the sequence and then returns the remaining items.
- Parameters
-
value The number of items from the beginning to skip.
- Returns
- Queryable<T>
◆ slice()
Queryable< T > fcpp::Queryable< T >::slice | ( | size_t | start_index, |
size_t | size, | ||
size_t | stride = 1 |
||
) |
Gets a slice of the sequence.
- Remarks
- See std::slice for exact behavior.
- Parameters
-
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.
- Returns
- Queryable<T>
◆ sort()
Queryable< T > fcpp::Queryable< T >::sort |
Sorts the items in the sequence.
- Returns
- Queryable<T>
◆ take()
Queryable< T > fcpp::Queryable< T >::take | ( | size_t | value | ) |
Takes a specified number of contiguous items from the start of a sequence.
- Parameters
-
value The number of items from the beginning to keep.
- Returns
- Queryable<T>
◆ take_random()
Queryable< T > fcpp::Queryable< T >::take_random | ( | size_t | value | ) |
Takes the specified number of items from the sequence at random places.
- Parameters
-
value The number of items to take randomly.
- Returns
- Queryable<T>
◆ to_multi_value_map()
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.
- Template Parameters
-
KeySelector Transform to key function type. std::function<K(T)>
- Parameters
-
key_selector Transform to key function to apply to each item.
- Returns
- std::map<K, std::vector<T>>
◆ to_set()
std::set< T > fcpp::Queryable< T >::to_set |
Gets the sequence as a set.
- Returns
- std::set<T>
◆ to_single_value_map()
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.
- Template Parameters
-
KeySelector Transform to key function type. std::function<K(T)>
- Parameters
-
key_selector Transform to key function to apply to each item.
- Returns
- std::map<K, T>
◆ to_vector()
std::vector< T > fcpp::Queryable< T >::to_vector |
Gets the sequence as a vector.
- Returns
- std::vector<T>
◆ trim()
Queryable< T > fcpp::Queryable< T >::trim | ( | size_t | size | ) |
Trim from the back of the sequence.
- Parameters
-
size The number of items to trim / erase from the back.
- Returns
- Queryable<T>
◆ unionize()
Queryable< T > fcpp::Queryable< T >::unionize | ( | std::vector< T > | rhs_items | ) |
Unions the left hand and right hand side sequences.
- Parameters
-
rhs_items The right hand side sequence to union.
- Returns
- Queryable<T>
◆ where()
Queryable< T > fcpp::Queryable< T >::where | ( | Predicate | predicate | ) |
Selects items in the sequence that satisfy the predicate / conditional.
- Parameters
-
predicate Function to test each item for a condition.
- Returns
- Queryable<T>
◆ zip() [1/2]
Queryable< std::tuple< T, U > > fcpp::Queryable< T >::zip | ( | std::initializer_list< U > | rhs_items, |
bool | truncate | ||
) |
Produces a sequence of tuples with items from the two specified sequences.
- Template Parameters
-
U Type of the right hand side items.
- Parameters
-
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 both T and U must have a default constructor so default values can be provided if one sequence is larger than the other.
- Returns
- Queryable<std::tuple<T, U>>
◆ zip() [2/2]
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.
- Template Parameters
-
U Type of the right hand side items.
- Parameters
-
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 both T and U must have a default constructor so default values can be provided if one sequence is larger than the other.
- Returns
- Queryable<std::tuple<T, U>>
Friends And Related Function Documentation
◆ operator== [1/2]
|
friend |
Equals overload operator.
- Parameters
-
lhs Left operand Queryable<T> to compare. rhs Right operand Queryable<T> to compare.
- Returns
- true if both sides are equal.
- false if both side are not equal.
◆ operator== [2/2]
|
friend |
Equals overload operator for Queryable<T> against a std::vector<T>.
- Remarks
- Based off of internal items_ stored inside Queryable<T>.
- Parameters
-
lhs Left operand Queryable<T> to compare. rhs Right operand std::vector<T> to compare.
- Returns
- true if both sides are equal.
- false if both side are not equal.
The documentation for this class was generated from the following file:
- /home/runner/work/fluentcpp/fluentcpp/src/query.h