Skip to content

Commit 0d92178

Browse files
committed
new file: Encoding/variable_length_run_encoding.sf
1 parent 1e728b0 commit 0d92178

File tree

5 files changed

+82
-11
lines changed

5 files changed

+82
-11
lines changed

Encoding/compress.sf

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
# This implementation reads from STDIN and outputs to STDOUT:
66
# sidef compress.sf < input.txt > output.Z
77

8-
# See also:
8+
# Reference:
9+
# Data Compression (Summer 2023) - Lecture 4 - The Unix 'compress' Program
910
# https://youtube.com/watch?v=1cJL9Va80Pk
11+
12+
# See also:
1013
# https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Welch
1114

1215
define (

Encoding/integers_binary_encoding.sf

+7-10
Original file line numberDiff line numberDiff line change
@@ -48,25 +48,22 @@ func encode_integers (Array integers) {
4848

4949
func decode_integers (String str) {
5050

51-
func str_shift(Ref s) {
52-
with ((*s).substr(0, 1)) { |t| (*s).substr!(1); t }
53-
}
54-
55-
var count_len = str_shift(\str).ord
51+
var str_fh = str.open_r(:raw)
52+
var count_len = str_fh.getc.ord
5653
var counts = []
5754

5855
count_len.times {
59-
var b = str_shift(\str).ord
60-
var l = Num(unpack('N', 4.of { str_shift(\str) }.join))
56+
var b = str_fh.getc.ord
57+
var l = Num(unpack('N', 4.of { str_fh.getc }.join))
6158
counts << [b, l] if (l > 0)
6259
}
6360

6461
var chunks = []
65-
var bits = str.ascii2bin
62+
var bits_fh = str_fh.slurp.ascii2bin.open_r(:raw)
6663

6764
for b,len in (counts) {
68-
chunks << bits.substr(0, b*len).slices(b)...
69-
bits.substr!(b*len)
65+
bits_fh.read(\var chunk, len*b)
66+
chunks << chunk.slices(b)...
7067
}
7168

7269
chunks.map { Num(_, 2) }
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/ruby
2+
3+
# Implementation of the Variable Length Run Encoding.
4+
5+
# Reference:
6+
# Data Compression (Summer 2023) - Lecture 5 - Basic Techniques
7+
# https://youtube.com/watch?v=TdFWb8mL5Gk
8+
9+
func VLR_encoding (String str) {
10+
11+
var bitstream = []
12+
13+
for c,v in (str.chars.run_length) {
14+
bitstream << c.ascii2bin
15+
if (v == 1) {
16+
bitstream << '0'
17+
}
18+
else {
19+
var t = (v-1).as_bin
20+
bitstream << ('1'*t.len + '0' + t.substr(1))
21+
}
22+
}
23+
24+
bitstream.join
25+
}
26+
27+
func VLR_decoding (String bitstring) {
28+
29+
var decoded = []
30+
var bits_fh = bitstring.open_r(:raw)
31+
32+
while (!bits_fh.eof) {
33+
var s = 8.of { bits_fh.getc }.join
34+
var c = pack('B*', s)
35+
36+
var bit_len = 0
37+
while (bits_fh.getc == '1') {
38+
++bit_len
39+
}
40+
41+
decoded << c
42+
43+
if (bit_len > 0) {
44+
decoded << c*Num('1' + (bit_len-1).of { bits_fh.getc }.join, 2)
45+
}
46+
}
47+
48+
decoded.join
49+
}
50+
51+
var str = join('', 'a'*13, 'b'*14, 'c'*10, 'd'*3, 'e'*1, 'f'*1, 'g'*4)
52+
53+
var enc = VLR_encoding(str)
54+
var dec = VLR_decoding(enc)
55+
56+
say enc
57+
say dec
58+
59+
assert_eq(dec, str)
60+
61+
with (File(__FILE__).read(':raw')) {|str|
62+
var enc = VLR_encoding(str)
63+
assert(enc.len < 8*str.len)
64+
assert_eq(VLR_decoding(enc), str)
65+
}
66+
67+
__END__
68+
011000011111010001100010111101010110001111110001011001001100011001010011001100011001111101
69+
aaaaaaaaaaaaabbbbbbbbbbbbbbccccccccccdddefgggg

Math/modular_binomial_fast.sf

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func modular_binomial(n,k,m) {
8181
if (k > n) { return 0 }
8282
if (k.is_zero || (k == n)) { return 1%m }
8383
if (k.is_one || (k == n-1)) { return n%m }
84+
if (n-k < k) { k = (n - k) }
8485

8586
var F = []
8687

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ A nice collection of day-to-day Sidef scripts.
3737
* [Lzw file compression utf8](./Encoding/lzw_file_compression_utf8.sf)
3838
* [Run length encoding](./Encoding/run_length_encoding.sf)
3939
* [Substitution cipher](./Encoding/substitution_cipher.sf)
40+
* [Variable length run encoding](./Encoding/variable_length_run_encoding.sf)
4041
* Encryption
4142
* [Age-lf](./Encryption/age-lf.sf)
4243
* [Backdoored rsa with x25519](./Encryption/backdoored_rsa_with_x25519.sf)

0 commit comments

Comments
 (0)