Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 1 | /* |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 2 | * Copyright 2008, 2009, 2014 Free Software Foundation, Inc. |
| 3 | * Copyright 2014 Range Networks, Inc. |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 4 | * |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 5 | * |
| 6 | * This software is distributed under the terms of the GNU Affero Public License. |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 7 | * See the COPYING file in the main directory for details. |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 8 | * |
| 9 | * This use of this software may be subject to additional restrictions. |
| 10 | * See the LEGAL file in the main directory for details. |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 11 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 12 | This program is free software: you can redistribute it and/or modify |
| 13 | it under the terms of the GNU Affero General Public License as published by |
| 14 | the Free Software Foundation, either version 3 of the License, or |
| 15 | (at your option) any later version. |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 16 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 17 | This program is distributed in the hope that it will be useful, |
| 18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | GNU Affero General Public License for more details. |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 21 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 22 | You should have received a copy of the GNU Affero General Public License |
| 23 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 24 | |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | #include "BitVector.h" |
| 31 | #include <iostream> |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 32 | #include <stdio.h> |
| 33 | #include <sstream> |
| 34 | #include <string.h> |
| 35 | //#include <Logger.h> |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 36 | |
| 37 | using namespace std; |
| 38 | |
| 39 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 40 | |
| 41 | BitVector::BitVector(const char *valString) |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 42 | { |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 43 | // 1-30-2013 pat: I dont know what this was intended to do, but it did not create a normalized BitVector, |
| 44 | // and it could even fail if the accum overlows 8 bits. |
| 45 | //uint32_t accum = 0; |
| 46 | //for (size_t i=0; i<size(); i++) { |
| 47 | // accum <<= 1; |
| 48 | // if (valString[i]=='1') accum |= 0x01; |
| 49 | // mStart[i] = accum; |
| 50 | //} |
| 51 | vInit(strlen(valString)); |
| 52 | char *rp = begin(); |
| 53 | for (const char *cp = valString; *cp; cp++, rp++) { |
| 54 | *rp = (*cp == '1'); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 59 | uint64_t BitVector::peekField(size_t readIndex, unsigned length) const |
| 60 | { |
| 61 | uint64_t accum = 0; |
| 62 | char *dp = mStart + readIndex; |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 63 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 64 | for (unsigned i=0; i<length; i++) { |
| 65 | accum = (accum<<1) | ((*dp++) & 0x01); |
| 66 | } |
| 67 | return accum; |
| 68 | } |
| 69 | |
| 70 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 71 | |
| 72 | |
| 73 | uint64_t BitVector::peekFieldReversed(size_t readIndex, unsigned length) const |
| 74 | { |
| 75 | uint64_t accum = 0; |
| 76 | char *dp = mStart + readIndex + length - 1; |
| 77 | assert(dp<mEnd); |
| 78 | for (int i=(length-1); i>=0; i--) { |
| 79 | accum = (accum<<1) | ((*dp--) & 0x01); |
| 80 | } |
| 81 | return accum; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 87 | uint64_t BitVector::readField(size_t& readIndex, unsigned length) const |
| 88 | { |
| 89 | const uint64_t retVal = peekField(readIndex,length); |
| 90 | readIndex += length; |
| 91 | return retVal; |
| 92 | } |
| 93 | |
| 94 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 95 | uint64_t BitVector::readFieldReversed(size_t& readIndex, unsigned length) const |
| 96 | { |
| 97 | |
| 98 | const uint64_t retVal = peekFieldReversed(readIndex,length); |
| 99 | readIndex += length; |
| 100 | return retVal; |
| 101 | |
| 102 | } |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 107 | void BitVector::fillField(size_t writeIndex, uint64_t value, unsigned length) |
| 108 | { |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 109 | if (length != 0) { |
| 110 | char *dpBase = mStart + writeIndex; |
| 111 | char *dp = dpBase + length - 1; |
| 112 | assert(dp < mEnd); |
| 113 | while (dp>=dpBase) { |
| 114 | *dp-- = value & 0x01; |
| 115 | value >>= 1; |
| 116 | } |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 120 | |
| 121 | void BitVector::fillFieldReversed(size_t writeIndex, uint64_t value, unsigned length) |
| 122 | { |
| 123 | if (length != 0) { |
| 124 | char *dp = mStart + writeIndex; |
| 125 | char *dpEnd = dp + length - 1; |
| 126 | assert(dpEnd < mEnd); |
| 127 | while (dp<=dpEnd) { |
| 128 | *dp++ = value & 0x01; |
| 129 | value >>= 1; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 137 | void BitVector::writeField(size_t& writeIndex, uint64_t value, unsigned length) |
| 138 | { |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 139 | if (length != 0) { |
| 140 | fillField(writeIndex,value,length); |
| 141 | writeIndex += length; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | |
| 146 | void BitVector::writeFieldReversed(size_t& writeIndex, uint64_t value, unsigned length) |
| 147 | { |
| 148 | if (length != 0) { |
| 149 | fillFieldReversed(writeIndex,value,length); |
| 150 | writeIndex += length; |
| 151 | } |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
| 155 | void BitVector::invert() |
| 156 | { |
| 157 | for (size_t i=0; i<size(); i++) { |
| 158 | mStart[i] = ~mStart[i]; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | void BitVector::reverse8() |
| 166 | { |
| 167 | assert(size()>=8); |
| 168 | |
| 169 | char tmp0 = mStart[0]; |
| 170 | mStart[0] = mStart[7]; |
| 171 | mStart[7] = tmp0; |
| 172 | |
| 173 | char tmp1 = mStart[1]; |
| 174 | mStart[1] = mStart[6]; |
| 175 | mStart[6] = tmp1; |
| 176 | |
| 177 | char tmp2 = mStart[2]; |
| 178 | mStart[2] = mStart[5]; |
| 179 | mStart[5] = tmp2; |
| 180 | |
| 181 | char tmp3 = mStart[3]; |
| 182 | mStart[3] = mStart[4]; |
| 183 | mStart[4] = tmp3; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | |
| 188 | void BitVector::LSB8MSB() |
| 189 | { |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 190 | if (size()<8) return; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 191 | size_t size8 = 8*(size()/8); |
| 192 | size_t iTop = size8 - 8; |
| 193 | for (size_t i=0; i<=iTop; i+=8) segment(i,8).reverse8(); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | uint64_t BitVector::syndrome(Generator& gen) const |
| 199 | { |
| 200 | gen.clear(); |
| 201 | const char *dp = mStart; |
| 202 | while (dp<mEnd) gen.syndromeShift(*dp++); |
| 203 | return gen.state(); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | uint64_t BitVector::parity(Generator& gen) const |
| 208 | { |
| 209 | gen.clear(); |
| 210 | const char *dp = mStart; |
| 211 | while (dp<mEnd) gen.encoderShift(*dp++); |
| 212 | return gen.state(); |
| 213 | } |
| 214 | |
| 215 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 216 | unsigned BitVector::sum() const |
| 217 | { |
| 218 | unsigned sum = 0; |
| 219 | for (size_t i=0; i<size(); i++) sum += mStart[i] & 0x01; |
| 220 | return sum; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | void BitVector::map(const unsigned *map, size_t mapSize, BitVector& dest) const |
| 227 | { |
| 228 | for (unsigned i=0; i<mapSize; i++) { |
| 229 | dest.mStart[i] = mStart[map[i]]; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | void BitVector::unmap(const unsigned *map, size_t mapSize, BitVector& dest) const |
| 237 | { |
| 238 | for (unsigned i=0; i<mapSize; i++) { |
| 239 | dest.mStart[map[i]] = mStart[i]; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 249 | ostream& operator<<(ostream& os, const BitVector& hv) |
| 250 | { |
| 251 | for (size_t i=0; i<hv.size(); i++) { |
| 252 | if (hv.bit(i)) os << '1'; |
| 253 | else os << '0'; |
| 254 | } |
| 255 | return os; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 261 | uint64_t Parity::syndrome(const BitVector& receivedCodeword) |
| 262 | { |
| 263 | return receivedCodeword.syndrome(*this); |
| 264 | } |
| 265 | |
| 266 | |
| 267 | void Parity::writeParityWord(const BitVector& data, BitVector& parityTarget, bool invert) |
| 268 | { |
| 269 | uint64_t pWord = data.parity(*this); |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 270 | if (invert) pWord = ~pWord; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 271 | parityTarget.fillField(0,pWord,size()); |
| 272 | } |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | SoftVector::SoftVector(const BitVector& source) |
| 283 | { |
| 284 | resize(source.size()); |
| 285 | for (size_t i=0; i<size(); i++) { |
| 286 | if (source.bit(i)) mStart[i]=1.0F; |
| 287 | else mStart[i]=0.0F; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | |
| 292 | BitVector SoftVector::sliced() const |
| 293 | { |
| 294 | size_t sz = size(); |
| 295 | BitVector newSig(sz); |
| 296 | for (size_t i=0; i<sz; i++) { |
| 297 | if (mStart[i]>0.5F) newSig[i]=1; |
| 298 | else newSig[i] = 0; |
| 299 | } |
| 300 | return newSig; |
| 301 | } |
| 302 | |
| 303 | |
| 304 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 305 | // (pat) Added 6-22-2012 |
| 306 | float SoftVector::getEnergy(float *plow) const |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 307 | { |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 308 | const SoftVector &vec = *this; |
| 309 | int len = vec.size(); |
| 310 | float avg = 0; float low = 1; |
| 311 | for (int i = 0; i < len; i++) { |
| 312 | float bit = vec[i]; |
| 313 | float energy = 2*((bit < 0.5) ? (0.5-bit) : (bit-0.5)); |
| 314 | if (energy < low) low = energy; |
| 315 | avg += energy/len; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 316 | } |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 317 | if (plow) { *plow = low; } |
| 318 | return avg; |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 319 | } |
| 320 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 321 | // (pat) Added 1-2014. Compute SNR of a soft vector. Very similar to above. |
| 322 | // Since we dont really know what the expected signal values are, we will assume that the signal is 0 or 1 |
| 323 | // and return the SNR on that basis. |
| 324 | // SNR is power(signal) / power(noise) where power can be calculated as (RMS(signal) / RMS(noise))**2 of the values. |
| 325 | // Since RMS is square-rooted, ie RMS = sqrt(1/n * (x1**2 + x2**2 ...)), we just add up the squares. |
| 326 | // To compute RMS of the signal we will remove any constant offset, so the signal values are either 0.5 or -0.5, |
| 327 | // so the RMS of the signal is just 0.5**2 * len; all we need to compute is the noise component. |
| 328 | float SoftVector::getSNR() const |
| 329 | { |
| 330 | float sumSquaresNoise = 0; |
| 331 | const SoftVector &vec = *this; |
| 332 | int len = vec.size(); |
| 333 | if (len == 0) { return 0.0; } |
| 334 | for (int i = 0; i < len; i++) { |
| 335 | float bit = vec[i]; |
| 336 | if (bit < 0.5) { |
| 337 | // Assume signal is 0. |
| 338 | sumSquaresNoise += (bit - 0.0) * (bit - 0.0); |
| 339 | } else { |
| 340 | // Assume signal is 1. |
| 341 | sumSquaresNoise += (bit - 1.0) * (bit - 1.0); |
| 342 | } |
| 343 | } |
| 344 | float sumSquaresSignal = 0.5 * 0.5 * len; |
| 345 | // I really want log10 of this to convert to dB, but log is expensive, and Harvind seems to like absolute SNR. |
| 346 | // Clamp max to 999; it shouldnt get up there but be sure. This also avoids divide by zero. |
| 347 | if (sumSquaresNoise * 1000 < sumSquaresSignal) return 999; |
| 348 | return sumSquaresSignal / sumSquaresNoise; |
| 349 | } |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 350 | |
| 351 | |
| 352 | |
| 353 | ostream& operator<<(ostream& os, const SoftVector& sv) |
| 354 | { |
| 355 | for (size_t i=0; i<sv.size(); i++) { |
| 356 | if (sv[i]<0.25) os << "0"; |
| 357 | else if (sv[i]>0.75) os << "1"; |
| 358 | else os << "-"; |
| 359 | } |
| 360 | return os; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | |
| 365 | void BitVector::pack(unsigned char* targ) const |
| 366 | { |
| 367 | // Assumes MSB-first packing. |
| 368 | unsigned bytes = size()/8; |
| 369 | for (unsigned i=0; i<bytes; i++) { |
| 370 | targ[i] = peekField(i*8,8); |
| 371 | } |
| 372 | unsigned whole = bytes*8; |
| 373 | unsigned rem = size() - whole; |
| 374 | if (rem==0) return; |
| 375 | targ[bytes] = peekField(whole,rem) << (8-rem); |
| 376 | } |
| 377 | |
Roman Khassraf | d38206c | 2015-06-07 16:26:29 +0200 | [diff] [blame] | 378 | void BitVector::pack2(unsigned char* targ) const |
| 379 | { |
| 380 | unsigned int i; |
| 381 | unsigned char curbyte = 0; |
| 382 | |
| 383 | for (i = 0; i < size(); i++) |
| 384 | { |
| 385 | uint8_t bitnum = 7 - (i % 8); |
| 386 | curbyte |= ((char)bit(i) << bitnum); |
| 387 | if(i % 8 == 7){ |
| 388 | *targ++ = curbyte; |
| 389 | curbyte = 0; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | // Assumes MSB-first packing. |
| 394 | // unsigned bytes = size()/8; |
| 395 | // for (unsigned i=0; i<bytes; i++) { |
| 396 | // targ[i] = peekField(i*8,8); |
| 397 | // } |
| 398 | // unsigned whole = bytes*8; |
| 399 | // unsigned rem = size() - whole; |
| 400 | // if (rem==0) return; |
| 401 | // targ[bytes] = peekField(whole,rem) << (8-rem); |
| 402 | } |
| 403 | |
| 404 | |
| 405 | |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 406 | string BitVector::packToString() const |
| 407 | { |
| 408 | string result; |
| 409 | result.reserve((size()+7)/8); |
| 410 | // Tempting to call this->pack(result.c_str()) but technically c_str() is read-only. |
| 411 | unsigned bytes = size()/8; |
| 412 | for (unsigned i=0; i<bytes; i++) { |
| 413 | result.push_back(peekField(i*8,8)); |
| 414 | } |
| 415 | unsigned whole = bytes*8; |
| 416 | unsigned rem = size() - whole; |
| 417 | if (rem==0) return result; |
| 418 | result.push_back(peekField(whole,rem) << (8-rem)); |
| 419 | return result; |
| 420 | } |
| 421 | |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 422 | |
| 423 | void BitVector::unpack(const unsigned char* src) |
| 424 | { |
| 425 | // Assumes MSB-first packing. |
| 426 | unsigned bytes = size()/8; |
| 427 | for (unsigned i=0; i<bytes; i++) { |
| 428 | fillField(i*8,src[i],8); |
| 429 | } |
| 430 | unsigned whole = bytes*8; |
| 431 | unsigned rem = size() - whole; |
| 432 | if (rem==0) return; |
Roman Khassraf | d71e658 | 2015-06-02 08:49:12 +0200 | [diff] [blame] | 433 | fillField(whole,src[bytes] >> (8-rem),rem); |
| 434 | } |
| 435 | |
| 436 | void BitVector::hex(ostream& os) const |
| 437 | { |
| 438 | os << std::hex; |
| 439 | unsigned digits = size()/4; |
| 440 | size_t wp=0; |
| 441 | for (unsigned i=0; i<digits; i++) { |
| 442 | os << readField(wp,4); |
| 443 | } |
| 444 | os << std::dec; |
| 445 | } |
| 446 | |
| 447 | std::string BitVector::hexstr() const |
| 448 | { |
| 449 | std::ostringstream ss; |
| 450 | hex(ss); |
| 451 | return ss.str(); |
| 452 | } |
| 453 | |
| 454 | |
| 455 | bool BitVector::unhex(const char* src) |
| 456 | { |
| 457 | // Assumes MSB-first packing. |
| 458 | unsigned int val; |
| 459 | unsigned digits = size()/4; |
| 460 | for (unsigned i=0; i<digits; i++) { |
| 461 | if (sscanf(src+i, "%1x", &val) < 1) { |
| 462 | return false; |
| 463 | } |
| 464 | fillField(i*4,val,4); |
| 465 | } |
| 466 | unsigned whole = digits*4; |
| 467 | unsigned rem = size() - whole; |
| 468 | if (rem>0) { |
| 469 | if (sscanf(src+digits, "%1x", &val) < 1) { |
| 470 | return false; |
| 471 | } |
| 472 | fillField(whole,val,rem); |
| 473 | } |
| 474 | return true; |
| 475 | } |
| 476 | |
| 477 | bool BitVector::operator==(const BitVector &other) const |
| 478 | { |
| 479 | unsigned l = size(); |
| 480 | return l == other.size() && 0==memcmp(begin(),other.begin(),l); |
| 481 | } |
| 482 | |
| 483 | void BitVector::copyPunctured(BitVector &dst, const unsigned *puncture, const size_t plth) |
| 484 | { |
| 485 | assert(size() - plth == dst.size()); |
| 486 | char *srcp = mStart; |
| 487 | char *dstp = dst.mStart; |
| 488 | const unsigned *pend = puncture + plth; |
| 489 | while (srcp < mEnd) { |
| 490 | if (puncture < pend) { |
| 491 | int n = (*puncture++) - (srcp - mStart); |
| 492 | assert(n >= 0); |
| 493 | for (int i = 0; i < n; i++) { |
| 494 | assert(srcp < mEnd && dstp < dst.mEnd); |
| 495 | *dstp++ = *srcp++; |
| 496 | } |
| 497 | srcp++; |
| 498 | } else { |
| 499 | while (srcp < mEnd) { |
| 500 | assert(dstp < dst.mEnd); |
| 501 | *dstp++ = *srcp++; |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | assert(dstp == dst.mEnd && puncture == pend); |
| 506 | } |
| 507 | |
| 508 | void SoftVector::copyUnPunctured(SoftVector &dst, const unsigned *puncture, const size_t plth) |
| 509 | { |
| 510 | assert(size() + plth == dst.size()); |
| 511 | float *srcp = mStart; |
| 512 | float *dstp = dst.mStart; |
| 513 | const unsigned *pend = puncture + plth; |
| 514 | while (dstp < dst.mEnd) { |
| 515 | if (puncture < pend) { |
| 516 | int n = (*puncture++) - (dstp - dst.mStart); |
| 517 | assert(n >= 0); |
| 518 | for (int i = 0; i < n; i++) { |
| 519 | assert(srcp < mEnd && dstp < dst.mEnd); |
| 520 | *dstp++ = *srcp++; |
| 521 | } |
| 522 | *dstp++ = 0.5; |
| 523 | } else { |
| 524 | while (srcp < mEnd) { |
| 525 | assert(dstp < dst.mEnd); |
| 526 | *dstp++ = *srcp++; |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | assert(dstp == dst.mEnd && puncture == pend); |
Roman Khassraf | 059bab9 | 2015-05-20 12:49:46 +0200 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // vim: ts=4 sw=4 |