Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "config.h" | 5 #include "config.h" |
| 6 #include "core/dom/NthIndexCache.h" | 6 #include "core/dom/NthIndexCache.h" |
| 7 | 7 |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 | 9 |
| 10 namespace blink { | 10 namespace blink { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 m_parentMap = adoptPtrWillBeNoop(new ParentMap()); | 28 m_parentMap = adoptPtrWillBeNoop(new ParentMap()); |
| 29 | 29 |
| 30 ParentMap::AddResult addResult = m_parentMap->add(&parent, nullptr); | 30 ParentMap::AddResult addResult = m_parentMap->add(&parent, nullptr); |
| 31 if (addResult.isNewEntry) | 31 if (addResult.isNewEntry) |
| 32 addResult.storedValue->value = adoptPtrWillBeNoop(new NthIndexData()); | 32 addResult.storedValue->value = adoptPtrWillBeNoop(new NthIndexData()); |
| 33 | 33 |
| 34 ASSERT(addResult.storedValue->value); | 34 ASSERT(addResult.storedValue->value); |
| 35 return *addResult.storedValue->value; | 35 return *addResult.storedValue->value; |
| 36 } | 36 } |
| 37 | 37 |
| 38 NthIndexCache::ElementTagNameAndNthIndexMap& NthIndexCache::ensureElementTagName AndNthIndexMapForType(Node& parent) | |
|
esprehn
2015/04/30 04:55:05
ensureTypeIndexMap(parent).
| |
| 39 { | |
| 40 if (!m_parentMapForType) | |
| 41 m_parentMapForType = adoptPtrWillBeNoop(new ParentMapForType()); | |
| 42 | |
| 43 ParentMapForType::AddResult addResult = m_parentMapForType->add(&parent, nul lptr); | |
| 44 if (addResult.isNewEntry) | |
| 45 addResult.storedValue->value = adoptPtrWillBeNoop(new ElementTagNameAndN thIndexMap()); | |
| 46 | |
| 47 ASSERT(addResult.storedValue->value); | |
| 48 return *addResult.storedValue->value; | |
| 49 } | |
| 50 | |
| 38 unsigned NthIndexCache::NthIndexData::cacheNthIndices(Element& element) | 51 unsigned NthIndexCache::NthIndexData::cacheNthIndices(Element& element) |
| 39 { | 52 { |
| 40 unsigned index = 0; | 53 unsigned index = 0; |
| 41 // The frequency at which we cache the nth-index for a set of siblings. | 54 // 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. | 55 // 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 | 56 // 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 ' | 57 // still be done in constant time in terms of sibling count, at most 'spread ' |
| 45 // elements will be traversed. | 58 // elements will be traversed. |
| 46 const unsigned spread = 3; | 59 const unsigned spread = 3; |
| 47 unsigned count = 0; | 60 unsigned count = 0; |
| 48 for (Element* sibling = ElementTraversal::firstChild(*element.parentNode()); sibling; sibling = ElementTraversal::nextSibling(*sibling)) { | 61 for (Element* sibling = ElementTraversal::firstChild(*element.parentNode()); sibling; sibling = ElementTraversal::nextSibling(*sibling)) { |
| 49 if (!(++count % spread)) | 62 if (!(++count % spread)) |
| 50 m_elementIndexMap.add(sibling, count); | 63 m_elementIndexMap.add(sibling, count); |
| 51 if (sibling == &element) | 64 if (sibling == &element) |
| 52 index = count; | 65 index = count; |
| 53 } | 66 } |
| 54 ASSERT(count && index); | 67 ASSERT(count && index); |
| 55 m_count = count; | 68 m_count = count; |
| 56 return index; | 69 return index; |
| 57 } | 70 } |
| 58 | 71 |
| 72 unsigned NthIndexCache::NthIndexData::cacheNthIndicesOfType(Element& element, co nst QualifiedName& type) | |
|
esprehn
2015/04/30 04:55:05
We should make NthIndexData not a nested class.
| |
| 73 { | |
| 74 unsigned index = 0; | |
|
esprehn
2015/04/30 04:55:05
This and the other method should both ASSERT(m_ele
| |
| 75 // The frequency at which we cache the nth-index for a set of siblings. | |
| 76 // A spread value of 3 means every third Element will have its nth-index cac hed. | |
| 77 // Using a spread value > 1 is done to save memory. Looking up the nth-index will | |
| 78 // still be done in constant time in terms of sibling count, at most 'spread ' | |
| 79 // elements will be traversed. | |
| 80 const unsigned spread = 3; | |
| 81 unsigned count = 0; | |
| 82 for (Element* sibling = ElementTraversal::firstChild(*element.parentNode(), HasTagName(type)); sibling; sibling = ElementTraversal::nextSibling(*sibling, Ha sTagName(type))) { | |
| 83 if (!(++count % spread)) | |
| 84 m_elementIndexMap.add(sibling, count); | |
| 85 if (sibling == &element) | |
| 86 index = count; | |
| 87 } | |
| 88 ASSERT(count && index); | |
| 89 m_count = count; | |
| 90 return index; | |
| 91 } | |
| 92 | |
| 93 NthIndexCache::NthIndexData& NthIndexCache::nthIndexDataWithTagName(ElementTagNa meAndNthIndexMap& m_elementTagNameAndNthIndexMap, Element& element) | |
| 94 { | |
| 95 | |
| 96 auto it = m_elementTagNameAndNthIndexMap.find(element.tagName()); | |
|
esprehn
2015/04/30 04:55:05
You should not call find() and then add(), always
| |
| 97 if (it != m_elementTagNameAndNthIndexMap.end()) | |
| 98 return *(it->value); | |
|
esprehn
2015/04/30 04:55:05
Delete this.
| |
| 99 ElementTagNameAndNthIndexMap::AddResult addResult = m_elementTagNameAndNthIn dexMap.add(element.tagName(), nullptr); | |
|
esprehn
2015/04/30 04:55:05
m_indexByType, you don't need these verbose names.
| |
| 100 if (addResult.isNewEntry) | |
| 101 addResult.storedValue->value = adoptPtrWillBeNoop(new NthIndexData()); | |
| 102 return *addResult.storedValue->value; | |
| 103 } | |
| 104 } | |
| 105 | |
| 59 DEFINE_TRACE(NthIndexCache::NthIndexData) | 106 DEFINE_TRACE(NthIndexCache::NthIndexData) |
| 60 { | 107 { |
| 61 #if ENABLE(OILPAN) | 108 #if ENABLE(OILPAN) |
| 62 visitor->trace(m_elementIndexMap); | 109 visitor->trace(m_elementIndexMap); |
| 63 #endif | 110 #endif |
| 64 } | 111 } |
| 65 | 112 |
| 66 #if !ENABLE(OILPAN) | 113 #if !ENABLE(OILPAN) |
| 67 NthIndexCache::NthIndexData::~NthIndexData() | 114 NthIndexCache::NthIndexData::~NthIndexData() |
| 68 { | 115 { |
| 69 } | 116 } |
| 70 #endif | 117 #endif |
| 71 | 118 |
| 72 } // namespace blink | 119 } // namespace blink |
| OLD | NEW |