xtd 0.2.0
Loading...
Searching...
No Matches
xtd::collections::generic::stack< type_t, container_t > Class Template Reference
Inheritance diagram for xtd::collections::generic::stack< type_t, container_t >:
xtd::object

Definition

template<class type_t, class container_t = std::deque<type_t>>
class xtd::collections::generic::stack< type_t, container_t >

Represents a last-in, first-out collection of objects.

Definition
template<class type_t, class container_t = std::deque<type_t>>
class stack : public xtd::object, public xtd::collections::generic::icollection<type_t>;
stack()=default
Initializes a new instance of the xtd::collections::generic::stack <type_t> class that is empty and h...
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
Header
#include <xtd/collections/generic/stack>
Namespace
xtd::collections::generic
Library
xtd.core
Remarks
This class implements a generic stack as a circular array. Objects stored in a xtd::collections::generic::stack <type_t> are inserted at one end and removed from the other. Queues and stacks are useful when you need temporary storage for information; that is, when you might want to discard an element after retrieving its value. Use xtd::collections::generic::stack <type_t> if you need to access the information in the same order that it is stored in the collection. Use xtd::collections::generic::stack <type_t> if you need to access the information in reverse order. Use xtd::collections::generic::concurrent_queue <type_t> or xtd::collections::generic::concurrent_stack <type_t> if you need to access the collection from multiple threads concurrently.
Three main operations can be performed on a xtd::collections::generic::stack <type_t> and its elements:
The capacity of a xtd::collections::generic::stack <type_t> is the number of elements the xtd::collections::generic::stack <type_t> can hold. As elements are added to a xtd::collections::generic::stack <type_t>, the capacity is automatically increased as required by reallocating the internal array. The capacity can be decreased by calling xtd::collections::generic::stack::trim_excess.
xtd::collections::generic::stack <type_t> allows duplicate elements.
Template Parameters
type_tThe value type of the generic container.
container_tThe underlying container type used by xtd::collections::generic::helpers::raw_stack. The default container type is std::deque.
Examples
The following code example demonstrates several methods of the xtd::collections::generic::stack <type_t> generic class. The code example creates a stack of strings with default capacity and uses the xtd::collections::generic::stack::push method to stack five strings. The elements of the stack are enumerated, which does not change the state of the stack. The xtd::collections::generic::stack::pop method is used to pop the first string. The xtd::collections::generic::stack::peek method is used to look at the next item in the stack, and then the xtd::collections::generic::stack::pop method is used to pop it. The xtd::collections::generic::stack::to_array method is used to create an array and copy the stack elements to it, then the array is passed to the xtd::collections::generic::stack <type_t> constructor that takes xtd::collections::generic::ienumerable <type_t>, creating a copy of the stack. The elements of the copy are displayed. An array twice the size of the stack is created, and the xtd::collections::generic::stack::copy_to method is used to copy the array elements beginning at the middle of the array. The xtd::collections::generic::stack <type_t> constructor is used again to create a second copy of the stack containing three null elements at the beginning. The xtd::collections::generic::stack::contains method is used to show that the string "four" is in the first copy of the stack, after which the Clear method clears the copy and the xtd::collections::generic::stack::count property shows that the stack is empty.

Public Aliases

using value_type
 Represents the list value type.
 
using base_type
 Represents the list base type.
 
using size_type
 Represents the list size type (usually xtd::size).
 
using reference
 Represents the reference of list value type.
 
using const_reference
 Represents the const reference of list value type.
 

Public Constructors

 stack ()=default
 Initializes a new instance of the xtd::collections::generic::stack <type_t> class that is empty and has the default initial capacity.
 
 stack (stack &&stack)
 Move constructor with specified stack.
 
 stack (const stack &stack)=default
 Default copy constructor with specified stack.
 
 stack (std::stack< type_t > &&stack)
 Move constructor with specified stack.
 
 stack (const std::stack< type_t > &stack)
 Default copy constructor with specified stack.
 
 stack (const ienumerable< value_type > &collection)
 Initializes a new instance of the xtd::collections::generic::stack <type_t> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.
 
 stack (size_type capacity)
 Initializes a new instance of the xtd::collections::generic::stack <type_t> class that is empty and has the specified initial capacity.
 
 stack (std::initializer_list< type_t > il)
 Constructs the container with the contents of the specified initializer list, and allocator.
 
