Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / cc / base / list_container_helper.cc
blob65be43adb3888183392f7930b1c69e67a0f7a9dd
1 // Copyright 2014 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.
5 #include "cc/base/list_container_helper.h"
7 #include <algorithm>
8 #include <vector>
10 #include "cc/base/scoped_ptr_vector.h"
12 namespace {
13 const size_t kDefaultNumElementTypesToReserve = 32;
14 } // namespace
16 namespace cc {
18 // CharAllocator
19 ////////////////////////////////////////////////////
20 // This class deals only with char* and void*. It does allocation and passing
21 // out raw pointers, as well as memory deallocation when being destroyed.
22 class ListContainerHelper::CharAllocator {
23 public:
24 // CharAllocator::InnerList
25 /////////////////////////////////////////////
26 // This class holds the raw memory chunk, as well as information about its
27 // size and availability.
28 struct InnerList {
29 scoped_ptr<char[]> data;
30 // The number of elements in total the memory can hold. The difference
31 // between capacity and size is the how many more elements this list can
32 // hold.
33 size_t capacity;
34 // The number of elements have been put into this list.
35 size_t size;
36 // The size of each element is in bytes. This is used to move from between
37 // elements' memory locations.
38 size_t step;
40 InnerList() : capacity(0), size(0), step(0) {}
42 void Erase(char* position) {
43 // Confident that destructor is called by caller of this function. Since
44 // CharAllocator does not handle construction after
45 // allocation, it doesn't handle desctrution before deallocation.
46 DCHECK_LE(position, LastElement());
47 DCHECK_GE(position, Begin());
48 char* start = position + step;
49 std::copy(start, End(), position);
51 --size;
52 // Decrease capacity to avoid creating not full not last InnerList.
53 --capacity;
56 void InsertBefore(char** position, size_t count) {
57 DCHECK_LE(*position, LastElement() + step);
58 DCHECK_GE(*position, Begin());
60 // Adjust the size and capacity
61 size_t old_size = size;
62 size += count;
63 capacity = size;
65 // Allocate the new data and update the iterator's pointer.
66 scoped_ptr<char[]> new_data(new char[size * step]);
67 size_t position_offset = *position - Begin();
68 *position = new_data.get() + position_offset;
70 // Copy the data before the inserted segment
71 memcpy(new_data.get(), data.get(), position_offset);
72 // Copy the data after the inserted segment.
73 memcpy(new_data.get() + position_offset + count * step,
74 data.get() + position_offset, old_size * step - position_offset);
75 new_data.swap(data);
78 bool IsEmpty() const { return !size; }
79 bool IsFull() { return capacity == size; }
80 size_t NumElementsAvailable() const { return capacity - size; }
82 void* AddElement() {
83 DCHECK_LT(size, capacity);
84 ++size;
85 return LastElement();
88 void RemoveLast() {
89 DCHECK(!IsEmpty());
90 --size;
93 char* Begin() const { return data.get(); }
94 char* End() const { return data.get() + size * step; }
95 char* LastElement() const { return data.get() + (size - 1) * step; }
96 char* ElementAt(size_t index) const { return data.get() + index * step; }
98 private:
99 DISALLOW_COPY_AND_ASSIGN(InnerList);
102 explicit CharAllocator(size_t element_size)
103 : element_size_(element_size),
104 size_(0),
105 last_list_index_(0),
106 last_list_(NULL) {
107 AllocateNewList(kDefaultNumElementTypesToReserve);
108 last_list_ = storage_[last_list_index_];
111 CharAllocator(size_t element_size, size_t element_count)
112 : element_size_(element_size),
113 size_(0),
114 last_list_index_(0),
115 last_list_(NULL) {
116 AllocateNewList(element_count > 0 ? element_count
117 : kDefaultNumElementTypesToReserve);
118 last_list_ = storage_[last_list_index_];
121 ~CharAllocator() {}
123 void* Allocate() {
124 if (last_list_->IsFull()) {
125 // Only allocate a new list if there isn't a spare one still there from
126 // previous usage.
127 if (last_list_index_ + 1 >= storage_.size())
128 AllocateNewList(last_list_->capacity * 2);
130 ++last_list_index_;
131 last_list_ = storage_[last_list_index_];
134 ++size_;
135 return last_list_->AddElement();
138 size_t element_size() const { return element_size_; }
139 size_t list_count() const { return storage_.size(); }
140 size_t size() const { return size_; }
141 bool IsEmpty() const { return size() == 0; }
143 size_t Capacity() const {
144 size_t capacity_sum = 0;
145 for (const auto& inner_list : storage_)
146 capacity_sum += inner_list->capacity;
147 return capacity_sum;
150 void Clear() {
151 // Remove all except for the first InnerList.
152 DCHECK(!storage_.empty());
153 storage_.erase(storage_.begin() + 1, storage_.end());
154 last_list_index_ = 0;
155 last_list_ = storage_[0];
156 last_list_->size = 0;
157 size_ = 0;
160 void RemoveLast() {
161 DCHECK(!IsEmpty());
162 last_list_->RemoveLast();
163 if (last_list_->IsEmpty() && last_list_index_ > 0) {
164 --last_list_index_;
165 last_list_ = storage_[last_list_index_];
167 // If there are now two empty inner lists, free one of them.
168 if (last_list_index_ + 2 < storage_.size())
169 storage_.pop_back();
171 --size_;
174 void Erase(PositionInCharAllocator* position) {
175 DCHECK_EQ(this, position->ptr_to_container);
177 // Update |position| to point to the element after the erased element.
178 InnerList* list = storage_[position->vector_index];
179 char* item_iterator = position->item_iterator;
180 if (item_iterator == list->LastElement())
181 position->Increment();
183 list->Erase(item_iterator);
184 // TODO(weiliangc): Free the InnerList if it is empty.
185 --size_;
188 void InsertBefore(ListContainerHelper::Iterator* position, size_t count) {
189 if (!count)
190 return;
192 // If |position| is End(), then append |count| elements at the end. This
193 // will happen to not invalidate any iterators or memory.
194 if (!position->item_iterator) {
195 // Set |position| to be the first inserted element.
196 Allocate();
197 position->vector_index = storage_.size() - 1;
198 position->item_iterator = storage_[position->vector_index]->LastElement();
199 // Allocate the rest.
200 for (size_t i = 1; i < count; ++i)
201 Allocate();
202 } else {
203 storage_[position->vector_index]->InsertBefore(&position->item_iterator,
204 count);
205 size_ += count;
209 InnerList* InnerListById(size_t id) const {
210 DCHECK_LT(id, storage_.size());
211 return storage_[id];
214 size_t FirstInnerListId() const {
215 // |size_| > 0 means that at least one vector in |storage_| will be
216 // non-empty.
217 DCHECK_GT(size_, 0u);
218 size_t id = 0;
219 while (storage_[id]->size == 0)
220 ++id;
221 return id;
224 size_t LastInnerListId() const {
225 // |size_| > 0 means that at least one vector in |storage_| will be
226 // non-empty.
227 DCHECK_GT(size_, 0u);
228 size_t id = storage_.size() - 1;
229 while (storage_[id]->size == 0)
230 --id;
231 return id;
234 size_t NumAvailableElementsInLastList() const {
235 return last_list_->NumElementsAvailable();
238 private:
239 void AllocateNewList(size_t list_size) {
240 scoped_ptr<InnerList> new_list(new InnerList);
241 new_list->capacity = list_size;
242 new_list->size = 0;
243 new_list->step = element_size_;
244 new_list->data.reset(new char[list_size * element_size_]);
245 storage_.push_back(new_list.Pass());
248 ScopedPtrVector<InnerList> storage_;
249 const size_t element_size_;
251 // The number of elements in the list.
252 size_t size_;
254 // The index of the last list to have had elements added to it, or the only
255 // list if the container has not had elements added since being cleared.
256 size_t last_list_index_;
258 // This is equivalent to |storage_[last_list_index_]|.
259 InnerList* last_list_;
261 DISALLOW_COPY_AND_ASSIGN(CharAllocator);
264 // PositionInCharAllocator
265 //////////////////////////////////////////////////////
266 ListContainerHelper::PositionInCharAllocator::PositionInCharAllocator(
267 const ListContainerHelper::PositionInCharAllocator& other)
268 : ptr_to_container(other.ptr_to_container),
269 vector_index(other.vector_index),
270 item_iterator(other.item_iterator) {}
272 ListContainerHelper::PositionInCharAllocator::PositionInCharAllocator(
273 ListContainerHelper::CharAllocator* container,
274 size_t vector_ind,
275 char* item_iter)
276 : ptr_to_container(container),
277 vector_index(vector_ind),
278 item_iterator(item_iter) {}
280 bool ListContainerHelper::PositionInCharAllocator::operator==(
281 const ListContainerHelper::PositionInCharAllocator& other) const {
282 DCHECK_EQ(ptr_to_container, other.ptr_to_container);
283 return vector_index == other.vector_index &&
284 item_iterator == other.item_iterator;
287 bool ListContainerHelper::PositionInCharAllocator::operator!=(
288 const ListContainerHelper::PositionInCharAllocator& other) const {
289 return !(*this == other);
292 ListContainerHelper::PositionInCharAllocator
293 ListContainerHelper::PositionInCharAllocator::Increment() {
294 CharAllocator::InnerList* list =
295 ptr_to_container->InnerListById(vector_index);
296 if (item_iterator == list->LastElement()) {
297 ++vector_index;
298 while (vector_index < ptr_to_container->list_count()) {
299 if (ptr_to_container->InnerListById(vector_index)->size != 0)
300 break;
301 ++vector_index;
303 if (vector_index < ptr_to_container->list_count())
304 item_iterator = ptr_to_container->InnerListById(vector_index)->Begin();
305 else
306 item_iterator = NULL;
307 } else {
308 item_iterator += list->step;
310 return *this;
313 ListContainerHelper::PositionInCharAllocator
314 ListContainerHelper::PositionInCharAllocator::ReverseIncrement() {
315 CharAllocator::InnerList* list =
316 ptr_to_container->InnerListById(vector_index);
317 if (item_iterator == list->Begin()) {
318 --vector_index;
319 // Since |vector_index| is unsigned, we compare < list_count() instead of
320 // comparing >= 0, as the variable will wrap around when it goes out of
321 // range (below 0).
322 while (vector_index < ptr_to_container->list_count()) {
323 if (ptr_to_container->InnerListById(vector_index)->size != 0)
324 break;
325 --vector_index;
327 if (vector_index < ptr_to_container->list_count()) {
328 item_iterator =
329 ptr_to_container->InnerListById(vector_index)->LastElement();
330 } else {
331 item_iterator = NULL;
333 } else {
334 item_iterator -= list->step;
336 return *this;
339 // ListContainerHelper
340 ////////////////////////////////////////////
341 ListContainerHelper::ListContainerHelper(size_t max_size_for_derived_class)
342 : data_(new CharAllocator(max_size_for_derived_class)) {}
344 ListContainerHelper::ListContainerHelper(size_t max_size_for_derived_class,
345 size_t num_of_elements_to_reserve_for)
346 : data_(new CharAllocator(max_size_for_derived_class,
347 num_of_elements_to_reserve_for)) {}
349 ListContainerHelper::~ListContainerHelper() {}
351 void ListContainerHelper::RemoveLast() {
352 data_->RemoveLast();
355 void ListContainerHelper::EraseAndInvalidateAllPointers(
356 ListContainerHelper::Iterator* position) {
357 data_->Erase(position);
360 void ListContainerHelper::InsertBeforeAndInvalidateAllPointers(
361 ListContainerHelper::Iterator* position,
362 size_t count) {
363 data_->InsertBefore(position, count);
366 ListContainerHelper::ConstReverseIterator ListContainerHelper::crbegin() const {
367 if (data_->IsEmpty())
368 return crend();
370 size_t id = data_->LastInnerListId();
371 return ConstReverseIterator(data_.get(), id,
372 data_->InnerListById(id)->LastElement(), 0);
375 ListContainerHelper::ConstReverseIterator ListContainerHelper::crend() const {
376 return ConstReverseIterator(data_.get(), static_cast<size_t>(-1), NULL,
377 size());
380 ListContainerHelper::ReverseIterator ListContainerHelper::rbegin() {
381 if (data_->IsEmpty())
382 return rend();
384 size_t id = data_->LastInnerListId();
385 return ReverseIterator(data_.get(), id,
386 data_->InnerListById(id)->LastElement(), 0);
389 ListContainerHelper::ReverseIterator ListContainerHelper::rend() {
390 return ReverseIterator(data_.get(), static_cast<size_t>(-1), NULL, size());
393 ListContainerHelper::ConstIterator ListContainerHelper::cbegin() const {
394 if (data_->IsEmpty())
395 return cend();
397 size_t id = data_->FirstInnerListId();
398 return ConstIterator(data_.get(), id, data_->InnerListById(id)->Begin(), 0);
401 ListContainerHelper::ConstIterator ListContainerHelper::cend() const {
402 if (data_->IsEmpty())
403 return ConstIterator(data_.get(), 0, NULL, size());
405 size_t id = data_->list_count();
406 return ConstIterator(data_.get(), id, NULL, size());
409 ListContainerHelper::Iterator ListContainerHelper::begin() {
410 if (data_->IsEmpty())
411 return end();
413 size_t id = data_->FirstInnerListId();
414 return Iterator(data_.get(), id, data_->InnerListById(id)->Begin(), 0);
417 ListContainerHelper::Iterator ListContainerHelper::end() {
418 if (data_->IsEmpty())
419 return Iterator(data_.get(), 0, NULL, size());
421 size_t id = data_->list_count();
422 return Iterator(data_.get(), id, NULL, size());
425 ListContainerHelper::ConstIterator ListContainerHelper::IteratorAt(
426 size_t index) const {
427 DCHECK_LT(index, size());
428 size_t original_index = index;
429 size_t list_index;
430 for (list_index = 0; list_index < data_->list_count(); ++list_index) {
431 size_t current_size = data_->InnerListById(list_index)->size;
432 if (index < current_size)
433 break;
434 index -= current_size;
436 return ConstIterator(data_.get(), list_index,
437 data_->InnerListById(list_index)->ElementAt(index),
438 original_index);
441 ListContainerHelper::Iterator ListContainerHelper::IteratorAt(size_t index) {
442 DCHECK_LT(index, size());
443 size_t original_index = index;
444 size_t list_index;
445 for (list_index = 0; list_index < data_->list_count(); ++list_index) {
446 size_t current_size = data_->InnerListById(list_index)->size;
447 if (index < current_size)
448 break;
449 index -= current_size;
451 return Iterator(data_.get(), list_index,
452 data_->InnerListById(list_index)->ElementAt(index),
453 original_index);
456 void* ListContainerHelper::Allocate(size_t size_of_actual_element_in_bytes) {
457 DCHECK_LE(size_of_actual_element_in_bytes, data_->element_size());
458 return data_->Allocate();
461 size_t ListContainerHelper::size() const {
462 return data_->size();
465 bool ListContainerHelper::empty() const {
466 return data_->IsEmpty();
469 size_t ListContainerHelper::MaxSizeForDerivedClass() const {
470 return data_->element_size();
473 size_t ListContainerHelper::GetCapacityInBytes() const {
474 return data_->Capacity() * data_->element_size();
477 void ListContainerHelper::clear() {
478 data_->Clear();
481 size_t ListContainerHelper::AvailableSizeWithoutAnotherAllocationForTesting()
482 const {
483 return data_->NumAvailableElementsInLastList();
486 // ListContainerHelper::Iterator
487 /////////////////////////////////////////////////
488 ListContainerHelper::Iterator::Iterator(CharAllocator* container,
489 size_t vector_ind,
490 char* item_iter,
491 size_t index)
492 : PositionInCharAllocator(container, vector_ind, item_iter),
493 index_(index) {}
495 ListContainerHelper::Iterator::~Iterator() {}
497 size_t ListContainerHelper::Iterator::index() const {
498 return index_;
501 // ListContainerHelper::ConstIterator
502 /////////////////////////////////////////////////
503 ListContainerHelper::ConstIterator::ConstIterator(
504 const ListContainerHelper::Iterator& other)
505 : PositionInCharAllocator(other), index_(other.index()) {}
507 ListContainerHelper::ConstIterator::ConstIterator(CharAllocator* container,
508 size_t vector_ind,
509 char* item_iter,
510 size_t index)
511 : PositionInCharAllocator(container, vector_ind, item_iter),
512 index_(index) {}
514 ListContainerHelper::ConstIterator::~ConstIterator() {}
516 size_t ListContainerHelper::ConstIterator::index() const {
517 return index_;
520 // ListContainerHelper::ReverseIterator
521 /////////////////////////////////////////////////
522 ListContainerHelper::ReverseIterator::ReverseIterator(CharAllocator* container,
523 size_t vector_ind,
524 char* item_iter,
525 size_t index)
526 : PositionInCharAllocator(container, vector_ind, item_iter),
527 index_(index) {}
529 ListContainerHelper::ReverseIterator::~ReverseIterator() {}
531 size_t ListContainerHelper::ReverseIterator::index() const {
532 return index_;
535 // ListContainerHelper::ConstReverseIterator
536 /////////////////////////////////////////////////
537 ListContainerHelper::ConstReverseIterator::ConstReverseIterator(
538 const ListContainerHelper::ReverseIterator& other)
539 : PositionInCharAllocator(other), index_(other.index()) {}
541 ListContainerHelper::ConstReverseIterator::ConstReverseIterator(
542 CharAllocator* container,
543 size_t vector_ind,
544 char* item_iter,
545 size_t index)
546 : PositionInCharAllocator(container, vector_ind, item_iter),
547 index_(index) {}
549 ListContainerHelper::ConstReverseIterator::~ConstReverseIterator() {}
551 size_t ListContainerHelper::ConstReverseIterator::index() const {
552 return index_;
555 } // namespace cc