- 從數(shù)據(jù)中均勻地提取一組數(shù)據(jù),有一個比較經(jīng)典的算法
- 以前在做數(shù)據(jù)抽取時非常地熟練
- 今天再次使用時,居然忘了怎樣實現(xiàn)了,主要是理不清求余和求商間的使用關(guān)系了
- 簡單推導(dǎo)一下
- 總共s個數(shù),均勻地分成n組,每組有g(shù)個,余數(shù)為 m
- 即:s = g*n + m
- 那么每輸出一組 就 累計一次 n,則 總共累計數(shù)值 為 n * m
- 所以,可以指定如下的規(guī)則:
- 每輸出一組后,累計一次m
- 當(dāng)m大于或等于n是,回滾一次n,而且當(dāng)前組數(shù)量為g+1
- 由于是從0開始累計的,所以第一組的數(shù)量一定為 g
- 這樣總共可以回滾m次
- m就是余數(shù)
//! now for compress
int rad, rem;
rad = inLen / maxOutLen;
rem = inLen % maxOutLen;
//! now for compress
int acc = 0;
int outIndex = 0;
for ( int i = 0; i < maxOutLen; i++, outIndex += rad )
{
pOut[ i ] = pIn[ outIndex * skipI ];
acc += rem;
if ( acc >= maxOutLen )
{
acc -= maxOutLen;
outIndex++;
}
}