OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef NthIndexCache_h |
| 6 #define NthIndexCache_h |
| 7 |
| 8 #include "core/dom/Element.h" |
| 9 #include "core/dom/ElementTraversal.h" |
| 10 #include "platform/heap/Handle.h" |
| 11 #include "wtf/HashMap.h" |
| 12 #include "wtf/OwnPtr.h" |
| 13 #include "wtf/RefPtr.h" |
| 14 |
| 15 namespace blink { |
| 16 |
| 17 class Document; |
| 18 |
| 19 class NthIndexCache : NoBaseWillBeGarbageCollected<NthIndexCache> { |
| 20 WTF_MAKE_NONCOPYABLE(NthIndexCache); |
| 21 public: |
| 22 static PassOwnPtr<NthIndexCache> create(Document& document) |
| 23 { |
| 24 return adoptPtr(new NthIndexCache(document)); |
| 25 } |
| 26 |
| 27 unsigned nthChildIndex(Element&); |
| 28 unsigned nthLastChildIndex(Element&); |
| 29 |
| 30 void clear(); |
| 31 |
| 32 DECLARE_TRACE(); |
| 33 |
| 34 private: |
| 35 NthIndexCache(Document& document) |
| 36 : m_document(document) { } |
| 37 |
| 38 class NthIndexData : public NoBaseWillBeGarbageCollected<NthIndexData> { |
| 39 WTF_MAKE_NONCOPYABLE(NthIndexData); |
| 40 public: |
| 41 NthIndexData() : m_count(0) { } |
| 42 |
| 43 inline unsigned nthIndex(Element&); |
| 44 inline unsigned nthLastIndex(Element&); |
| 45 |
| 46 unsigned cacheNthIndices(Element&); |
| 47 |
| 48 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM
ap; |
| 49 unsigned m_count; |
| 50 |
| 51 DECLARE_TRACE(); |
| 52 }; |
| 53 |
| 54 NthIndexData& ensureNthIndexDataFor(Node&); |
| 55 inline unsigned nthIndex(Element&); |
| 56 |
| 57 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe
mber<NthIndexData>>; |
| 58 |
| 59 OwnPtrWillBeMember<ParentMap> m_parentMap; |
| 60 RawPtrWillBeMember<Document> m_document; |
| 61 }; |
| 62 |
| 63 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element) |
| 64 { |
| 65 if (!m_count) |
| 66 return cacheNthIndices(element); |
| 67 |
| 68 unsigned index = 0; |
| 69 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ
ousSibling(*sibling), index++) { |
| 70 auto it = m_elementIndexMap.find(sibling); |
| 71 if (it != m_elementIndexMap.end()) |
| 72 return it->value + index; |
| 73 } |
| 74 return index; |
| 75 } |
| 76 |
| 77 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element) |
| 78 { |
| 79 unsigned index = nthIndex(element); |
| 80 return m_count - index + 1; |
| 81 } |
| 82 |
| 83 inline unsigned NthIndexCache::nthChildIndex(Element& element) |
| 84 { |
| 85 ASSERT(element.parentNode()); |
| 86 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); |
| 87 } |
| 88 |
| 89 inline unsigned NthIndexCache::nthLastChildIndex(Element& element) |
| 90 { |
| 91 ASSERT(element.parentNode()); |
| 92 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element); |
| 93 } |
| 94 |
| 95 } // namespace blink |
| 96 |
| 97 #endif // NthIndexCache_h |
OLD | NEW |