| 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 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 nodeAt(collection, UINT_MAX); | 120 nodeAt(collection, UINT_MAX); |
| 121 ASSERT(isCachedNodeCountValid()); | 121 ASSERT(isCachedNodeCountValid()); |
| 122 | 122 |
| 123 return cachedNodeCount(); | 123 return cachedNodeCount(); |
| 124 } | 124 } |
| 125 | 125 |
| 126 template <typename Collection, typename NodeType> | 126 template <typename Collection, typename NodeType> |
| 127 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeAt(const Collec
tion& collection, unsigned index) | 127 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeAt(const Collec
tion& collection, unsigned index) |
| 128 { | 128 { |
| 129 if (isCachedNodeCountValid() && index >= cachedNodeCount()) | 129 if (isCachedNodeCountValid() && index >= cachedNodeCount()) |
| 130 return 0; | 130 return nullptr; |
| 131 | 131 |
| 132 if (cachedNode()) { | 132 if (cachedNode()) { |
| 133 if (index > cachedNodeIndex()) | 133 if (index > cachedNodeIndex()) |
| 134 return nodeAfterCachedNode(collection, index); | 134 return nodeAfterCachedNode(collection, index); |
| 135 if (index < cachedNodeIndex()) | 135 if (index < cachedNodeIndex()) |
| 136 return nodeBeforeCachedNode(collection, index); | 136 return nodeBeforeCachedNode(collection, index); |
| 137 return cachedNode(); | 137 return cachedNode(); |
| 138 } | 138 } |
| 139 | 139 |
| 140 // No valid cache yet, let's find the first matching element. | 140 // No valid cache yet, let's find the first matching element. |
| 141 ASSERT(!isCachedNodeCountValid()); | 141 ASSERT(!isCachedNodeCountValid()); |
| 142 NodeType* firstNode = collection.traverseToFirst(); | 142 NodeType* firstNode = collection.traverseToFirst(); |
| 143 if (!firstNode) { | 143 if (!firstNode) { |
| 144 // The collection is empty. | 144 // The collection is empty. |
| 145 setCachedNodeCount(0); | 145 setCachedNodeCount(0); |
| 146 return 0; | 146 return nullptr; |
| 147 } | 147 } |
| 148 setCachedNode(firstNode, 0); | 148 setCachedNode(firstNode, 0); |
| 149 return index ? nodeAfterCachedNode(collection, index) : firstNode; | 149 return index ? nodeAfterCachedNode(collection, index) : firstNode; |
| 150 } | 150 } |
| 151 | 151 |
| 152 template <typename Collection, typename NodeType> | 152 template <typename Collection, typename NodeType> |
| 153 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeBeforeCachedNod
e(const Collection& collection, unsigned index) | 153 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeBeforeCachedNod
e(const Collection& collection, unsigned index) |
| 154 { | 154 { |
| 155 ASSERT(cachedNode()); // Cache should be valid. | 155 ASSERT(cachedNode()); // Cache should be valid. |
| 156 unsigned currentIndex = cachedNodeIndex(); | 156 unsigned currentIndex = cachedNodeIndex(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 if (index < cachedNodeCount() - 1) | 189 if (index < cachedNodeCount() - 1) |
| 190 return nodeBeforeCachedNode(collection, index); | 190 return nodeBeforeCachedNode(collection, index); |
| 191 return lastItem; | 191 return lastItem; |
| 192 } | 192 } |
| 193 | 193 |
| 194 // Forward traversal from the cached node to the requested index. | 194 // Forward traversal from the cached node to the requested index. |
| 195 NodeType* currentNode = collection.traverseForwardToOffset(index, *cachedNod
e(), currentIndex); | 195 NodeType* currentNode = collection.traverseForwardToOffset(index, *cachedNod
e(), currentIndex); |
| 196 if (!currentNode) { | 196 if (!currentNode) { |
| 197 // Did not find the node. On plus side, we now know the length. | 197 // Did not find the node. On plus side, we now know the length. |
| 198 setCachedNodeCount(currentIndex + 1); | 198 setCachedNodeCount(currentIndex + 1); |
| 199 return 0; | 199 return nullptr; |
| 200 } | 200 } |
| 201 setCachedNode(currentNode, currentIndex); | 201 setCachedNode(currentNode, currentIndex); |
| 202 return currentNode; | 202 return currentNode; |
| 203 } | 203 } |
| 204 | 204 |
| 205 } // namespace blink | 205 } // namespace blink |
| 206 | 206 |
| 207 #endif // CollectionIndexCache_h | 207 #endif // CollectionIndexCache_h |
| OLD | NEW |