summaryrefslogtreecommitdiff
path: root/chromium/third_party/WebKit/Source/core/dom/CollectionIndexCache.h
blob: d395ce2061d910c85aaabe0817d9706662f91be8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 * Copyright (C) 2013 Apple Inc. All rights reserved.
 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef CollectionIndexCache_h
#define CollectionIndexCache_h

#include "platform/heap/Handle.h"

namespace blink {

template <typename Collection, typename NodeType>
class CollectionIndexCache {
  DISALLOW_NEW();

 public:
  CollectionIndexCache();

  bool IsEmpty(const Collection& collection) {
    if (IsCachedNodeCountValid())
      return !CachedNodeCount();
    if (CachedNode())
      return false;
    return !NodeAt(collection, 0);
  }
  bool HasExactlyOneNode(const Collection& collection) {
    if (IsCachedNodeCountValid())
      return CachedNodeCount() == 1;
    if (CachedNode())
      return !CachedNodeIndex() && !NodeAt(collection, 1);
    return NodeAt(collection, 0) && !NodeAt(collection, 1);
  }

  unsigned NodeCount(const Collection&);
  NodeType* NodeAt(const Collection&, unsigned index);

  void Invalidate();

  void NodeInserted();
  void NodeRemoved();

  DEFINE_INLINE_VIRTUAL_TRACE() { visitor->Trace(current_node_); }

 protected:
  ALWAYS_INLINE NodeType* CachedNode() const { return current_node_; }
  ALWAYS_INLINE unsigned CachedNodeIndex() const {
    DCHECK(CachedNode());
    return cached_node_index_;
  }
  ALWAYS_INLINE void SetCachedNode(NodeType* node, unsigned index) {
    DCHECK(node);
    current_node_ = node;
    cached_node_index_ = index;
  }

  ALWAYS_INLINE bool IsCachedNodeCountValid() const {
    return is_length_cache_valid_;
  }
  ALWAYS_INLINE unsigned CachedNodeCount() const { return cached_node_count_; }
  ALWAYS_INLINE void SetCachedNodeCount(unsigned length) {
    cached_node_count_ = length;
    is_length_cache_valid_ = true;
  }

 private:
  NodeType* NodeBeforeCachedNode(const Collection&, unsigned index);
  NodeType* NodeAfterCachedNode(const Collection&, unsigned index);

  Member<NodeType> current_node_;
  unsigned cached_node_count_;
  unsigned cached_node_index_ : 31;
  unsigned is_length_cache_valid_ : 1;
};

template <typename Collection, typename NodeType>
CollectionIndexCache<Collection, NodeType>::CollectionIndexCache()
    : current_node_(nullptr),
      cached_node_count_(0),
      cached_node_index_(0),
      is_length_cache_valid_(false) {}

template <typename Collection, typename NodeType>
void CollectionIndexCache<Collection, NodeType>::Invalidate() {
  current_node_ = nullptr;
  is_length_cache_valid_ = false;
}

template <typename Collection, typename NodeType>
void CollectionIndexCache<Collection, NodeType>::NodeInserted() {
  cached_node_count_++;
  current_node_ = nullptr;
}

template <typename Collection, typename NodeType>
void CollectionIndexCache<Collection, NodeType>::NodeRemoved() {
  cached_node_count_--;
  current_node_ = nullptr;
}

template <typename Collection, typename NodeType>
inline unsigned CollectionIndexCache<Collection, NodeType>::NodeCount(
    const Collection& collection) {
  if (IsCachedNodeCountValid())
    return CachedNodeCount();

  NodeAt(collection, UINT_MAX);
  DCHECK(IsCachedNodeCountValid());

  return CachedNodeCount();
}

template <typename Collection, typename NodeType>
inline NodeType* CollectionIndexCache<Collection, NodeType>::NodeAt(
    const Collection& collection,
    unsigned index) {
  if (IsCachedNodeCountValid() && index >= CachedNodeCount())
    return nullptr;

  if (CachedNode()) {
    if (index > CachedNodeIndex())
      return NodeAfterCachedNode(collection, index);
    if (index < CachedNodeIndex())
      return NodeBeforeCachedNode(collection, index);
    return CachedNode();
  }

  // No valid cache yet, let's find the first matching element.
  NodeType* first_node = collection.TraverseToFirst();
  if (!first_node) {
    // The collection is empty.
    SetCachedNodeCount(0);
    return nullptr;
  }
  SetCachedNode(first_node, 0);
  return index ? NodeAfterCachedNode(collection, index) : first_node;
}

template <typename Collection, typename NodeType>
inline NodeType*
CollectionIndexCache<Collection, NodeType>::NodeBeforeCachedNode(
    const Collection& collection,
    unsigned index) {
  DCHECK(CachedNode());  // Cache should be valid.
  unsigned current_index = CachedNodeIndex();
  DCHECK_GT(current_index, index);

  // Determine if we should traverse from the beginning of the collection
  // instead of the cached node.
  bool first_is_closer = index < current_index - index;
  if (first_is_closer || !collection.CanTraverseBackward()) {
    NodeType* first_node = collection.TraverseToFirst();
    DCHECK(first_node);
    SetCachedNode(first_node, 0);
    return index ? NodeAfterCachedNode(collection, index) : first_node;
  }

  // Backward traversal from the cached node to the requested index.
  DCHECK(collection.CanTraverseBackward());
  NodeType* current_node =
      collection.TraverseBackwardToOffset(index, *CachedNode(), current_index);
  DCHECK(current_node);
  SetCachedNode(current_node, current_index);
  return current_node;
}

template <typename Collection, typename NodeType>
inline NodeType*
CollectionIndexCache<Collection, NodeType>::NodeAfterCachedNode(
    const Collection& collection,
    unsigned index) {
  DCHECK(CachedNode());  // Cache should be valid.
  unsigned current_index = CachedNodeIndex();
  DCHECK_LT(current_index, index);

  // Determine if we should traverse from the end of the collection instead of
  // the cached node.
  bool last_is_closer = IsCachedNodeCountValid() &&
                        CachedNodeCount() - index < index - current_index;
  if (last_is_closer && collection.CanTraverseBackward()) {
    NodeType* last_item = collection.TraverseToLast();
    DCHECK(last_item);
    SetCachedNode(last_item, CachedNodeCount() - 1);
    if (index < CachedNodeCount() - 1)
      return NodeBeforeCachedNode(collection, index);
    return last_item;
  }

  // Forward traversal from the cached node to the requested index.
  NodeType* current_node =
      collection.TraverseForwardToOffset(index, *CachedNode(), current_index);
  if (!current_node) {
    // Did not find the node. On plus side, we now know the length.
    if (IsCachedNodeCountValid()) {
      DCHECK_EQ(current_index + 1, CachedNodeCount());
    }
    SetCachedNodeCount(current_index + 1);
    return nullptr;
  }
  SetCachedNode(current_node, current_index);
  return current_node;
}

}  // namespace blink

#endif  // CollectionIndexCache_h