iosobjective-cswiftparticle-filter

Objective C " * " syntax and usage


I am re-writing the particle filter library of iOS in Swift from Objective C which is available on Bitbucket and I have a question regarding a syntax of Objective C which I cannot understand.

The code goes as follows:

- (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]
                                  );
}    
  }

The class continues with more code and functions which are not important and what I do not understand are these two lines

NSInteger* pidx = self.rssiBuffer;
*(pidx+self.bufferIndex++) = rssi;

Variable pidx is initialized to the size of a buffer which was previously defined and then in the next line the size of that buffer and buffer plus one is equal to the RSSI variable which is passed as a parameter in the function.

I assume that * has something to do with reference but I just can't figure out the purpose of this line. Variable pidx is used only in this function for calculating trailing mean and standard deviation.


Solution

  • Let explain those code:

    1. NSInteger* pidx = self.rssiBuffer; means that you are getting pointer of the first value of the buffer.
    2. *(pidx+self.bufferIndex++) = rssi; means that you are setting the value of the buffer at index 0+self.bufferIndex to rssiand then increase bufferIndex by 1. Thanks to @Jakub Vano point it out.

    In C++, it will look like that

    int self.rssiBuffer[1000]; // I assume we have buffer like that
    self.rssiBuffer[self.bufferIndex++] = rssi