| 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() |
| 47 { | 45 : m_size(0) {} |
| 48 OwnPtr<Node> ptr = m_head.release(); | |
| 49 while (ptr) | |
| 50 ptr = ptr->m_next.release(); | |
| 51 } | |
| 52 | 46 |
| 53 bool isEmpty(); | 47 // Iterative cleanup to prevent stack overflow problems. |
| 48 ~LinkedStack() { |
| 49 OwnPtr<Node> ptr = m_head.release(); |
| 50 while (ptr) |
| 51 ptr = ptr->m_next.release(); |
| 52 } |
| 54 | 53 |
| 55 void push(const T&); | 54 bool isEmpty(); |
| 56 const T& peek(); | |
| 57 void pop(); | |
| 58 | 55 |
| 59 size_t size(); | 56 void push(const T&); |
| 57 const T& peek(); |
| 58 void pop(); |
| 60 | 59 |
| 61 // This inner class used to be private but is now public on account of a | 60 size_t size(); |
| 62 // possible MSVC bug. It can be made private again if we get rid of | |
| 63 // USING_FAST_MALLOC ever. | |
| 64 class Node { | |
| 65 USING_FAST_MALLOC(LinkedStack::Node); | |
| 66 public: | |
| 67 Node(const T&, PassOwnPtr<Node> next); | |
| 68 | 61 |
| 69 T m_data; | 62 // This inner class used to be private but is now public on account of a |
| 70 OwnPtr<Node> m_next; | 63 // possible MSVC bug. It can be made private again if we get rid of |
| 71 }; | 64 // USING_FAST_MALLOC ever. |
| 65 class Node { |
| 66 USING_FAST_MALLOC(LinkedStack::Node); |
| 72 | 67 |
| 73 private: | 68 public: |
| 74 OwnPtr<Node> m_head; | 69 Node(const T&, PassOwnPtr<Node> next); |
| 75 size_t m_size; | 70 |
| 71 T m_data; |
| 72 OwnPtr<Node> m_next; |
| 73 }; |
| 74 |
| 75 private: |
| 76 OwnPtr<Node> m_head; |
| 77 size_t m_size; |
| 76 }; | 78 }; |
| 77 | 79 |
| 78 template <typename T> | 80 template <typename T> |
| 79 LinkedStack<T>::Node::Node(const T& data, PassOwnPtr<Node> next) | 81 LinkedStack<T>::Node::Node(const T& data, PassOwnPtr<Node> next) |
| 80 : m_data(data) | 82 : m_data(data), m_next(next) { |
| 81 , m_next(next) | |
| 82 { | |
| 83 } | 83 } |
| 84 | 84 |
| 85 template <typename T> | 85 template <typename T> |
| 86 inline bool LinkedStack<T>::isEmpty() | 86 inline bool LinkedStack<T>::isEmpty() { |
| 87 { | 87 return !m_head; |
| 88 return !m_head; | |
| 89 } | 88 } |
| 90 | 89 |
| 91 template <typename T> | 90 template <typename T> |
| 92 inline void LinkedStack<T>::push(const T& data) | 91 inline void LinkedStack<T>::push(const T& data) { |
| 93 { | 92 m_head = adoptPtr(new Node(data, m_head.release())); |
| 94 m_head = adoptPtr(new Node(data, m_head.release())); | 93 ++m_size; |
| 95 ++m_size; | |
| 96 } | 94 } |
| 97 | 95 |
| 98 template <typename T> | 96 template <typename T> |
| 99 inline const T& LinkedStack<T>::peek() | 97 inline const T& LinkedStack<T>::peek() { |
| 100 { | 98 return m_head->m_data; |
| 101 return m_head->m_data; | |
| 102 } | 99 } |
| 103 | 100 |
| 104 template <typename T> | 101 template <typename T> |
| 105 inline void LinkedStack<T>::pop() | 102 inline void LinkedStack<T>::pop() { |
| 106 { | 103 ASSERT(m_head && m_size); |
| 107 ASSERT(m_head && m_size); | 104 m_head = m_head->m_next.release(); |
| 108 m_head = m_head->m_next.release(); | 105 --m_size; |
| 109 --m_size; | |
| 110 } | 106 } |
| 111 | 107 |
| 112 template <typename T> | 108 template <typename T> |
| 113 inline size_t LinkedStack<T>::size() | 109 inline size_t LinkedStack<T>::size() { |
| 114 { | 110 return m_size; |
| 115 return m_size; | |
| 116 } | 111 } |
| 117 | |
| 118 } | 112 } |
| 119 | 113 |
| 120 using WTF::LinkedStack; | 114 using WTF::LinkedStack; |
| 121 | 115 |
| 122 #endif | 116 #endif |
| OLD | NEW |