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 #include <stdint.h> |
| 10 |
| 11 #include <vector> |
| 12 |
| 13 #include "base/base_export.h" |
| 14 |
| 15 namespace base { |
| 16 namespace internal { |
| 17 |
| 18 // A stack of integers in [0, 63]. Supports removal of arbitrary values. Doesn't |
| 19 // allow multiple insertions of the same value. |
| 20 // |
| 21 // The stack can only contain integers in [0, 63] because it maintains a bit |
| 22 // field that indicates whether a value is on the stack. |
| 23 // |
| 24 // Push() runs in amortized O(1). Remove() runs in O(1). Pop() runs in O(x + 1) |
| 25 // where x is the number of values removed from the stack since the previous |
| 26 // Pop(). |
| 27 |
| 28 // This class is NOT thread-safe. |
| 29 class BASE_EXPORT SchedulerStack { |
| 30 public: |
| 31 SchedulerStack(); |
| 32 ~SchedulerStack(); |
| 33 |
| 34 // Inserts |val| at the top of the stack. |val| must be in [0, 63] and must |
| 35 // not already be on the stack. |
| 36 void Push(int val); |
| 37 |
| 38 // Removes the top value from the stack and returns it. Cannot be called on an |
| 39 // empty stack. |
| 40 int Pop(); |
| 41 |
| 42 // Removes |val| from the stack if it was on it. |
| 43 void Remove(int val); |
| 44 |
| 45 // Returns the number of values on the stack. |
| 46 size_t Size() const { return size_; } |
| 47 |
| 48 // Returns true if the stack is empty. |
| 49 bool Empty() const { return size_ == 0; } |
| 50 |
| 51 private: |
| 52 std::vector<int> stack_; |
| 53 |
| 54 // A bit field that indicates whether a value is on |stack_|. |
| 55 uint64_t bit_field_ = 0; |
| 56 |
| 57 // Number of elements on the stack. |
| 58 size_t size_ = 0; |
| 59 }; |
| 60 |
| 61 } // namespace internal |
| 62 } // namespace base |
| 63 |
| 64 #endif // BASE_TASK_SCHEDULER_SCHEDULER_STACK_H_ |
OLD | NEW |