OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2013 Apple Inc. All rights reserved. | 2 * Copyright (C) 2013 Apple Inc. All rights reserved. |
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved. | 3 * Copyright (C) 2014 Samsung Electronics. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
7 * met: | 7 * met: |
8 * | 8 * |
9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 template <typename Collection> | 123 template <typename Collection> |
124 inline Node* CollectionIndexCache<Collection>::nodeAt(const Collection& collecti
on, unsigned index) | 124 inline Node* CollectionIndexCache<Collection>::nodeAt(const Collection& collecti
on, unsigned index) |
125 { | 125 { |
126 if (cachedNode() && cachedNodeIndex() == index) | 126 if (cachedNode() && cachedNodeIndex() == index) |
127 return cachedNode(); | 127 return cachedNode(); |
128 | 128 |
129 if (isCachedNodeCountValid() && cachedNodeCount() <= index) | 129 if (isCachedNodeCountValid() && cachedNodeCount() <= index) |
130 return 0; | 130 return 0; |
131 | 131 |
132 ContainerNode& root = collection.rootNode(); | 132 ContainerNode& root = collection.rootNode(); |
133 if (isCachedNodeCountValid() && !collection.overridesItemAfter() && isLastNo
deCloserThanLastOrCachedNode(index)) { | 133 if (isCachedNodeCountValid() && collection.canTraverseBackward() && isLastNo
deCloserThanLastOrCachedNode(index)) { |
134 Node* lastNode = collection.itemBefore(0); | 134 Node* lastNode = collection.itemBefore(0); |
135 ASSERT(lastNode); | 135 ASSERT(lastNode); |
136 setCachedNode(lastNode, cachedNodeCount() - 1); | 136 setCachedNode(lastNode, cachedNodeCount() - 1); |
137 } else if (!cachedNode() || isFirstNodeCloserThanCachedNode(index) || (colle
ction.overridesItemAfter() && index < cachedNodeIndex())) { | 137 } else if (!cachedNode() || isFirstNodeCloserThanCachedNode(index) || (!coll
ection.canTraverseBackward() && index < cachedNodeIndex())) { |
138 Node* firstNode = collection.traverseToFirstElement(root); | 138 Node* firstNode = collection.traverseToFirstElement(root); |
139 if (!firstNode) { | 139 if (!firstNode) { |
140 setCachedNodeCount(0); | 140 setCachedNodeCount(0); |
141 return 0; | 141 return 0; |
142 } | 142 } |
143 setCachedNode(firstNode, 0); | 143 setCachedNode(firstNode, 0); |
144 ASSERT(!cachedNodeIndex()); | 144 ASSERT(!cachedNodeIndex()); |
145 } | 145 } |
146 | 146 |
147 if (cachedNodeIndex() == index) | 147 if (cachedNodeIndex() == index) |
148 return cachedNode(); | 148 return cachedNode(); |
149 | 149 |
150 return nodeBeforeOrAfterCachedNode(collection, index, root); | 150 return nodeBeforeOrAfterCachedNode(collection, index, root); |
151 } | 151 } |
152 | 152 |
153 template <typename Collection> | 153 template <typename Collection> |
154 inline Node* CollectionIndexCache<Collection>::nodeBeforeOrAfterCachedNode(const
Collection& collection, unsigned index, const ContainerNode &root) | 154 inline Node* CollectionIndexCache<Collection>::nodeBeforeOrAfterCachedNode(const
Collection& collection, unsigned index, const ContainerNode &root) |
155 { | 155 { |
156 unsigned currentIndex = cachedNodeIndex(); | 156 unsigned currentIndex = cachedNodeIndex(); |
157 Node* currentNode = cachedNode(); | 157 Node* currentNode = cachedNode(); |
158 ASSERT(currentNode); | 158 ASSERT(currentNode); |
159 ASSERT(currentIndex != index); | 159 ASSERT(currentIndex != index); |
160 | 160 |
161 if (index < cachedNodeIndex()) { | 161 if (index < cachedNodeIndex()) { |
162 ASSERT(!collection.overridesItemAfter()); | 162 ASSERT(collection.canTraverseBackward()); |
163 while ((currentNode = collection.itemBefore(currentNode))) { | 163 while ((currentNode = collection.itemBefore(currentNode))) { |
164 ASSERT(currentIndex); | 164 ASSERT(currentIndex); |
165 currentIndex--; | 165 currentIndex--; |
166 if (currentIndex == index) { | 166 if (currentIndex == index) { |
167 setCachedNode(currentNode, currentIndex); | 167 setCachedNode(currentNode, currentIndex); |
168 return currentNode; | 168 return currentNode; |
169 } | 169 } |
170 } | 170 } |
171 ASSERT_NOT_REACHED(); | 171 ASSERT_NOT_REACHED(); |
172 return 0; | 172 return 0; |
(...skipping 26 matching lines...) Expand all Loading... |
199 if (cachedNodeIndex() < index) | 199 if (cachedNodeIndex() < index) |
200 return false; | 200 return false; |
201 | 201 |
202 unsigned distanceFromCachedNode = cachedNodeIndex() - index; | 202 unsigned distanceFromCachedNode = cachedNodeIndex() - index; |
203 return index < distanceFromCachedNode; | 203 return index < distanceFromCachedNode; |
204 } | 204 } |
205 | 205 |
206 } // namespace WebCore | 206 } // namespace WebCore |
207 | 207 |
208 #endif // CollectionIndexCache_h | 208 #endif // CollectionIndexCache_h |
OLD | NEW |