// -*- mode: c++; c-basic-offset: 4 -*- /* * This file is part of the KDE libraries * Copyright (C) 2005, 2006 Apple Computer, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */ #ifndef WTF_Vector_h #define WTF_Vector_h #include "Assertions.h" #include "FastMalloc.h" #include "VectorTraits.h" #include #include #include namespace WTF { using std::min; using std::max; template class VectorDestructor; template struct VectorDestructor { static void destruct(T*, T*) {} }; template struct VectorDestructor { static void destruct(T* begin, T* end) { for (T* cur = begin; cur != end; ++cur) cur->~T(); } }; template class VectorInitializer; template struct VectorInitializer { static void initialize(T*, T*) {} }; template struct VectorInitializer { static void initialize(T* begin, T* end) { for (T* cur = begin; cur != end; ++cur) new (cur) T; } }; template struct VectorInitializer { static void initialize(T* begin, T* end) { memset(begin, 0, reinterpret_cast(end) - reinterpret_cast(begin)); } }; template class VectorMover; template struct VectorMover { static void move(const T* src, const T* srcEnd, T* dst) { while (src != srcEnd) { new (dst) T(*src); src->~T(); ++dst; ++src; } } static void moveOverlapping(const T* src, const T* srcEnd, T* dst) { if (src > dst) move(src, srcEnd, dst); else { T* dstEnd = dst + (srcEnd - src); while (src != srcEnd) { --srcEnd; --dstEnd; new (dstEnd) T(*srcEnd); srcEnd->~T(); } } } }; template struct VectorMover { static void move(const T* src, const T* srcEnd, T* dst) { memcpy(dst, src, reinterpret_cast(srcEnd) - reinterpret_cast(src)); } static void moveOverlapping(const T* src, const T* srcEnd, T* dst) { memmove(dst, src, reinterpret_cast(srcEnd) - reinterpret_cast(src)); } }; template class VectorCopier; template struct VectorCopier { static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) { while (src != srcEnd) { new (dst) T(*src); ++dst; ++src; } } }; template struct VectorCopier { static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) { memcpy(dst, src, reinterpret_cast(srcEnd) - reinterpret_cast(src)); } }; template class VectorFiller; template struct VectorFiller { static void uninitializedFill(T* dst, T* dstEnd, const T& val) { while (dst != dstEnd) { new (dst) T(val); ++dst; } } }; template struct VectorFiller { static void uninitializedFill(T* dst, T* dstEnd, const T& val) { ASSERT(sizeof(T) == sizeof(char)); memset(dst, val, dstEnd - dst); } }; template class VectorComparer; template struct VectorComparer { static bool compare(const T* a, const T* b, size_t size) { for (size_t i = 0; i < size; ++i) if (a[i] != b[i]) return false; return true; } }; template struct VectorComparer { static bool compare(const T* a, const T* b, size_t size) { return memcmp(a, b, sizeof(T) * size) == 0; } }; template struct VectorTypeOperations { static void destruct(T* begin, T* end) { VectorDestructor::needsDestruction, T>::destruct(begin, end); } static void initialize(T* begin, T* end) { VectorInitializer::needsInitialization, VectorTraits::canInitializeWithMemset, T>::initialize(begin, end); } static void move(const T* src, const T* srcEnd, T* dst) { VectorMover::canMoveWithMemcpy, T>::move(src, srcEnd, dst); } static void moveOverlapping(const T* src, const T* srcEnd, T* dst) { VectorMover::canMoveWithMemcpy, T>::moveOverlapping(src, srcEnd, dst); } static void uninitializedCopy(const T* src, const T* srcEnd, T* dst) { VectorCopier::canCopyWithMemcpy, T>::uninitializedCopy(src, srcEnd, dst); } static void uninitializedFill(T* dst, T* dstEnd, const T& val) { VectorFiller::canFillWithMemset, T>::uninitializedFill(dst, dstEnd, val); } static bool compare(const T* a, const T* b, size_t size) { return VectorComparer::canCompareWithMemcmp, T>::compare(a, b, size); } }; template class VectorBuffer; template class VectorBuffer { public: VectorBuffer() : m_buffer(0), m_capacity(0) { } VectorBuffer(size_t capacity) #if !ASSERT_DISABLED : m_capacity(0) #endif { allocateBuffer(capacity); } ~VectorBuffer() { deallocateBuffer(m_buffer); } static void deallocateBuffer(T* buffer) { fastFree(buffer); } void allocateBuffer(size_t newCapacity) { ASSERT(newCapacity >= m_capacity); m_capacity = newCapacity; if (newCapacity > std::numeric_limits::max() / sizeof(T)) abort(); m_buffer = static_cast(fastMalloc(newCapacity * sizeof(T))); } T* buffer() { return m_buffer; } const T* buffer() const { return m_buffer; } size_t capacity() const { return m_capacity; } void swap(VectorBuffer& other) { std::swap(m_capacity, other.m_capacity); std::swap(m_buffer, other.m_buffer); } T* releaseBuffer() { T* buffer = m_buffer; m_buffer = 0; m_capacity = 0; return buffer; } protected: VectorBuffer(T* buffer, size_t capacity) : m_buffer(buffer), m_capacity(capacity) { } T* m_buffer; private: size_t m_capacity; }; template class VectorBuffer : private VectorBuffer { private: typedef VectorBuffer BaseBuffer; public: VectorBuffer() : BaseBuffer(inlineBuffer(), inlineCapacity) { } VectorBuffer(size_t capacity) : BaseBuffer(inlineBuffer(), inlineCapacity) { if (capacity > inlineCapacity) allocateBuffer(capacity); } ~VectorBuffer() { if (buffer() == inlineBuffer()) BaseBuffer::m_buffer = 0; } void deallocateBuffer(T* buffer) { if (buffer != inlineBuffer()) BaseBuffer::deallocateBuffer(buffer); } using BaseBuffer::allocateBuffer; using BaseBuffer::buffer; using BaseBuffer::capacity; T* releaseBuffer() { if (buffer() == inlineBuffer()) return 0; return BaseBuffer::releaseBuffer(); } void swap(VectorBuffer&); private: static const size_t m_inlineBufferSize = inlineCapacity * sizeof(T); T* inlineBuffer() { return reinterpret_cast(&m_inlineBuffer); } // FIXME: Nothing guarantees this buffer is appropriately aligned to hold objects of type T. char m_inlineBuffer[m_inlineBufferSize]; }; template class Vector { private: typedef VectorBuffer Impl; typedef VectorTypeOperations TypeOperations; public: typedef T* iterator; typedef const T* const_iterator; Vector() : m_size(0) { } explicit Vector(size_t size) : m_size(0) { resize(size); } ~Vector() { clear(); } Vector(const Vector&); template Vector(const Vector&); Vector& operator=(const Vector&); template Vector& operator=(const Vector&); size_t size() const { return m_size; } size_t capacity() const { return m_impl.capacity(); } bool isEmpty() const { return !size(); } T& at(size_t i) { ASSERT(i < size()); return m_impl.buffer()[i]; } const T& at(size_t i) const { ASSERT(i < size()); return m_impl.buffer()[i]; } T& operator[](long i) { return at(i); } const T& operator[](long i) const { return at(i); } T& operator[](unsigned long i) { return at(i); } const T& operator[](unsigned long i) const { return at(i); } T& operator[](int i) { return at(i); } const T& operator[](int i) const { return at(i); } T& operator[](unsigned i) { return at(i); } const T& operator[](unsigned i) const { return at(i); } T& operator[](short i) { return at(i); } const T& operator[](short i) const { return at(i); } T& operator[](unsigned short i) { return at(i); } const T& operator[](unsigned short i) const { return at(i); } T* data() { return m_impl.buffer(); } const T* data() const { return m_impl.buffer(); } operator T*() { return data(); } operator const T*() const { return data(); } iterator begin() { return data(); } iterator end() { return begin() + m_size; } const_iterator begin() const { return data(); } const_iterator end() const { return begin() + m_size; } T& first() { return at(0); } const T& first() const { return at(0); } T& last() { return at(size() - 1); } const T& last() const { return at(size() - 1); } void resize(size_t size); void reserveCapacity(size_t newCapacity); void clear() { resize(0); } template void append(const U*, size_t); template void append(const U&); template void append(const Vector&); template void insert(size_t position, const U*, size_t); template void insert(size_t position, const U&); template void insert(size_t position, const Vector&); template void prepend(const U*, size_t); template void prepend(const U&); template void prepend(const Vector&); void remove(size_t position); void removeLast() { ASSERT(!isEmpty()); resize(size() - 1); } Vector(size_t size, const T& val) : m_size(size) , m_impl(size) { TypeOperations::uninitializedFill(begin(), end(), val); } void fill(const T&, size_t); void fill(const T& val) { fill(val, size()); } T* releaseBuffer(); void swap(Vector& 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*); template U* expandCapacity(size_t newMinCapacity, U*); size_t m_size; Impl m_impl; }; template Vector::Vector(const Vector& other) : m_size(other.size()) , m_impl(other.capacity()) { TypeOperations::uninitializedCopy(other.begin(), other.end(), begin()); } template template Vector::Vector(const Vector& other) : m_size(other.size()) , m_impl(other.capacity()) { TypeOperations::uninitializedCopy(other.begin(), other.end(), begin()); } template Vector& Vector::operator=(const Vector& other) { if (&other == this) return *this; if (size() > other.size()) resize(other.size()); else if (other.size() > capacity()) { clear(); reserveCapacity(other.size()); } std::copy(other.begin(), other.begin() + size(), begin()); TypeOperations::uninitializedCopy(other.begin() + size(), other.end(), end()); m_size = other.size(); return *this; } template template Vector& Vector::operator=(const Vector& other) { if (&other == this) return *this; if (size() > other.size()) resize(other.size()); else if (other.size() > capacity()) { clear(); reserveCapacity(other.size()); } std::copy(other.begin(), other.begin() + size(), begin()); TypeOperations::uninitializedCopy(other.begin() + size(), other.end(), end()); m_size = other.size(); return *this; } template void Vector::fill(const T& val, size_t newSize) { if (size() > newSize) resize(newSize); else if (newSize > capacity()) { clear(); reserveCapacity(newSize); } std::fill(begin(), end(), val); TypeOperations::uninitializedFill(end(), begin() + newSize, val); m_size = newSize; } template void Vector::expandCapacity(size_t newMinCapacity) { reserveCapacity(max(newMinCapacity, max(static_cast(16), capacity() + capacity() / 4 + 1))); } template const T* Vector::expandCapacity(size_t newMinCapacity, const T* ptr) { if (ptr < begin() || ptr >= end()) { expandCapacity(newMinCapacity); return ptr; } size_t index = ptr - begin(); expandCapacity(newMinCapacity); return begin() + index; } template template inline U* Vector::expandCapacity(size_t newMinCapacity, U* ptr) { expandCapacity(newMinCapacity); return ptr; } template void Vector::resize(size_t size) { if (size <= m_size) TypeOperations::destruct(begin() + size, end()); else { if (size > capacity()) expandCapacity(size); TypeOperations::initialize(end(), begin() + size); } m_size = size; } template void Vector::reserveCapacity(size_t newCapacity) { if (newCapacity < capacity()) return; T* oldBuffer = begin(); T* oldEnd = end(); m_impl.allocateBuffer(newCapacity); TypeOperations::move(oldBuffer, oldEnd, begin()); m_impl.deallocateBuffer(oldBuffer); } // Templatizing these is better than just letting the conversion happen implicitly, // because for instance it allows a PassRefPtr to be appended to a RefPtr vector // without refcount thrash. template template void Vector::append(const U* data, size_t dataSize) { size_t newSize = m_size + dataSize; if (newSize > capacity()) data = expandCapacity(newSize, data); T* dest = end(); for (size_t i = 0; i < dataSize; ++i) new (&dest[i]) T(data[i]); m_size = newSize; } template template inline void Vector::append(const U& val) { const U* ptr = &val; if (size() == capacity()) ptr = expandCapacity(size() + 1, ptr); new (end()) T(*ptr); ++m_size; } template template inline void Vector::append(const Vector& val) { append(val.begin(), val.size()); } template template void Vector::insert(size_t position, const U* data, size_t dataSize) { ASSERT(position <= size()); size_t newSize = m_size + dataSize; if (newSize > capacity()) data = expandCapacity(newSize, data); T* spot = begin() + position; TypeOperations::moveOverlapping(spot, end(), spot + dataSize); for (size_t i = 0; i < dataSize; ++i) new (&spot[i]) T(data[i]); m_size = newSize; } template template inline void Vector::insert(size_t position, const U& val) { ASSERT(position <= size()); const U* data = &val; if (size() == capacity()) data = expandCapacity(size() + 1, data); T* spot = begin() + position; TypeOperations::moveOverlapping(spot, end(), spot + 1); new (spot) T(*data); ++m_size; } template template inline void Vector::insert(size_t position, const Vector& val) { insert(position, val.begin(), val.size()); } template template void Vector::prepend(const U* data, size_t dataSize) { insert(0, data, dataSize); } template template inline void Vector::prepend(const U& val) { insert(0, val); } template template inline void Vector::prepend(const Vector& val) { insert(0, val.begin(), val.size()); } template inline void Vector::remove(size_t position) { ASSERT(position < size()); T* spot = begin() + position; spot->~T(); TypeOperations::moveOverlapping(spot + 1, end(), spot); --m_size; } template T* Vector::releaseBuffer() { T* buffer = m_impl.releaseBuffer(); if (!buffer && m_size) { // If the vector had some data, but no buffer to release, // that means it was using the inline buffer. In that case, // we create a brand new buffer so the caller always gets one. size_t bytes = m_size * sizeof(T); buffer = static_cast(fastMalloc(bytes)); memcpy(buffer, data(), bytes); } m_size = 0; return buffer; } template void deleteAllValues(const Vector& collection) { typedef typename Vector::const_iterator iterator; iterator end = collection.end(); for (iterator it = collection.begin(); it != end; ++it) delete *it; } template inline void swap(Vector& a, Vector& b) { a.swap(b); } template bool operator==(const Vector& a, const Vector& b) { if (a.size() != b.size()) return false; return VectorTypeOperations::compare(a.data(), b.data(), a.size()); } template inline bool operator!=(const Vector& a, const Vector& b) { return !(a == b); } } // namespace WTF using WTF::Vector; #endif // WTF_Vector_h