Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1234)

Unified Diff: third_party/WebKit/Source/wtf/Vector.h

Issue 1985393002: WTF: Add support for std::initializer_list in Vector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add tests for different ways of construction. Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/VectorTest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/wtf/Vector.h
diff --git a/third_party/WebKit/Source/wtf/Vector.h b/third_party/WebKit/Source/wtf/Vector.h
index 0d55085c371cba442e8a3047be62badee6fded92..0e500ce18f53e24c01cb38b729cfd2dcbdba8c9c 100644
--- a/third_party/WebKit/Source/wtf/Vector.h
+++ b/third_party/WebKit/Source/wtf/Vector.h
@@ -30,6 +30,7 @@
#include "wtf/VectorTraits.h"
#include "wtf/allocator/PartitionAllocator.h"
#include <algorithm>
+#include <initializer_list>
#include <iterator>
#include <string.h>
#include <utility>
@@ -831,6 +832,9 @@ public:
Vector(Vector&&);
Vector& operator=(Vector&&);
+ Vector(std::initializer_list<T> elements);
+ Vector& operator=(std::initializer_list<T> elements);
+
size_t size() const { return m_size; }
size_t capacity() const { return Base::capacity(); }
bool isEmpty() const { return !size(); }
@@ -1050,6 +1054,34 @@ Vector<T, inlineCapacity, Allocator>& Vector<T, inlineCapacity, Allocator>::oper
}
template <typename T, size_t inlineCapacity, typename Allocator>
+Vector<T, inlineCapacity, Allocator>::Vector(std::initializer_list<T> elements)
+ : Base(elements.size())
+{
+ ANNOTATE_NEW_BUFFER(begin(), capacity(), elements.size());
+ m_size = elements.size();
+ TypeOperations::uninitializedCopy(elements.begin(), elements.end(), begin());
+}
+
+template <typename T, size_t inlineCapacity, typename Allocator>
+Vector<T, inlineCapacity, Allocator>& Vector<T, inlineCapacity, Allocator>::operator=(std::initializer_list<T> elements)
+{
+ if (size() > elements.size()) {
+ shrink(elements.size());
+ } else if (elements.size() > capacity()) {
+ clear();
+ reserveCapacity(elements.size());
+ DCHECK(begin());
+ }
+
+ ANNOTATE_CHANGE_SIZE(begin(), capacity(), m_size, elements.size());
+ std::copy(elements.begin(), elements.begin() + m_size, begin());
+ TypeOperations::uninitializedCopy(elements.begin() + m_size, elements.end(), end());
+ m_size = elements.size();
+
+ return *this;
+}
+
+template <typename T, size_t inlineCapacity, typename Allocator>
template <typename U>
bool Vector<T, inlineCapacity, Allocator>::contains(const U& value) const
{
« no previous file with comments | « no previous file | third_party/WebKit/Source/wtf/VectorTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698