Index: media/blink/rangemap.h |
diff --git a/media/blink/rangemap.h b/media/blink/rangemap.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7f2fa2793cd1a53f72128303ec3c6807fb5f3eb8 |
--- /dev/null |
+++ b/media/blink/rangemap.h |
@@ -0,0 +1,87 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef MEDIA_BLINK_RANGEMAP_H_ |
+#define MEDIA_BLINK_RANGEMAP_H_ |
+ |
+#include <map> |
+ |
+#include "base/logging.h" |
+ |
+namespace media { |
+ |
+template<typename KeyType, typename ValueType> |
+class RangeMap { |
+ public: |
+ typedef std::map<KeyType, ValueType> MapType; |
+ |
+ ValueType operator[](KeyType k) const { |
+ typename MapType::const_iterator i = map_.upper_bound(k); |
+ if (i == map_.begin()) { |
+ return 0; |
+ } else { |
+ --i; |
+ return i->second; |
+ } |
+ } |
+ |
+ // Increase [from..to) by howmuch. |
+ void IncrementRange(KeyType from, KeyType to, ValueType howmuch) { |
+ DCHECK_GE(to, from); |
+ if (from == to || howmuch == 0) return; |
+ typename MapType::iterator a = MakeEntry(from); |
+ typename MapType::iterator b = MakeEntry(to); |
+ for (typename MapType::iterator i = a; i != b; ++i) { |
+ i->second += howmuch; |
+ } |
+ RemoveDuplicates(a); |
+ // b may be invalid |
+ RemoveDuplicates(map_.lower_bound(to)); |
+ } |
+ |
+ const MapType& map() { return map_; } |
+ |
+ private: |
+ typename MapType::iterator MakeEntry(KeyType k) { |
+ typename MapType::value_type tmp(k, 0); |
+ std::pair<typename MapType::iterator, bool> insert_result; |
+ insert_result = map_.insert(tmp); |
+ if (insert_result.second) { |
+ if (insert_result.first != map_.begin()) { |
+ typename MapType::iterator i = insert_result.first; |
+ --i; |
+ insert_result.first->second = i->second; |
+ } |
+ } |
+ return insert_result.first; |
+ } |
+ |
+ void RemoveDuplicates(typename MapType::iterator i) { |
+ if (i == map_.end()) return; |
+ if (i == map_.begin() && i->second == 0) { |
+ typename MapType::iterator j = i; |
+ ++i; |
+ map_.erase(j); |
+ if (i == map_.end()) return; |
+ } |
+ if (i != map_.begin()) { |
+ typename MapType::iterator j = i; |
+ --i; |
+ if (i ->second == j->second) { |
+ map_.erase(j); |
+ } |
+ } |
+ typename MapType::iterator j = i; |
+ ++j; |
+ if (j != map_.end() && i ->second == j->second) { |
+ map_.erase(j); |
+ } |
+ } |
+ |
+ MapType map_; |
+}; |
+ |
+} // namespace media |
+ |
+#endif // MEDIA_BLINK_RANGEMAP_H_ |