template<std::input_iterator input_iterator_t>
 stack (input_iterator_t first, input_iterator_t last)
 Constructs the container with the contents of the range [first, last).
 

Public Properties

auto capacity () const noexcept -> size_type
 Gets the total numbers of elements the internal data structure can hold without resizing.
 
auto count () const noexcept -> size_type override
 Gets the number of nodes actually contained in the xtd::collections::generic::stack <type_t>.
 
auto items () const
 Gets a std::stack<type_t>.
 

Public Methods

auto clear () -> void override
 Removes all elements from the xtd::collections::generic::stack <type_t>.
 
auto contains (const_reference value) const noexcept -> bool override
 Determines whether an element is in the xtd::collections::generic::stack <type_t>.
 
auto copy_to (xtd::array< type_t > &array, size_type array_index) const -> void override
 Copies the entire xtd::colllections::generic::linked_list <type_t> to a compatible one-dimensional array, starting at the specified index of the target array.
 
auto pop () -> value_type
 Removes and returns the object at the beginning of the xtd::collections::generic::stack <type_t>.
 
auto push (const_reference value) -> void
 Adds an object to the end of the xtd::collections::generic::stack <type_t>.
 
auto push (value_type &&value) -> void
 Adds an object to the end of the xtd::collections::generic::stack <type_t>.
 
auto ensure_capacity (size_type capacity) -> size_type
 Ensures that the capacity of this stack is at least the specified capacity. If the current capacity is less than capacity, it is increased to at least the specified capacity.
 
enumerator< value_typeget_enumerator () const noexcept override
 Returns an enumerator that iterates through the xtd::collections::generic::stack <type_t>.
 
auto peek () const -> value_type
 Returns the object at the beginning of the xtd::collections::generic::stack <type_t> without removing it.
 
auto to_array () const -> xtd::array< value_type >
 Copies the xtd::collections::generic::stack <type_t> elements to a new array.
 
auto to_string () const noexcept -> string override
 Returns a xtd::string that represents the current object.
 
auto trim_excess () -> void
 Sets the capacity to the actual number of elements in the xtd::collections::generic::stack <type_t>, if that number is less than 90 percent of current capacity.
 
auto trim_excess (size_type capacity) -> void
 Sets the capacity of a xtd::collections::generic::stack <type_t> object to the specified number of entries.
 
auto try_pop (value_type &result) noexcept -> bool
 Removes the object at the beginning of the xtd::collections::generic::stack <type_t>, and copies it to the result parameter.
 
auto try_peek (value_type &result) const noexcept -> bool
 Returns a value that indicates whether there is an object at the beginning of the xtd::collections::generic::stack <type_t>, and if one is present, copies it to the result parameter. The object is not removed from the xtd::collections::generic::stack <type_t>.
 

Public Operators

auto operator= (const stack &other) -> stack &=default
 Copy assignment operator. Replaces the contents with a copy of the contents of other.
 
auto operator= (stack &&other) noexcept -> stack &
 Move assignment operator. Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container). other is in a valid but unspecified state afterwards.
 
auto operator= (const std::initializer_list< type_t > &items) -> stack &
 Replaces the contents with those identified by initializer list ilist.
 
 operator std::stack< type_t > () const
 Gets a std::stack<type_t>.
 

Additional Inherited Members

 object ()=default
 Create a new instance of the ultimate base class object.
 
virtual bool equals (const object &obj) const noexcept
 Determines whether the specified object is equal to the current object.
 
