Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Stroika::Foundation::Containers::Queue< T > Class Template Reference

A Queue is a first-in-first-out (FIFO) data structure, where elements are arranged in well-ordered fashion, from HEAD to TAIL. More...

#include <Queue.h>

Inheritance diagram for Stroika::Foundation::Containers::Queue< T >:
Stroika::Foundation::Traversal::Iterable< T > Stroika::Foundation::Containers::Private::ArrayBasedContainer< Queue_Array< T >, Queue< T >, false > Stroika::Foundation::Containers::Concrete::Queue_DoublyLinkedList< T > Stroika::Foundation::Containers::Deque< T > Stroika::Foundation::Containers::Concrete::Queue_Array< T > Stroika::Foundation::Containers::Concrete::Deque_DoublyLinkedList< T >

Classes

class  _IRep
 Implementation detail for Queue<T> implementors. More...
 

Public Types

using ArchetypeContainerType = Queue
 
using value_type = typename inherited::value_type
 
template<typename T_EQUALS_COMPARER = equal_to<T>>
using EqualsComparer = typename Iterable< T >::template SequentialEqualsComparer< T_EQUALS_COMPARER >
 Simply indirect to @Iterable<T>::SequentialEqualsComparer.
 
- Public Types inherited from Stroika::Foundation::Traversal::Iterable< T >
using value_type = T
 value_type is an alias for the type iterated over - like vector<T>::value_type
 
using iterator = Iterator< T >
 
using const_iterator = Iterator< T >
 

Public Member Functions

 Queue ()
 
nonvirtual void AddTail (ArgByValueType< value_type > item)
 
nonvirtual void push_back (ArgByValueType< value_type > item)
 Add the given item to the end of the Q, so it will be removed last of all the items currently in the Q (stlish alias)
 
nonvirtual value_type Head () const
 
nonvirtual T front () const
 
nonvirtual value_type RemoveHead ()
 
nonvirtual value_type pop_back ()
 
nonvirtual optional< value_typeRemoveHeadIf ()
 
nonvirtual void Enqueue (ArgByValueType< value_type > item)
 add item to the end of the Q (line).
 
nonvirtual value_type Dequeue ()
 Alias for RemoveHead () - remove item from the head of the Q (line).
 
template<IIterableOfTo< T > ITERABLE_OF_ADDABLE>
nonvirtual void AddAllToTail (ITERABLE_OF_ADDABLE &&s)
 
nonvirtual void RemoveAll ()
 
nonvirtual void clear ()
 STL-ish alias for RemoveAll ().
 
nonvirtual bool operator== (const Queue &rhs) const
 
nonvirtual auto operator<=> (const Queue &rhs) const
 
- Public Member Functions inherited from Stroika::Foundation::Traversal::Iterable< T >
 Iterable (const Iterable &) noexcept=default
 Iterable are safely copyable (by value). Since Iterable uses COW, this just copies the underlying pointer and increments the reference count.
 
 Iterable (Iterable &&) noexcept=default
 Iterable are safely moveable.
 
template<IIterableOfTo< T > CONTAINER_OF_T>
requires (not derived_from<remove_cvref_t<CONTAINER_OF_T>, Iterable<T>>)
 Iterable (CONTAINER_OF_T &&from)
 
 Iterable (const initializer_list< T > &from)
 
nonvirtual operator bool () const
 
nonvirtual Iterator< T > MakeIterator () const
 Create an iterator object which can be used to traverse the 'Iterable'.
 
nonvirtual size_t size () const
 Returns the number of items contained.
 
nonvirtual bool empty () const
 Returns true iff size() == 0.
 
template<Common::IPotentiallyComparer< T > EQUALS_COMPARER = equal_to<T>>
nonvirtual bool Contains (ArgByValueType< T > element, EQUALS_COMPARER &&equalsComparer=EQUALS_COMPARER{}) const
 
nonvirtual Iterator< T > begin () const
 Support for ranged for, and STL syntax in general.
 
