| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Flanderra
Revision: 22
Author: pcherkas
Date: 31 Oct 2008 18:47:55
Changes:bit logic refactoring and optimization.
Files:| ... | ...@@ -1,10 +1,8 @@ | |
| 1 | 1 | package com.flanderra.commons.test.bits; |
| 2 | 2 | |
| 3 | import java.util.Arrays; | |
| 4 | 3 | import java.util.BitSet; |
| 5 | 4 | import java.util.Random; |
| 6 | 5 | |
| 7 | import org.apache.commons.jexl.junit.Asserter; | |
| 8 | 6 | import org.apache.commons.logging.Log; |
| 9 | 7 | import org.apache.commons.logging.LogFactory; |
| 10 | 8 | import org.junit.Assert; |
| ... | ...@@ -72,36 +70,52 @@ | |
| 72 | 70 | Random rint = new Random(); |
| 73 | 71 | long[] longValues = new long[] { rint.nextLong(), rint.nextLong(), rint.nextLong(), rint.nextLong(), rint.nextLong(), |
| 74 | 72 | rint.nextLong(), rint.nextLong() }; |
| 75 | Assert.assertEquals(BitUtils.calculateMaxBitCountSI(longValues), _calculateMaxBitCountSI(longValues)); | |
| 73 | Assert.assertEquals(BitUtils.calculateMaxBitCountSI(longValues), BitUtils._calculateMaxBitCountSI(longValues)); | |
| 76 | 74 | } |
| 77 | 75 | |
| 78 | @Test | |
| 76 | @Ignore | |
| 79 | 77 | public void testFlipBytes() { |
| 80 | ||
| 81 | ||
| 78 | Random rint = new Random(); | |
| 79 | for (long testValue = 0, i = 0; i <= 100000; testValue = rint.nextLong(), i++) { | |
| 80 | BitSet result1 = BitUtils.longToBits(testValue, 64); | |
| 81 | Bits result2 = BitUtils._longToBits(testValue, 64); | |
| 82 | Assert.assertEquals(BitUtils.bin(BitUtils.flipBytes(result1, 8), 64), BitUtils._bin(BitUtils._flipBytes(result2))); | |
| 83 | } | |
| 82 | 84 | } |
| 83 | 85 | |
| 84 | public Bits _flipBytes(Bits val){ | |
| 85 | return val; | |
| 86 | } | |
| 87 | ||
| 88 | public static int _calculateMaxBitCountSI(long[] data) { | |
| 89 | for (int i = 0; i < data.length; i++) { | |
| 90 | if (data[i] < 0) { | |
| 91 | data[i] = (data[i] ^ -1) + 1; | |
| 92 | } | |
| 93 | } | |
| 94 | Arrays.sort(data); | |
| 95 | long maximum = data[data.length - 1]; | |
| 96 | if (maximum == 0) { | |
| 97 | return 0; | |
| 98 | } else { | |
| 99 | int result = 0; | |
| 100 | for(int i = 1; i<=64; i++){ | |
| 101 | //TODO: implement this | |
| 102 | } | |
| 103 | return result; | |
| 86 | @Test | |
| 87 | public void testSubConcat() { | |
| 88 | Random rint = new Random(); | |
| 89 | for (long testValue = 0, i = 0; i <= 100000; testValue = rint.nextLong(), i++) { | |
| 90 | BitSet result1 = BitUtils.longToBits(testValue, 64); | |
| 91 | Bits result2 = BitUtils._longToBits(testValue, 64); | |
| 92 | Assert.assertEquals( | |
| 93 | BitUtils.bin(BitUtils.concat(result1, result1, 64, 64), 128), | |
| 94 | BitUtils._bin(BitUtils._concat(result2, result2))); | |
| 95 | Assert.assertEquals(BitUtils._bin(BitUtils._sub(BitUtils._concat(result2, result2), 0, 63)), | |
| 96 | BitUtils._bin(BitUtils._sub(BitUtils._concat(result2, result2), 64, 127))); | |
| 104 | 97 | } |
| 105 | 98 | } |
| 106 | 99 | |
| 100 | ||
| 101 | ||
| 102 | ||
| 103 | ||
| 104 | ||
| 105 | ||
| 106 | ||
| 107 | ||
| 108 | ||
| 109 | ||
| 110 | ||
| 111 | ||
| 112 | ||
| 113 | ||
| 114 | ||
| 115 | ||
| 116 | ||
| 117 | ||
| 118 | ||
| 119 | ||
| 120 | ||
| 107 | 121 | } |
| ... | ...@@ -939,8 +939,14 @@ | |
| 939 | 939 | } |
| 940 | 940 | |
| 941 | 941 | public static Bits _bytesToBits(byte[] val) { |
| 942 | // TODO: implement this | |
| 943 | throw new IllegalStateException("not implemented yet"); | |
| 942 | Bits result = new Bits(); | |
| 943 | if(val == null || val.length==0){ | |
| 944 | return result; | |
| 945 | } else { | |
| 946 | result.setOffset(val.length<<3); | |
| 947 | result.setData(val); | |
| 948 | return result; | |
| 949 | } | |
| 944 | 950 | } |
| 945 | 951 | |
| 946 | 952 | public static BitSet concat(BitSet bs1, BitSet bs2, int size1, int size2) { |
| ... | ...@@ -955,8 +961,46 @@ | |
| 955 | 961 | } |
| 956 | 962 | |
| 957 | 963 | public static Bits _concat(Bits val1, Bits val2) { |
| 958 | // TODO: implement this | |
| 959 | throw new IllegalStateException("not implemented yet"); | |
| 964 | Bits result = new Bits(); | |
| 965 | boolean emptyVal1 = false; | |
| 966 | boolean emptyVal2 = false; | |
| 967 | if(val1 == null || val1.getData() == null || val1.getData().length == 0 || val1.getOffset() == 0){ | |
| 968 | emptyVal1 = true; | |
| 969 | } | |
| 970 | if(val2 == null || val2.getData() == null || val2.getData().length == 0 || val2.getOffset() == 0){ | |
| 971 | emptyVal2 = true; | |
| 972 | } | |
| 973 | if(emptyVal1 && emptyVal2){ | |
| 974 | return result; | |
| 975 | } else if (emptyVal1) { | |
| 976 | return val2; | |
| 977 | } else if (emptyVal2) { | |
| 978 | return val1; | |
| 979 | } else { | |
| 980 | result.setOffset(val1.getOffset() + val2.getOffset()); | |
| 981 | int blen = (val1.getOffset() + val2.getOffset()) / 8; | |
| 982 | blen += ((val1.getOffset() + val2.getOffset()) % 8 == 0) ? 0 : 1; | |
| 983 | byte[] data = new byte[blen]; | |
| 984 | int bitnum = 0, bytenum = 0; | |
| 985 | for(int i = 0; i < val1.getOffset(); i++){ | |
| 986 | data[bytenum] |= (val1.getBitAsInt(i) << (7 - bitnum)); | |
| 987 | bitnum++; | |
| 988 | if(bitnum == 8){ | |
| 989 | bytenum++; | |
| 990 | bitnum = 0; | |
| 991 | } | |
| 992 | } | |
| 993 | for(int i = 0; i < val1.getOffset(); i++){ | |
| 994 | data[bytenum] |= (val2.getBitAsInt(i) << (7 - bitnum)); | |
| 995 | bitnum++; | |
| 996 | if(bitnum == 8){ | |
| 997 | bytenum++; | |
| 998 | bitnum = 0; | |
| 999 | } | |
| 1000 | } | |
| 1001 | result.setData(data); | |
| 1002 | return result; | |
| 1003 | } | |
| 960 | 1004 | } |
| 961 | 1005 | |
| 962 | 1006 | public static BitSet sub(BitSet val, int start, int end) { |
| ... | ...@@ -968,8 +1012,26 @@ | |
| 968 | 1012 | } |
| 969 | 1013 | |
| 970 | 1014 | public static Bits _sub(Bits val, int start, int end) { |
| 971 | // TODO: implement this | |
| 972 | throw new IllegalStateException("not implemented yet"); | |
| 1015 | Bits result = new Bits(); | |
| 1016 | if( (val == null) || (end <= start) || (start > val.getOffset()) || | |
| 1017 | (val.getData() == null) || (val.getData().length == 0) || (val.getOffset() == 0)){ | |
| 1018 | return result; | |
| 1019 | } else { | |
| 1020 | result.setOffset(end-start); | |
| 1021 | int blen = (end - start) / 8; | |
| 1022 | blen += ((end - start) % 8 == 0) ? 0 : 1; | |
| 1023 | byte[] data = new byte[blen]; | |
| 1024 | for (int i = start, bitnum = 0, bytenum = 0; i <= end; i++){ | |
| 1025 | data[bytenum] |= (val.getBitAsInt(i) << (7 - bitnum)); | |
| 1026 | bitnum++; | |
| 1027 | if(bitnum == 8){ | |
| 1028 | bytenum++; | |
| 1029 | bitnum = 0; | |
| 1030 | } | |
| 1031 | } | |
| 1032 | result.setData(data); | |
| 1033 | return result; | |
| 1034 | } | |
| 973 | 1035 | } |
| 974 | 1036 | |
| 975 | 1037 | public static BitSet flipBytes(BitSet val, int byteNumber) { |
| ... | ...@@ -982,9 +1044,20 @@ | |
| 982 | 1044 | return result; |
| 983 | 1045 | } |
| 984 | 1046 | |
| 985 | public static Bits _flipBytes(Bits val) { | |
| 986 | // TODO: implement this | |
| 987 | throw new IllegalStateException("not implemented yet"); | |
| 1047 | public static Bits _flipBytes(Bits val){ | |
| 1048 | Bits result = new Bits(); | |
| 1049 | if( val == null || val.getOffset()==0 || val.getData()==null || val.getData().length == 0){ | |
| 1050 | return result; | |
| 1051 | } else { | |
| 1052 | int blen = val.getData().length - 1; | |
| 1053 | byte[] resData = new byte[blen + 1]; | |
| 1054 | for(int i = 0; i <= blen; i++){ | |
| 1055 | resData[(blen - i)] = val.getData()[i]; | |
| 1056 | } | |
| 1057 | result.setOffset(val.getOffset()); | |
| 1058 | result.setData(resData); | |
| 1059 | return result; | |
| 1060 | } | |
| 988 | 1061 | } |
| 989 | 1062 | |
| 990 | 1063 | public static int toTweeps(double val) { |
| ... | ...@@ -7,6 +7,7 @@ | |
| 7 | 7 | public class Bits { |
| 8 | 8 | private int offset; |
| 9 | 9 | private byte[] data; |
| 10 | private static final int[] MASKS = new int[]{0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; | |
| 10 | 11 | |
| 11 | 12 | public Bits() { |
| 12 | 13 | |
| ... | ...@@ -30,4 +31,26 @@ | |
| 30 | 31 | public void setData(byte[] data) { |
| 31 | 32 | this.data = data; |
| 32 | 33 | } |
| 34 | ||
| 35 | public int getBitAsInt(int i){ | |
| 36 | return getBitAsBoolean(i)?1:0; | |
| 37 | } | |
| 38 | ||
| 39 | public int getBitAsInt(int bytenum, int bitnum){ | |
| 40 | return getBitAsBoolean(bytenum, bitnum)?1:0; | |
| 41 | } | |
| 42 | ||
| 43 | public boolean getBitAsBoolean(int i){ | |
| 44 | if((data == null) || (data.length == 0) || (offset < i)) { | |
| 45 | return false; | |
| 46 | } else { | |
| 47 | int bytenum = i / 8; | |
| 48 | int bitnum = i % 8; | |
| 49 | return (data[bytenum] & MASKS[bitnum]) != 0; | |
| 50 | } | |
| 51 | } | |
| 52 | ||
| 53 | public boolean getBitAsBoolean(int bytenum, int bitnum){ | |
| 54 | return (data[bytenum] & MASKS[bitnum]) != 0; | |
| 55 | } | |
| 33 | 56 | } |