-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.h
118 lines (94 loc) · 2.54 KB
/
stack.h
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
/* THOR - THOR Template Library
* Joshua M. Kriegshauser
*
* stack.h
*
* This file defines the STL-compatible container adapter stack.
* Note that stack is not an actual container, it provides a subset of functionality for an underlying container
*/
#ifndef THOR_STACK_H
#define THOR_STACK_H
#pragma once
#ifndef THOR_VECTOR_H
#include "vector.h"
#endif
namespace thor
{
// extension: typically STL stacks are implemented with deque, but the default in this case is a vector
template <class T, class Sequence = vector<T> >
class stack
{
public:
typedef typename Sequence::value_type value_type;
typedef typename Sequence::size_type size_type;
stack()
{}
stack(const stack& rhs) :
m_container(rhs.m_container)
{}
stack& operator = (const stack& rhs)
{
m_container = rhs.m_container;
return *this;
}
bool empty() const
{
return m_container.empty();
}
size_type size() const
{
return m_container.size();
}
value_type& top()
{
THOR_DEBUG_ASSERT(!empty());
return m_container.back();
}
const value_type& top() const
{
THOR_DEBUG_ASSERT(!empty());
return m_container.back();
}
void push(const value_type& x)
{
m_container.push_back(x);
}
void pop()
{
THOR_DEBUG_ASSERT(!empty());
m_container.pop_back();
}
// Extensions
// Default-construct
void push() { m_container.push_back(); }
const Sequence& get_container() const { return m_container; }
private:
Sequence m_container;
};
}
// Global operators
template <class T, class Sequence> bool operator == (const stack<T,Sequence>& lhs, const stack<T,Sequence>& rhs)
{
return lhs.get_container() == rhs.get_container();
}
template <class T, class Sequence> bool operator != (const stack<T,Sequence>& lhs, const stack<T,Sequence>& rhs)
{
return !(lhs == rhs);
}
template <class T, class Sequence> bool operator < (const stack<T,Sequence>& lhs, const stack<T,Sequence>& rhs)
{
return lhs.get_container() < rhs.get_container();
}
template <class T, class Sequence> bool operator > (const stack<T,Sequence>& lhs, const stack<T,Sequence>& rhs)
{
return rhs.get_container() < lhs.get_container();
}
template <class T, class Sequence> bool operator <= (const stack<T,Sequence>& lhs, const stack<T,Sequence>& rhs)
{
return !(lhs > rhs);
}
template <class T, class Sequence> bool operator >= (const stack<T,Sequence>& lhs, const stack<T,Sequence>& rhs)
{
return !(lhs < rhs);
}
#endif