How Deque is Implemented in C++:

C++'s implementation fo deque also uses a dynamic array philosophy, which is very similar to our exercies.

The difference is that because C++ deque needs to hold general objects, it stores the pointers in the dynamic array.

Try to have a look of this push_back, can you tell how C++ increases the size of its buffer ?

    void push_back(const T& value) {
        if (size == 0 || finish == BLOCK_SIZE) {
            if (back_block == map_size - 1) {
                // Resize map
                size_t new_map_size = map_size * 2;
                T** new_map = new T*[new_map_size];
                for (size_t i = 0; i < map_size; ++i) {
                    new_map[i] = map[i];
                }
                for (size_t i = map_size; i < new_map_size; ++i) {
                    new_map[i] = nullptr;
                }
                delete[] map;
                map = new_map;
                map_size = new_map_size;
            }
            if (map[back_block + 1] == nullptr) {
                map[back_block + 1] = new T[BLOCK_SIZE];
            }
            ++back_block;
            finish = 0;
        }
        map[back_block][finish] = value;
        ++finish;
        ++size;
    }

Last updated