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

Side by Side Diff: Source/core/dom/NthIndexCache.h

Issue 1096813005: Extending the NthIndexCache to support caching for the type of index. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Incorporated Review Comments Created 5 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 unified diff | Download patch
« no previous file with comments | « Source/core/css/SiblingTraversalStrategies.h ('k') | Source/core/dom/NthIndexCache.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #ifndef NthIndexCache_h 5 #ifndef NthIndexCache_h
6 #define NthIndexCache_h 6 #define NthIndexCache_h
7 7
8 #include "core/CoreExport.h" 8 #include "core/CoreExport.h"
9 #include "core/dom/Element.h" 9 #include "core/dom/Element.h"
10 #include "core/dom/ElementTraversal.h" 10 #include "core/dom/ElementTraversal.h"
11 #include "platform/heap/Handle.h" 11 #include "platform/heap/Handle.h"
12 #include "wtf/HashMap.h" 12 #include "wtf/HashMap.h"
13 #include "wtf/OwnPtr.h" 13 #include "wtf/OwnPtr.h"
14 #include "wtf/RefPtr.h" 14 #include "wtf/RefPtr.h"
15 15
16 namespace blink { 16 namespace blink {
17 17
18 class Document; 18 class Document;
19 class HasTagName {
20 public:
21 explicit HasTagName(const QualifiedName& tagName) : m_tagName(tagName) { }
22 bool operator() (const Element& element) const { return element.hasTagName(m _tagName); }
23 private:
24 const QualifiedName m_tagName;
25 };
19 26
20 class CORE_EXPORT NthIndexCache final { 27 class CORE_EXPORT NthIndexCache final {
21 STACK_ALLOCATED(); 28 STACK_ALLOCATED();
22 WTF_MAKE_NONCOPYABLE(NthIndexCache); 29 WTF_MAKE_NONCOPYABLE(NthIndexCache);
23 public: 30 public:
24 explicit NthIndexCache(Document&); 31 explicit NthIndexCache(Document&);
25 ~NthIndexCache(); 32 ~NthIndexCache();
26 33
27 inline unsigned nthChildIndex(Element&); 34 inline unsigned nthChildIndex(Element&);
35 inline unsigned nthChildIndexOfType(Element&, const QualifiedName&);
28 inline unsigned nthLastChildIndex(Element&); 36 inline unsigned nthLastChildIndex(Element&);
37 inline unsigned nthLastChildIndexOfType(Element&, const QualifiedName&);
29 38
30 private: 39 private:
31 class NthIndexData final : public NoBaseWillBeGarbageCollected<NthIndexData> { 40 class NthIndexData final : public NoBaseWillBeGarbageCollected<NthIndexData> {
32 WTF_MAKE_NONCOPYABLE(NthIndexData); 41 WTF_MAKE_NONCOPYABLE(NthIndexData);
33 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData); 42 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData);
34 public: 43 public:
35 NthIndexData() { } 44 NthIndexData() { }
36 45
37 inline unsigned nthIndex(Element&); 46 inline unsigned nthIndex(Element&);
47 inline unsigned nthIndexOfType(Element&, const QualifiedName&);
38 inline unsigned nthLastIndex(Element&); 48 inline unsigned nthLastIndex(Element&);
49 inline unsigned nthLastIndexOfType(Element&, const QualifiedName&);
39 50
40 unsigned cacheNthIndices(Element&); 51 unsigned cacheNthIndices(Element&);
52 unsigned cacheNthIndicesOfType(Element&, const QualifiedName&);
41 53
42 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM ap; 54 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM ap;
43 unsigned m_count = 0; 55 unsigned m_count = 0;
44 56
45 DECLARE_TRACE(); 57 DECLARE_TRACE();
46 }; 58 };
47 59
60 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe mber<NthIndexData>>;
61 OwnPtrWillBeMember<ParentMap> m_parentMap;
62
63 using IndexByType = WillBeHeapHashMap<String, OwnPtrWillBeMember<NthIndexDat a>>;
64 OwnPtrWillBeMember<IndexByType> m_IndexByType;
esprehn 2015/05/07 04:16:10 m_indexByType, lower case
65
66 using ParentMapForType = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrW illBeMember<IndexByType>>;
67 OwnPtrWillBeMember<ParentMapForType> m_parentMapForType;
68
69
48 NthIndexData& ensureNthIndexDataFor(Node&); 70 NthIndexData& ensureNthIndexDataFor(Node&);
71 NthIndexCache::IndexByType& ensureTypeIndexMap(Node&);
49 72
50 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe mber<NthIndexData>>; 73 NthIndexCache::NthIndexData& nthIndexDataWithTagName(Element&);
51 74
52 OwnPtrWillBeMember<ParentMap> m_parentMap;
53 RawPtrWillBeMember<Document> m_document; 75 RawPtrWillBeMember<Document> m_document;
54 uint64_t m_domTreeVersion; 76 uint64_t m_domTreeVersion;
55 }; 77 };
56 78
57 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element) 79 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element)
58 { 80 {
59 if (element.isPseudoElement()) 81 if (element.isPseudoElement())
60 return 1; 82 return 1;
61 if (!m_count) 83 if (!m_count)
62 return cacheNthIndices(element); 84 return cacheNthIndices(element);
63 85
64 unsigned index = 0; 86 unsigned index = 0;
65 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ ousSibling(*sibling), index++) { 87 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ ousSibling(*sibling), index++) {
66 auto it = m_elementIndexMap.find(sibling); 88 auto it = m_elementIndexMap.find(sibling);
67 if (it != m_elementIndexMap.end()) 89 if (it != m_elementIndexMap.end())
68 return it->value + index; 90 return it->value + index;
69 } 91 }
70 return index; 92 return index;
71 } 93 }
72 94
95 inline unsigned NthIndexCache::NthIndexData::nthIndexOfType(Element& element, co nst QualifiedName& type)
96 {
97 if (element.isPseudoElement())
98 return 1;
99 if (!m_count)
100 return cacheNthIndicesOfType(element, type);
101 unsigned index = 0;
102 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ ousSibling(*sibling, HasTagName(type)), index++) {
103 auto it = m_elementIndexMap.find(sibling);
104 if (it != m_elementIndexMap.end())
105 return it->value + index;
106 }
107 return index;
108 }
109
73 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element) 110 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element)
74 { 111 {
75 if (element.isPseudoElement()) 112 if (element.isPseudoElement())
76 return 1; 113 return 1;
77 unsigned index = nthIndex(element); 114 unsigned index = nthIndex(element);
78 return m_count - index + 1; 115 return m_count - index + 1;
79 } 116 }
80 117
118 inline unsigned NthIndexCache::NthIndexData::nthLastIndexOfType(Element& element , const QualifiedName& type)
119 {
120 if (element.isPseudoElement())
121 return 1;
122 unsigned index = nthIndexOfType(element, type);
123 return m_count - index + 1;
124 }
125
81 inline unsigned NthIndexCache::nthChildIndex(Element& element) 126 inline unsigned NthIndexCache::nthChildIndex(Element& element)
82 { 127 {
83 ASSERT(element.parentNode()); 128 ASSERT(element.parentNode());
84 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); 129 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element);
85 } 130 }
86 131
132 inline unsigned NthIndexCache::nthChildIndexOfType(Element& element, const Quali fiedName& type)
133 {
134 ASSERT(element.parentNode());
135 return nthIndexDataWithTagName(element).nthIndexOfType(element, type);
136 }
137
87 inline unsigned NthIndexCache::nthLastChildIndex(Element& element) 138 inline unsigned NthIndexCache::nthLastChildIndex(Element& element)
88 { 139 {
89 ASSERT(element.parentNode()); 140 ASSERT(element.parentNode());
90 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element); 141 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element);
91 } 142 }
92 143
144 inline unsigned NthIndexCache::nthLastChildIndexOfType(Element& element, const Q ualifiedName& type)
145 {
146 ASSERT(element.parentNode());
147 return nthIndexDataWithTagName(element).nthLastIndexOfType(element, type);
148 }
149
150
93 } // namespace blink 151 } // namespace blink
94 152
95 #endif // NthIndexCache_h 153 #endif // NthIndexCache_h
OLDNEW
« no previous file with comments | « Source/core/css/SiblingTraversalStrategies.h ('k') | Source/core/dom/NthIndexCache.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698