Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(327)

Side by Side Diff: cc/base/random_access_list_container.h

Issue 1340703002: cc: Add RandomAccessListContainer, which has more restricted API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « cc/base/list_container_helper.h ('k') | cc/base/random_access_list_container_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
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)) {}
34
35 // This constructor reserves the requested memory up front so only single
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 15:25:40 BaseElementType* const here?
vmpstr 2015/09/15 18:10:25 I've added a non const overload.
73 DCHECK_GE(index, 0u);
74 DCHECK_LT(index, items_.size());
75 return items_[index];
76 }
77
78 using ConstIterator = typename std::deque<BaseElementType*>::const_iterator;
79 ConstIterator begin() const { return items_.begin(); }
80 ConstIterator end() const { return items_.end(); }
81
82 private:
83 ListContainerHelper helper_;
84 std::deque<BaseElementType*> items_;
85 };
86
87 } // namespace cc
88
89 #endif // CC_BASE_RANDOM_ACCESS_LIST_CONTAINER_H_
OLDNEW
« no previous file with comments | « cc/base/list_container_helper.h ('k') | cc/base/random_access_list_container_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698