xtd 0.2.0
Loading...
Searching...
No Matches
raw_queue.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <queue>
6
8namespace xtd {
10 namespace collections {
12 namespace generic {
14 namespace helpers {
15 template<class type_t, class container_t = std::deque<type_t>>
16 class raw_queue final : public std::queue<type_t, container_t> {
17 public:
19
21 using base_type = std::queue<type_t, container_t>;
22 using container_type = typename base_type::container_type;
23 using value_type = typename base_type::value_type;
24 using size_type = typename base_type::size_type;
25 using reference = typename base_type::reference;
26 using const_reference = typename base_type::const_reference;
27 using iterator = typename container_type::const_iterator;
28 using const_iterator = typename container_type::const_iterator;
30
32
34 raw_queue() {shrink_to_fit();}
35 explicit raw_queue(size_type capacity) {
36 shrink_to_fit();
37 ensure_capacity(capacity);
38 }
39 raw_queue(const raw_queue& other) : base_type(other) {shrink_to_fit();}
40 raw_queue(raw_queue&& other) : base_type(std::move(other)) {shrink_to_fit();}
41 template<class input_iterator_t>
42 raw_queue(input_iterator_t first, input_iterator_t last) : base_type(first, last) {shrink_to_fit();}
43 template<class allocator_t>
44 explicit raw_queue(const allocator_t& alloc) : base_type(alloc) {shrink_to_fit();}
45 template<class allocator_t>
46 raw_queue(const container_t& cont, const allocator_t& alloc) : base_type(cont, alloc) {shrink_to_fit();}
47 template<class allocator_t>
48 raw_queue(container_t&& cont, const allocator_t& alloc) : base_type(std::move(cont), alloc) {shrink_to_fit();}
49 template<class allocator_t >
50 raw_queue(const raw_queue& other, const allocator_t& alloc) : base_type(other, alloc) {shrink_to_fit();}
51 template<class allocator_t >
52 raw_queue(raw_queue&& other, const allocator_t& alloc) : base_type(std::move(other), alloc) {shrink_to_fit();}
53 template<class input_iterator_t, class allocator_t >
54 raw_queue(input_iterator_t first, input_iterator_t last, const allocator_t& alloc) : base_type(first, last, alloc) {shrink_to_fit();}
56
58
60 auto items() const noexcept -> const base_type& {return *this;}
61 auto items() noexcept -> base_type& {return *this;}
63
65
67 auto begin() const -> const_iterator {return base_type::c.cbegin();}
68
69 auto capacity() const noexcept -> size_type {return capacity_;}
70
71 auto cbegin() const -> const_iterator {return base_type::c.cbegin();}
72
73 auto cend() const -> const_iterator {return base_type::c.cend();}
74
75 auto end() const -> const_iterator {return base_type::c.cend();}
76
77 auto push(value_type&& value) -> void {
78 base_type::push(std::move(value));
79 ensure_capacity(base_type::c.size());
80 }
81
82 auto pop() -> void {base_type::pop();}
83
84 auto push(const value_type& value) -> void {
85 base_type::push(value);
86 ensure_capacity(base_type::c.size());
87 }
88
89 auto shrink_to_fit() -> void {
90 base_type::c.shrink_to_fit();
91 ensure_capacity(base_type::c.size());
92 }
93
94 auto size() const noexcept -> size_type {return base_type::c.size();}
95
96 auto reserve(size_type count) -> void {
97 if (capacity_ >= count) return;
98 ensure_capacity(count);
99 }
101
103
105 raw_queue& operator =(const raw_queue& other) = default;
106 raw_queue& operator =(raw_queue&& other) = default;
107
108 operator const base_type& () const noexcept {return *this;}
109 operator base_type& () noexcept {return *this;}
111
112 private:
113 auto ensure_capacity(size_type capacity) -> void {
114 if (capacity <= capacity_) return;
115 capacity_ = capacity;
116 auto original_size = base_type::size();
117 base_type::c.resize(capacity_);
118 base_type::c.resize(original_size); // Restore the original size and the capacity remains unchanged.
119 }
120
121 size_type capacity_ = base_type::size();
122 };
123
125 // Deduction guides for xtd::collections::generic::helpers::raw_queue
126 // {
127 template<class container_t>
129
130 template<class container_t, class allocator_t>
132
133 template< class input_iterator_t>
135
136 template< class input_iterator_t, class allocator_t>
137 raw_queue(input_iterator_t, input_iterator_t, allocator_t) -> raw_queue<typename std::iterator_traits<input_iterator_t>::value_type, std::deque<typename std::iterator_traits<input_iterator_t>::value_type, allocator_t>>;
138 // }
140 }
141 }
142 }
143}
@ other
The operating system is other.
Definition platform_id.hpp:60
The xtd::collections::generic::helpers namespace contains helpers for generic collections,...
Definition allocator.hpp:14
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
read_only_span< type_t, count > last() const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:307