summaryrefslogtreecommitdiff
path: root/Source/WTF/wtf/Insertion.h
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
committerLorry Tar Creator <lorry-tar-importer@lorry>2017-06-27 06:07:23 +0000
commit1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch)
tree46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WTF/wtf/Insertion.h
parent32761a6cee1d0dee366b885b7b9c777e67885688 (diff)
downloadWebKitGtk-tarball-master.tar.gz
Diffstat (limited to 'Source/WTF/wtf/Insertion.h')
-rw-r--r--Source/WTF/wtf/Insertion.h31
1 files changed, 19 insertions, 12 deletions
diff --git a/Source/WTF/wtf/Insertion.h b/Source/WTF/wtf/Insertion.h
index 9c4bccfb7..901d8bb30 100644
--- a/Source/WTF/wtf/Insertion.h
+++ b/Source/WTF/wtf/Insertion.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
+ * Copyright (C) 2013, 2015-2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
@@ -32,15 +32,17 @@ template<typename T>
class Insertion {
public:
Insertion() { }
-
- Insertion(size_t index, T element)
+
+ template<typename U>
+ Insertion(size_t index, U&& element)
: m_index(index)
- , m_element(element)
+ , m_element(std::forward<U>(element))
{
}
size_t index() const { return m_index; }
- T element() const { return m_element; }
+ const T& element() const { return m_element; }
+ T& element() { return m_element; }
bool operator<(const Insertion& other) const
{
@@ -53,21 +55,26 @@ private:
};
template<typename TargetVectorType, typename InsertionVectorType>
-void executeInsertions(TargetVectorType& target, InsertionVectorType& insertions)
+size_t executeInsertions(TargetVectorType& target, InsertionVectorType& insertions)
{
- if (!insertions.size())
- return;
- target.grow(target.size() + insertions.size());
+ size_t numInsertions = insertions.size();
+ if (!numInsertions)
+ return 0;
+ size_t originalTargetSize = target.size();
+ target.grow(target.size() + numInsertions);
size_t lastIndex = target.size();
- for (size_t indexInInsertions = insertions.size(); indexInInsertions--;) {
+ for (size_t indexInInsertions = numInsertions; indexInInsertions--;) {
+ ASSERT(!indexInInsertions || insertions[indexInInsertions].index() >= insertions[indexInInsertions - 1].index());
+ ASSERT_UNUSED(originalTargetSize, insertions[indexInInsertions].index() <= originalTargetSize);
size_t firstIndex = insertions[indexInInsertions].index() + indexInInsertions;
size_t indexOffset = indexInInsertions + 1;
for (size_t i = lastIndex; --i > firstIndex;)
- target[i] = target[i - indexOffset];
- target[firstIndex] = insertions[indexInInsertions].element();
+ target[i] = WTFMove(target[i - indexOffset]);
+ target[firstIndex] = WTFMove(insertions[indexInInsertions].element());
lastIndex = firstIndex;
}
insertions.resize(0);
+ return numInsertions;
}
} // namespace WTF