ADS
Algorithms and Data Structures
Toggle main menu visibility
Loading...
Searching...
No Matches
MinStack.h
Go to the documentation of this file.
1
/*--------------------------------------------------------------------------*/
2
/*---------------------------- File MinStack.h -----------------------------*/
3
/*--------------------------------------------------------------------------*/
34
/*--------------------------------------------------------------------------*/
35
/*----------------------------- DEFINITIONS --------------------------------*/
36
/*--------------------------------------------------------------------------*/
37
38
#ifndef __ADS_MinStack
39
#define __ADS_MinStack
40
/* self-identification: #endif constant */
41
42
/*--------------------------------------------------------------------------*/
43
/*------------------------------ INCLUDES ----------------------------------*/
44
/*--------------------------------------------------------------------------*/
45
46
#include "
ADS/Contract.h
"
47
#include "
ADS/Portable.h
"
48
49
#include <algorithm>
50
#include <cstddef>
51
#include <functional>
52
#include <utility>
53
#include <vector>
54
55
/*--------------------------------------------------------------------------*/
56
/*--------------------------- NAMESPACE ------------------------------------*/
57
/*--------------------------------------------------------------------------*/
58
59
namespace
ADS
60
{
61
62
/*--------------------------------------------------------------------------*/
63
/*----------------------------- CLASS MinStack -----------------------------*/
64
/*--------------------------------------------------------------------------*/
65
67
template
<
class
T ,
class
C = std::less<> >
68
class
MinStack
69
{
70
71
/*--------------------------------------------------------------------------*/
72
public
:
73
using
Item
= T;
74
76
explicit
MinStack
( C less = C() ) : f_less( std::move( less ) ) { }
77
79
[[nodiscard]]
bool
empty
(
void
)
const
{
return
( f_A.empty() ); }
80
82
[[nodiscard]] std::size_t
size
(
void
)
const
{
return
( f_A.size() ); }
83
86
[[nodiscard]]
const
T &
top
(
void
)
const
{
87
ADS_PRE
( ! f_A.empty() );
88
return
( f_A.back() );
89
}
90
93
[[nodiscard]]
const
T &
min
(
void
)
const
{
94
ADS_PRE
( ! f_A.empty() );
95
return
( f_M.back() );
96
}
97
112
void
push
(
const
T & x );
113
129
T
pop
(
void
);
130
132
void
clear
(
void
) {
133
f_A.clear();
134
f_M.clear();
135
}
136
138
[[nodiscard]]
const
std::vector< T > &
items
(
void
)
const
{
return
( f_A ); }
139
141
[[nodiscard]] std::size_t
minima
(
void
)
const
{
return
( f_M.size() ); }
142
143
/*--------------------------------------------------------------------------*/
144
private
:
146
[[nodiscard]]
bool
ordered(
void
)
const
;
147
148
std::vector< T > f_A;
149
std::vector< T > f_M;
150
ADS_NO_UNIQUE_ADDRESS
C f_less;
151
152
};
// end( class MinStack )
153
154
/*--------------------------------------------------------------------------*/
155
156
}
// end( namespace ADS )
157
158
/*--------------------------------------------------------------------------*/
159
/*-------------------------- IMPLEMENTATION --------------------------------*/
160
/*--------------------------------------------------------------------------*/
161
162
#include "ADS/stack/impl/MinStack.tpp"
163
164
/*--------------------------------------------------------------------------*/
165
166
#endif
/* MinStack.h included */
167
168
/*--------------------------------------------------------------------------*/
169
/*-------------------------- End File MinStack.h ---------------------------*/
170
/*--------------------------------------------------------------------------*/
Contract.h
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
Portable.h
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
ADS::MinStack::items
const std::vector< T > & items(void) const
the items from the bottom to the top
Definition
MinStack.h:138
ADS::MinStack::top
const T & top(void) const
the item on top
Definition
MinStack.h:86
ADS::MinStack::MinStack
MinStack(C less=C())
the empty stack, ordered by less
Definition
MinStack.h:76
ADS::MinStack::pop
T pop(void)
removes the item on top and returns it, from the minima too if it is their top
ADS::MinStack::Item
T Item
the type of the items
Definition
MinStack.h:73
ADS::MinStack::empty
bool empty(void) const
true if there is no item
Definition
MinStack.h:79
ADS::MinStack::size
std::size_t size(void) const
the number of items
Definition
MinStack.h:82
ADS::MinStack::min
const T & min(void) const
the least item
Definition
MinStack.h:93
ADS::MinStack::clear
void clear(void)
removes all the items
Definition
MinStack.h:132
ADS::MinStack::minima
std::size_t minima(void) const
the number of minima kept, at most
Definition
MinStack.h:141
ADS::MinStack::push
void push(const T &x)
puts on top, and among the minima if it is not larger than the least item
ADS_PRE
#define ADS_PRE(cond)
[check]
Definition
Contract.h:121
ADS_NO_UNIQUE_ADDRESS
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition
Portable.h:81
ADS
Definition
ADT.h:96
ADS
stack
MinStack.h
Generated by
1.17.0