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_UNIQUE_STACK_H_ | |
6 #define BASE_TASK_SCHEDULER_SCHEDULER_UNIQUE_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 DCHECKs when a value is | |
20 // inserted multiple times. Push() is amortized O(1). Pop(), Size() and Empty() | |
21 // are O(1). Remove is O(n). This class is NOT thread-safe. | |
22 template <typename T> | |
danakj
2016/04/22 18:44:47
Are you going to use this for more than worker thr
fdoray
2016/04/22 20:45:19
Done. Template was nice to test without instantiat
| |
23 class SchedulerUniqueStack { | |
24 public: | |
25 SchedulerUniqueStack(); | |
26 ~SchedulerUniqueStack(); | |
27 | |
28 // Inserts |val| at the top of the stack. |val| must not already be on the | |
29 // stack. | |
30 void Push(const T& val); | |
31 | |
32 // Removes the top value from the stack and returns it. Cannot be called on an | |
33 // empty stack. | |
34 T Pop(); | |
35 | |
36 // Removes |val| from the stack. | |
37 void Remove(const T& val); | |
38 | |
39 // Returns the number of values on the stack. | |
40 size_t Size() const; | |
41 | |
42 // Returns true if the stack is empty. | |
43 bool Empty() const; | |
44 | |
45 private: | |
46 std::vector<T> stack_; | |
47 | |
48 DISALLOW_COPY_AND_ASSIGN(SchedulerUniqueStack); | |
49 }; | |
50 | |
51 template <typename T> | |
52 SchedulerUniqueStack<T>::SchedulerUniqueStack() = default; | |
53 | |
54 template <typename T> | |
55 SchedulerUniqueStack<T>::~SchedulerUniqueStack() = default; | |
56 | |
57 template <typename T> | |
58 void SchedulerUniqueStack<T>::Push(const T& val) { | |
59 DCHECK(std::find(stack_.begin(), stack_.end(), val) == stack_.end()) | |
60 << "Value already on stack"; | |
61 stack_.push_back(val); | |
62 } | |
63 | |
64 template <typename T> | |
65 T SchedulerUniqueStack<T>::Pop() { | |
66 DCHECK(!stack_.empty()); | |
67 const T val = stack_.back(); | |
danakj
2016/04/22 18:44:47
std::move, tho that may not make sense once this i
fdoray
2016/04/22 20:45:19
Doesn't make sense now that this isn't templated.
| |
68 stack_.pop_back(); | |
69 return val; | |
70 } | |
71 | |
72 template <typename T> | |
73 void SchedulerUniqueStack<T>::Remove(const T& val) { | |
74 auto it = std::find(stack_.begin(), stack_.end(), val); | |
75 if (it != stack_.end()) | |
76 stack_.erase(it); | |
77 } | |
78 | |
79 template <typename T> | |
80 size_t SchedulerUniqueStack<T>::Size() const { | |
81 return stack_.size(); | |
82 } | |
83 | |
84 template <typename T> | |
85 bool SchedulerUniqueStack<T>::Empty() const { | |
86 return stack_.empty(); | |
87 } | |
88 | |
89 } // namespace internal | |
90 } // namespace base | |
91 | |
92 #endif // BASE_TASK_SCHEDULER_SCHEDULER_UNIQUE_STACK_H_ | |
OLD | NEW |