OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 14 matching lines...) Expand all Loading... |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #ifndef LinkedStack_h | 31 #ifndef LinkedStack_h |
32 #define LinkedStack_h | 32 #define LinkedStack_h |
33 | 33 |
34 #include "wtf/Allocator.h" | 34 #include "wtf/Allocator.h" |
35 #include "wtf/OwnPtr.h" | 35 #include "wtf/PtrUtil.h" |
| 36 #include <memory> |
36 | 37 |
37 namespace WTF { | 38 namespace WTF { |
38 | 39 |
39 template <typename T> | 40 template <typename T> |
40 class LinkedStack { | 41 class LinkedStack { |
41 USING_FAST_MALLOC(LinkedStack); | 42 USING_FAST_MALLOC(LinkedStack); |
42 public: | 43 public: |
43 LinkedStack() : m_size(0) { } | 44 LinkedStack() : m_size(0) { } |
44 | 45 |
45 // Iterative cleanup to prevent stack overflow problems. | 46 // Iterative cleanup to prevent stack overflow problems. |
46 ~LinkedStack() | 47 ~LinkedStack() |
47 { | 48 { |
48 OwnPtr<Node> ptr = m_head.release(); | 49 std::unique_ptr<Node> ptr = m_head.release(); |
49 while (ptr) | 50 while (ptr) |
50 ptr = ptr->m_next.release(); | 51 ptr = ptr->m_next.release(); |
51 } | 52 } |
52 | 53 |
53 bool isEmpty(); | 54 bool isEmpty(); |
54 | 55 |
55 void push(const T&); | 56 void push(const T&); |
56 const T& peek(); | 57 const T& peek(); |
57 void pop(); | 58 void pop(); |
58 | 59 |
59 size_t size(); | 60 size_t size(); |
60 | 61 |
61 private: | 62 private: |
62 class Node { | 63 class Node { |
63 USING_FAST_MALLOC(LinkedStack::Node); | 64 USING_FAST_MALLOC(LinkedStack::Node); |
64 public: | 65 public: |
65 Node(const T&, PassOwnPtr<Node> next); | 66 Node(const T&, std::unique_ptr<Node> next); |
66 | 67 |
67 T m_data; | 68 T m_data; |
68 OwnPtr<Node> m_next; | 69 std::unique_ptr<Node> m_next; |
69 }; | 70 }; |
70 | 71 |
71 OwnPtr<Node> m_head; | 72 std::unique_ptr<Node> m_head; |
72 size_t m_size; | 73 size_t m_size; |
73 }; | 74 }; |
74 | 75 |
75 template <typename T> | 76 template <typename T> |
76 LinkedStack<T>::Node::Node(const T& data, PassOwnPtr<Node> next) | 77 LinkedStack<T>::Node::Node(const T& data, std::unique_ptr<Node> next) |
77 : m_data(data) | 78 : m_data(data) |
78 , m_next(next) | 79 , m_next(next) |
79 { | 80 { |
80 } | 81 } |
81 | 82 |
82 template <typename T> | 83 template <typename T> |
83 inline bool LinkedStack<T>::isEmpty() | 84 inline bool LinkedStack<T>::isEmpty() |
84 { | 85 { |
85 return !m_head; | 86 return !m_head; |
86 } | 87 } |
87 | 88 |
88 template <typename T> | 89 template <typename T> |
89 inline void LinkedStack<T>::push(const T& data) | 90 inline void LinkedStack<T>::push(const T& data) |
90 { | 91 { |
91 m_head = adoptPtr(new Node(data, m_head.release())); | 92 m_head = wrapUnique(new Node(data, m_head.release())); |
92 ++m_size; | 93 ++m_size; |
93 } | 94 } |
94 | 95 |
95 template <typename T> | 96 template <typename T> |
96 inline const T& LinkedStack<T>::peek() | 97 inline const T& LinkedStack<T>::peek() |
97 { | 98 { |
98 return m_head->m_data; | 99 return m_head->m_data; |
99 } | 100 } |
100 | 101 |
101 template <typename T> | 102 template <typename T> |
102 inline void LinkedStack<T>::pop() | 103 inline void LinkedStack<T>::pop() |
103 { | 104 { |
104 ASSERT(m_head && m_size); | 105 ASSERT(m_head && m_size); |
105 m_head = m_head->m_next.release(); | 106 m_head = m_head->m_next.release(); |
106 --m_size; | 107 --m_size; |
107 } | 108 } |
108 | 109 |
109 template <typename T> | 110 template <typename T> |
110 inline size_t LinkedStack<T>::size() | 111 inline size_t LinkedStack<T>::size() |
111 { | 112 { |
112 return m_size; | 113 return m_size; |
113 } | 114 } |
114 | 115 |
115 } // namespace WTF | 116 } // namespace WTF |
116 | 117 |
117 using WTF::LinkedStack; | 118 using WTF::LinkedStack; |
118 | 119 |
119 #endif | 120 #endif |
OLD | NEW |