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

Side by Side Diff: webrtc/base/array_view.h

Issue 2502383004: Introduce ArrayView::subview function (Closed)
Patch Set: nullptr -> ArrayView() Created 4 years, 1 month 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
« no previous file with comments | « no previous file | webrtc/base/array_view_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2015 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 T& operator[](size_t idx) const { 113 T& operator[](size_t idx) const {
114 RTC_DCHECK_LT(idx, size_); 114 RTC_DCHECK_LT(idx, size_);
115 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant. 115 RTC_DCHECK(data_); // Follows from size_ > idx and the class invariant.
116 return data_[idx]; 116 return data_[idx];
117 } 117 }
118 T* begin() const { return data_; } 118 T* begin() const { return data_; }
119 T* end() const { return data_ + size_; } 119 T* end() const { return data_ + size_; }
120 const T* cbegin() const { return data_; } 120 const T* cbegin() const { return data_; }
121 const T* cend() const { return data_ + size_; } 121 const T* cend() const { return data_ + size_; }
122 122
123 ArrayView subview(size_t offset, size_t size) const {
124 if (offset >= size_)
125 return ArrayView();
126 return ArrayView(data_ + offset, std::min(size, size_ - offset));
127 }
128 ArrayView subview(size_t offset) const { return subview(offset, size_); }
129
123 // Comparing two ArrayViews compares their (pointer,size) pairs; it does 130 // Comparing two ArrayViews compares their (pointer,size) pairs; it does
124 // *not* dereference the pointers. 131 // *not* dereference the pointers.
125 friend bool operator==(const ArrayView& a, const ArrayView& b) { 132 friend bool operator==(const ArrayView& a, const ArrayView& b) {
126 return a.data_ == b.data_ && a.size_ == b.size_; 133 return a.data_ == b.data_ && a.size_ == b.size_;
127 } 134 }
128 friend bool operator!=(const ArrayView& a, const ArrayView& b) { 135 friend bool operator!=(const ArrayView& a, const ArrayView& b) {
129 return !(a == b); 136 return !(a == b);
130 } 137 }
131 138
132 private: 139 private:
133 // Invariant: !data_ iff size_ == 0. 140 // Invariant: !data_ iff size_ == 0.
134 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); } 141 void CheckInvariant() const { RTC_DCHECK_EQ(!data_, size_ == 0); }
135 T* data_; 142 T* data_;
136 size_t size_; 143 size_t size_;
137 }; 144 };
138 145
139 template <typename T> 146 template <typename T>
140 inline ArrayView<T> MakeArrayView(T* data, size_t size) { 147 inline ArrayView<T> MakeArrayView(T* data, size_t size) {
141 return ArrayView<T>(data, size); 148 return ArrayView<T>(data, size);
142 } 149 }
143 150
144 } // namespace rtc 151 } // namespace rtc
145 152
146 #endif // WEBRTC_BASE_ARRAY_VIEW_H_ 153 #endif // WEBRTC_BASE_ARRAY_VIEW_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/base/array_view_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698