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/PtrUtil.h" | 35 #include "wtf/OwnPtr.h" |
36 #include <memory> | |
37 | 36 |
38 namespace WTF { | 37 namespace WTF { |
39 | 38 |
40 template <typename T> | 39 template <typename T> |
41 class LinkedStack { | 40 class LinkedStack { |
42 USING_FAST_MALLOC(LinkedStack); | 41 USING_FAST_MALLOC(LinkedStack); |
43 public: | 42 public: |
44 LinkedStack() : m_size(0) { } | 43 LinkedStack() : m_size(0) { } |
45 | 44 |
46 // Iterative cleanup to prevent stack overflow problems. | 45 // Iterative cleanup to prevent stack overflow problems. |
47 ~LinkedStack() | 46 ~LinkedStack() |
48 { | 47 { |
49 std::unique_ptr<Node> ptr = m_head.release(); | 48 OwnPtr<Node> ptr = m_head.release(); |
50 while (ptr) | 49 while (ptr) |
51 ptr = ptr->m_next.release(); | 50 ptr = ptr->m_next.release(); |
52 } | 51 } |
53 | 52 |
54 bool isEmpty(); | 53 bool isEmpty(); |
55 | 54 |
56 void push(const T&); | 55 void push(const T&); |
57 const T& peek(); | 56 const T& peek(); |
58 void pop(); | 57 void pop(); |
59 | 58 |
60 size_t size(); | 59 size_t size(); |
61 | 60 |
62 private: | 61 private: |
63 class Node { | 62 class Node { |
64 USING_FAST_MALLOC(LinkedStack::Node); | 63 USING_FAST_MALLOC(LinkedStack::Node); |
65 public: | 64 public: |
66 Node(const T&, std::unique_ptr<Node> next); | 65 Node(const T&, PassOwnPtr<Node> next); |
67 | 66 |
68 T m_data; | 67 T m_data; |
69 std::unique_ptr<Node> m_next; | 68 OwnPtr<Node> m_next; |
70 }; | 69 }; |
71 | 70 |
72 std::unique_ptr<Node> m_head; | 71 OwnPtr<Node> m_head; |
73 size_t m_size; | 72 size_t m_size; |
74 }; | 73 }; |
75 | 74 |
76 template <typename T> | 75 template <typename T> |
77 LinkedStack<T>::Node::Node(const T& data, std::unique_ptr<Node> next) | 76 LinkedStack<T>::Node::Node(const T& data, PassOwnPtr<Node> next) |
78 : m_data(data) | 77 : m_data(data) |
79 , m_next(next) | 78 , m_next(next) |
80 { | 79 { |
81 } | 80 } |
82 | 81 |
83 template <typename T> | 82 template <typename T> |
84 inline bool LinkedStack<T>::isEmpty() | 83 inline bool LinkedStack<T>::isEmpty() |
85 { | 84 { |
86 return !m_head; | 85 return !m_head; |
87 } | 86 } |
88 | 87 |
89 template <typename T> | 88 template <typename T> |
90 inline void LinkedStack<T>::push(const T& data) | 89 inline void LinkedStack<T>::push(const T& data) |
91 { | 90 { |
92 m_head = wrapUnique(new Node(data, m_head.release())); | 91 m_head = adoptPtr(new Node(data, m_head.release())); |
93 ++m_size; | 92 ++m_size; |
94 } | 93 } |
95 | 94 |
96 template <typename T> | 95 template <typename T> |
97 inline const T& LinkedStack<T>::peek() | 96 inline const T& LinkedStack<T>::peek() |
98 { | 97 { |
99 return m_head->m_data; | 98 return m_head->m_data; |
100 } | 99 } |
101 | 100 |
102 template <typename T> | 101 template <typename T> |
103 inline void LinkedStack<T>::pop() | 102 inline void LinkedStack<T>::pop() |
104 { | 103 { |
105 ASSERT(m_head && m_size); | 104 ASSERT(m_head && m_size); |
106 m_head = m_head->m_next.release(); | 105 m_head = m_head->m_next.release(); |
107 --m_size; | 106 --m_size; |
108 } | 107 } |
109 | 108 |
110 template <typename T> | 109 template <typename T> |
111 inline size_t LinkedStack<T>::size() | 110 inline size_t LinkedStack<T>::size() |
112 { | 111 { |
113 return m_size; | 112 return m_size; |
114 } | 113 } |
115 | 114 |
116 } // namespace WTF | 115 } // namespace WTF |
117 | 116 |
118 using WTF::LinkedStack; | 117 using WTF::LinkedStack; |
119 | 118 |
120 #endif | 119 #endif |
OLD | NEW |