nonvirtual void Apply (const function< void(ArgByValueType< T > item)> &doToElement, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
 Run the argument function (or lambda) on each element of the container.
 
template<predicate< T > THAT_FUNCTION>
nonvirtual Iterator< T > Find (THAT_FUNCTION &&that, Execution::SequencePolicy seq=Execution::SequencePolicy::eDEFAULT) const
 Run the argument bool-returning function (or lambda) on each element of the container, and return an iterator pointing at the first element (depending on seq) found true. (or use First() to do same thing but return optional<>)
 
template<IIterableOfFrom< T > CONTAINER_OF_T, typename... CONTAINER_OF_T_CONSTRUCTOR_ARGS>
nonvirtual CONTAINER_OF_T As (CONTAINER_OF_T_CONSTRUCTOR_ARGS... args) const
 
nonvirtual T Nth (ptrdiff_t n) const
 Find the Nth element of the Iterable<>
 
nonvirtual T NthValue (ptrdiff_t n, ArgByValueType< T > defaultValue={}) const
 Find the Nth element of the Iterable<>, but allow for n to be out of range, and just return argument default-value.
 
template<derived_from< Iterable< T > > RESULT_CONTAINER = Iterable<T>, predicate< T > INCLUDE_PREDICATE>
nonvirtual RESULT_CONTAINER Where (INCLUDE_PREDICATE &&includeIfTrue) const
 produce a subset of this iterable where argument function returns true
 
template<Common::IPotentiallyComparer< T > EQUALS_COMPARER = equal_to<T>>
nonvirtual Iterable< T > Distinct (EQUALS_COMPARER &&equalsComparer=EQUALS_COMPARER{}) const
 
template<ranges::range RESULT_CONTAINER = Iterable<T>, invocable< T > ELEMENT_MAPPER>
requires (convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, typename RESULT_CONTAINER::value_type> or convertible_to<invoke_result_t<ELEMENT_MAPPER, T>, optional<typename RESULT_CONTAINER::value_type>>)
nonvirtual RESULT_CONTAINER Map (ELEMENT_MAPPER &&elementMapper) const
 functional API which iterates over all members of an Iterable, applies a map function to each element, and collects the results in a new Iterable
 
template<typename REDUCED_TYPE = T>
nonvirtual optional< REDUCED_TYPE > Reduce (const function< REDUCED_TYPE(ArgByValueType< T >, ArgByValueType< T >)> &op) const
 Walk the entire list of items, and use the argument 'op' to combine (reduce) items to a resulting single item.
 
template<typename REDUCED_TYPE = T>
nonvirtual REDUCED_TYPE ReduceValue (const function< REDUCED_TYPE(ArgByValueType< T >, ArgByValueType< T >)> &op, ArgByValueType< REDUCED_TYPE > defaultValue={}) const
 
template<typename RESULT_T = Characters::String, invocable< T > CONVERT_TO_RESULT = decltype (kDefaultToStringConverter<>), invocable< RESULT_T, RESULT_T, bool > COMBINER = decltype (Characters::kDefaultStringCombiner)>
requires (convertible_to<invoke_result_t<CONVERT_TO_RESULT, T>, RESULT_T> and convertible_to<invoke_result_t<COMBINER, RESULT_T, RESULT_T, bool>, RESULT_T>)
nonvirtual RESULT_T Join (const CONVERT_TO_RESULT &convertToResult=kDefaultToStringConverter<>, const COMBINER &combiner=Characters::kDefaultStringCombiner) const
 ape the JavaScript/python 'join' function - take the parts of 'this' iterable and combine them into a new object (typically a string)
 
nonvirtual Iterable< T > Skip (size_t nItems) const
 
nonvirtual Iterable< T > Take (size_t nItems) const
 
nonvirtual Iterable< T > Slice (size_t from, size_t to) const
 
nonvirtual Iterable< T > Top () const
 return the top/largest (possibly just top N) values from this Iterable<T>
 
template<Common::IPotentiallyComparer< T > INORDER_COMPARER_TYPE = less<T>>
nonvirtual Iterable< T > OrderBy (INORDER_COMPARER_TYPE &&inorderComparer=INORDER_COMPARER_TYPE{}, Execution::SequencePolicy seq=Execution::SequencePolicy::ePar) const
 
template<Common::IPotentiallyComparer< T > INORDER_COMPARER_TYPE = less<T>>
nonvirtual bool IsOrderedBy (INORDER_COMPARER_TYPE &&inorderComparer=INORDER_COMPARER_TYPE{}) const
 
nonvirtual optional< T > First () const
 return first element in iterable, or if 'that' specified, first where 'that' is true, (or return nullopt if none)
 
nonvirtual T FirstValue (ArgByValueType< T > defaultValue={}) const
 return first element in iterable provided default
 
nonvirtual optional< T > Last () const
 return last element in iterable, or if 'that' specified, last where 'that' is true, (or return missing)
 
nonvirtual T LastValue (ArgByValueType< T > defaultValue={}) const
 
nonvirtual bool All (const function< bool(ArgByValueType< T >)> &testEachElt) const
 return true iff argument predicate returns true for each element of the iterable
 
nonvirtual optional< T > Min () const
 
template<typename RESULT_TYPE = T>
nonvirtual RESULT_TYPE MinValue (ArgByValueType< RESULT_TYPE > defaultValue={}) const
 
nonvirtual optional< T > Max () const
 
template<typename RESULT_TYPE = T>
nonvirtual RESULT_TYPE MaxValue (ArgByValueType< RESULT_TYPE > defaultValue={}) const
 
template<typename RESULT_TYPE = T>
nonvirtual optional< RESULT_TYPE > Mean () const
 
template<typename RESULT_TYPE = T>
nonvirtual RESULT_TYPE MeanValue (ArgByValueType< RESULT_TYPE > defaultValue={}) const
 
template<typename RESULT_TYPE = T>
nonvirtual optional< RESULT_TYPE > Sum () const
 
template<typename RESULT_TYPE = T>
nonvirtual RESULT_TYPE SumValue (ArgByValueType< RESULT_TYPE > defaultValue={}) const
 
template<constructible_from< T > RESULT_TYPE = T, Common::IPotentiallyComparer< RESULT_TYPE > INORDER_COMPARE_FUNCTION = less<RESULT_TYPE>>
nonvirtual optional< RESULT_TYPE > Median (const INORDER_COMPARE_FUNCTION &compare={}) const
 
template<constructible_from< T > RESULT_TYPE = T>
nonvirtual RESULT_TYPE MedianValue (ArgByValueType< RESULT_TYPE > defaultValue={}) const
 
nonvirtual Iterable< T > Repeat (size_t count) const
 
nonvirtual bool Any () const
 Any() same as not empty (); Any (includeIfTrue) returns true iff includeIfTrue returns true on any values in iterable.
 
nonvirtual size_t Count () const
 with no args, same as size, with function filter arg, returns number of items that pass.
 
nonvirtual size_t length () const
 STL-ish alias for size() - really in STL only used in string, I think, but still makes sense as an alias.
 

Additional Inherited Members

- Static Public Member Functions inherited from Stroika::Foundation::Traversal::Iterable< T >
template<ranges::range LHS_CONTAINER_TYPE, ranges::range RHS_CONTAINER_TYPE, IEqualsComparer< T > EQUALS_COMPARER = equal_to<T>>
static bool SetEquals (const LHS_CONTAINER_TYPE &lhs, const RHS_CONTAINER_TYPE &rhs, EQUALS_COMPARER &&equalsComparer=EQUALS_COMPARER{})
 
template<ranges::range LHS_CONTAINER_TYPE, ranges::range RHS_CONTAINER_TYPE, IEqualsComparer< T > EQUALS_COMPARER = equal_to<T>>
static bool MultiSetEquals (const LHS_CONTAINER_TYPE &lhs, const RHS_CONTAINER_TYPE &rhs, EQUALS_COMPARER &&equalsComparer=EQUALS_COMPARER{})
 
template<ranges::range LHS_CONTAINER_TYPE, ranges::range RHS_CONTAINER_TYPE, IEqualsComparer< T > EQUALS_COMPARER = equal_to<T>>
static bool SequentialEquals (const LHS_CONTAINER_TYPE &lhs, const RHS_CONTAINER_TYPE &rhs, EQUALS_COMPARER &&equalsComparer=EQUALS_COMPARER{}, bool useIterableSize=false)
 
static constexpr default_sentinel_t end () noexcept
 Support for ranged for, and STL syntax in general.
 
- Static Public Attributes inherited from Stroika::Foundation::Traversal::Iterable< T >
template<same_as< Characters::String > RESULT_T = Characters::String>
static const function< RESULT_T(T)> kDefaultToStringConverter
 
- Protected Types inherited from Stroika::Foundation::Traversal::Iterable< T >
using _SharedByValueRepType = Memory::SharedByValue< _IRep, Memory::SharedByValue_Traits< _IRep, shared_ptr< _IRep >, Rep_Cloner_ > >
 Lazy-copying smart pointer mostly used by implementors (can generally be ignored by users). However, protected because manipulation needed in some subclasses (rarely) - like UpdatableIteratable.
 
- Protected Member Functions inherited from Stroika::Foundation::Traversal::Iterable< T >
 Iterable (const shared_ptr< _IRep > &rep) noexcept
 Iterable's are typically constructed as concrete subtype objects, whose CTOR passed in a shared copyable rep.
 
nonvirtual Memory::SharedByValue_State _GetSharingState () const
 
- Protected Attributes inherited from Stroika::Foundation::Traversal::Iterable< T >
_SharedByValueRepType _fRep
 

Detailed Description

template<typename T>
class Stroika::Foundation::Containers::Queue< T >

A Queue is a first-in-first-out (FIFO) data structure, where elements are arranged in well-ordered fashion, from HEAD to TAIL.

The HEAD if the Queue is where elements are removed. The TAIL of the queue is where elements enter the queue.

Queues always iterate from Head (aka front) to Tail: the same order as removals would encounter items, dequeing them.

See also
Sedgewick, 30-31.(

Related classes include Deques, which allow addition and removal at either end, and PriorityQueues, which allow removal based on the priority assigned to an item.

Note
Diagram:
      |   |     |   |     |   |     |   |
FRONT=>| - | | - | | - | | - |<= BACK (HEAD) | 0 | | 1 | | 2 | | 3 | (TAIL) | | | | | | | | items removed from this end new items added here ^^dont access middle^^
See also
Deque<T> - which allow addition and removal at either end
PriorityQueues<T, TRAITS> - which allow removal based on the priority assigned to an item.

Concrete Implementations: o

See also
Concrete::Queue_Array<> o
Concrete::Queue_DoublyLinkedList<>

Factory:

See also
Queue_Factory<> to see default implementations.
Note
Thread-Safety C++-Standard-Thread-Safety
See ReadMe.md for common features of all Stroika containers (especially constructors, iterators, etc)
Comparisons: o static_assert (totally_ordered<T> ==> totally_ordered<Queue<T>>);

o EqualsComparer provided as alias to SequentialEqualsComparer Two Queues are considered equal if they contain the same elements (by comparing them with EQUALS_COMPARER (which defaults to equal_to<T>) in exactly the same order (iteration). o Since ordering in a Queue is well defined, we can use this ordering between elements to define the obvious sequential ordering three way comparison on queues (Iterable::SequentialThreeWayComparer)

Definition at line 95 of file Queue.h.

Member Typedef Documentation

◆ ArchetypeContainerType

template<typename T >
using Stroika::Foundation::Containers::Queue< T >::ArchetypeContainerType = Queue

Use this typedef in templates to recover the basic functional container pattern of concrete types.

Definition at line 103 of file Queue.h.

◆ value_type

template<typename T >
using Stroika::Foundation::Containers::Queue< T >::value_type = typename inherited::value_type
See also
inherited::value_type

Definition at line 109 of file Queue.h.

Constructor & Destructor Documentation

◆ Queue()

template<typename T >
Stroika::Foundation::Containers::Queue< T >::Queue ( )

Construct a new Queue object. Overloads that take an argument container or start/end iterators, iterate from start to end, adding the items to the tail of the Queue, so the resulting Queue will be in the same order (when iterated or dequeued) as the order of the items its created from.

Note
See general information about container constructors that applies here

Definition at line 18 of file Queue.inl.

Member Function Documentation

◆ AddTail()

template<typename T >
void Stroika::Foundation::Containers::Queue< T >::AddTail ( ArgByValueType< value_type item)

Add the given item to the end of the Q, so it will be removed last of all the items currently in the Q.

Note
mutates container
Aliases
Enqueue, push_back

Definition at line 63 of file Queue.inl.

◆ Head()

template<typename T >
auto Stroika::Foundation::Containers::Queue< T >::Head ( ) const
Precondition
not empty ()
See also
HeadIf ()

Definition at line 73 of file Queue.inl.

◆ front()

template<typename T >
nonvirtual T Stroika::Foundation::Containers::Queue< T >::front ( ) const
Aliases
Head
Precondition
not empty ()

Definition at line 78 of file Queue.inl.

◆ RemoveHead()

template<typename T >
auto Stroika::Foundation::Containers::Queue< T >::RemoveHead ( )
Precondition
not empty ()
Note
mutates container

Definition at line 88 of file Queue.inl.

◆ pop_back()

template<typename T >
auto Stroika::Foundation::Containers::Queue< T >::pop_back ( )
Precondition
not empty ()
Note
mutates container

Definition at line 94 of file Queue.inl.

◆ RemoveHeadIf()

template<typename T >
auto Stroika::Foundation::Containers::Queue< T >::RemoveHeadIf ( )
Note
mutates container

Definition at line 99 of file Queue.inl.

◆ Enqueue()

template<typename T >
void Stroika::Foundation::Containers::Queue< T >::Enqueue ( ArgByValueType< value_type item)

add item to the end of the Q (line).

Aliases
AddTail ()
Note
mutates container

Definition at line 104 of file Queue.inl.

◆ Dequeue()

template<typename T >
auto Stroika::Foundation::Containers::Queue< T >::Dequeue ( )

Alias for RemoveHead () - remove item from the head of the Q (line).

Note
mutates container
Precondition
not empty ()

Definition at line 109 of file Queue.inl.

◆ AddAllToTail()

template<typename T >
template<IIterableOfTo< T > ITERABLE_OF_ADDABLE>
nonvirtual void Stroika::Foundation::Containers::Queue< T >::AddAllToTail ( ITERABLE_OF_ADDABLE &&  s)

Items are appended to the tail of the Q in same order they are encountered iterating from start to end of the container (or start to end iterators given).

This also implies that ordering will be preserved in iterating over the Queue, or in Dequeing those elements.

Precondition
IIterableOfTo<ITERABLE_OF_ADDABLE, T> or IInputIterator<T>
Note
This works efficiently because a Queue<> iterates from head to tail, and that's the order in which you would want to add them to copy the Queue (unlike with Stack).
mutates container

◆ RemoveAll()

template<typename T >
void Stroika::Foundation::Containers::Queue< T >::RemoveAll ( )
Note
mutates container

Definition at line 133 of file Queue.inl.

◆ clear()

template<typename T >
void Stroika::Foundation::Containers::Queue< T >::clear ( )

STL-ish alias for RemoveAll ().

Note
mutates container

Definition at line 141 of file Queue.inl.

◆ operator==()

template<typename T >
requires (equality_comparable<T>)
bool Stroika::Foundation::Containers::Queue< T >::operator== ( const Queue< T > &  rhs) const

simply indirect to @Queue<>EqualsComparer

Definition at line 153 of file Queue.inl.

◆ operator<=>()

template<typename T >
requires (three_way_comparable<T>)
auto Stroika::Foundation::Containers::Queue< T >::operator<=> ( const Queue< T > &  rhs) const

simply indirect to @Queue<>ThreeWayComparer (only defined if T::operator<=> is defined)

Definition at line 159 of file Queue.inl.


The documentation for this class was generated from the following files: