-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Оптимизировал расход памяти в тестах
- Loading branch information
Showing
7 changed files
with
230 additions
and
11 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.github.demidko.aot; | ||
|
||
import static java.util.BitSet.valueOf; | ||
|
||
import java.util.BitSet; | ||
|
||
/** | ||
* Класс предназначен для побитового чтения из примитивов | ||
*/ | ||
public class BitReader { | ||
|
||
private final BitSet bs; | ||
private int pos = -1; | ||
|
||
public BitReader(long... n) { | ||
bs = valueOf(n); | ||
} | ||
|
||
public BitReader(byte... b) { | ||
bs = valueOf(b); | ||
} | ||
|
||
public BitReader(BitReader other) { | ||
bs = (BitSet) other.bs.clone(); | ||
pos = other.pos; | ||
} | ||
|
||
/** | ||
* @return метод читает следующий по порядку бит | ||
*/ | ||
public boolean readBit() { | ||
return bs.get(++pos); | ||
} | ||
|
||
/** | ||
* Метод читает следующие N бит по порядку | ||
* | ||
* @param size количество бит | ||
* @return биты сохраненные в длинное целое число | ||
*/ | ||
public long readLong(int size) { | ||
BitWriter w = new BitWriter(); | ||
for (int i = 0; i < size; ++i) { | ||
w.write(readBit()); | ||
} | ||
return w.toLong(); | ||
} | ||
|
||
/** | ||
* @return прочитать следующее длинное целое | ||
*/ | ||
public long readLong() { | ||
return readLong(64); | ||
} | ||
|
||
/** | ||
* Метод читает следующие N бит по порядку | ||
* | ||
* @param size количество бит | ||
* @return биты сохраненные в целое число | ||
*/ | ||
public int readInt(int size) { | ||
return (int) readLong(size); | ||
} | ||
|
||
/** | ||
* @return прочитать следующее целое | ||
*/ | ||
public int readInt() { | ||
return readInt(32); | ||
} | ||
|
||
|
||
/** | ||
* Метод читает следующие N бит по порядку | ||
* | ||
* @param size количество бит | ||
* @return биты сохраненные в короткое целое число | ||
*/ | ||
public short readShort(int size) { | ||
return (short) readLong(size); | ||
} | ||
|
||
public short readShort() { | ||
return readShort(16); | ||
} | ||
|
||
/** | ||
* Метод читает следующие N бит по порядку | ||
* | ||
* @param size количество бит | ||
* @return биты сохраненные в байт | ||
*/ | ||
public byte readByte(int size) { | ||
return (byte) readLong(size); | ||
} | ||
|
||
public byte readByte() { | ||
return readByte(8); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.github.demidko.aot; | ||
|
||
import static java.util.BitSet.valueOf; | ||
|
||
import java.util.BitSet; | ||
|
||
/** | ||
* Класс предназначен для побитовой записи в примитивы | ||
*/ | ||
public class BitWriter { | ||
|
||
private final BitSet bs; | ||
private int pos = -1; | ||
|
||
public BitWriter(byte... b) { | ||
bs = valueOf(b); | ||
} | ||
|
||
public BitWriter(long... l) { | ||
bs = valueOf(l); | ||
} | ||
|
||
public BitWriter() { | ||
this(new byte[8]); | ||
} | ||
|
||
public BitWriter(BitWriter other) { | ||
bs = (BitSet) other.bs.clone(); | ||
pos = other.pos; | ||
} | ||
|
||
/** | ||
* Метод записывает следующий по порядку бит | ||
*/ | ||
public void write(boolean bit) { | ||
bs.set(++pos, bit); | ||
} | ||
|
||
/** | ||
* Метод сохраняет результат записи в long | ||
* | ||
* @return результат записи | ||
*/ | ||
public long toLong() { | ||
return bs.toLongArray().length == 0 ? 0 : bs.toLongArray()[0]; | ||
} | ||
|
||
public int toInt() { | ||
return (int) toLong(); | ||
} | ||
|
||
public short toShort() { | ||
return (short) toLong(); | ||
} | ||
|
||
public byte toByte() { | ||
return (byte) toLong(); | ||
} | ||
|
||
/** | ||
* Метод записывает по порядку заданное количество бит | ||
* | ||
* @param bits набор бит | ||
* @param len количество бит из набора | ||
*/ | ||
public void write(long bits, int len) { | ||
BitReader r = new BitReader(bits); | ||
for (int i = 0; i < len; ++i) { | ||
write(r.readBit()); | ||
} | ||
} | ||
|
||
public void writeLong(long l) { | ||
write(l, 64); | ||
} | ||
|
||
public void writeInt(int i) { | ||
write(i, 32); | ||
} | ||
|
||
public void writeShort(short s) { | ||
write(s, 16); | ||
} | ||
|
||
public void writeByte(byte b) { | ||
write(b, 8); | ||
} | ||
} |
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
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
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
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