You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
iter del_start = inserted_start != my_map.end() ? inserted_start : begin_intersect;
310
-
if (del_start->first == begin) {
310
+
if (del_start->first< begin || (del_start->first== begin && std::prev(del_start)->second != v)) {
311
311
del_start++;
312
312
}
313
313
314
314
iter del_end = inserted_end != my_map.end() ? inserted_end : end_intersect;
315
+
if (del_end != my_map.end() && del_end->first == end && std::next(del_end) != my_map.end() && del_end->second == v) {
316
+
del_end++;
317
+
}
315
318
316
-
if (del_start != my_map.end()) {
319
+
if (del_start != my_map.end() && del_start->first < del_end->first) {
317
320
my_map.erase(del_start, del_end);
318
321
}
319
322
}
320
323
321
324
// iterator which traverses elements in sorted order (smallest to largest)
322
325
// O(1)
323
-
constexprinlineiter &begin() {
326
+
constexprinlineauto &begin() {
324
327
return my_map.begin();
325
328
}
326
329
327
330
// end of elements
328
331
// O(1)
329
-
constexprinlineiter &end() {
332
+
constexprinlineauto &end() {
330
333
return my_map.end();
331
334
}
332
335
@@ -338,7 +341,12 @@ class IntervalMap
338
341
339
342
// get value at key `k`
340
343
// O(log N)
341
-
const V operator[](K const& k) {
344
+
constinline V& operator[](K const& k) const {
345
+
return (--my_map.upper_bound(k))->second;
346
+
}
347
+
348
+
// don't return reference because we don't want to allow map[whatever] = value as it would edit the next-earliest value instead of inserting a new element. (TODO: write better.)
349
+
inline V operator[](K const& k) {
342
350
return (--my_map.upper_bound(k))->second;
343
351
}
344
352
@@ -347,6 +355,18 @@ class IntervalMap
347
355
my_map.clear();
348
356
my_map.insert(my_map.end(), { std::numeric_limits<K>::lowest(), v });
0 commit comments