dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, 2009 Free Software Foundation, Inc. |
| 3 | * |
Pau Espin Pedrol | 21d03d3 | 2019-07-22 12:05:52 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: AGPL-3.0+ |
| 5 | * |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 6 | * This software is distributed under the terms of the GNU Affero Public License. |
| 7 | * See the COPYING file in the main directory for details. |
| 8 | * |
| 9 | * This use of this software may be subject to additional restrictions. |
| 10 | * See the LEGAL file in the main directory for details. |
| 11 | |
| 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. |
| 16 | |
| 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. |
| 21 | |
| 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/>. |
| 24 | |
| 25 | */ |
| 26 | |
| 27 | |
| 28 | #ifndef FECVECTORS_H |
| 29 | #define FECVECTORS_H |
| 30 | |
| 31 | #include "Vector.h" |
| 32 | #include <stdint.h> |
| 33 | |
| 34 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 35 | class BitVector : public Vector<char> { |
| 36 | |
| 37 | |
| 38 | public: |
| 39 | |
| 40 | /**@name Constructors. */ |
| 41 | //@{ |
| 42 | |
| 43 | /**@name Casts of Vector constructors. */ |
| 44 | //@{ |
| 45 | BitVector(char* wData, char* wStart, char* wEnd) |
| 46 | :Vector<char>(wData,wStart,wEnd) |
| 47 | { } |
| 48 | BitVector(size_t len=0):Vector<char>(len) {} |
| 49 | BitVector(const Vector<char>& source):Vector<char>(source) {} |
| 50 | BitVector(Vector<char>& source):Vector<char>(source) {} |
| 51 | BitVector(const Vector<char>& source1, const Vector<char> source2):Vector<char>(source1,source2) {} |
| 52 | //@} |
| 53 | |
| 54 | /** Construct from a string of "0" and "1". */ |
| 55 | BitVector(const char* valString); |
| 56 | //@} |
| 57 | |
| 58 | /** Index a single bit. */ |
| 59 | bool bit(size_t index) const |
| 60 | { |
| 61 | // We put this code in .h for fast inlining. |
| 62 | const char *dp = mStart+index; |
| 63 | assert(dp<mEnd); |
| 64 | return (*dp) & 0x01; |
| 65 | } |
| 66 | |
| 67 | /**@name Casts and overrides of Vector operators. */ |
| 68 | //@{ |
| 69 | BitVector segment(size_t start, size_t span) |
| 70 | { |
| 71 | char* wStart = mStart + start; |
| 72 | char* wEnd = wStart + span; |
| 73 | assert(wEnd<=mEnd); |
| 74 | return BitVector(NULL,wStart,wEnd); |
| 75 | } |
| 76 | |
| 77 | BitVector alias() |
| 78 | { return segment(0,size()); } |
| 79 | |
| 80 | const BitVector segment(size_t start, size_t span) const |
| 81 | { return (BitVector)(Vector<char>::segment(start,span)); } |
| 82 | |
| 83 | BitVector head(size_t span) { return segment(0,span); } |
| 84 | const BitVector head(size_t span) const { return segment(0,span); } |
| 85 | BitVector tail(size_t start) { return segment(start,size()-start); } |
| 86 | const BitVector tail(size_t start) const { return segment(start,size()-start); } |
| 87 | //@} |
| 88 | |
| 89 | |
| 90 | void zero() { fill(0); } |
| 91 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 92 | |
| 93 | /** Invert 0<->1. */ |
| 94 | void invert(); |
| 95 | |
| 96 | /**@name Byte-wise operations. */ |
| 97 | //@{ |
| 98 | /** Reverse an 8-bit vector. */ |
| 99 | void reverse8(); |
| 100 | /** Reverse groups of 8 within the vector (byte reversal). */ |
| 101 | void LSB8MSB(); |
| 102 | //@} |
| 103 | |
| 104 | /**@name Serialization and deserialization. */ |
| 105 | //@{ |
| 106 | uint64_t peekField(size_t readIndex, unsigned length) const; |
| 107 | uint64_t peekFieldReversed(size_t readIndex, unsigned length) const; |
| 108 | uint64_t readField(size_t& readIndex, unsigned length) const; |
| 109 | uint64_t readFieldReversed(size_t& readIndex, unsigned length) const; |
| 110 | void fillField(size_t writeIndex, uint64_t value, unsigned length); |
| 111 | void fillFieldReversed(size_t writeIndex, uint64_t value, unsigned length); |
| 112 | void writeField(size_t& writeIndex, uint64_t value, unsigned length); |
| 113 | void writeFieldReversed(size_t& writeIndex, uint64_t value, unsigned length); |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 114 | void write0(size_t& writeIndex) { writeField(writeIndex,0,1); } |
| 115 | void write1(size_t& writeIndex) { writeField(writeIndex,1,1); } |
| 116 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 117 | //@} |
| 118 | |
| 119 | /** Sum of bits. */ |
| 120 | unsigned sum() const; |
| 121 | |
| 122 | /** Reorder bits, dest[i] = this[map[i]]. */ |
| 123 | void map(const unsigned *map, size_t mapSize, BitVector& dest) const; |
| 124 | |
| 125 | /** Reorder bits, dest[map[i]] = this[i]. */ |
| 126 | void unmap(const unsigned *map, size_t mapSize, BitVector& dest) const; |
| 127 | |
| 128 | /** Pack into a char array. */ |
| 129 | void pack(unsigned char*) const; |
| 130 | |
| 131 | /** Unpack from a char array. */ |
| 132 | void unpack(const unsigned char*); |
| 133 | |
| 134 | /** Make a hexdump string. */ |
| 135 | void hex(std::ostream&) const; |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 136 | std::string hexstr() const; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 137 | |
| 138 | /** Unpack from a hexdump string. |
| 139 | * @returns true on success, false on error. */ |
| 140 | bool unhex(const char*); |
| 141 | |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 142 | void set(BitVector other) // That's right. No ampersand. |
| 143 | { |
| 144 | clear(); |
| 145 | mData=other.mData; |
| 146 | mStart=other.mStart; |
| 147 | mEnd=other.mEnd; |
| 148 | other.mData=NULL; |
| 149 | } |
| 150 | |
| 151 | void settfb(int i, int j) const |
| 152 | { |
| 153 | mStart[i] = j; |
| 154 | } |
| 155 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | |
| 159 | |
| 160 | std::ostream& operator<<(std::ostream&, const BitVector&); |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | /** |
| 168 | The SoftVector class is used to represent a soft-decision signal. |
| 169 | Values 0..1 represent probabilities that a bit is "true". |
| 170 | */ |
| 171 | class SoftVector: public Vector<float> { |
| 172 | |
| 173 | public: |
| 174 | |
| 175 | /** Build a SoftVector of a given length. */ |
| 176 | SoftVector(size_t wSize=0):Vector<float>(wSize) {} |
| 177 | |
| 178 | /** Construct a SoftVector from a C string of "0", "1", and "X". */ |
| 179 | SoftVector(const char* valString); |
| 180 | |
| 181 | /** Construct a SoftVector from a BitVector. */ |
| 182 | SoftVector(const BitVector& source); |
| 183 | |
| 184 | /** |
| 185 | Wrap a SoftVector around a block of floats. |
| 186 | The block will be delete[]ed upon desctuction. |
| 187 | */ |
| 188 | SoftVector(float *wData, unsigned length) |
| 189 | :Vector<float>(wData,length) |
| 190 | {} |
| 191 | |
| 192 | SoftVector(float* wData, float* wStart, float* wEnd) |
| 193 | :Vector<float>(wData,wStart,wEnd) |
| 194 | { } |
| 195 | |
| 196 | /** |
| 197 | Casting from a Vector<float>. |
| 198 | Note that this is NOT pass-by-reference. |
| 199 | */ |
| 200 | SoftVector(Vector<float> source) |
| 201 | :Vector<float>(source) |
| 202 | {} |
| 203 | |
| 204 | |
| 205 | /**@name Casts and overrides of Vector operators. */ |
| 206 | //@{ |
| 207 | SoftVector segment(size_t start, size_t span) |
| 208 | { |
| 209 | float* wStart = mStart + start; |
| 210 | float* wEnd = wStart + span; |
| 211 | assert(wEnd<=mEnd); |
| 212 | return SoftVector(NULL,wStart,wEnd); |
| 213 | } |
| 214 | |
| 215 | SoftVector alias() |
| 216 | { return segment(0,size()); } |
| 217 | |
| 218 | const SoftVector segment(size_t start, size_t span) const |
| 219 | { return (SoftVector)(Vector<float>::segment(start,span)); } |
| 220 | |
| 221 | SoftVector head(size_t span) { return segment(0,span); } |
| 222 | const SoftVector head(size_t span) const { return segment(0,span); } |
| 223 | SoftVector tail(size_t start) { return segment(start,size()-start); } |
| 224 | const SoftVector tail(size_t start) const { return segment(start,size()-start); } |
| 225 | //@} |
| 226 | |
Alexander Chemeris | 7db522b | 2017-03-17 18:00:50 -0700 | [diff] [blame] | 227 | // How good is the SoftVector in the sense of the bits being solid? |
Alexander Chemeris | 38b6987 | 2017-03-17 18:20:09 -0700 | [diff] [blame] | 228 | // Result of 1 is perfect and 0 means all the bits were 0.0 |
kurtis.heimerl | 5a87247 | 2013-05-31 21:47:25 +0000 | [diff] [blame] | 229 | // If plow is non-NULL, also return the lowest energy bit. |
| 230 | float getEnergy(float *low=0) const; |
| 231 | |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 232 | /** Fill with "unknown" values. */ |
Alexander Chemeris | 38b6987 | 2017-03-17 18:20:09 -0700 | [diff] [blame] | 233 | void unknown() { fill(0.0F); } |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 234 | |
| 235 | /** Return a hard bit value from a given index by slicing. */ |
| 236 | bool bit(size_t index) const |
| 237 | { |
| 238 | const float *dp = mStart+index; |
| 239 | assert(dp<mEnd); |
Alexander Chemeris | 38b6987 | 2017-03-17 18:20:09 -0700 | [diff] [blame] | 240 | return (*dp)>0.0F; |
dburgess | 82c46ff | 2011-10-07 02:40:51 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | /** Slice the whole signal into bits. */ |
| 244 | BitVector sliced() const; |
| 245 | |
| 246 | }; |
| 247 | |
| 248 | |
| 249 | |
| 250 | std::ostream& operator<<(std::ostream&, const SoftVector&); |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | #endif |
| 258 | // vim: ts=4 sw=4 |