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