Chromium Code Reviews| 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> { | |
|
sof
2015/03/30 20:55:17
nit: add final.
rune
2015/03/30 22:01:45
Done. Same for NthIndexData.
| |
| 20 WTF_MAKE_NONCOPYABLE(NthIndexCache); | |
| 21 public: | |
| 22 static PassOwnPtr<NthIndexCache> create(Document& document) | |
| 23 { | |
| 24 return adoptPtr(new NthIndexCache(document)); | |
| 25 } | |
| 26 | |
|
sof
2015/03/30 20:55:17
nit: it wouldn't hurt to define a dtor for non-Oil
rune
2015/03/30 22:01:45
Done. Did the same for NthIndexData, but had to ha
| |
| 27 unsigned nthChildIndex(Element&); | |
| 28 unsigned nthLastChildIndex(Element&); | |
| 29 | |
| 30 void clear(); | |
| 31 | |
| 32 DECLARE_TRACE(); | |
| 33 | |
| 34 private: | |
| 35 NthIndexCache(Document& document) | |
|
sof
2015/03/30 20:55:17
nit: add explicit
rune
2015/03/30 22:01:45
Done.
| |
| 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 class NthIndexCacheScope { | |
| 64 STACK_ALLOCATED(); | |
| 65 WTF_MAKE_NONCOPYABLE(NthIndexCacheScope); | |
| 66 public: | |
| 67 NthIndexCacheScope(Document&); | |
| 68 ~NthIndexCacheScope(); | |
| 69 private: | |
| 70 RawPtrWillBeMember<Document> m_document; | |
| 71 }; | |
| 72 | |
| 73 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element) | |
| 74 { | |
| 75 if (!m_count) | |
| 76 return cacheNthIndices(element); | |
| 77 | |
| 78 unsigned index = 0; | |
| 79 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ ousSibling(*sibling), index++) { | |
| 80 auto it = m_elementIndexMap.find(sibling); | |
| 81 if (it != m_elementIndexMap.end()) | |
| 82 return it->value + index; | |
| 83 } | |
| 84 return index; | |
| 85 } | |
| 86 | |
| 87 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element) | |
| 88 { | |
| 89 unsigned index = nthIndex(element); | |
| 90 return m_count - index + 1; | |
| 91 } | |
| 92 | |
| 93 inline unsigned NthIndexCache::nthChildIndex(Element& element) | |
| 94 { | |
| 95 ASSERT(element.parentNode()); | |
| 96 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); | |
| 97 } | |
| 98 | |
| 99 inline unsigned NthIndexCache::nthLastChildIndex(Element& element) | |
| 100 { | |
| 101 ASSERT(element.parentNode()); | |
| 102 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element); | |
| 103 } | |
| 104 | |
| 105 } // namespace blink | |
| 106 | |
| 107 #endif // NthIndexCache_h | |
| OLD | NEW |