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.
5 #ifndef NthIndexCache_h
6 #define NthIndexCache_h
8 #include "core/CoreExport.h"
9 #include "core/dom/Element.h"
10 #include "platform/heap/Handle.h"
11 #include "wtf/HashMap.h"
12 #include "wtf/OwnPtr.h"
13 #include "wtf/RefPtr.h"
19 class CORE_EXPORT NthIndexData final
: public NoBaseWillBeGarbageCollected
<NthIndexData
> {
20 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED(NthIndexData
);
21 WTF_MAKE_NONCOPYABLE(NthIndexData
);
22 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData
);
26 unsigned nthIndex(Element
&);
27 unsigned nthIndexOfType(Element
&, const QualifiedName
&);
28 unsigned nthLastIndex(Element
&);
29 unsigned nthLastIndexOfType(Element
&, const QualifiedName
&);
32 unsigned cacheNthIndices(Element
&);
33 unsigned cacheNthIndicesOfType(Element
&, const QualifiedName
&);
35 WillBeHeapHashMap
<RawPtrWillBeMember
<Element
>, unsigned> m_elementIndexMap
;
41 class CORE_EXPORT NthIndexCache final
{
43 WTF_MAKE_NONCOPYABLE(NthIndexCache
);
45 explicit NthIndexCache(Document
&);
48 unsigned nthChildIndex(Element
& element
)
50 ASSERT(element
.parentNode());
51 return ensureNthIndexDataFor(*element
.parentNode()).nthIndex(element
);
54 unsigned nthChildIndexOfType(Element
& element
, const QualifiedName
& type
)
56 ASSERT(element
.parentNode());
57 return nthIndexDataWithTagName(element
).nthIndexOfType(element
, type
);
60 unsigned nthLastChildIndex(Element
& element
)
62 ASSERT(element
.parentNode());
63 return ensureNthIndexDataFor(*element
.parentNode()).nthLastIndex(element
);
66 unsigned nthLastChildIndexOfType(Element
& element
, const QualifiedName
& type
)
68 ASSERT(element
.parentNode());
69 return nthIndexDataWithTagName(element
).nthLastIndexOfType(element
, type
);
73 using IndexByType
= WillBeHeapHashMap
<String
, OwnPtrWillBeMember
<NthIndexData
>>;
74 using ParentMap
= WillBeHeapHashMap
<RefPtrWillBeMember
<Node
>, OwnPtrWillBeMember
<NthIndexData
>>;
75 using ParentMapForType
= WillBeHeapHashMap
<RefPtrWillBeMember
<Node
>, OwnPtrWillBeMember
<IndexByType
>>;
77 NthIndexData
& ensureNthIndexDataFor(Node
&);
78 IndexByType
& ensureTypeIndexMap(Node
&);
79 NthIndexData
& nthIndexDataWithTagName(Element
&);
81 RawPtrWillBeMember
<Document
> m_document
;
82 OwnPtrWillBeMember
<ParentMap
> m_parentMap
;
83 OwnPtrWillBeMember
<ParentMapForType
> m_parentMapForType
;
86 uint64_t m_domTreeVersion
;
92 #endif // NthIndexCache_h