Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 CC_BASE_RANDOM_ACCESS_LIST_CONTAINER_H_ | |
| 6 #define CC_BASE_RANDOM_ACCESS_LIST_CONTAINER_H_ | |
| 7 | |
| 8 #include <deque> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "cc/base/list_container_helper.h" | |
| 12 | |
| 13 namespace cc { | |
| 14 | |
| 15 // RandomAccessListContainer is a container similar to ListContainer (see | |
| 16 // list_container.h), but it allows random access into its elements via | |
| 17 // operator[]. In order to have efficient support for random access, some | |
| 18 // functionality is not available for RandomAccessListContainers, such as | |
| 19 // insert/deletes in the middle of the list. | |
| 20 template <class BaseElementType> | |
| 21 class RandomAccessListContainer { | |
| 22 public: | |
| 23 // BaseElementType is the type of raw pointers this class hands out; however, | |
| 24 // its derived classes might require different memory sizes. | |
| 25 // max_size_for_derived_class the largest memory size required for all the | |
| 26 // derived classes to use for allocation. | |
| 27 explicit RandomAccessListContainer(size_t max_size_for_derived_class) | |
| 28 : helper_(max_size_for_derived_class) {} | |
| 29 | |
| 30 // This constructor omits input variable for max_size_for_derived_class. This | |
|
danakj
2015/09/15 20:21:37
"omits /the/ input variable for"
vmpstr
2015/09/15 21:05:52
Done.
| |
| 31 // is used when there is no derived classes from BaseElementType we need to | |
| 32 // worry about, and allocation size is just sizeof(BaseElementType). | |
| 33 RandomAccessListContainer() : helper_(sizeof(BaseElementType)) {} | |
|
danakj
2015/09/15 20:21:37
will there be any callers of this? do we need it?
vmpstr
2015/09/15 21:05:52
Removed.
| |
| 34 | |
| 35 // This constructor reserves the requested memory up front so only single | |
|
danakj
2015/09/15 20:21:37
only a single
vmpstr
2015/09/15 21:05:52
Done.
| |
| 36 // allocation is needed. When num_of_elements_to_reserve_for is zero, use the | |
| 37 // default size. | |
| 38 RandomAccessListContainer(size_t max_size_for_derived_class, | |
| 39 size_t num_of_elements_to_reserve_for) | |
| 40 : helper_(max_size_for_derived_class, num_of_elements_to_reserve_for) {} | |
| 41 | |
| 42 ~RandomAccessListContainer() { | |
| 43 for (BaseElementType* item : items_) | |
| 44 item->~BaseElementType(); | |
| 45 } | |
| 46 | |
| 47 void clear() { | |
| 48 for (BaseElementType* item : items_) | |
| 49 item->~BaseElementType(); | |
| 50 helper_.clear(); | |
| 51 items_.clear(); | |
| 52 } | |
| 53 | |
| 54 bool empty() const { return helper_.empty(); } | |
| 55 size_t size() const { return helper_.size(); } | |
| 56 size_t GetCapacityInBytes() const { return helper_.GetCapacityInBytes(); } | |
| 57 | |
| 58 template <typename DerivedElementType> | |
| 59 DerivedElementType* AllocateAndConstruct() { | |
| 60 auto* value = | |
| 61 new (helper_.Allocate(sizeof(DerivedElementType))) DerivedElementType; | |
| 62 items_.push_back(value); | |
| 63 return value; | |
| 64 } | |
| 65 | |
| 66 void RemoveLast() { | |
| 67 items_.back()->~BaseElementType(); | |
| 68 items_.pop_back(); | |
| 69 helper_.RemoveLast(); | |
| 70 } | |
| 71 | |
| 72 const BaseElementType* operator[](size_t index) const { | |
|
weiliangc
2015/09/15 19:01:17
nit: // Return a const pointer isn't very useful.
vmpstr
2015/09/15 20:20:00
Ehh I'd prefer to leave that comment off, unless y
| |
| 73 DCHECK_GE(index, 0u); | |
| 74 DCHECK_LT(index, items_.size()); | |
| 75 return items_[index]; | |
| 76 } | |
| 77 | |
| 78 BaseElementType* operator[](size_t index) { | |
|
danakj
2015/09/15 20:21:37
can't this still be a const method? it's returning
vmpstr
2015/09/15 21:05:52
Can't overload methods on the return type. This is
danakj
2015/09/15 21:19:50
oh.. ok thanks
| |
| 79 DCHECK_GE(index, 0u); | |
| 80 DCHECK_LT(index, items_.size()); | |
| 81 return items_[index]; | |
| 82 } | |
| 83 | |
| 84 // Note that although BaseElementType objects can change, the pointer itself | |
| 85 // (in the deque) cannot. So this class only supports a const iterator. | |
| 86 using ConstIterator = typename std::deque<BaseElementType*>::const_iterator; | |
| 87 ConstIterator begin() const { return items_.begin(); } | |
| 88 ConstIterator end() const { return items_.end(); } | |
| 89 | |
| 90 private: | |
| 91 ListContainerHelper helper_; | |
| 92 std::deque<BaseElementType*> items_; | |
|
danakj
2015/09/15 20:21:37
Can you leave a comment about why you're using a d
vmpstr
2015/09/15 21:05:52
I can change it to a vector, if you'd prefer. I we
danakj
2015/09/15 21:19:50
I'd like to change that when we choose a specific
vmpstr
2015/09/15 22:06:58
Switched to a vector.
| |
| 93 }; | |
| 94 | |
| 95 } // namespace cc | |
| 96 | |
| 97 #endif // CC_BASE_RANDOM_ACCESS_LIST_CONTAINER_H_ | |
| OLD | NEW |