Skip to content

Commit

Permalink
add ReadMuller, Hamming coding, saving messages and info about every …
Browse files Browse the repository at this point in the history
…message
  • Loading branch information
Alevs2R committed Nov 20, 2017
1 parent 520819b commit 8c4b9c1
Show file tree
Hide file tree
Showing 9 changed files with 995 additions and 39 deletions.
1 change: 1 addition & 0 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class App extends React.Component {
logout = () => {
this.setState({screen: LOGIN_SCREEN, nickname: ''});
AsyncStorage.removeItem('nickname');
AsyncStorage.removeItem('messages');
};

render() {
Expand Down
275 changes: 275 additions & 0 deletions android/app/src/main/java/com/coder/BitArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,275 @@
/*
* Copyright 1997-2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/

package com.coder;

import java.io.ByteArrayOutputStream;
import java.util.Arrays;

/**
* A packed array of booleans.
*
* @author Joshua Bloch
* @author Douglas Hoover
*/

public class BitArray {

private byte[] repn;
private int length;

private static final int BITS_PER_UNIT = 8;

private static int subscript(int idx) {
return idx / BITS_PER_UNIT;
}

private static int position(int idx) { // bits big-endian in each unit
return 1 << (BITS_PER_UNIT - 1 - (idx % BITS_PER_UNIT));
}

/**
* Creates a BitArray of the specified size, initialized to zeros.
*/
public BitArray(int length) throws IllegalArgumentException {
if (length < 0) {
throw new IllegalArgumentException("Negative length for BitArray");
}

this.length = length;

repn = new byte[(length + BITS_PER_UNIT - 1)/BITS_PER_UNIT];
}


/**
* Creates a BitArray of the specified size, initialized from the
* specified byte array. The most significant bit of a[0] gets
* index zero in the BitArray. The array a must be large enough
* to specify a value for every bit in the BitArray. In other words,
* 8*a.length <= length.
*/
public BitArray(int length, byte[] a) throws IllegalArgumentException {

if (length < 0) {
throw new IllegalArgumentException("Negative length for BitArray");
}
if (a.length * BITS_PER_UNIT < length) {
throw new IllegalArgumentException("Byte array too short to represent " +
"bit array of given length");
}

this.length = length;

int repLength = ((length + BITS_PER_UNIT - 1)/BITS_PER_UNIT);
int unusedBits = repLength*BITS_PER_UNIT - length;
byte bitMask = (byte) (0xFF << unusedBits);

/*
normalize the representation:
1. discard extra bytes
2. zero out extra bits in the last byte
*/
repn = new byte[repLength];
System.arraycopy(a, 0, repn, 0, repLength);
if (repLength > 0) {
repn[repLength - 1] &= bitMask;
}
}

/**
* Create a BitArray whose bits are those of the given array
* of Booleans.
*/
public BitArray(boolean[] bits) {
length = bits.length;
repn = new byte[(length + 7)/8];

for (int i=0; i < length; i++) {
set(i, bits[i]);
}
}


/**
* Copy constructor (for cloning).
*/
private BitArray(BitArray ba) {
length = ba.length;
repn = ba.repn.clone();
}

/**
* Returns the indexed bit in this BitArray.
*/
public boolean get(int index) throws ArrayIndexOutOfBoundsException {
if (index < 0 || index >= length) {
throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
}

return (repn[subscript(index)] & position(index)) != 0;
}

/**
* Sets the indexed bit in this BitArray.
*/
public void set(int index, boolean value)
throws ArrayIndexOutOfBoundsException {
if (index < 0 || index >= length) {
throw new ArrayIndexOutOfBoundsException(Integer.toString(index));
}
int idx = subscript(index);
int bit = position(index);

if (value) {
repn[idx] |= bit;
} else {
repn[idx] &= ~bit;
}
}

/**
* Returns the length of this BitArray.
*/
public int length() {
return length;
}

/**
* Returns a Byte array containing the contents of this BitArray.
* The bit stored at index zero in this BitArray will be copied
* into the most significant bit of the zeroth element of the
* returned byte array. The last byte of the returned byte array
* will be contain zeros in any bits that do not have corresponding
* bits in the BitArray. (This matters only if the BitArray's size
* is not a multiple of 8.)
*/
public byte[] toByteArray() {
return repn.clone();
}

public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || !(obj instanceof BitArray)) return false;

BitArray ba = (BitArray) obj;

if (ba.length != length) return false;

for (int i = 0; i < repn.length; i += 1) {
if (repn[i] != ba.repn[i]) return false;
}
return true;
}

/**
* Return a boolean array with the same bit values a this BitArray.
*/
public boolean[] toBooleanArray() {
boolean[] bits = new boolean[length];

for (int i=0; i < length; i++) {
bits[i] = get(i);
}
return bits;
}

/**
* Returns a hash code value for this bit array.
*
* @return a hash code value for this bit array.
*/
public int hashCode() {
int hashCode = 0;

for (int i = 0; i < repn.length; i++)
hashCode = 31*hashCode + repn[i];

return hashCode ^ length;
}


public Object clone() {
return new BitArray(this);
}


private static final byte[][] NYBBLE = {
{ (byte)'0',(byte)'0',(byte)'0',(byte)'0'},
{ (byte)'0',(byte)'0',(byte)'0',(byte)'1'},
{ (byte)'0',(byte)'0',(byte)'1',(byte)'0'},
{ (byte)'0',(byte)'0',(byte)'1',(byte)'1'},
{ (byte)'0',(byte)'1',(byte)'0',(byte)'0'},
{ (byte)'0',(byte)'1',(byte)'0',(byte)'1'},
{ (byte)'0',(byte)'1',(byte)'1',(byte)'0'},
{ (byte)'0',(byte)'1',(byte)'1',(byte)'1'},
{ (byte)'1',(byte)'0',(byte)'0',(byte)'0'},
{ (byte)'1',(byte)'0',(byte)'0',(byte)'1'},
{ (byte)'1',(byte)'0',(byte)'1',(byte)'0'},
{ (byte)'1',(byte)'0',(byte)'1',(byte)'1'},
{ (byte)'1',(byte)'1',(byte)'0',(byte)'0'},
{ (byte)'1',(byte)'1',(byte)'0',(byte)'1'},
{ (byte)'1',(byte)'1',(byte)'1',(byte)'0'},
{ (byte)'1',(byte)'1',(byte)'1',(byte)'1'}
};

private static final int BYTES_PER_LINE = 8;

/**
* Returns a string representation of this BitArray.
*/
public String toString() {
ByteArrayOutputStream out = new ByteArrayOutputStream();

for (int i = 0; i < repn.length - 1; i++) {
out.write(NYBBLE[(repn[i] >> 4) & 0x0F], 0, 4);
out.write(NYBBLE[repn[i] & 0x0F], 0, 4);

if (i % BYTES_PER_LINE == BYTES_PER_LINE - 1) {
out.write('\n');
} else {
out.write(' ');
}
}

// in last byte of repn, use only the valid bits
for (int i = BITS_PER_UNIT * (repn.length - 1); i < length; i++) {
out.write(get(i) ? '1' : '0');
}

return new String(out.toByteArray());

}

public BitArray truncate() {
for (int i=length-1; i>=0; i--) {
if (get(i)) {
return new BitArray(i+1, Arrays.copyOf(repn, (i + BITS_PER_UNIT)/BITS_PER_UNIT));
}
}
return new BitArray(1);
}

}
98 changes: 98 additions & 0 deletions android/app/src/main/java/com/coder/Hamming/Hamming74Coder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* This class used for Hamming(7,4) coding
* @author Pavel Kozlov
*/

