1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
|
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_STACK_KERNEl_ABSTRACT_
#ifdef DLIB_STACK_KERNEl_ABSTRACT_
#include "../interfaces/enumerable.h"
#include "../interfaces/remover.h"
#include "../serialize.h"
#include "../algs.h"
namespace dlib
{
template <
typename T,
typename mem_manager = default_memory_manager
>
class stack : public enumerable<T>,
public remover<T>
{
/*!
REQUIREMENTS ON T
T must be swappable by a global swap() and
T must have a default constructor
REQUIREMENTS ON mem_manager
must be an implementation of memory_manager/memory_manager_kernel_abstract.h or
must be an implementation of memory_manager_global/memory_manager_global_kernel_abstract.h or
must be an implementation of memory_manager_stateless/memory_manager_stateless_kernel_abstract.h
mem_manager::type can be set to anything.
POINTERS AND REFERENCES TO INTERNAL DATA
swap() and current() functions do not invalidate pointers
or references to internal data.
All other functions have no such guarantee.
INITIAL VALUE
size() == 0
ENUMERATION ORDER
The enumerator will iterate over the elements in the stack in the
same order they would be removed in by repeated calls to pop().
(e.g. current() would be the first element enumerated)
WHAT THIS OBJECT REPRESENTS
This is a last in first out stack containing items of type T.
e.g. if the stack is {b,c,d,e} then a is put in
the stack becomes {a,b,c,d,e} and then pop takes a back out
returning the stack to {b,c,d,e}
Also note that unless specified otherwise, no member functions
of this object throw exceptions.
!*/
public:
typedef T type;
typedef mem_manager mem_manager_type;
stack (
);
/*!
ensures
- #*this is properly initialized
throws
- std::bad_alloc or any exception thrown by T's constructor
!*/
virtual ~stack (
);
/*!
ensures
- all memory associated with *this has been released
!*/
void clear(
);
/*!
ensures
- #*this has its initial value
throws
- std::bad_alloc or any exception thrown by T's constructor
if this exception is thrown then *this is unusable
until clear() is called and succeeds
!*/
void push (
T& item
);
/*!
ensures
- item has been swapped onto the top of the stack
- #current() == item
- #item has an initial value for its type
- #size() == size() + 1
- #at_start() == true
throws
- std::bad_alloc or any exception thrown by T's constructor
if push() throws then it has no effect
!*/
void pop (
T& item
);
/*!
requires
- size() != 0
ensures
- #size() == size() - 1
- #item == current()
i.e. the top element of *this has been removed and swapped
into #item
- #at_start() == true
!*/
T& current (
);
/*!
requires
- size() != 0
ensures
- returns a const reference to the element at the top of *this
!*/
const T& current (
) const;
/*!
requires
- size() != 0
ensures
- returns a non-const reference to the element at the top of *this
!*/
void swap (
stack& item
);
/*!
ensures
- swaps *this and item
!*/
private:
// restricted functions
stack(stack&); // copy constructor
stack& operator=(stack&); // assignment operator
};
template <
typename T,
typename mem_manager
>
inline void swap (
stack<T,mem_manager>& a,
stack<T,mem_manager>& b
) { a.swap(b); }
/*!
provides a global swap function
!*/
template <
typename T,
typename mem_manager
>
void deserialize (
stack<T,mem_manager>& item,
std::istream& in
);
/*!
provides deserialization support
!*/
}
#endif // DLIB_STACK_KERNEl_ABSTRACT_
|