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

Side by Side Diff: webrtc/modules/audio_coding/neteq/audio_vector.h

Issue 2670643007: Make AudioVector::operator[] inline and modify index calculation (Closed)
Patch Set: Review comments Created 3 years, 10 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
« no previous file with comments | « no previous file | webrtc/modules/audio_coding/neteq/audio_vector.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 (c) 2012 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2012 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
11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_ 11 #ifndef WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_ 12 #define WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
13 13
14 #include <string.h> // Access to size_t. 14 #include <string.h> // Access to size_t.
15 #include <memory> 15 #include <memory>
16 16
17 #include "webrtc/base/checks.h"
17 #include "webrtc/base/constructormagic.h" 18 #include "webrtc/base/constructormagic.h"
18 #include "webrtc/typedefs.h" 19 #include "webrtc/typedefs.h"
19 20
20 namespace webrtc { 21 namespace webrtc {
21 22
22 class AudioVector { 23 class AudioVector {
23 public: 24 public:
24 // Creates an empty AudioVector. 25 // Creates an empty AudioVector.
25 AudioVector(); 26 AudioVector();
26 27
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // region. 104 // region.
104 virtual void CrossFade(const AudioVector& append_this, size_t fade_length); 105 virtual void CrossFade(const AudioVector& append_this, size_t fade_length);
105 106
106 // Returns the number of elements in this AudioVector. 107 // Returns the number of elements in this AudioVector.
107 virtual size_t Size() const; 108 virtual size_t Size() const;
108 109
109 // Returns true if this AudioVector is empty. 110 // Returns true if this AudioVector is empty.
110 virtual bool Empty() const; 111 virtual bool Empty() const;
111 112
112 // Accesses and modifies an element of AudioVector. 113 // Accesses and modifies an element of AudioVector.
113 const int16_t& operator[](size_t index) const; 114 inline const int16_t& operator[](size_t index) const {
114 int16_t& operator[](size_t index); 115 return array_[WrapIndex(index, begin_index_, capacity_)];
116 }
117
118 inline int16_t& operator[](size_t index) {
119 return array_[WrapIndex(index, begin_index_, capacity_)];
120 }
115 121
116 private: 122 private:
117 static const size_t kDefaultInitialSize = 10; 123 static const size_t kDefaultInitialSize = 10;
118 124
125 // This method is used by the [] operators to calculate an index within the
126 // capacity of the array, but without using the modulo operation (%).
127 static inline size_t WrapIndex(size_t index,
128 size_t begin_index,
129 size_t capacity) {
130 RTC_DCHECK_GE(begin_index + index, index); // Check for overflow.
131 const size_t ix = begin_index + index >= capacity
132 ? begin_index + index - capacity
133 : begin_index + index;
134 RTC_DCHECK_LT(ix, capacity);
135 return ix;
136 }
137
119 void Reserve(size_t n); 138 void Reserve(size_t n);
120 139
121 void InsertByPushBack(const int16_t* insert_this, size_t length, 140 void InsertByPushBack(const int16_t* insert_this, size_t length,
122 size_t position); 141 size_t position);
123 142
124 void InsertByPushFront(const int16_t* insert_this, size_t length, 143 void InsertByPushFront(const int16_t* insert_this, size_t length,
125 size_t position); 144 size_t position);
126 145
127 void InsertZerosByPushBack(size_t length, size_t position); 146 void InsertZerosByPushBack(size_t length, size_t position);
128 147
129 void InsertZerosByPushFront(size_t length, size_t position); 148 void InsertZerosByPushFront(size_t length, size_t position);
130 149
131 std::unique_ptr<int16_t[]> array_; 150 std::unique_ptr<int16_t[]> array_;
132 151
133 size_t capacity_; // Allocated number of samples in the array. 152 size_t capacity_; // Allocated number of samples in the array.
134 153
135 // The index of the first sample in |array_|, except when 154 // The index of the first sample in |array_|, except when
136 // |begin_index_ == end_index_|, which indicates an empty buffer. 155 // |begin_index_ == end_index_|, which indicates an empty buffer.
137 size_t begin_index_; 156 size_t begin_index_;
138 157
139 // The index of the sample after the last sample in |array_|. 158 // The index of the sample after the last sample in |array_|.
140 size_t end_index_; 159 size_t end_index_;
141 160
142 RTC_DISALLOW_COPY_AND_ASSIGN(AudioVector); 161 RTC_DISALLOW_COPY_AND_ASSIGN(AudioVector);
143 }; 162 };
144 163
145 } // namespace webrtc 164 } // namespace webrtc
146 #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_ 165 #endif // WEBRTC_MODULES_AUDIO_CODING_NETEQ_AUDIO_VECTOR_H_
OLDNEW
« no previous file with comments | « no previous file | webrtc/modules/audio_coding/neteq/audio_vector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698