2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #ifndef CollectionIndexCache_h
33 #define CollectionIndexCache_h
35 #include "core/dom/Element.h"
39 template <typename Collection, typename NodeType>
40 class CollectionIndexCache {
42 CollectionIndexCache();
44 bool isEmpty(const Collection& collection)
46 if (isCachedNodeCountValid())
47 return !cachedNodeCount();
50 return !nodeAt(collection, 0);
52 bool hasExactlyOneNode(const Collection& collection)
54 if (isCachedNodeCountValid())
55 return cachedNodeCount() == 1;
57 return !cachedNodeIndex() && !nodeAt(collection, 1);
58 return nodeAt(collection, 0) && !nodeAt(collection, 1);
61 unsigned nodeCount(const Collection&);
62 NodeType* nodeAt(const Collection&, unsigned index);
67 NodeType* nodeBeforeCachedNode(const Collection&, unsigned index);
68 NodeType* nodeAfterCachedNode(const Collection&, unsigned index);
70 ALWAYS_INLINE NodeType* cachedNode() const { return m_currentNode; }
71 ALWAYS_INLINE unsigned cachedNodeIndex() const { ASSERT(cachedNode()); return m_cachedNodeIndex; }
72 ALWAYS_INLINE void setCachedNode(NodeType* node, unsigned index)
76 m_cachedNodeIndex = index;
79 ALWAYS_INLINE bool isCachedNodeCountValid() const { return m_isLengthCacheValid; }
80 ALWAYS_INLINE unsigned cachedNodeCount() const { return m_cachedNodeCount; }
81 ALWAYS_INLINE void setCachedNodeCount(unsigned length)
83 m_cachedNodeCount = length;
84 m_isLengthCacheValid = true;
87 NodeType* m_currentNode;
88 unsigned m_cachedNodeCount;
89 unsigned m_cachedNodeIndex;
90 unsigned m_isLengthCacheValid : 1;
93 template <typename Collection, typename NodeType>
94 CollectionIndexCache<Collection, NodeType>::CollectionIndexCache()
96 , m_cachedNodeCount(0)
97 , m_cachedNodeIndex(0)
98 , m_isLengthCacheValid(false)
102 template <typename Collection, typename NodeType>
103 void CollectionIndexCache<Collection, NodeType>::invalidate()
106 m_isLengthCacheValid = false;
109 template <typename Collection, typename NodeType>
110 inline unsigned CollectionIndexCache<Collection, NodeType>::nodeCount(const Collection& collection)
112 if (isCachedNodeCountValid())
113 return cachedNodeCount();
115 nodeAt(collection, UINT_MAX);
116 ASSERT(isCachedNodeCountValid());
118 return cachedNodeCount();
121 template <typename Collection, typename NodeType>
122 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeAt(const Collection& collection, unsigned index)
124 if (isCachedNodeCountValid() && index >= cachedNodeCount())
128 if (index > cachedNodeIndex())
129 return nodeAfterCachedNode(collection, index);
130 if (index < cachedNodeIndex())
131 return nodeBeforeCachedNode(collection, index);
135 // No valid cache yet, let's find the first matching element.
136 ASSERT(!isCachedNodeCountValid());
137 NodeType* firstNode = collection.traverseToFirstElement();
139 // The collection is empty.
140 setCachedNodeCount(0);
143 setCachedNode(firstNode, 0);
144 return index ? nodeAfterCachedNode(collection, index) : firstNode;
147 template <typename Collection, typename NodeType>
148 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeBeforeCachedNode(const Collection& collection, unsigned index)
150 ASSERT(cachedNode()); // Cache should be valid.
151 unsigned currentIndex = cachedNodeIndex();
152 ASSERT(currentIndex > index);
154 // Determine if we should traverse from the beginning of the collection instead of the cached node.
155 bool firstIsCloser = index < currentIndex - index;
156 if (firstIsCloser || !collection.canTraverseBackward()) {
157 NodeType* firstNode = collection.traverseToFirstElement();
159 setCachedNode(firstNode, 0);
160 return index ? nodeAfterCachedNode(collection, index) : firstNode;
163 // Backward traversal from the cached node to the requested index.
164 ASSERT(collection.canTraverseBackward());
165 NodeType* currentNode = collection.traverseBackwardToOffset(index, *cachedNode(), currentIndex);
167 setCachedNode(currentNode, currentIndex);
171 template <typename Collection, typename NodeType>
172 inline NodeType* CollectionIndexCache<Collection, NodeType>::nodeAfterCachedNode(const Collection& collection, unsigned index)
174 ASSERT(cachedNode()); // Cache should be valid.
175 unsigned currentIndex = cachedNodeIndex();
176 ASSERT(currentIndex < index);
178 // Determine if we should traverse from the end of the collection instead of the cached node.
179 bool lastIsCloser = isCachedNodeCountValid() && cachedNodeCount() - index < index - currentIndex;
180 if (lastIsCloser && collection.canTraverseBackward()) {
181 NodeType* lastItem = collection.traverseToLastElement();
183 setCachedNode(lastItem, cachedNodeCount() - 1);
184 if (index < cachedNodeCount() - 1)
185 return nodeBeforeCachedNode(collection, index);
189 // Forward traversal from the cached node to the requested index.
190 NodeType* currentNode = collection.traverseForwardToOffset(index, *cachedNode(), currentIndex);
192 // Did not find the node. On plus side, we now know the length.
193 setCachedNodeCount(currentIndex + 1);
196 setCachedNode(currentNode, currentIndex);
200 } // namespace WebCore
202 #endif // CollectionIndexCache_h