Reviewed by Anders.
authordarin <darin@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 22 Oct 2006 01:55:53 +0000 (01:55 +0000)
committerdarin <darin@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 22 Oct 2006 01:55:53 +0000 (01:55 +0000)
        - http://bugs.webkit.org/show_bug.cgi?id=11377
          swap(Vector, Vector) should be O(1) instead of O(n)

        * wtf/Vector.h:
        (WTF::VectorBuffer::swap): Added.
        (WTF::Vector::swap): Added.
        (WTF::swap): Added overload that takes two Vector objects.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@17200 268f45cc-cd09-0410-ab3c-d52691b4dbfc

JavaScriptCore/ChangeLog
JavaScriptCore/wtf/Vector.h

index 43dda6ac3831cfe4616c30d52c4aef5a9791ef80..04c67776bd56eac485b3f3c1575ca90c5538f731 100644 (file)
@@ -1,3 +1,15 @@
+2006-10-21  Darin Adler  <darin@apple.com>
+
+        Reviewed by Anders.
+
+        - http://bugs.webkit.org/show_bug.cgi?id=11377
+          swap(Vector, Vector) should be O(1) instead of O(n)
+
+        * wtf/Vector.h:
+        (WTF::VectorBuffer::swap): Added.
+        (WTF::Vector::swap): Added.
+        (WTF::swap): Added overload that takes two Vector objects.
+
 2006-10-21  Darin Adler  <darin@apple.com>
 
         Reviewed by Adam.
index 96377fbbe6ddd66b3a4223b71e71f74f25dd4a8d..89f5fd518b252d9769c6775529185107965fb710 100644 (file)
@@ -82,7 +82,7 @@ namespace WTF {
     {
         static void initialize(T* begin, T* end) 
         {
-            memset(begin, 0, reinterpret_cast<char *>(end) - reinterpret_cast<char *>(begin));
+            memset(begin, 0, reinterpret_cast<char*>(end) - reinterpret_cast<char*>(begin));
         }
     };
 
@@ -122,11 +122,11 @@ namespace WTF {
     {
         static void move(const T* src, const T* srcEnd, T* dst) 
         {
-            memcpy(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
+            memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
         }
         static void moveOverlapping(const T* src, const T* srcEnd, T* dst) 
         {
-            memmove(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
+            memmove(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
         }
     };
 
@@ -151,7 +151,7 @@ namespace WTF {
     {
         static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) 
         {
-            memcpy(dst, src, reinterpret_cast<const char *>(srcEnd) - reinterpret_cast<const char *>(src));
+            memcpy(dst, src, reinterpret_cast<const char*>(srcEnd) - reinterpret_cast<const char*>(src));
         }
     };
 
@@ -256,6 +256,12 @@ namespace WTF {
         const T* buffer() const { return m_buffer; }
         size_t capacity() const { return m_capacity; }
 
+        void swap(VectorBuffer<T, 0>& other)
+        {
+            std::swap(m_capacity, other.m_capacity);
+            std::swap(m_buffer, other.m_buffer);
+        }
+
         T* releaseBuffer()
         {
             T* buffer = m_buffer;
@@ -317,10 +323,13 @@ namespace WTF {
             return BaseBuffer::releaseBuffer();
         }
 
+        void swap(VectorBuffer<T, inlineCapacity>&);
+
     private:
         static const size_t m_inlineBufferSize = inlineCapacity * sizeof(T);
         T* inlineBuffer() { return reinterpret_cast<T*>(&m_inlineBuffer); }
 
+        // FIXME: Nothing guarantees this buffer is appropriately aligned to hold objects of type T.
         char m_inlineBuffer[m_inlineBufferSize];
     };
 
@@ -427,11 +436,17 @@ namespace WTF {
             TypeOperations::uninitializedFill(begin(), end(), val);
         }
 
-        void fill(const T& val, size_t size);
+        void fill(const T&, size_t);
         void fill(const T& val) { fill(val, size()); }
 
         T* releaseBuffer();
 
+        void swap(Vector<T, inlineCapacity>& other)
+        {
+            std::swap(m_size, other.m_size);
+            m_impl.swap(other.m_impl);
+        }
+
     private:
         void expandCapacity(size_t newMinCapacity);
         const T* expandCapacity(size_t newMinCapacity, const T*);
@@ -645,6 +660,12 @@ namespace WTF {
             delete *it;
     }
 
+    template<typename T, size_t inlineCapacity>
+    inline void swap(Vector<T, inlineCapacity>& a, Vector<T, inlineCapacity>& b)
+    {
+        a.swap(b);
+    }
+
 } // namespace WTF
 
 using WTF::Vector;