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 #include "base/task_scheduler/scheduler_stack.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 | |
| 11 namespace base { | |
| 12 namespace internal { | |
| 13 | |
| 14 SchedulerStack::SchedulerStack() = default; | |
| 15 | |
| 16 SchedulerStack::~SchedulerStack() = default; | |
| 17 | |
| 18 void SchedulerStack::Push(int val) { | |
| 19 DCHECK_GE(val, 0); | |
| 20 DCHECK_LE(val, 63); | |
| 21 DCHECK(!Contains(val)); | |
| 22 | |
| 23 stack_.push_back(val); | |
| 24 bit_field_ |= 1ULL << val; | |
|
gab
2016/04/18 18:35:40
Don't think it's guaranteed that ULL will always b
fdoray
2016/04/18 20:33:12
Done.
| |
| 25 } | |
| 26 | |
| 27 int SchedulerStack::Pop() { | |
| 28 DCHECK(!Empty()); | |
| 29 | |
| 30 const int val = stack_.back(); | |
| 31 stack_.pop_back(); | |
| 32 bit_field_ &= ~(1ULL << val); | |
| 33 | |
| 34 return val; | |
| 35 } | |
| 36 | |
| 37 void SchedulerStack::Remove(int val) { | |
| 38 if (!Contains(val)) | |
| 39 return; | |
| 40 | |
| 41 auto it = std::find(stack_.begin(), stack_.end(), val); | |
| 42 DCHECK(it != stack_.end()); | |
| 43 stack_.erase(it); | |
|
gab
2016/04/18 18:35:40
Lines 41+43 are O(2n) :-(. Can we make Remove() co
fdoray
2016/04/18 20:33:12
Done.
| |
| 44 bit_field_ &= ~(1ULL << val); | |
| 45 } | |
| 46 | |
| 47 size_t SchedulerStack::Size() const { | |
| 48 return stack_.size(); | |
| 49 } | |
| 50 | |
| 51 bool SchedulerStack::Empty() const { | |
| 52 return stack_.empty(); | |
| 53 } | |
| 54 | |
| 55 bool SchedulerStack::Contains(int val) const { | |
| 56 DCHECK_GE(val, 0); | |
| 57 DCHECK_LE(val, 63); | |
| 58 return (bit_field_ & (1ULL << val)) != 0; | |
| 59 } | |
| 60 | |
| 61 } // namespace internal | |
| 62 } // namespace base | |
| OLD | NEW |