Created
October 23, 2008 18:39
-
-
Save repeatedly/19142 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written in the D programming language | |
/** | |
* Basic mixed sources by Slepian-Wolf in 1973 | |
*/ | |
import std.math : abs; | |
alias byte Data; | |
alias int[] Points; | |
invariant DataSize = 7; | |
invariant Data[DataSize] F = [0b001, 0b010, 0b011, 0b100, 0b101, 0b110, 0b111]; | |
void main() | |
{ | |
auto x = 0b0111101; | |
auto y = 0b0110101; | |
auto m = compressData(x); | |
assert(m == 7); | |
assert(detectError(y, m) == 4); | |
} | |
/** | |
* 7bit character to 3bit. | |
*/ | |
private Data compressData(Data x) | |
{ | |
Data compressed; | |
auto points = searchPoints(x); | |
foreach (point; points) | |
compressed ^= F[point]; | |
return compressed; | |
} | |
/** | |
* Detects error bit as Reciever. | |
*/ | |
private Data detectError(Data y, Data m) | |
{ | |
return abs(compressData(y) - m); | |
} | |
/** | |
* Searches and collects point of 1 value bit. | |
*/ | |
private Points searchPoints(Data data) | |
{ | |
Points points; | |
for (int i; i < DataSize; i++) | |
if (data >> i & 0x01) | |
points ~= DataSize - i - 1; | |
return points.reverse; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment