Index: base/task_scheduler/scheduler_stack.h |
diff --git a/base/task_scheduler/scheduler_stack.h b/base/task_scheduler/scheduler_stack.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2c9647357e265718187720226697bbb5bbb0f0b9 |
--- /dev/null |
+++ b/base/task_scheduler/scheduler_stack.h |
@@ -0,0 +1,93 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef BASE_TASK_SCHEDULER_SCHEDULER_STACK_H_ |
+#define BASE_TASK_SCHEDULER_SCHEDULER_STACK_H_ |
+ |
+#include <stddef.h> |
+ |
+#include <algorithm> |
+#include <vector> |
+ |
+#include "base/logging.h" |
+#include "base/macros.h" |
+ |
+namespace base { |
+namespace internal { |
+ |
+// A stack that supports removal of arbitrary values and doesn't allow multiple |
+// insertions of the same value. |
+ |
robliao
2016/04/20 20:54:58
Remove extra linebreak.
fdoray
2016/04/20 21:04:41
Done.
|
+// This class is NOT thread-safe. |
+template <typename T> |
+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.
|
+ public: |
+ SchedulerStack(); |
+ ~SchedulerStack(); |
+ |
+ // Inserts |val| at the top of the stack. |val| must not already be on the |
+ // stack. |
+ void Push(const T& val); |
+ |
+ // Removes the top value from the stack and returns it. Cannot be called on an |
+ // empty stack. |
+ 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
|
+ |
+ // Removes |val| from the stack if it was on it. |
+ void Remove(const T& val); |
+ |
+ // Returns the number of values on the stack. |
+ size_t Size() const; |
+ |
+ // Returns true if the stack is empty. |
+ bool Empty() const; |
+ |
+ private: |
+ std::vector<T> stack_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SchedulerStack); |
+}; |
+ |
+template <typename T> |
+SchedulerStack<T>::SchedulerStack() = default; |
+ |
+template <typename T> |
+SchedulerStack<T>::~SchedulerStack() = default; |
+ |
+template <typename T> |
+void SchedulerStack<T>::Push(const T& val) { |
+ DCHECK(std::find(stack_.begin(), stack_.end(), val) == stack_.end()) |
+ << "Value already on stack"; |
+ stack_.push_back(val); |
+} |
+ |
+template <typename T> |
+T SchedulerStack<T>::Pop() { |
+ DCHECK(!stack_.empty()); |
+ const T val = stack_.back(); |
+ stack_.pop_back(); |
+ return val; |
+} |
+ |
+template <typename T> |
+void SchedulerStack<T>::Remove(const T& val) { |
+ auto it = std::find(stack_.begin(), stack_.end(), val); |
+ if (it != stack_.end()) |
+ stack_.erase(it); |
+} |
+ |
+template <typename T> |
+size_t SchedulerStack<T>::Size() const { |
+ return stack_.size(); |
+} |
+ |
+template <typename T> |
+bool SchedulerStack<T>::Empty() const { |
+ return stack_.empty(); |
+} |
+ |
+} // namespace internal |
+} // namespace base |
+ |
+#endif // BASE_TASK_SCHEDULER_SCHEDULER_STACK_H_ |