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/RefPtr.h" | |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 class Document; | |
| 17 | |
| 18 class NthIndexCache : NoBaseWillBeGarbageCollected<NthIndexCache> { | |
| 19 WTF_MAKE_NONCOPYABLE(NthIndexCache); | |
| 20 public: | |
| 21 NthIndexCache(Document& document) | |
| 22 : m_document(document) { } | |
| 23 | |
| 24 unsigned nthChildIndex(Element&); | |
| 25 unsigned nthLastChildIndex(Element&); | |
| 26 | |
| 27 void clear(); | |
| 28 | |
| 29 DECLARE_TRACE(); | |
| 30 | |
| 31 private: | |
| 32 class NthIndexData : public NoBaseWillBeGarbageCollected<NthIndexData> { | |
| 33 WTF_MAKE_NONCOPYABLE(NthIndexData); | |
| 34 public: | |
| 35 NthIndexData() : m_count(0) { } | |
| 36 | |
| 37 inline unsigned nthIndex(Element&); | |
| 38 inline unsigned nthLastIndex(Element&); | |
| 39 | |
| 40 unsigned cacheNthIndices(Element&); | |
| 41 | |
| 42 WillBeHeapHashMap<RawPtrWillBeMember<Element>, unsigned> m_elementIndexM ap; | |
| 43 unsigned m_count; | |
| 44 | |
| 45 DECLARE_TRACE(); | |
| 46 }; | |
| 47 | |
| 48 NthIndexData& ensureNthIndexDataFor(Node&); | |
| 49 inline unsigned nthIndex(Element&); | |
| 50 | |
| 51 typedef WillBeHeapHashMap<RefPtrWillBeMember<Node>, OwnPtrWillBeMember<NthIn dexData>> ParentMap; | |
|
sof
2015/03/28 08:33:59
nit: "using" is preferred over "typedef" now.
rune
2015/03/30 09:00:03
Done.
| |
| 52 | |
| 53 OwnPtrWillBeMember<ParentMap> m_parentMap; | |
| 54 Document& m_document; | |
|
sof
2015/03/28 08:33:59
Make this a RawPtrWillBeMember<Document> and have
rune
2015/03/30 09:00:03
Done.
| |
| 55 }; | |
| 56 | |
| 57 inline unsigned NthIndexCache::NthIndexData::nthIndex(Element& element) | |
| 58 { | |
| 59 if (!m_count) | |
| 60 return cacheNthIndices(element); | |
| 61 | |
| 62 unsigned index = 0; | |
| 63 for (Element* sibling = &element; sibling; sibling = ElementTraversal::previ ousSibling(*sibling), index++) { | |
| 64 auto it = m_elementIndexMap.find(sibling); | |
| 65 if (it != m_elementIndexMap.end()) | |
| 66 return it->value + index; | |
| 67 } | |
| 68 return index; | |
| 69 } | |
| 70 | |
| 71 inline unsigned NthIndexCache::NthIndexData::nthLastIndex(Element& element) | |
| 72 { | |
| 73 unsigned index = nthIndex(element); | |
| 74 return m_count - index + 1; | |
| 75 } | |
| 76 | |
| 77 inline unsigned NthIndexCache::nthChildIndex(Element& element) | |
| 78 { | |
| 79 ASSERT(element.parentNode()); | |
| 80 return ensureNthIndexDataFor(*element.parentNode()).nthIndex(element); | |
| 81 } | |
| 82 | |
| 83 inline unsigned NthIndexCache::nthLastChildIndex(Element& element) | |
| 84 { | |
| 85 ASSERT(element.parentNode()); | |
| 86 return ensureNthIndexDataFor(*element.parentNode()).nthLastIndex(element); | |
| 87 } | |
| 88 | |
| 89 } // namespace blink | |
| 90 | |
| 91 #endif // NthIndexCache_h | |
| OLD | NEW |