OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/dom/NthIndexCache.h" |
| 7 |
| 8 #include "core/dom/Document.h" |
| 9 |
| 10 namespace blink { |
| 11 |
| 12 NthIndexCache::NthIndexCache(Document& document) |
| 13 : m_document(&document) |
| 14 , m_domTreeVersion(document.domTreeVersion()) |
| 15 { |
| 16 document.setNthIndexCache(this); |
| 17 } |
| 18 |
| 19 NthIndexCache::~NthIndexCache() |
| 20 { |
| 21 ASSERT(m_domTreeVersion == m_document->domTreeVersion()); |
| 22 m_document->setNthIndexCache(nullptr); |
| 23 } |
| 24 |
| 25 NthIndexCache::NthIndexData& NthIndexCache::ensureNthIndexDataFor(Node& parent) |
| 26 { |
| 27 if (!m_parentMap) |
| 28 m_parentMap = adoptPtrWillBeNoop(new ParentMap()); |
| 29 |
| 30 ParentMap::AddResult addResult = m_parentMap->add(&parent, nullptr); |
| 31 if (addResult.isNewEntry) |
| 32 addResult.storedValue->value = adoptPtrWillBeNoop(new NthIndexData()); |
| 33 |
| 34 ASSERT(addResult.storedValue->value); |
| 35 return *addResult.storedValue->value; |
| 36 } |
| 37 |
| 38 unsigned NthIndexCache::NthIndexData::cacheNthIndices(Element& element) |
| 39 { |
| 40 unsigned index = 0; |
| 41 // The frequency at which we cache the nth-index for a set of siblings. |
| 42 // A spread value of 3 means every third Element will have its nth-index cac
hed. |
| 43 // Using a spread value > 1 is done to save memory. Looking up the nth-index
will |
| 44 // still be done in constant time in terms of sibling count, at most 'spread
' |
| 45 // elements will be traversed. |
| 46 const unsigned spread = 3; |
| 47 unsigned count = 0; |
| 48 for (Element* sibling = ElementTraversal::firstChild(*element.parentNode());
sibling; sibling = ElementTraversal::nextSibling(*sibling)) { |
| 49 if (!(++count % spread)) |
| 50 m_elementIndexMap.add(sibling, count); |
| 51 if (sibling == &element) |
| 52 index = count; |
| 53 } |
| 54 ASSERT(count && index); |
| 55 m_count = count; |
| 56 return index; |
| 57 } |
| 58 |
| 59 DEFINE_TRACE(NthIndexCache::NthIndexData) |
| 60 { |
| 61 #if ENABLE(OILPAN) |
| 62 visitor->trace(m_elementIndexMap); |
| 63 #endif |
| 64 } |
| 65 |
| 66 #if !ENABLE(OILPAN) |
| 67 NthIndexCache::NthIndexData::~NthIndexData() |
| 68 { |
| 69 } |
| 70 #endif |
| 71 |
| 72 } // namespace blink |
OLD | NEW |