Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(437)

Side by Side Diff: Source/core/html/CollectionIndexCache.h

Issue 134193005: Move caching out of LiveNodeListBase and into a new CollectionIndexCache class (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/dom/LiveNodeList.cpp ('k') | Source/core/html/HTMLCollection.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
14 * distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef CollectionIndexCache_h
33 #define CollectionIndexCache_h
34
35 #include "core/dom/Element.h"
36
37 namespace WebCore {
38
39 template <typename Collection>
40 class CollectionIndexCache {
41 public:
42 CollectionIndexCache();
43
44 bool isEmpty(const Collection& collection)
45 {
46 if (isCachedNodeCountValid())
47 return !cachedNodeCount();
48 if (cachedNode())
49 return false;
50 return !nodeAt(collection, 0);
51 }
52 bool hasExactlyOneNode(const Collection& collection)
53 {
54 if (isCachedNodeCountValid())
55 return cachedNodeCount() == 1;
56 if (cachedNode())
57 return !cachedNodeIndex() && !nodeAt(collection, 1);
58 return nodeAt(collection, 0) && !nodeAt(collection, 1);
59 }
60
61 unsigned nodeCount(const Collection&);
62 Node* nodeAt(const Collection&, unsigned index);
63
64 void invalidate();
65
66 ALWAYS_INLINE void setCachedNode(Node* node, unsigned index)
67 {
68 ASSERT(node);
69 m_currentNode = node;
70 m_cachedNodeIndex = index;
71 }
72
73 private:
74 Node* nodeBeforeOrAfterCachedNode(const Collection&, unsigned index, const C ontainerNode& root);
75 bool isLastNodeCloserThanLastOrCachedNode(unsigned index) const;
76 bool isFirstNodeCloserThanCachedNode(unsigned index) const;
77
78 ALWAYS_INLINE Node* cachedNode() const { return m_currentNode; }
79 ALWAYS_INLINE unsigned cachedNodeIndex() const { ASSERT(cachedNode()); retur n m_cachedNodeIndex; }
80
81 ALWAYS_INLINE bool isCachedNodeCountValid() const { return m_isLengthCacheVa lid; }
82 ALWAYS_INLINE unsigned cachedNodeCount() const { return m_cachedNodeCount; }
83 ALWAYS_INLINE void setCachedNodeCount(unsigned length)
84 {
85 m_cachedNodeCount = length;
86 m_isLengthCacheValid = true;
87 }
88
89 Node* m_currentNode;
90 unsigned m_cachedNodeCount;
91 unsigned m_cachedNodeIndex;
92 unsigned m_isLengthCacheValid : 1;
93 };
94
95 template <typename Collection>
96 CollectionIndexCache<Collection>::CollectionIndexCache()
97 : m_currentNode(0)
98 , m_cachedNodeCount(0)
99 , m_cachedNodeIndex(0)
100 , m_isLengthCacheValid(false)
101 {
102 }
103
104 template <typename Collection>
105 void CollectionIndexCache<Collection>::invalidate()
106 {
107 m_currentNode = 0;
108 m_isLengthCacheValid = false;
109 }
110
111 template <typename Collection>
112 inline unsigned CollectionIndexCache<Collection>::nodeCount(const Collection& co llection)
113 {
114 if (isCachedNodeCountValid())
115 return cachedNodeCount();
116
117 nodeAt(collection, UINT_MAX);
118 ASSERT(isCachedNodeCountValid());
119
120 return cachedNodeCount();
121 }
122
123 template <typename Collection>
124 inline Node* CollectionIndexCache<Collection>::nodeAt(const Collection& collecti on, unsigned index)
125 {
126 if (cachedNode() && cachedNodeIndex() == index)
127 return cachedNode();
128
129 if (isCachedNodeCountValid() && cachedNodeCount() <= index)
130 return 0;
131
132 ContainerNode& root = collection.rootNode();
133 if (isCachedNodeCountValid() && !collection.overridesItemAfter() && isLastNo deCloserThanLastOrCachedNode(index)) {
134 Node* lastNode = collection.itemBefore(0);
135 ASSERT(lastNode);
136 setCachedNode(lastNode, cachedNodeCount() - 1);
137 } else if (!cachedNode() || isFirstNodeCloserThanCachedNode(index) || (colle ction.overridesItemAfter() && index < cachedNodeIndex())) {
138 Node* firstNode = collection.traverseToFirstElement(root);
139 if (!firstNode) {
140 setCachedNodeCount(0);
141 return 0;
142 }
143 setCachedNode(firstNode, 0);
144 ASSERT(!cachedNodeIndex());
145 }
146
147 if (cachedNodeIndex() == index)
148 return cachedNode();
149
150 return nodeBeforeOrAfterCachedNode(collection, index, root);
151 }
152
153 template <typename Collection>
154 inline Node* CollectionIndexCache<Collection>::nodeBeforeOrAfterCachedNode(const Collection& collection, unsigned index, const ContainerNode &root)
155 {
156 unsigned currentIndex = cachedNodeIndex();
157 Node* currentNode = cachedNode();
158 ASSERT(currentNode);
159 ASSERT(currentIndex != index);
160
161 if (index < cachedNodeIndex()) {
162 ASSERT(!collection.overridesItemAfter());
163 while ((currentNode = collection.itemBefore(currentNode))) {
164 ASSERT(currentIndex);
165 currentIndex--;
166 if (currentIndex == index) {
167 setCachedNode(currentNode, currentIndex);
168 return currentNode;
169 }
170 }
171 ASSERT_NOT_REACHED();
172 return 0;
173 }
174
175 currentNode = collection.traverseForwardToOffset(index, *currentNode, curren tIndex, root);
176 if (!currentNode) {
177 // Did not find the node. On plus side, we now know the length.
178 setCachedNodeCount(currentIndex + 1);
179 return 0;
180 }
181 setCachedNode(currentNode, currentIndex);
182 return currentNode;
183 }
184
185 template <typename Collection>
186 ALWAYS_INLINE bool CollectionIndexCache<Collection>::isLastNodeCloserThanLastOrC achedNode(unsigned index) const
187 {
188 ASSERT(isCachedNodeCountValid());
189 unsigned distanceFromLastNode = cachedNodeCount() - index;
190 if (!cachedNode())
191 return distanceFromLastNode < index;
192
193 return cachedNodeIndex() < index && distanceFromLastNode < index - cachedNod eIndex();
194 }
195
196 template <typename Collection>
197 ALWAYS_INLINE bool CollectionIndexCache<Collection>::isFirstNodeCloserThanCached Node(unsigned index) const
198 {
199 if (cachedNodeIndex() < index)
200 return false;
201
202 unsigned distanceFromCachedNode = cachedNodeIndex() - index;
203 return index < distanceFromCachedNode;
204 }
205
206 } // namespace WebCore
207
208 #endif // CollectionIndexCache_h
OLDNEW
« no previous file with comments | « Source/core/dom/LiveNodeList.cpp ('k') | Source/core/html/HTMLCollection.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698