package com.coder.Hamming;

public class Hamming74Coder {

public Hamming74Coder() {
}

public byte[] coding(byte[] byteSequence){

int[] bits = new int[8 * byteSequence.length];
//converting all bytes to bits
for (int i = 0; i < bits.length; i++) {
bits[i] = (byteSequence[i / 8] >> (7 - i % 8)) & 1;
}


int[] codedBits = new int[7 * (bits.length/4)];


for (int i = 0; i < (bits.length/4); i++) {
int[] tempBits = code4bits(new int[]{bits[4*i],bits[4*i+1],bits[4*i+2],bits[4*i+3]});
codedBits[7*i] = tempBits[0];
codedBits[7*i+1] = tempBits[1];
codedBits[7*i+2] = tempBits[2];
codedBits[7*i+3] = tempBits[3];
codedBits[7*i+4] = tempBits[4];
codedBits[7*i+5] = tempBits[5];
codedBits[7*i+6] = tempBits[6];
}

if(codedBits.length % 8 != 0){
int[] oldcodedBits = codedBits;
codedBits = new int[(oldcodedBits.length/8+1) * 8];
int cnst = codedBits.length-oldcodedBits.length;
for (int i = 0; i < cnst; i++) {
codedBits[i] = 0;
}

for (int i = 0; i < oldcodedBits.length; i++) {
codedBits[cnst+i] = oldcodedBits[i];
}
}

byte[] encodedSequence = new byte[codedBits.length/8];

String str;
int count = 0;
//converting bits to bytes
for(int i=0; i < codedBits.length; i+=8){
str = new String();
for(int k=0; k<8; k++) {
str += codedBits[i+k];
}
int tr = Integer.parseInt(str, 2);
encodedSequence[count] = (byte) tr;
count++;
}

return encodedSequence;

}

private int[] code4bits (int[] initialBits){
int[] out_boolean = new int[]{0,0,0,0,0,0,0};
out_boolean[0] = initialBits[0];
out_boolean[1] = initialBits[0];
out_boolean[2] = initialBits[0];

out_boolean[0] += initialBits[1];
out_boolean[4] = initialBits[1];
out_boolean[3] += initialBits[1];

out_boolean[1] += initialBits[2];
out_boolean[5] = initialBits[2];
out_boolean[3] += initialBits[2];

out_boolean[0] += initialBits[3];
out_boolean[6] = initialBits[3];
out_boolean[1] += initialBits[3];
out_boolean[3] += initialBits[3];

out_boolean[0] %= 2;
out_boolean[1] %= 2;
out_boolean[2] %= 2;
out_boolean[3] %= 2;
out_boolean[4] %= 2;
out_boolean[5] %= 2;
out_boolean[6] %= 2;

return out_boolean;
}

}
Loading

0 comments on commit 8c4b9c1

Please sign in to comment.