Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_TASK_SCHEDULER_SCHEDULER_STACK_H_ | |
| 6 #define BASE_TASK_SCHEDULER_SCHEDULER_STACK_H_ | |
| 7 | |
| 8 #include <stddef.h> | |
| 9 | |
| 10 #include <algorithm> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/logging.h" | |
| 14 #include "base/macros.h" | |
| 15 | |
| 16 namespace base { | |
| 17 namespace internal { | |
| 18 | |
| 19 // A stack that supports removal of arbitrary values and doesn't allow multiple | |
| 20 // insertions of the same value. | |
| 21 | |
|
robliao
2016/04/20 20:54:58
Remove extra linebreak.
fdoray
2016/04/20 21:04:41
Done.
| |
| 22 // This class is NOT thread-safe. | |
| 23 template <typename T> | |
| 24 class SchedulerStack { | |
|
robliao
2016/04/20 20:54:58
Given that this doesn't allow multiple insertions
fdoray
2016/04/20 21:04:41
Done.
| |
| 25 public: | |
| 26 SchedulerStack(); | |
| 27 ~SchedulerStack(); | |
| 28 | |
| 29 // Inserts |val| at the top of the stack. |val| must not already be on the | |
| 30 // stack. | |
| 31 void Push(const T& val); | |
| 32 | |
| 33 // Removes the top value from the stack and returns it. Cannot be called on an | |
| 34 // empty stack. | |
| 35 T Pop(); | |
|
robliao
2016/04/20 20:54:58
Would peek be a useful operation that we may use i
fdoray
2016/04/20 21:04:41
We don't need it currently. With this API, we can
| |
| 36 | |
| 37 // Removes |val| from the stack if it was on it. | |
| 38 void Remove(const T& val); | |
| 39 | |
| 40 // Returns the number of values on the stack. | |
| 41 size_t Size() const; | |
| 42 | |
| 43 // Returns true if the stack is empty. | |
| 44 bool Empty() const; | |
| 45 | |
| 46 private: | |
| 47 std::vector<T> stack_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(SchedulerStack); | |
| 50 }; | |
| 51 | |
| 52 template <typename T> | |
| 53 SchedulerStack<T>::SchedulerStack() = default; | |
| 54 | |
| 55 template <typename T> | |
| 56 SchedulerStack<T>::~SchedulerStack() = default; | |
| 57 | |
| 58 template <typename T> | |
| 59 void SchedulerStack<T>::Push(const T& val) { | |
| 60 DCHECK(std::find(stack_.begin(), stack_.end(), val) == stack_.end()) | |
| 61 << "Value already on stack"; | |
| 62 stack_.push_back(val); | |
| 63 } | |
| 64 | |
| 65 template <typename T> | |
| 66 T SchedulerStack<T>::Pop() { | |
| 67 DCHECK(!stack_.empty()); | |
| 68 const T val = stack_.back(); | |
| 69 stack_.pop_back(); | |
| 70 return val; | |
| 71 } | |
| 72 | |
| 73 template <typename T> | |
| 74 void SchedulerStack<T>::Remove(const T& val) { | |
| 75 auto it = std::find(stack_.begin(), stack_.end(), val); | |
| 76 if (it != stack_.end()) | |
| 77 stack_.erase(it); | |
| 78 } | |
| 79 | |
| 80 template <typename T> | |
| 81 size_t SchedulerStack<T>::Size() const { | |
| 82 return stack_.size(); | |
| 83 } | |
| 84 | |
| 85 template <typename T> | |
| 86 bool SchedulerStack<T>::Empty() const { | |
| 87 return stack_.empty(); | |
| 88 } | |
| 89 | |
| 90 } // namespace internal | |
| 91 } // namespace base | |
| 92 | |
| 93 #endif // BASE_TASK_SCHEDULER_SCHEDULER_STACK_H_ | |
| OLD | NEW |