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 final { | |
| 20 STACK_ALLOCATED(); | |
| 21 WTF_MAKE_NONCOPYABLE(NthIndexCache); | |
| 22 public: | |
| 23 NthIndexCache(Document&); | |
|
esprehn
2015/03/31 17:35:15
explicit
rune
2015/03/31 20:57:37
Done.
| |
| 24 ~NthIndexCache(); | |
| 25 | |
| 26 static NthIndexCache* getInstance() { return s_instance; } | |
|
sof
2015/03/31 16:59:21
nit: instance() (or current())
esprehn
2015/03/31 17:35:15
instance()
rune
2015/03/31 20:57:37
Done.
| |
| 27 | |
| 28 inline unsigned nthChildIndex(Element&); | |
| 29 inline unsigned nthLastChildIndex(Element&); | |
| 30 | |
| 31 private: | |
| 32 class NthIndexData final : public NoBaseWillBeGarbageCollected<NthIndexData> { | |
| 33 WTF_MAKE_NONCOPYABLE(NthIndexData); | |
| 34 DECLARE_EMPTY_DESTRUCTOR_WILL_BE_REMOVED(NthIndexData); | |
| 35 public: | |
| 36 NthIndexData() : m_count(0) { } | |
|
esprehn
2015/03/31 17:35:15
I think you can actually do m_count = 0; below now
rune
2015/03/31 20:57:37
Done. I had to keep the constructor since there's
| |
| 37 | |
| 38 inline unsigned nthIndex(Element&); | |
| 39 inline unsigned nthLastIndex(Element&); | |
| 40 | |
| 41 unsigned cacheNthIndices(Element&); | |
| 42 | |
| 43 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM ap; | |
| 44 unsigned m_count; | |
| 45 | |
| 46 DECLARE_TRACE(); | |
| 47 }; | |
| 48 | |
| 49 NthIndexData& ensureNthIndexDataFor(Node&); | |
| 50 inline unsigned nthIndex(Element&); | |
| 51 | |
| 52 using ParentMap = WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMe mber<NthIndexData>>; | |
| 53 | |
| 54 OwnPtrWillBeMember<ParentMap> m_parentMap; | |
| 55 RawPtrWillBeMember<Document> m_document; | |
| 56 uint64_t m_domTreeVersion; | |
| 57 | |
| 58 static NthIndexCache* s_instance; | |
| 59 }; | |
| 60 | |
| 61 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element) | |
| 62 { | |
| 63 if (!m_count) | |
| 64 return cacheNthIndices(element); | |
| 65 | |
| 66 unsigned index = 0; | |
| 67 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ ousSibling(*sibling), index++) { | |
| 68 auto it = m_elementIndexMap.find(sibling); | |
| 69 if (it != m_elementIndexMap.end()) | |
| 70 return it->value + index; | |
| 71 } | |
| 72 return index; | |
| 73 } | |
| 74 | |
| 75 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element) | |
| 76 { | |
| 77 unsigned index = nthIndex(element); | |
| 78 return m_count - index + 1; | |
| 79 } | |
| 80 | |
| 81 inline unsigned NthIndexCache::nthChildIndex(Element& element) | |
| 82 { | |
| 83 ASSERT(element.parentNode()); | |
| 84 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); | |
| 85 } | |
| 86 | |
| 87 inline unsigned NthIndexCache::nthLastChildIndex(Element& element) | |
| 88 { | |
| 89 ASSERT(element.parentNode()); | |
| 90 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element); | |
| 91 } | |
| 92 | |
| 93 } // namespace blink | |
| 94 | |
| 95 #endif // NthIndexCache_h | |
| OLD | NEW |