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 #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 "platform/heap/Handle.h" | 10 #include "platform/heap/Handle.h" |
11 #include "wtf/HashMap.h" | 11 #include "wtf/HashMap.h" |
12 #include "wtf/OwnPtr.h" | 12 #include "wtf/OwnPtr.h" |
13 #include "wtf/RefPtr.h" | 13 #include "wtf/RefPtr.h" |
14 | 14 |
15 namespace blink { | 15 namespace blink { |
16 | 16 |
17 class Document; | 17 class Document; |
18 | 18 |
19 class CORE_EXPORT NthIndexData final : public NoBaseWillBeGarbageCollected<NthIn
dexData> { | 19 class CORE_EXPORT NthIndexData final : public NoBaseWillBeGarbageCollected<NthIn
dexData> { |
20 USING_FAST_MALLOC_WILL_BE_REMOVED(NthIndexData); | 20 USING_FAST_MALLOC_WILL_BE_REMOVED(NthIndexData); |
21 WTF_MAKE_NONCOPYABLE(NthIndexData); | 21 WTF_MAKE_NONCOPYABLE(NthIndexData); |
22 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData); | 22 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData); |
23 public: | 23 public: |
24 NthIndexData() { } | 24 NthIndexData(ContainerNode&); |
| 25 NthIndexData(ContainerNode&, const QualifiedName& type); |
25 | 26 |
26 unsigned nthIndex(Element&); | 27 unsigned nthIndex(Element&) const; |
27 unsigned nthIndexOfType(Element&, const QualifiedName&); | 28 unsigned nthLastIndex(Element&) const; |
28 unsigned nthLastIndex(Element&); | 29 unsigned nthOfTypeIndex(Element&) const; |
29 unsigned nthLastIndexOfType(Element&, const QualifiedName&); | 30 unsigned nthLastOfTypeIndex(Element&) const; |
30 | 31 |
31 private: | 32 private: |
32 unsigned cacheNthIndices(Element&); | |
33 unsigned cacheNthIndicesOfType(Element&, const QualifiedName&); | |
34 | |
35 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexMap; | 33 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexMap; |
36 unsigned m_count = 0; | 34 unsigned m_count = 0; |
37 | 35 |
38 DECLARE_TRACE(); | 36 DECLARE_TRACE(); |
39 }; | 37 }; |
40 | 38 |
41 class CORE_EXPORT NthIndexCache final { | 39 class CORE_EXPORT NthIndexCache final { |
42 STACK_ALLOCATED(); | 40 STACK_ALLOCATED(); |
43 WTF_MAKE_NONCOPYABLE(NthIndexCache); | 41 WTF_MAKE_NONCOPYABLE(NthIndexCache); |
44 public: | 42 public: |
45 explicit NthIndexCache(Document&); | 43 explicit NthIndexCache(Document&); |
46 ~NthIndexCache(); | 44 ~NthIndexCache(); |
47 | 45 |
48 unsigned nthChildIndex(Element& element) | 46 static unsigned nthChildIndex(Element&); |
49 { | 47 static unsigned nthLastChildIndex(Element&); |
50 ASSERT(element.parentNode()); | 48 static unsigned nthOfTypeIndex(Element&); |
51 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); | 49 static unsigned nthLastOfTypeIndex(Element&); |
52 } | |
53 | |
54 unsigned nthChildIndexOfType(Element& element, const QualifiedName& type) | |
55 { | |
56 ASSERT(element.parentNode()); | |
57 return nthIndexDataWithTagName(element).nthIndexOfType(element, type); | |
58 } | |
59 | |
60 unsigned nthLastChildIndex(Element& element) | |
61 { | |
62 ASSERT(element.parentNode()); | |
63 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element
); | |
64 } | |
65 | |
66 unsigned nthLastChildIndexOfType(Element& element, const QualifiedName& type
) | |
67 { | |
68 ASSERT(element.parentNode()); | |
69 return nthIndexDataWithTagName(element).nthLastIndexOfType(element, type
); | |
70 } | |
71 | 50 |
72 private: | 51 private: |
73 using IndexByType = WillBeHeapHashMap<String, OwnPtrWillBeMember<NthIndexDat
a>>; | 52 using IndexByType = WillBeHeapHashMap<String, OwnPtrWillBeMember<NthIndexDat
a>>; |
74 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe
mber<NthIndexData>>; | 53 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe
mber<NthIndexData>>; |
75 using ParentMapForType = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrW
illBeMember<IndexByType>>; | 54 using ParentMapForType = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrW
illBeMember<IndexByType>>; |
76 | 55 |
77 NthIndexData& ensureNthIndexDataFor(Node&); | 56 void cacheNthIndexDataForParent(Element&); |
78 IndexByType& ensureTypeIndexMap(Node&); | 57 void cacheNthOfTypeIndexDataForParent(Element&); |
79 NthIndexData& nthIndexDataWithTagName(Element&); | 58 IndexByType& ensureTypeIndexMap(ContainerNode&); |
| 59 NthIndexData* nthTypeIndexDataForParent(Element&) const; |
80 | 60 |
81 RawPtrWillBeMember<Document> m_document; | 61 RawPtrWillBeMember<Document> m_document; |
82 OwnPtrWillBeMember<ParentMap> m_parentMap; | 62 OwnPtrWillBeMember<ParentMap> m_parentMap; |
83 OwnPtrWillBeMember<ParentMapForType> m_parentMapForType; | 63 OwnPtrWillBeMember<ParentMapForType> m_parentMapForType; |
84 | 64 |
85 #if ENABLE(ASSERT) | 65 #if ENABLE(ASSERT) |
86 uint64_t m_domTreeVersion; | 66 uint64_t m_domTreeVersion; |
87 #endif | 67 #endif |
88 }; | 68 }; |
89 | 69 |
90 } // namespace blink | 70 } // namespace blink |
91 | 71 |
92 #endif // NthIndexCache_h | 72 #endif // NthIndexCache_h |
OLD | NEW |