-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
96 lines (70 loc) · 2.48 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using System.Text;
using System.Collections;
namespace exc.jdbi.Algorithms.MinMaxScalers;
using static exc.jdbi.Systems.RandomHolder;
using static exc.jdbi.Algorithms.MinMaxScalers.MinMaxScalerByte;
//What is noticeable is, that data compression is taking place.
//This works much better with shorter English texts than with
//random bytes.
//With data compression there is always the problem that
//compression is not possible with short texts.
//This could be the solution to this problem.
public class Program
{
public static void Main()
{
KopieSourceFiles();
var round = 10;
for (var i = 0; i < round; i++)
{
TestMinMaxScalerText("0b.txt"); // 4.txt
TestMinMaxScalerRandom();
}
Console.WriteLine("Finish");
Console.ReadLine();
}
private static void TestMinMaxScalerText(string filepath)
{
//var txt = FileText(filepath);
var bytes = Encoding.UTF8.GetBytes(FileText(filepath));
var balen1 = new BitArray(bytes).Length;
var sblen1 = ShortestBitLength(bytes);
var limit = ToLimited(bytes);
var (src, minmax) = MinMaxScaler(bytes, limit);
var balen2 = new BitArray(src).Length;
var sblen2 = ShortestBitLength(src);
var bsblen = sblen1 > sblen2;
var result = MinMaxDescaler(src, minmax);
if (!bytes.SequenceEqual(result))
throw new Exception();
}
private static void TestMinMaxScalerRandom()
{
var size = Rand.Next(5, 15);
//var size = Rand.Next(10, 1024);
var bytes = RngBytes(size);
//var bytes = RngBytes(size, 10, 190);
var limit = ToLimited(bytes);
if (MinMaxScaler(bytes, limit, out var src_minmax))
if (!bytes.SequenceEqual(MinMaxDescaler(src_minmax)))
throw new Exception();
}
private static void KopieSourceFiles()
{
var filefolder = @"..\..\..\..\TextSource\";
foreach (var file in Directory.GetFiles(filefolder, "*", SearchOption.AllDirectories))
if (File.Exists(file))
File.Copy(file, string.Join("", Path.GetFileName(file)), true);
}
private static string FileText(string filepath) =>
File.ReadAllText(filepath);
private static int ShortestBitLength(ReadOnlySpan<byte> input)
{
//if x not power 2 ->> 2,4,8,16,32,64,...
//result = log(x0,2)+1 + log(x1,2)+1 ... + log(xn,2)+1
var result = 0;
foreach (var number in input)
result += Convert.ToString(number, 2).Length;
return result;
}
}