Changeset 30538 in webkit for trunk/JavaScriptCore/wtf/Vector.h


Ignore:
Timestamp:
Feb 23, 2008, 9:19:27 PM (17 years ago)
Author:
Darin Adler
Message:

Reviewed by Anders.

  • wtf/Deque.h: Wrote an all-new version of this class that uses a circular buffer. Growth policy is identical to vector. Added iterators.
  • wtf/Vector.h: Made two small refinements while using this to implement Deque: Made VectorBufferBase derive from Noncopyable, which would have saved me some debugging time if it had been there. Renamed Impl and m_impl to Buffer and m_buffer.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/Vector.h

    r29470 r30538  
    2525#include "Assertions.h"
    2626#include "FastMalloc.h"
     27#include "Noncopyable.h"
    2728#include "VectorTraits.h"
    2829#include <limits>
     
    241242
    242243    template<typename T>
    243     class VectorBufferBase {
     244    class VectorBufferBase : Noncopyable {
    244245    public:
    245246        void allocateBuffer(size_t newCapacity)
     
    386387    class Vector {
    387388    private:
    388         typedef VectorBuffer<T, inlineCapacity> Impl;
     389        typedef VectorBuffer<T, inlineCapacity> Buffer;
    389390        typedef VectorTypeOperations<T> TypeOperations;
    390391
     
    402403        explicit Vector(size_t size)
    403404            : m_size(size)
    404             , m_impl(size)
     405            , m_buffer(size)
    405406        {
    406407            TypeOperations::initialize(begin(), end());
     
    421422
    422423        size_t size() const { return m_size; }
    423         size_t capacity() const { return m_impl.capacity(); }
     424        size_t capacity() const { return m_buffer.capacity(); }
    424425        bool isEmpty() const { return !size(); }
    425426
     
    427428        {
    428429            ASSERT(i < size());
    429             return m_impl.buffer()[i];
     430            return m_buffer.buffer()[i];
    430431        }
    431432        const T& at(size_t i) const
    432433        {
    433434            ASSERT(i < size());
    434             return m_impl.buffer()[i];
     435            return m_buffer.buffer()[i];
    435436        }
    436437
     
    438439        const T& operator[](size_t i) const { return at(i); }
    439440
    440         T* data() { return m_impl.buffer(); }
    441         const T* data() const { return m_impl.buffer(); }
     441        T* data() { return m_buffer.buffer(); }
     442        const T* data() const { return m_buffer.buffer(); }
    442443
    443444        iterator begin() { return data(); }
     
    481482        Vector(size_t size, const T& val)
    482483            : m_size(size)
    483             , m_impl(size)
     484            , m_buffer(size)
    484485        {
    485486            TypeOperations::uninitializedFill(begin(), end(), val);
     
    496497        {
    497498            std::swap(m_size, other.m_size);
    498             m_impl.swap(other.m_impl);
     499            m_buffer.swap(other.m_buffer);
    499500        }
    500501
     
    505506
    506507        size_t m_size;
    507         Impl m_impl;
     508        Buffer m_buffer;
    508509    };
    509510
     
    511512    Vector<T, inlineCapacity>::Vector(const Vector& other)
    512513        : m_size(other.size())
    513         , m_impl(other.capacity())
     514        , m_buffer(other.capacity())
    514515    {
    515516        TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
     
    520521    Vector<T, inlineCapacity>::Vector(const Vector<T, otherCapacity>& other)
    521522        : m_size(other.size())
    522         , m_impl(other.capacity())
     523        , m_buffer(other.capacity())
    523524    {
    524525        TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
     
    653654        T* oldBuffer = begin();
    654655        T* oldEnd = end();
    655         m_impl.allocateBuffer(newCapacity);
     656        m_buffer.allocateBuffer(newCapacity);
    656657        TypeOperations::move(oldBuffer, oldEnd, begin());
    657         m_impl.deallocateBuffer(oldBuffer);
     658        m_buffer.deallocateBuffer(oldBuffer);
    658659    }
    659660
     
    681682            ptr = expandCapacity(size() + 1, ptr);
    682683           
     684#if COMPILER(MSVC7)
    683685        // FIXME: MSVC7 generates compilation errors when trying to assign
    684686        // a pointer to a Vector of its base class (i.e. can't downcast). So far
    685687        // I've been unable to determine any logical reason for this, so I can
    686         // only assume it is a bug with the compiler. Casting is very bad
    687         // however because it subverts implicit conversions, so a better
    688         // solution is direly needed.
    689 #if COMPILER(MSVC7)
     688        // only assume it is a bug with the compiler. Casting is a bad solution,
     689        // however, because it subverts implicit conversions, so a better
     690        // one is needed.
    690691        new (end()) T(static_cast<T>(*ptr));
    691692#else
     
    777778    inline T* Vector<T, inlineCapacity>::releaseBuffer()
    778779    {
    779         T* buffer = m_impl.releaseBuffer();
     780        T* buffer = m_buffer.releaseBuffer();
    780781        if (inlineCapacity && !buffer && m_size) {
    781782            // If the vector had some data, but no buffer to release,
Note: See TracChangeset for help on using the changeset viewer.