Chromium Code Reviews| 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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 // Determine if we should traverse from the beginning of the collection inst ead of the cached node. | 154 // Determine if we should traverse from the beginning of the collection inst ead of the cached node. |
| 155 bool firstIsCloser = index < currentIndex - index; | 155 bool firstIsCloser = index < currentIndex - index; |
| 156 if (firstIsCloser || !collection.canTraverseBackward()) { | 156 if (firstIsCloser || !collection.canTraverseBackward()) { |
| 157 NodeType* firstNode = collection.traverseToFirstElement(); | 157 NodeType* firstNode = collection.traverseToFirstElement(); |
| 158 ASSERT(firstNode); | 158 ASSERT(firstNode); |
| 159 setCachedNode(firstNode, 0); | 159 setCachedNode(firstNode, 0); |
| 160 return index ? nodeAfterCachedNode(collection, index) : firstNode; | 160 return index ? nodeAfterCachedNode(collection, index) : firstNode; |
| 161 } | 161 } |
| 162 | 162 |
| 163 // Backward traversal from the cached node to the requested index. | 163 // Backward traversal from the cached node to the requested index. |
| 164 NodeType* currentNode = cachedNode(); | |
| 165 ASSERT(collection.canTraverseBackward()); | 164 ASSERT(collection.canTraverseBackward()); |
| 166 while ((currentNode = collection.itemBefore(currentNode))) { | 165 NodeType* currentNode = collection.traverseBackwardToOffset(index, *cachedNo de(), currentIndex); |
|
Inactive
2014/04/09 23:08:58
We now pass a reference instead of a pointer to th
| |
| 167 ASSERT(currentIndex); | 166 ASSERT(currentNode); |
| 168 --currentIndex; | 167 setCachedNode(currentNode, currentIndex); |
| 169 if (currentIndex == index) { | 168 return currentNode; |
| 170 setCachedNode(currentNode, currentIndex); | |
| 171 return currentNode; | |
| 172 } | |
| 173 } | |
| 174 ASSERT_NOT_REACHED(); | |
| 175 return 0; | |
| 176 } | 169 } |
| 177 | 170 |
| 178 template <typename Collection, typename NodeType> | 171 template <typename Collection, typename NodeType> |
| 179 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeAfterCachedNode (const Collection& collection, unsigned index) | 172 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeAfterCachedNode (const Collection& collection, unsigned index) |
| 180 { | 173 { |
| 181 ASSERT(cachedNode()); // Cache should be valid. | 174 ASSERT(cachedNode()); // Cache should be valid. |
| 182 unsigned currentIndex = cachedNodeIndex(); | 175 unsigned currentIndex = cachedNodeIndex(); |
| 183 ASSERT(currentIndex < index); | 176 ASSERT(currentIndex < index); |
| 184 | 177 |
| 185 // Determine if we should traverse from the end of the collection instead of the cached node. | 178 // Determine if we should traverse from the end of the collection instead of the cached node. |
| 186 bool lastIsCloser = isCachedNodeCountValid() && cachedNodeCount() - index < index - currentIndex; | 179 bool lastIsCloser = isCachedNodeCountValid() && cachedNodeCount() - index < index - currentIndex; |
| 187 if (lastIsCloser && collection.canTraverseBackward()) { | 180 if (lastIsCloser && collection.canTraverseBackward()) { |
| 188 NodeType* lastItem = collection.itemBefore(0); | 181 NodeType* lastItem = collection.traverseToLastElement(); |
|
Inactive
2014/04/09 23:08:58
We now call traverseToLastElement() here instead o
| |
| 189 ASSERT(lastItem); | 182 ASSERT(lastItem); |
| 190 setCachedNode(lastItem, cachedNodeCount() - 1); | 183 setCachedNode(lastItem, cachedNodeCount() - 1); |
| 191 if (index < cachedNodeCount() - 1) | 184 if (index < cachedNodeCount() - 1) |
| 192 return nodeBeforeCachedNode(collection, index); | 185 return nodeBeforeCachedNode(collection, index); |
| 193 return lastItem; | 186 return lastItem; |
| 194 } | 187 } |
| 195 | 188 |
| 196 // Forward traversal from the cached node to the requested index. | 189 // Forward traversal from the cached node to the requested index. |
| 197 NodeType* currentNode = collection.traverseForwardToOffset(index, *cachedNod e(), currentIndex); | 190 NodeType* currentNode = collection.traverseForwardToOffset(index, *cachedNod e(), currentIndex); |
| 198 if (!currentNode) { | 191 if (!currentNode) { |
| 199 // Did not find the node. On plus side, we now know the length. | 192 // Did not find the node. On plus side, we now know the length. |
| 200 setCachedNodeCount(currentIndex + 1); | 193 setCachedNodeCount(currentIndex + 1); |
| 201 return 0; | 194 return 0; |
| 202 } | 195 } |
| 203 setCachedNode(currentNode, currentIndex); | 196 setCachedNode(currentNode, currentIndex); |
| 204 return currentNode; | 197 return currentNode; |
| 205 } | 198 } |
| 206 | 199 |
| 207 } // namespace WebCore | 200 } // namespace WebCore |
| 208 | 201 |
| 209 #endif // CollectionIndexCache_h | 202 #endif // CollectionIndexCache_h |
| OLD | NEW |