| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 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. | |
| 29 */ | |
| 30 | |
| 31 #ifndef LinkedStack_h | |
| 32 #define LinkedStack_h | |
| 33 | |
| 34 #include "wtf/Allocator.h" | |
| 35 #include "wtf/PtrUtil.h" | |
| 36 #include <memory> | |
| 37 | |
| 38 namespace WTF { | |
| 39 | |
| 40 template <typename T> | |
| 41 class LinkedStack { | |
| 42 USING_FAST_MALLOC(LinkedStack); | |
| 43 | |
| 44 public: | |
| 45 LinkedStack() : m_size(0) {} | |
| 46 | |
| 47 // Iterative cleanup to prevent stack overflow problems. | |
| 48 ~LinkedStack() { | |
| 49 std::unique_ptr<Node> ptr = m_head.release(); | |
| 50 while (ptr) | |
| 51 ptr = ptr->m_next.release(); | |
| 52 } | |
| 53 | |
| 54 bool isEmpty(); | |
| 55 | |
| 56 void push(const T&); | |
| 57 const T& peek(); | |
| 58 void pop(); | |
| 59 | |
| 60 size_t size(); | |
| 61 | |
| 62 private: | |
| 63 class Node { | |
| 64 USING_FAST_MALLOC(LinkedStack::Node); | |
| 65 | |
| 66 public: | |
| 67 Node(const T&, std::unique_ptr<Node> next); | |
| 68 | |
| 69 T m_data; | |
| 70 std::unique_ptr<Node> m_next; | |
| 71 }; | |
| 72 | |
| 73 std::unique_ptr<Node> m_head; | |
| 74 size_t m_size; | |
| 75 }; | |
| 76 | |
| 77 template <typename T> | |
| 78 LinkedStack<T>::Node::Node(const T& data, std::unique_ptr<Node> next) | |
| 79 : m_data(data), m_next(next) {} | |
| 80 | |
| 81 template <typename T> | |
| 82 inline bool LinkedStack<T>::isEmpty() { | |
| 83 return !m_head; | |
| 84 } | |
| 85 | |
| 86 template <typename T> | |
| 87 inline void LinkedStack<T>::push(const T& data) { | |
| 88 m_head = WTF::wrapUnique(new Node(data, m_head.release())); | |
| 89 ++m_size; | |
| 90 } | |
| 91 | |
| 92 template <typename T> | |
| 93 inline const T& LinkedStack<T>::peek() { | |
| 94 return m_head->m_data; | |
| 95 } | |
| 96 | |
| 97 template <typename T> | |
| 98 inline void LinkedStack<T>::pop() { | |
| 99 DCHECK(m_head); | |
| 100 DCHECK(m_size); | |
| 101 m_head = m_head->m_next.release(); | |
| 102 --m_size; | |
| 103 } | |
| 104 | |
| 105 template <typename T> | |
| 106 inline size_t LinkedStack<T>::size() { | |
| 107 return m_size; | |
| 108 } | |
| 109 | |
| 110 } // namespace WTF | |
| 111 | |
| 112 using WTF::LinkedStack; | |
| 113 | |
| 114 #endif | |
| OLD | NEW |