我正在使用目标C在Swift中重写iOS的粒子过滤器库,这可以在Bitbucket上找到,我对Objective C的语法有疑问,我无法理解。
代码如下:
- (void)setRssi:(NSInteger)rssi {
_rssi = rssi;
// Ignore zeros in average, StdDev -- we clear the value before setting it to
// prevent old values from hanging around if there's no reading
if (rssi == 0) {
self.meters = 0;
return;
}
self.meters = [self metersFromRssi:rssi];
NSInteger* pidx = self.rssiBuffer;
*(pidx+self.bufferIndex++) = rssi;
if (self.bufferIndex >= RSSIBUFFERSIZE) {
self.bufferIndex %= RSSIBUFFERSIZE;
self.bufferFull = YES;
}
if (self.bufferFull) {
// Only calculate trailing mean and Std Dev when we have enough data
double accumulator = 0;
for (NSInteger i = 0; i < RSSIBUFFERSIZE; i++) {
accumulator += *(pidx+i);
}
self.meanRssi = accumulator / RSSIBUFFERSIZE;
self.meanMeters = [self metersFromRssi:self.meanRssi];
accumulator = 0;
for (NSInteger i = 0; i < RSSIBUFFERSIZE; i++) {
NSInteger difference = *(pidx+i) - self.meanRssi;
accumulator += difference*difference;
}
self.stdDeviationRssi = sqrt( accumulator / RSSIBUFFERSIZE);
self.meanMetersVariance = ABS(
[self metersFromRssi:self.meanRssi]
- [self metersFromRssi:self.meanRssi+self.stdDeviationRssi]
);
}
}
课程继续提供更多不重要的代码和功能,而我不理解的是这两行
NSInteger* pidx = self.rssiBuffer;
*(pidx+self.bufferIndex++) = rssi;
将变量pidx初始化为先前定义的缓冲区大小,然后在下一行中,该缓冲区和缓冲区加上1的大小等于RSSI变量,该变量作为函数中的参数传递。 p>
我认为*与参考有关,但我无法弄清楚这一行的目的。变量pidx仅用于此函数,用于计算拖尾均值和标准差。
答案 0 :(得分:1)
让我们解释一下这些代码:
min(num)
表示您正在获取缓冲区第一个值的指针。NSInteger* pidx = self.rssiBuffer;
表示您将索引*(pidx+self.bufferIndex++) = rssi;
的缓冲区值设置为0+self.bufferIndex
,然后将rssi
增加bufferIndex
。感谢@Jakub Vano指出它。在C ++中,它看起来像那样
1