virtual xtd::size get_hash_code () const noexcept
 Serves as a hash function for a particular type.
 
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
 
template<class object_t>
xtd::unique_ptr_object< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
template<class object_a_t, class object_b_t>
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
 
template<class object_a_t, class object_b_t>
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.
 

Member Typedef Documentation

◆ value_type

template<class type_t, class container_t = std::deque<type_t>>
using xtd::collections::generic::stack< type_t, container_t >::value_type

Represents the list value type.

◆ base_type

template<class type_t, class container_t = std::deque<type_t>>
using xtd::collections::generic::stack< type_t, container_t >::base_type

Represents the list base type.

◆ size_type

template<class type_t, class container_t = std::deque<type_t>>
using xtd::collections::generic::stack< type_t, container_t >::size_type

Represents the list size type (usually xtd::size).

◆ reference

template<class type_t, class container_t = std::deque<type_t>>
using xtd::collections::generic::stack< type_t, container_t >::reference

Represents the reference of list value type.

◆ const_reference

template<class type_t, class container_t = std::deque<type_t>>
using xtd::collections::generic::stack< type_t, container_t >::const_reference

Represents the const reference of list value type.

Constructor & Destructor Documentation

◆ stack() [1/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( )
default

Initializes a new instance of the xtd::collections::generic::stack <type_t> class that is empty and has the default initial capacity.

◆ stack() [2/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( stack< type_t, container_t > && stack)
inline

Move constructor with specified stack.

Parameters
stackThe xtd::collections::generic::stack <type_t> which elements will be moved from.

◆ stack() [3/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( const stack< type_t, container_t > & stack)
default

Default copy constructor with specified stack.

Parameters
stackThe xtd::collections::generic::stack <type_t> which elements will be inserted from.

◆ stack() [4/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( std::stack< type_t > && stack)
inline

Move constructor with specified stack.

Parameters
stackThe std::stack <type_t> which elements will be moved from.

◆ stack() [5/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( const std::stack< type_t > & stack)
inline

Default copy constructor with specified stack.

Parameters
stackThe std::stack <type_t> which elements will be inserted from.

◆ stack() [6/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( const ienumerable< value_type > & collection)
inline

Initializes a new instance of the xtd::collections::generic::stack <type_t> class that contains elements copied from the specified collection and has sufficient capacity to accommodate the number of elements copied.

Parameters
collectionThe collection whose elements are copied to the new xtd::collections::generic::stack <type_t>.

◆ stack() [7/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( size_type capacity)
inline

Initializes a new instance of the xtd::collections::generic::stack <type_t> class that is empty and has the specified initial capacity.

Parameters
capacityThe initial number of elements that the xtd::collections::generic::stack <type_t> can contain.

◆ stack() [8/9]

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::stack ( std::initializer_list< type_t > il)
inline

Constructs the container with the contents of the specified initializer list, and allocator.

Parameters
itemsThe initializer list to initialize the elements of the container with.

◆ stack() [9/9]

template<class type_t, class container_t = std::deque<type_t>>
template<std::input_iterator input_iterator_t>
xtd::collections::generic::stack< type_t, container_t >::stack ( input_iterator_t first,
input_iterator_t last )
inline

Constructs the container with the contents of the range [first, last).

Parameters
firstThe first iterator the range to copy the elements from.
lastThe last iterator the range to copy the elements from.

Member Function Documentation

◆ capacity()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::capacity ( ) const -> size_type
inlinenoexcept

Gets the total numbers of elements the internal data structure can hold without resizing.

Returns
The total numbers of elements the internal data structure can hold without resizing.

◆ count()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::count ( ) const -> size_type
inlinenodiscardoverridenoexcept

Gets the number of nodes actually contained in the xtd::collections::generic::stack <type_t>.

Returns
The number of nodes actually contained in the xtd::collections::generic::stack <type_t>.
Remarks
Retrieving the value of this property is an O(1) operation.

◆ items()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::items ( ) const
inline

Gets a std::stack<type_t>.

Returns
A std::stack<type_t>.

◆ clear()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::clear ( ) -> void
inlineoverride

Removes all elements from the xtd::collections::generic::stack <type_t>.

Remarks
Count is set to zero, and references to other objects from elements of the collection are also released.
The capacity remains unchanged. To reset the capacity of the xtd::collections::generic::stack <type_t>, call xtd::collections::generic::stack::trim_excess. Trimming an empty xtd::collections::generic::stack <type_t> sets the capacity of the xtd::collections::generic::stack <type_t> to the default capacity.
This method is an O(n) operation, where n is Count.

◆ contains()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::contains ( const_reference value) const -> bool
inlineoverridenoexcept

Determines whether an element is in the xtd::collections::generic::stack <type_t>.

Parameters
itemThe object to locate in the xtd::collections::generic::stack <type_t>.
Returns
true if item is found in the xtd::collections::generic::stack <type_t>; otherwise, false.

◆ copy_to()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::copy_to ( xtd::array< type_t > & array,
size_type array_index ) const -> void
inlineoverride

Copies the entire xtd::colllections::generic::linked_list <type_t> to a compatible one-dimensional array, starting at the specified index of the target array.

Parameters
arrayThe one-dimensional Array that is the destination of the elements copied from xtd::colllections::generic::linked_list <type_t>. The Array must have zero-based indexing.
array_indexThe zero-based index in array at which copying begins.
Exceptions
xtd::argument_out_of_range_exceptionThe number of elements in the source xtd::colllections::generic::linked_list <type_t> is greater than the available space from arrayIndex to the end of the destination array.
Remarks
This method uses xtd::array::copy to copy the elements.
The elements are copied to the xtd::array in the same order in which the enumerator iterates through the xtd::colllections::generic::linked_list <type_t>.
This method is an O(n) operation, where n is xtd::colllections::generic::linked_list::count.

◆ pop()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::pop ( ) -> value_type
inline

Removes and returns the object at the beginning of the xtd::collections::generic::stack <type_t>.

Returns
The object that is removed from the beginning of the xtd::collections::generic::stack <type_t>.
Exceptions
xtd::invalid_operation_exceptionThe xtd::collections::generic::stack <type_t> is empty.

◆ push() [1/2]

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::push ( const_reference value) -> void
inline

Adds an object to the end of the xtd::collections::generic::stack <type_t>.

Parameters
itemThe object to add to the xtd::collections::generic::stack <type_t>. The value can be null for reference types.

◆ push() [2/2]

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::push ( value_type && value) -> void
inline

Adds an object to the end of the xtd::collections::generic::stack <type_t>.

Parameters
itemThe object to add to the xtd::collections::generic::stack <type_t>. The value can be null for reference types.

◆ ensure_capacity()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::ensure_capacity ( size_type capacity) -> size_type
inline

Ensures that the capacity of this stack is at least the specified capacity. If the current capacity is less than capacity, it is increased to at least the specified capacity.

Parameters
capacityThe minimum capacity to ensure.
Returns
The new capacity of this stack.

◆ get_enumerator()

template<class type_t, class container_t = std::deque<type_t>>
enumerator< value_type > xtd::collections::generic::stack< type_t, container_t >::get_enumerator ( ) const
inlineoverridenoexcept

Returns an enumerator that iterates through the xtd::collections::generic::stack <type_t>.

Returns
A xtd::collections::generic::.enumerator for the xtd::collections::generic::stack <type_t>.

◆ peek()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::peek ( ) const -> value_type
inlinenodiscard

Returns the object at the beginning of the xtd::collections::generic::stack <type_t> without removing it.

Returns
The object at the beginning of the xtd::collections::generic::stack <type_t>.
Exceptions
xtd::invalid_operation_exceptionThe xtd::collections::generic::stack <type_t> is empty.

◆ to_array()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::to_array ( ) const -> xtd::array<value_type>
inline

Copies the xtd::collections::generic::stack <type_t> elements to a new array.

Returns
A new array containing elements copied from the xtd::collections::generic::stack <type_t>.

◆ to_string()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::to_string ( ) const -> string
inlineoverridevirtualnoexcept

Returns a xtd::string that represents the current object.

Returns
A string that represents the current object.

Reimplemented from xtd::object.

◆ trim_excess() [1/2]

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::trim_excess ( ) -> void
inline

Sets the capacity to the actual number of elements in the xtd::collections::generic::stack <type_t>, if that number is less than 90 percent of current capacity.

Remarks
This method can be used to minimize a collection's memory overhead if no new elements will be added to the collection. The cost of reallocating and copying a large xtd::collections::generic::stack <type_t> can be considerable, however, so the xtd::collections::generic::stack::trim_excess method does nothing if the list is at more than 90 percent of capacity. This avoids incurring a large reallocation cost for a relatively small gain.
This method is an O(n) operation, where n is xtd::collections::generic::stack::count.
To reset a xtd::collections::generic::stack <type_t> to its initial state, call the xtd::collections::generic::stack::clear method before calling xtd::collections::generic::stack::trim_excess method. Trimming an empty xtd::collections::generic::stack <type_t> sets the capacity of the xtd::collections::generic::stack <type_t> to the default capacity.

◆ trim_excess() [2/2]

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::trim_excess ( size_type capacity) -> void
inline

Sets the capacity of a xtd::collections::generic::stack <type_t> object to the specified number of entries.

Parameters
capacityThe new capacity.
Exceptions
xtd::argument_out_of_range_exceptionPassed capacity is lower than entries count.

◆ try_pop()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::try_pop ( value_type & result) -> bool
inlinenodiscardnoexcept

Removes the object at the beginning of the xtd::collections::generic::stack <type_t>, and copies it to the result parameter.

Parameters
Theremoved object.
Returns
true if the object is successfully removed; false if the xtd::collections::generic::stack <type_t> is empty.

◆ try_peek()

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::try_peek ( value_type & result) const -> bool
inlinenodiscardnoexcept

Returns a value that indicates whether there is an object at the beginning of the xtd::collections::generic::stack <type_t>, and if one is present, copies it to the result parameter. The object is not removed from the xtd::collections::generic::stack <type_t>.

Parameters
Ifpresent, the object at the beginning of the xtd::collections::generic::stack <type_t>; otherwise, the default value of type_t.
Returns
true if there is an object at the beginning of the xtd::collections::generic::stack <type_t>; false if the xtd::collections::generic::stack <type_t> is empty.

◆ operator=() [1/3]

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::operator= ( const stack< type_t, container_t > & other) -> stack &=default
default

Copy assignment operator. Replaces the contents with a copy of the contents of other.

Parameters
otherAnother container to use as data source.
Returns
This current instance.

◆ operator=() [2/3]

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::operator= ( stack< type_t, container_t > && other) -> stack&
inlinenoexcept

Move assignment operator. Replaces the contents with those of other using move semantics (i.e. the data in other is moved from other into this container). other is in a valid but unspecified state afterwards.

Parameters
otherAnother base type container to use as data source.
Returns
This current instance.

◆ operator=() [3/3]

template<class type_t, class container_t = std::deque<type_t>>
auto xtd::collections::generic::stack< type_t, container_t >::operator= ( const std::initializer_list< type_t > & items) -> stack&
inline

Replaces the contents with those identified by initializer list ilist.

Parameters
itemsInitializer list to use as data source
Returns
This current instance.

◆ operator std::stack< type_t >()

template<class type_t, class container_t = std::deque<type_t>>
xtd::collections::generic::stack< type_t, container_t >::operator std::stack< type_t > ( ) const
inline

Gets a std::stack<type_t>.

Returns
A std::stack<type_t>.

The documentation for this class was generated from the following file:
  • xtd.core/include/xtd/collections/generic/stack.hpp