Make AudioVector::operator[] inline and modify the index calculation to avoid the modulo operation.
BUG=webrtc:7159 Review-Url: https://codereview.webrtc.org/2670643007 Cr-Commit-Position: refs/heads/master@{#16627}
This commit is contained in:
parent
a7b682aa18
commit
280eb224e2
@ -284,14 +284,6 @@ bool AudioVector::Empty() const {
|
||||
return begin_index_ == end_index_;
|
||||
}
|
||||
|
||||
const int16_t& AudioVector::operator[](size_t index) const {
|
||||
return array_[(begin_index_ + index) % capacity_];
|
||||
}
|
||||
|
||||
int16_t& AudioVector::operator[](size_t index) {
|
||||
return array_[(begin_index_ + index) % capacity_];
|
||||
}
|
||||
|
||||
void AudioVector::Reserve(size_t n) {
|
||||
if (capacity_ > n)
|
||||
return;
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include <string.h> // Access to size_t.
|
||||
#include <memory>
|
||||
|
||||
#include "webrtc/base/checks.h"
|
||||
#include "webrtc/base/constructormagic.h"
|
||||
#include "webrtc/typedefs.h"
|
||||
|
||||
@ -110,12 +111,30 @@ class AudioVector {
|
||||
virtual bool Empty() const;
|
||||
|
||||
// Accesses and modifies an element of AudioVector.
|
||||
const int16_t& operator[](size_t index) const;
|
||||
int16_t& operator[](size_t index);
|
||||
inline const int16_t& operator[](size_t index) const {
|
||||
return array_[WrapIndex(index, begin_index_, capacity_)];
|
||||
}
|
||||
|
||||
inline int16_t& operator[](size_t index) {
|
||||
return array_[WrapIndex(index, begin_index_, capacity_)];
|
||||
}
|
||||
|
||||
private:
|
||||
static const size_t kDefaultInitialSize = 10;
|
||||
|
||||
// This method is used by the [] operators to calculate an index within the
|
||||
// capacity of the array, but without using the modulo operation (%).
|
||||
static inline size_t WrapIndex(size_t index,
|
||||
size_t begin_index,
|
||||
size_t capacity) {
|
||||
RTC_DCHECK_GE(begin_index + index, index); // Check for overflow.
|
||||
const size_t ix = begin_index + index >= capacity
|
||||
? begin_index + index - capacity
|
||||
: begin_index + index;
|
||||
RTC_DCHECK_LT(ix, capacity);
|
||||
return ix;
|
||||
}
|
||||
|
||||
void Reserve(size_t n);
|
||||
|
||||
void InsertByPushBack(const int16_t* insert_this, size_t length,
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user