OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_BLINK_INTERVAL_MAP_H_ | 5 #ifndef MEDIA_BLINK_INTERVAL_MAP_H_ |
6 #define MEDIA_BLINK_INTERVAL_MAP_H_ | 6 #define MEDIA_BLINK_INTERVAL_MAP_H_ |
7 | 7 |
8 #include <limits> | 8 #include <limits> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 } | 117 } |
118 | 118 |
119 // Returns the value associated with the current interval. | 119 // Returns the value associated with the current interval. |
120 ValueType value() const { | 120 ValueType value() const { |
121 DCHECK(iter_ != map_->end()); | 121 DCHECK(iter_ != map_->end()); |
122 return iter_->second; | 122 return iter_->second; |
123 } | 123 } |
124 | 124 |
125 // Needed to make the following construct work: | 125 // Needed to make the following construct work: |
126 // for (const auto& interval_value_pair : interval_map) | 126 // for (const auto& interval_value_pair : interval_map) |
127 // Note however that this will skip the "end" interval, which | |
128 // is usually ok since it generally has the default value. | |
129 std::pair<Interval<KeyType>, ValueType> operator*() const { | 127 std::pair<Interval<KeyType>, ValueType> operator*() const { |
130 return std::make_pair(interval(), value()); | 128 return std::make_pair(interval(), value()); |
131 } | 129 } |
132 | 130 |
133 // Go to the next interval. | 131 // Go to the next interval. |
134 // The beginning of the next interval always matches the end of the current | 132 // The beginning of the next interval always matches the end of the current |
135 // interval. (But should always have a different value.) | 133 // interval. (But should always have a different value.) |
136 // Not allowed if we're already at map_->end(). | 134 // Not allowed if we're already at map_->end(). |
137 void operator++() { | 135 void operator++() { |
138 DCHECK(iter_ != map_->end()); | 136 DCHECK(iter_ != map_->end()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 // Defaults to ValueType(). | 173 // Defaults to ValueType(). |
176 ValueType operator[](const KeyType& k) const { | 174 ValueType operator[](const KeyType& k) const { |
177 typename MapType::const_iterator i = map_.upper_bound(k); | 175 typename MapType::const_iterator i = map_.upper_bound(k); |
178 DCHECK(i != map_.begin()); | 176 DCHECK(i != map_.begin()); |
179 --i; | 177 --i; |
180 return i->second; | 178 return i->second; |
181 } | 179 } |
182 | 180 |
183 // Increase [from..to) by |how_much|. | 181 // Increase [from..to) by |how_much|. |
184 void IncrementInterval(KeyType from, KeyType to, ValueType how_much) { | 182 void IncrementInterval(KeyType from, KeyType to, ValueType how_much) { |
185 DCHECK_GT(to, from); | 183 if (to <= from || how_much == 0) |
186 if (how_much == 0) | |
187 return; | 184 return; |
188 typename MapType::iterator a = MakeEntry(from); | 185 typename MapType::iterator a = MakeEntry(from); |
189 typename MapType::iterator b = MakeEntry(to); | 186 typename MapType::iterator b = MakeEntry(to); |
190 for (typename MapType::iterator i = a; i != b; ++i) { | 187 for (typename MapType::iterator i = a; i != b; ++i) { |
191 i->second += how_much; | 188 i->second += how_much; |
192 } | 189 } |
193 RemoveDuplicates(a); | 190 RemoveDuplicates(a); |
194 // b may be invalid | 191 // b may be invalid |
195 RemoveDuplicates(map_.lower_bound(to)); | 192 RemoveDuplicates(map_.lower_bound(to)); |
196 } | 193 } |
197 | 194 |
198 // Set [from..to) to |how_much|. | 195 // Set [from..to) to |how_much|. |
199 void SetInterval(KeyType from, KeyType to, ValueType how_much) { | 196 void SetInterval(KeyType from, KeyType to, ValueType how_much) { |
200 DCHECK_GT(to, from); | 197 if (to <= from) |
| 198 return; |
201 typename MapType::iterator a = MakeEntry(from); | 199 typename MapType::iterator a = MakeEntry(from); |
202 typename MapType::iterator b = MakeEntry(to); | 200 typename MapType::iterator b = MakeEntry(to); |
203 a->second = how_much; | 201 a->second = how_much; |
204 while (true) { | 202 while (true) { |
205 typename MapType::iterator c = a; | 203 typename MapType::iterator c = a; |
206 ++c; | 204 ++c; |
207 if (c == b) { | 205 if (c == b) { |
208 break; | 206 break; |
209 } else { | 207 } else { |
210 map_.erase(c); | 208 map_.erase(c); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 map_.erase(second); | 279 map_.erase(second); |
282 } | 280 } |
283 } | 281 } |
284 | 282 |
285 MapType map_; | 283 MapType map_; |
286 }; | 284 }; |
287 | 285 |
288 } // namespace media | 286 } // namespace media |
289 | 287 |
290 #endif // MEDIA_BLINK_INTERVAL_MAP_H_ | 288 #endif // MEDIA_BLINK_INTERVAL_MAP_H_ |
OLD | NEW |