19 #ifndef PAGESPEED_KERNEL_BASE_VECTOR_DEQUE_H_
20 #define PAGESPEED_KERNEL_BASE_VECTOR_DEQUE_H_
27 namespace net_instaweb {
53 size_minus_1_(static_cast<size_t>(-1)),
54 capacity_minus_1_(initial_capacity() - 1),
55 data_(new T[initial_capacity()]) {
62 static size_t initial_capacity() {
return 4; }
64 void push_back(T value) {
67 *PointerAt(size_minus_1_) = value;
73 # define SPECIAL_CASE_POINTER_AT_0 1
75 void push_front(T value) {
77 start_position_ = ModCapacity(start_position_ - 1);
78 # if SPECIAL_CASE_POINTER_AT_0
79 *PointerAt0() = value;
81 *PointerAt(0) = value;
91 start_position_ = ModCapacity(start_position_ + 1);
96 return *PointerAt(size_minus_1_);
100 # if SPECIAL_CASE_POINTER_AT_0
101 return *PointerAt0();
103 return *PointerAt(0);
107 size_t capacity()
const {
return capacity_minus_1_ + 1; }
108 size_t size()
const {
return size_minus_1_ + 1; }
109 bool empty()
const {
return size_minus_1_ ==
static_cast<size_t>(-1); }
116 size_t ModCapacity(
size_t index)
const {
return index & capacity_minus_1_; }
119 T* PointerAt(
size_t position) {
120 return data_ + ModCapacity(start_position_ + position);
123 const T* PointerAt(
size_t position)
const {
124 return data_ + ModCapacity(start_position_ + position);
127 # if SPECIAL_CASE_POINTER_AT_0
129 return data_ + start_position_;
132 const T* PointerAt0()
const {
133 return data_ + start_position_;
136 # undef SPECIAL_CASE_POINTER_AT_0
140 void ExpandIfNecessary() {
142 if (size_minus_1_ == capacity_minus_1_) {
165 capacity_minus_1_ = 2 * capacity() - 1;
167 data_ =
new T[capacity()];
169 if (start_position_ == 0) {
170 memcpy(data_, old_data, sz *
sizeof(*data_));
174 memcpy(data_, old_data, start_position_ *
sizeof(*data_));
175 size_t size_of_right_chunk = sz - start_position_;
176 size_t new_start_position = start_position_ + sz;
177 memcpy(data_ + new_start_position,
178 old_data + start_position_,
179 size_of_right_chunk *
sizeof(*data_));
180 start_position_ = new_start_position;
186 size_t start_position_;
187 size_t size_minus_1_;
188 size_t capacity_minus_1_;
Definition: vector_deque.h:47
VectorDeque()
Definition: vector_deque.h:51