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. |
127 std::pair<Interval<KeyType>, ValueType> operator*() const { | 129 std::pair<Interval<KeyType>, ValueType> operator*() const { |
128 return std::make_pair(interval(), value()); | 130 return std::make_pair(interval(), value()); |
129 } | 131 } |
130 | 132 |
131 // Go to the next interval. | 133 // Go to the next interval. |
132 // The beginning of the next interval always matches the end of the current | 134 // The beginning of the next interval always matches the end of the current |
133 // interval. (But should always have a different value.) | 135 // interval. (But should always have a different value.) |
134 // Not allowed if we're already at map_->end(). | 136 // Not allowed if we're already at map_->end(). |
135 void operator++() { | 137 void operator++() { |
136 DCHECK(iter_ != map_->end()); | 138 DCHECK(iter_ != map_->end()); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 // Defaults to ValueType(). | 175 // Defaults to ValueType(). |
174 ValueType operator[](const KeyType& k) const { | 176 ValueType operator[](const KeyType& k) const { |
175 typename MapType::const_iterator i = map_.upper_bound(k); | 177 typename MapType::const_iterator i = map_.upper_bound(k); |
176 DCHECK(i != map_.begin()); | 178 DCHECK(i != map_.begin()); |
177 --i; | 179 --i; |
178 return i->second; | 180 return i->second; |
179 } | 181 } |
180 | 182 |
181 // Increase [from..to) by |how_much|. | 183 // Increase [from..to) by |how_much|. |
182 void IncrementInterval(KeyType from, KeyType to, ValueType how_much) { | 184 void IncrementInterval(KeyType from, KeyType to, ValueType how_much) { |
183 if (to <= from || how_much == 0) | 185 DCHECK_GT(to, from); |
| 186 if (how_much == 0) |
184 return; | 187 return; |
185 typename MapType::iterator a = MakeEntry(from); | 188 typename MapType::iterator a = MakeEntry(from); |
186 typename MapType::iterator b = MakeEntry(to); | 189 typename MapType::iterator b = MakeEntry(to); |
187 for (typename MapType::iterator i = a; i != b; ++i) { | 190 for (typename MapType::iterator i = a; i != b; ++i) { |
188 i->second += how_much; | 191 i->second += how_much; |
189 } | 192 } |
190 RemoveDuplicates(a); | 193 RemoveDuplicates(a); |
191 // b may be invalid | 194 // b may be invalid |
192 RemoveDuplicates(map_.lower_bound(to)); | 195 RemoveDuplicates(map_.lower_bound(to)); |
193 } | 196 } |
194 | 197 |
195 // Set [from..to) to |how_much|. | 198 // Set [from..to) to |how_much|. |
196 void SetInterval(KeyType from, KeyType to, ValueType how_much) { | 199 void SetInterval(KeyType from, KeyType to, ValueType how_much) { |
197 if (to <= from) | 200 DCHECK_GT(to, from); |
198 return; | |
199 typename MapType::iterator a = MakeEntry(from); | 201 typename MapType::iterator a = MakeEntry(from); |
200 typename MapType::iterator b = MakeEntry(to); | 202 typename MapType::iterator b = MakeEntry(to); |
201 a->second = how_much; | 203 a->second = how_much; |
202 while (true) { | 204 while (true) { |
203 typename MapType::iterator c = a; | 205 typename MapType::iterator c = a; |
204 ++c; | 206 ++c; |
205 if (c == b) { | 207 if (c == b) { |
206 break; | 208 break; |
207 } else { | 209 } else { |
208 map_.erase(c); | 210 map_.erase(c); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 map_.erase(second); | 281 map_.erase(second); |
280 } | 282 } |
281 } | 283 } |
282 | 284 |
283 MapType map_; | 285 MapType map_; |
284 }; | 286 }; |
285 | 287 |
286 } // namespace media | 288 } // namespace media |
287 | 289 |
288 #endif // MEDIA_BLINK_INTERVAL_MAP_H_ | 290 #endif // MEDIA_BLINK_INTERVAL_MAP_H_ |
OLD | NEW |