Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: media/blink/rangemap.h

Issue 1165903002: Multi reader/writer cache/buffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: more tests Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef MEDIA_BLINK_RANGEMAP_H_
6 #define MEDIA_BLINK_RANGEMAP_H_
7
8 #include <limits>
9 #include <map>
10
11 #include "base/logging.h"
12
13 namespace media {
14
15 template<typename T>
16 struct Range {
17 public:
18 Range(const T& begin_, const T& end_) : begin(begin_), end(end_) {}
19 T begin;
20 T end;
21 };
22
23 template<typename KeyType, typename ValueType>
24 class RangeMapConstIterator {
25 public:
26 typedef std::map<KeyType, ValueType> MapType;
27 RangeMapConstIterator() {}
28 RangeMapConstIterator(const MapType* map,
29 typename MapType::const_iterator first,
30 typename MapType::const_iterator second) :
31 map_(map),
32 first_(first),
33 second_(second) {
34 }
35 bool operator==(const RangeMapConstIterator& other) {
36 return first_ == other.first_ && second_ == other.second_;
37 }
38 bool operator!=(const RangeMapConstIterator& other) {
39 return first_ != other.first_ || second_ != other.second_;
40 }
41 KeyType range_begin() const {
42 if (first_ == map_->end()) {
43 return std::numeric_limits<KeyType>::min();
44 } else {
45 return first_->first;
46 }
47 }
48 KeyType range_end() const {
49 if (second_ == map_->end()) {
50 return std::numeric_limits<KeyType>::max();
51 } else {
52 return second_->first;
53 }
54 }
55 Range<KeyType> range() const {
56 return Range<KeyType>(range_begin(), range_end());
57 }
58
59 ValueType value() const {
60 if (first_ == map_->end())
61 return 0;
62 return first_->second;
63 }
64 void operator++() {
65 first_ = second_;
66 second_++;
67 }
68 void operator--() {
69 second_ = first_;
70 if (first_ == map_->begin()) {
71 first_ = map_->end();
72 } else {
73 first_--;
74 }
75 }
76 private:
77 typename MapType::const_iterator first_;
78 typename MapType::const_iterator second_;
79 const MapType* map_;
80 };
81
82 template<typename KeyType, typename ValueType>
83 class RangeMap {
84 public:
85 typedef std::map<KeyType, ValueType> MapType;
86 typedef RangeMapConstIterator<KeyType, ValueType> const_iterator;
87
88 ValueType operator[](KeyType k) const {
89 typename MapType::const_iterator i = map_.upper_bound(k);
90 if (i == map_.begin()) {
91 return 0;
92 } else {
93 --i;
94 return i->second;
95 }
96 }
97
98 // Increase [from..to) by howmuch.
99 void IncrementRange(KeyType from, KeyType to, ValueType howmuch) {
100 DCHECK_GE(to, from);
101 if (from == to || howmuch == 0)
102 return;
103 typename MapType::iterator a = MakeEntry(from);
104 typename MapType::iterator b = MakeEntry(to);
105 for (typename MapType::iterator i = a; i != b; ++i) {
106 i->second += howmuch;
107 }
108 RemoveDuplicates(a);
109 // b may be invalid
110 RemoveDuplicates(map_.lower_bound(to));
111 }
112
113 // Returns an iterator to the first range.
114 // Note, there is always at least one range.
115 const_iterator first_range() const {
116 return const_iterator(&map(), map_.end(), map_.begin());
117 }
118
119 // Returns an iterator to the last range.
120 // Note, there is always at least one range.
121 const_iterator last_range() const {
122 if (map_.empty()) {
123 return const_iterator(&map(), map_.end(), map_.end());
124 }
125 typename MapType::const_iterator i = map_.end();
126 --i;
127 return const_iterator(&map(), i, map_.end());
128 }
129
130 // Returns an iterator to the range containing |k|.
131 // Always returns a valid iterator.
132 const_iterator find(KeyType k) const {
133 if (map_.empty()) {
134 return const_iterator(&map(), map_.end(), map_.end());
135 }
136 typename MapType::const_iterator i = map_.upper_bound(k);
137 typename MapType::const_iterator j = i;
138 if (j == map_.begin()) {
139 j = map_.end();
140 } else {
141 --j;
142 }
143 return const_iterator(&map(), j, i);
144 }
145
146 const MapType& map() const { return map_; }
147
148 private:
149 typename MapType::iterator MakeEntry(KeyType k) {
150 typename MapType::value_type tmp(k, 0);
151 std::pair<typename MapType::iterator, bool> insert_result;
152 insert_result = map_.insert(tmp);
153 if (insert_result.second) {
154 if (insert_result.first != map_.begin()) {
155 typename MapType::iterator i = insert_result.first;
156 --i;
157 insert_result.first->second = i->second;
158 }
159 }
160 return insert_result.first;
161 }
162
163 void RemoveDuplicates(typename MapType::iterator i) {
164 if (i == map_.end())
165 return;
166 if (i == map_.begin() && i->second == 0) {
167 typename MapType::iterator j = i;
168 ++i;
169 map_.erase(j);
170 if (i == map_.end())
171 return;
172 }
173 if (i != map_.begin()) {
174 typename MapType::iterator j = i;
175 --i;
176 if (i ->second == j->second) {
177 map_.erase(j);
178 }
179 }
180 typename MapType::iterator j = i;
181 ++j;
182 if (j != map_.end() && i ->second == j->second) {
183 map_.erase(j);
184 }
185 }
186
187 MapType map_;
188 };
189
190
191 } // namespace media
192
193 #endif // MEDIA_BLINK_RANGEMAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698