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

Unified Diff: third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp

Issue 2770413003: Add SpellCheckMarkerList in preparation for DocumentMarkerController refactor (Closed)
Patch Set: Optimize case where markers are being added in order Created 3 years, 9 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
Index: third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp
diff --git a/third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp b/third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..5853852526811fc4d1de5ffd392e6e4f87cf0852
--- /dev/null
+++ b/third_party/WebKit/Source/core/editing/markers/SpellCheckMarkerList.cpp
@@ -0,0 +1,73 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "core/editing/markers/SpellCheckMarkerList.h"
+
+#include <algorithm>
+
+namespace blink {
+
+SpellCheckMarkerList::SpellCheckMarkerList(DocumentMarker::MarkerType type)
+ : m_type(type) {}
+
+DocumentMarker::MarkerType SpellCheckMarkerList::allowedMarkerType() const {
+ return m_type;
+}
+
+bool SpellCheckMarkerList::isSpellCheckMarkerList() const {
+ return true;
+}
+
+void SpellCheckMarkerList::add(DocumentMarker* marker) {
+ DCHECK(marker->type() == allowedMarkerType());
+ // Optimize case where (non-merging) markers are being added in order so we
+ // can add n markers in O(n) time instead of O(n log n)
+ if (m_markers.isEmpty() ||
+ m_markers.back()->endOffset() < marker->startOffset()) {
+ m_markers.push_back(marker);
+ return;
+ }
+
+ auto firstOverlappingIt = std::lower_bound(
+ m_markers.begin(), m_markers.end(), marker,
+ [](const Member<DocumentMarker>& lhv, const DocumentMarker* rhv) {
+ return lhv->endOffset() < rhv->startOffset();
+ });
+
+ size_t index = firstOverlappingIt - m_markers.begin();
+ m_markers.insert(index, marker);
+ auto insertedIt = m_markers.begin() + index;
+
+ unsigned newStart = marker->startOffset();
+ unsigned newEnd = marker->endOffset();
+
+ iterator it;
+ for (it = insertedIt + 1;
+ it != m_markers.end() && (*it)->startOffset() <= newEnd; ++it) {
+ newStart = std::min(newStart, (*it)->startOffset());
+ newEnd = std::max(newEnd, (*it)->endOffset());
+ }
+ m_markers.erase(index + 1, it - (insertedIt + 1));
+
+ (*insertedIt)->setStartOffset(newStart);
+ (*insertedIt)->setEndOffset(newEnd);
+}
+
+void SpellCheckMarkerList::removeMarkersForWords(const String& nodeText,
+ const Vector<String>& words) {
+ // Build a second vector and swap with m_markers to avoid O(n^2) performance
+ HeapVector<Member<DocumentMarker>> newMarkerList;
+
+ for (Member<DocumentMarker> marker : m_markers) {
+ unsigned start = marker->startOffset();
+ unsigned length = marker->endOffset() - marker->startOffset();
+ String markerText = nodeText.substring(start, length);
+ if (!words.contains(markerText))
+ newMarkerList.push_back(marker);
+ }
+
+ std::swap(m_markers, newMarkerList);
+}
+
+} // namespace blink

Powered by Google App Engine
This is Rietveld 408576698