Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / dom / NthIndexCache.h
blob70f3e3aeff804c803ecebd04c1040c3d7d3694a7
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"
15 namespace blink {
17 class Document;
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);
23 public:
24 NthIndexData() { }
26 unsigned nthIndex(Element&);
27 unsigned nthIndexOfType(Element&, const QualifiedName&);
28 unsigned nthLastIndex(Element&);
29 unsigned nthLastIndexOfType(Element&, const QualifiedName&);
31 private:
32 unsigned cacheNthIndices(Element&);
33 unsigned cacheNthIndicesOfType(Element&, const QualifiedName&);
35 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexMap;
36 unsigned m_count = 0;
38 DECLARE_TRACE();
41 class CORE_EXPORT NthIndexCache final {
42 STACK_ALLOCATED();
43 WTF_MAKE_NONCOPYABLE(NthIndexCache);
44 public:
45 explicit NthIndexCache(Document&);
46 ~NthIndexCache();
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);
72 private:
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;
85 #if ENABLE(ASSERT)
86 uint64_t m_domTreeVersion;
87 #endif
90 } // namespace blink
92 #endif // NthIndexCache_h