| 199 | |
| 200 | template <class Val, class Metric, class MetricFunc> |
| 201 | void CoalescedKeyRangeMap<Val, Metric, MetricFunc>::insert(const KeyRangeRef& keys, const Val& value) { |
| 202 | ASSERT(keys.end <= mapEnd); |
| 203 | |
| 204 | if (keys.empty()) |
| 205 | return; |
| 206 | |
| 207 | auto begin = RangeMap<Key, Val, KeyRangeRef, Metric, MetricFunc>::map.lower_bound(keys.begin); |
| 208 | auto end = RangeMap<Key, Val, KeyRangeRef, Metric, MetricFunc>::map.lower_bound(keys.end); |
| 209 | bool insertEnd = false; |
| 210 | bool insertBegin = false; |
| 211 | Val endVal; |
| 212 | |
| 213 | if (keys.end != mapEnd) { |
| 214 | if (end->key != keys.end) { |
| 215 | auto before_end = end; |
| 216 | before_end.decrementNonEnd(); |
| 217 | if (value != before_end->value) { |
| 218 | insertEnd = true; |
| 219 | endVal = before_end->value; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | if (!insertEnd && end->value == value && end->key != mapEnd) { |
| 224 | ++end; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (keys.begin == allKeys.begin) { |
| 229 | insertBegin = true; |
| 230 | } else { |
| 231 | auto before_begin = begin; |
| 232 | before_begin.decrementNonEnd(); |
| 233 | if (before_begin->value != value) |
| 234 | insertBegin = true; |
| 235 | } |
| 236 | |
| 237 | RangeMap<Key, Val, KeyRangeRef, Metric, MetricFunc>::map.erase(begin, end); |
| 238 | if (insertEnd) { |
| 239 | MapPair<Key, Val> p(keys.end, endVal); |
| 240 | RangeMap<Key, Val, KeyRangeRef, Metric, MetricFunc>::map.insert( |
| 241 | p, true, RangeMap<Key, Val, KeyRangeRef, Metric, MetricFunc>::mf(p)); |
| 242 | } |
| 243 | if (insertBegin) { |
| 244 | MapPair<Key, Val> p(keys.begin, value); |
| 245 | RangeMap<Key, Val, KeyRangeRef, Metric, MetricFunc>::map.insert( |
| 246 | p, true, RangeMap<Key, Val, KeyRangeRef, Metric, MetricFunc>::mf(p)); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | template <class Val, class Metric, class MetricFunc> |
| 251 | void CoalescedKeyRangeMap<Val, Metric, MetricFunc>::insert(const KeyRef& key, const Val& value) { |