Data update
This commit is contained in:
parent
81fd053722
commit
52a6ef48dd
10248 changed files with 63654 additions and 6775 deletions
114
Task/Bitwise-IO/C++/bitwise-io.cpp
Normal file
114
Task/Bitwise-IO/C++/bitwise-io.cpp
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class BitFilter {
|
||||
public:
|
||||
BitFilter(const std::string& filePath) {
|
||||
writer.open(filePath, std::ios_base::binary);
|
||||
reader.open(filePath, std::ios_base::binary);
|
||||
accumulator = 0;
|
||||
bits = 0;
|
||||
}
|
||||
|
||||
void write(const std::vector<char>& chars, const uint32_t& startIndex,
|
||||
uint32_t bitCount, uint32_t bitsOmittedCount) {
|
||||
uint32_t index = startIndex + bitsOmittedCount / 8;
|
||||
bitsOmittedCount %= 8;
|
||||
|
||||
while ( bitCount > 0 || bits >= 8 ) {
|
||||
while ( bits >= 8 ) {
|
||||
bits -= 8;
|
||||
writer.put(accumulator >> bits);
|
||||
accumulator &= ( 1 << bits ) - 1;
|
||||
}
|
||||
|
||||
while ( bits < 8 && bitCount > 0 ) {
|
||||
accumulator = ( accumulator << 1 ) |
|
||||
( ( ( 128 >> bitsOmittedCount ) & chars[index] ) >> ( 7 - bitsOmittedCount ) );
|
||||
bitCount -= 1;
|
||||
bits += 1;
|
||||
if ( ++bitsOmittedCount == 8 ) {
|
||||
bitsOmittedCount = 0;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void read(std::vector<char>& chars, const uint32_t& startIndex,
|
||||
uint32_t bitCount, uint32_t bitsOmittedCount) {
|
||||
uint32_t index = startIndex + bitsOmittedCount / 8;
|
||||
bitsOmittedCount %= 8;
|
||||
|
||||
while ( bitCount > 0 ) {
|
||||
while ( bits > 0 && bitCount > 0 ) {
|
||||
const uint32_t mask = 128 >> bitsOmittedCount;
|
||||
if ( ( accumulator & ( 1 << ( bits - 1 ) ) ) > 0 ) {
|
||||
chars[index] = chars[index] | mask;
|
||||
} else {
|
||||
chars[index] = chars[index] & ( ~mask & 0xff );
|
||||
}
|
||||
|
||||
bitCount -= 1;
|
||||
bits -= 1;
|
||||
if ( ++bitsOmittedCount >= 8 ) {
|
||||
bitsOmittedCount = 0;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if ( bitCount > 0 ) {
|
||||
char qq;
|
||||
reader.read((&qq), 1);
|
||||
accumulator = ( accumulator << 8 ) | qq;
|
||||
bits += 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void close_writer() {
|
||||
if ( bits != 0 ) {
|
||||
accumulator <<= 8 - bits;
|
||||
writer.put(accumulator);
|
||||
}
|
||||
|
||||
writer.close();
|
||||
accumulator = 0;
|
||||
bits = 0;
|
||||
}
|
||||
|
||||
void close_reader() {
|
||||
reader.close();
|
||||
accumulator = 0;
|
||||
bits = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
uint32_t accumulator, bits;
|
||||
std::ofstream writer;
|
||||
std::ifstream reader;
|
||||
};
|
||||
|
||||
int main() {
|
||||
BitFilter bitFilter("BitwiseIO.dat");
|
||||
std::string source = "abcde12345";
|
||||
std::vector<char> chars(source.begin(), source.end());
|
||||
|
||||
// For each char in 'chars', write 7 bits omitting the most significant bit
|
||||
for ( uint32_t i = 0; i < chars.size(); ++i ) {
|
||||
bitFilter.write(chars, i, 7, 1);
|
||||
}
|
||||
bitFilter.close_writer();
|
||||
|
||||
// Read 7 bits and expand to a char of 'destination' by reconstructing the most significant bit
|
||||
std::vector<char> destination(chars.size());
|
||||
for ( uint32_t i = 0; i < destination.size(); ++i ) {
|
||||
bitFilter.read(destination, i, 7, 1);
|
||||
}
|
||||
bitFilter.close_reader();
|
||||
|
||||
std::string result(destination.data());
|
||||
std::cout << result << std::endl;
|
||||
}
|
||||
112
Task/Bitwise-IO/Java/bitwise-io.java
Normal file
112
Task/Bitwise-IO/Java/bitwise-io.java
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public final class BitwiseIO {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
BitFilter bitFilter = new BitFilter("BitwiseIO.dat");
|
||||
byte[] source = "abcde12345".getBytes(StandardCharsets.ISO_8859_1);
|
||||
|
||||
// For each byte in 'source', write 7 bits omitting the most significant bit
|
||||
for ( int i = 0; i < source.length; i++ ) {
|
||||
bitFilter.write(source, i, 7, 1);
|
||||
}
|
||||
bitFilter.closeWriter();
|
||||
|
||||
// Read 7 bits and expand to a byte of 'destination' by reconstructing the most significant bit
|
||||
byte[] destination = new byte[source.length];
|
||||
for ( int i = 0; i < destination.length; i++ ) {
|
||||
bitFilter.read(destination, i, 7, 1);
|
||||
}
|
||||
bitFilter.closeReader();
|
||||
|
||||
System.out.println( new String(destination) );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
final class BitFilter {
|
||||
|
||||
public BitFilter(String filePath) throws IOException {
|
||||
bufferedWriter = new BufferedWriter( new FileWriter(filePath) );
|
||||
bufferedReader = new BufferedReader( new FileReader(filePath) );
|
||||
}
|
||||
|
||||
public void write(byte[] bytes, int startIndex, int bitCount, int bitsOmittedCount) throws IOException {
|
||||
int index = startIndex + bitsOmittedCount / 8;
|
||||
bitsOmittedCount %= 8;
|
||||
|
||||
while ( bitCount > 0 || bits >= 8 ) {
|
||||
while ( bits >= 8 ) {
|
||||
bits -= 8;
|
||||
bufferedWriter.write(accumulator >>> bits);
|
||||
accumulator &= ( 1 << bits ) - 1;
|
||||
}
|
||||
|
||||
while ( bits < 8 && bitCount > 0 ) {
|
||||
accumulator = ( accumulator << 1 ) |
|
||||
( ( ( 128 >>> bitsOmittedCount ) & bytes[index] ) >>> ( 7 - bitsOmittedCount ) );
|
||||
bitCount -= 1;
|
||||
bits += 1;
|
||||
if ( ++bitsOmittedCount == 8 ) {
|
||||
bitsOmittedCount = 0;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void read(byte[] bytes, int startIndex, int bitCount, int bitsOmittedCount) throws IOException {
|
||||
int index = startIndex + bitsOmittedCount / 8;
|
||||
bitsOmittedCount %= 8;
|
||||
|
||||
while ( bitCount > 0 ) {
|
||||
while ( bits > 0 && bitCount > 0 ) {
|
||||
final int mask = 128 >>> bitsOmittedCount;
|
||||
if ( ( accumulator & ( 1 << ( bits - 1 ) ) ) > 0 ) {
|
||||
bytes[index] = (byte) ( bytes[index] | mask );
|
||||
} else {
|
||||
bytes[index] = (byte) ( bytes[index] & ( ~mask & 0xff ) );
|
||||
}
|
||||
|
||||
bitCount -= 1;
|
||||
bits -= 1;
|
||||
if ( ++bitsOmittedCount >= 8 ) {
|
||||
bitsOmittedCount = 0;
|
||||
index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
if ( bitCount > 0 ) {
|
||||
accumulator = ( accumulator << 8 ) | bufferedReader.read();
|
||||
bits += 8;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void closeWriter() throws IOException {
|
||||
if ( bits != 0 ) {
|
||||
accumulator <<= 8 - bits;
|
||||
bufferedWriter.write(accumulator);
|
||||
}
|
||||
|
||||
bufferedWriter.close();
|
||||
accumulator = 0;
|
||||
bits = 0;
|
||||
}
|
||||
|
||||
public void closeReader() throws IOException {
|
||||
bufferedReader.close();
|
||||
accumulator = 0;
|
||||
bits = 0;
|
||||
}
|
||||
|
||||
private int accumulator, bits;
|
||||
private BufferedWriter bufferedWriter;
|
||||
private BufferedReader bufferedReader;
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue