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