Compare commits
3 Commits
0d5a7a93d3
...
e1c0d98a68
| Author | SHA1 | Date | |
|---|---|---|---|
| e1c0d98a68 | |||
| 38b738ecbb | |||
| a1deae1072 |
4
.gitignore
vendored
4
.gitignore
vendored
@ -5,4 +5,6 @@
|
||||
.sass-cache
|
||||
__pycache__
|
||||
/CSharp/obj
|
||||
/CSharp/bin
|
||||
/CSharp/bin
|
||||
/VB/obj
|
||||
/VB/bin
|
||||
@ -2,6 +2,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ErrorsManager{
|
||||
@ -120,14 +121,15 @@ namespace ErrorsManager{
|
||||
}
|
||||
|
||||
public string to_string(int code){
|
||||
string hexas = "";
|
||||
|
||||
StringBuilder hexas = new StringBuilder();
|
||||
|
||||
while(code != 0){
|
||||
hexas += alphabet[(byte)(code % _base)];
|
||||
hexas.Append(alphabet[(byte)(code % _base)]);
|
||||
code /= _base;
|
||||
}
|
||||
|
||||
return hexas;
|
||||
return hexas.ToString();
|
||||
}
|
||||
|
||||
public string to_string(string code){
|
||||
@ -176,32 +178,6 @@ namespace ErrorsManager{
|
||||
binary.PadLeft(binary.Length + power - remainer, '0'));
|
||||
}
|
||||
|
||||
public (int, string)[] process(string code, IEnumerable<string> messages){
|
||||
return process(to_array(code), messages);
|
||||
}
|
||||
|
||||
public (int, string)[] process(byte[] code, IEnumerable<string> messages){
|
||||
|
||||
List<(int, string)> response = new List<(int, string)>();
|
||||
|
||||
for_each_enumerate(code, (hexa, i) => {
|
||||
for(byte j = 0; j < power && (hexa & 1 << j) <= hexa; j ++)
|
||||
if((hexa & 1 << j) != 0){
|
||||
|
||||
int x = i * power + j;
|
||||
|
||||
response.Add((x, messages.ElementAtOrDefault(x) ?? "error_message_" + x.ToString()));
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return response.ToArray();
|
||||
}
|
||||
|
||||
public (int, string)[] process(int code, IEnumerable<string> messages){
|
||||
return process(to_array(code), messages);
|
||||
}
|
||||
|
||||
public int get_bits(string code){
|
||||
return code.Length == 0 ? 0 : (code.Length - 1) * power + (int)Math.Ceiling(Math.Log2(dictionary[code.Last<char>()] + 1));
|
||||
}
|
||||
@ -214,6 +190,30 @@ namespace ErrorsManager{
|
||||
return (int)Math.Ceiling(Math.Log2(code + 1));
|
||||
}
|
||||
|
||||
public void get_from_bits(string code, ref int from, ref int bits){
|
||||
get_from_bits(to_array(code), ref from, ref bits);
|
||||
}
|
||||
|
||||
public void get_from_bits(byte[] code, ref int from, ref int bits){
|
||||
if(from < 0){
|
||||
from = get_bits(code) + from;
|
||||
if(from < 0)
|
||||
from = 0;
|
||||
}
|
||||
if(bits < 0){
|
||||
from += bits;
|
||||
bits *= -1;
|
||||
if(from < 0){
|
||||
bits += from;
|
||||
from = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void get_from_bits(int code, ref int from, ref int bits){
|
||||
get_from_bits(to_array(code), ref from, ref bits);
|
||||
}
|
||||
|
||||
public string clean(string code){
|
||||
|
||||
int l = code.Length;
|
||||
@ -244,6 +244,109 @@ namespace ErrorsManager{
|
||||
return code;
|
||||
}
|
||||
|
||||
public string get_range(string code, int from, int bits = 0){
|
||||
return to_string(get_range(to_array(code), from, bits));
|
||||
}
|
||||
|
||||
public byte[] get_range(byte[] code, int from, int bits = 0){
|
||||
|
||||
List<byte> hexas;
|
||||
|
||||
get_from_bits(code, ref from, ref bits);
|
||||
|
||||
if(bits == 0)
|
||||
bits = get_bits(code) - from;
|
||||
if(bits <= 0)
|
||||
return new byte[]{0};
|
||||
|
||||
hexas = code.ToList<byte>();
|
||||
|
||||
if(from > 0){
|
||||
|
||||
byte shift = (byte)(from % power);
|
||||
int mask = ~-_base;
|
||||
|
||||
hexas = hexas.Skip(from / power).ToList<byte>();
|
||||
if(shift != 0 && hexas.Count > 0){
|
||||
|
||||
int l = hexas.Count - 1;
|
||||
|
||||
for(int i = 0; i < l; i ++)
|
||||
hexas[i] = (byte)((hexas[i] >> shift) | ((hexas[i + 1] << (power - shift)) & mask));
|
||||
hexas[l] >>= shift;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
if(bits > 0){
|
||||
|
||||
byte shift = (byte)(bits % power);
|
||||
|
||||
hexas = hexas.Take<byte>((int)Math.Ceiling(bits / (double)power)).ToList<byte>();
|
||||
if(shift != 0 && hexas.Count > 0)
|
||||
hexas[hexas.Count - 1] &= (byte)((1 << shift) - 1);
|
||||
|
||||
}
|
||||
|
||||
return clean(hexas.ToArray<byte>());
|
||||
}
|
||||
|
||||
public int get_range(int code, int from, int bits = 0){
|
||||
|
||||
get_from_bits(code, ref from, ref bits);
|
||||
|
||||
if(from > 0)
|
||||
code = (code >> from) & ((1 << (31 - from)) - 1);
|
||||
|
||||
if(bits <= 0 || bits >= 31)
|
||||
return code;
|
||||
|
||||
if(from + bits > 31)
|
||||
bits = 31 - from;
|
||||
|
||||
return code & ((1 << bits) - 1);
|
||||
}
|
||||
|
||||
public (int, string)[] process(string code, IEnumerable<string> messages){
|
||||
return process(to_array(code), messages);
|
||||
}
|
||||
|
||||
public (int, string)[] process(byte[] code, IEnumerable<string> messages, Dictionary<int, int>? blocks = null){
|
||||
|
||||
List<(int, string)> response = new List<(int, string)>();
|
||||
int k = 0;
|
||||
|
||||
if(blocks == null)
|
||||
blocks = new Dictionary<int, int>();
|
||||
|
||||
for(int i = 0, l = code.Length * power; i < l;)
|
||||
if(blocks.ContainsKey(i) && blocks[i] > 0){
|
||||
|
||||
int logarithm = (int)Math.Ceiling(Math.Log2(blocks[i]));
|
||||
int _k = to_integer(get_range(code, i, logarithm == 0 ? logarithm = 1 : logarithm));
|
||||
|
||||
if(_k > 0){
|
||||
_k = k + _k;
|
||||
response.Add((_k, messages.ElementAtOrDefault(_k) ?? "error_message_" + _k.ToString()));
|
||||
}
|
||||
|
||||
k += blocks[i];
|
||||
i += logarithm;
|
||||
|
||||
}else{
|
||||
if((code[i / power] & (1 << i % power)) != 0)
|
||||
response.Add((k, messages.ElementAtOrDefault(k) ?? "error_message_" + k.ToString()));
|
||||
k ++;
|
||||
i ++;
|
||||
}
|
||||
|
||||
return response.ToArray<(int, string)>();
|
||||
}
|
||||
|
||||
public (int, string)[] process(int code, IEnumerable<string> messages){
|
||||
return process(to_array(code), messages);
|
||||
}
|
||||
|
||||
public string bitwise(string code, int bits){
|
||||
return to_string(bitwise(to_array(code), bits));
|
||||
}
|
||||
@ -299,30 +402,6 @@ namespace ErrorsManager{
|
||||
code);
|
||||
}
|
||||
|
||||
public void get_from_bits(string code, ref int from, ref int bits){
|
||||
get_from_bits(to_array(code), ref from, ref bits);
|
||||
}
|
||||
|
||||
public void get_from_bits(byte[] code, ref int from, ref int bits){
|
||||
if(from < 0){
|
||||
from = get_bits(code) + from;
|
||||
if(from < 0)
|
||||
from = 0;
|
||||
}
|
||||
if(bits < 0){
|
||||
from += bits;
|
||||
bits *= -1;
|
||||
if(from < 0){
|
||||
bits += from;
|
||||
from = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void get_from_bits(int code, ref int from, ref int bits){
|
||||
get_from_bits(to_array(code), ref from, ref bits);
|
||||
}
|
||||
|
||||
public string reset(string code, int from, int bits = 0, bool reversed = false){
|
||||
return to_string(reset(to_array(code), from, bits, reversed));
|
||||
}
|
||||
@ -375,7 +454,7 @@ namespace ErrorsManager{
|
||||
}
|
||||
}
|
||||
|
||||
return hexas.ToArray<byte>();
|
||||
return clean(hexas.ToArray<byte>());
|
||||
}
|
||||
|
||||
public int reset(int code, int from, int bits = 0, bool reversed = false){
|
||||
@ -387,68 +466,6 @@ namespace ErrorsManager{
|
||||
return code & (reversed ?
|
||||
~-(1 << bits) << from :
|
||||
(~-(1 << get_bits(code)) << from + bits) | ~-(1 << from));
|
||||
|
||||
}
|
||||
|
||||
public string get_range(string code, int from, int bits = 0){
|
||||
return to_string(get_range(to_array(code), from, bits));
|
||||
}
|
||||
|
||||
public byte[] get_range(byte[] code, int from, int bits = 0){
|
||||
|
||||
List<byte> hexas;
|
||||
|
||||
get_from_bits(code, ref from, ref bits);
|
||||
|
||||
if(bits == 0)
|
||||
bits = get_bits(code) - from;
|
||||
if(bits <= 0)
|
||||
return new byte[]{0};
|
||||
|
||||
hexas = code.ToList<byte>();
|
||||
|
||||
if(from > 0){
|
||||
|
||||
byte shift = (byte)(from % power);
|
||||
int mask = ~-_base;
|
||||
|
||||
hexas = hexas.Skip(from / power).ToList<byte>();
|
||||
if(shift != 0 && hexas.Count > 0){
|
||||
|
||||
int l = hexas.Count - 1;
|
||||
for(int i = 0; i < l; i ++)
|
||||
hexas[i] = (byte)((hexas[i] >> shift) | ((hexas[i + 1] << (power - shift)) & mask));
|
||||
hexas[hexas.Count - 1] >>= shift;
|
||||
}
|
||||
|
||||
}
|
||||
if(bits > 0){
|
||||
|
||||
byte shift = (byte)(bits % power);
|
||||
|
||||
hexas = hexas.Take<byte>((int)Math.Ceiling(bits / (double)power)).ToList<byte>();
|
||||
if(shift != 0 && hexas.Count > 0)
|
||||
hexas[hexas.Count - 1] &= (byte)((1 << shift) - 1);
|
||||
|
||||
}
|
||||
|
||||
return clean(hexas.ToArray<byte>());
|
||||
}
|
||||
|
||||
public int get_range(int code, int from, int bits = 0){
|
||||
|
||||
get_from_bits(code, ref from, ref bits);
|
||||
|
||||
if(from > 0)
|
||||
code = (code >> from) & ((1 << (31 - from)) - 1);
|
||||
|
||||
if(bits <= 0 || bits >= 31)
|
||||
return code;
|
||||
|
||||
if(from + bits > 31)
|
||||
bits = 31 - from;
|
||||
|
||||
return code & ((1 << bits) - 1);
|
||||
}
|
||||
|
||||
public bool has(string code, int from = 0, int bits = 0){
|
||||
@ -493,14 +510,15 @@ namespace ErrorsManager{
|
||||
List<byte> results = new List<byte>();
|
||||
|
||||
if(bits != 0)
|
||||
error = reset(error, _from, bits);
|
||||
m = (error = reset(error, _from, bits)).Length;
|
||||
if(_from != 0)
|
||||
n = (code = bitwise(code, _from)).Length;
|
||||
l = m > n ? m : n;
|
||||
|
||||
for(int i = 0; i < l; i ++)
|
||||
results.Add((byte)(
|
||||
(i < m ? error[i] : (byte)0) | (i < n ? code[i] : (byte)0)
|
||||
(i < m ? error[i] : (byte)0) |
|
||||
(i < n ? code[i] : (byte)0)
|
||||
));
|
||||
|
||||
return clean(results.ToArray<byte>());
|
||||
@ -566,12 +584,12 @@ namespace ErrorsManager{
|
||||
return dictionaries;
|
||||
}
|
||||
|
||||
public static T get<T>(object keys, object dictionaries, T _default = default(T)){
|
||||
public static T get<T>(object keys, object inputs, T _default = default(T)){
|
||||
|
||||
List<string> keys_list = get_keys(keys);
|
||||
|
||||
if(keys_list.Count != 0)
|
||||
foreach(Dictionary<string, object> dictionary in get_dictionaries(dictionaries))
|
||||
foreach(Dictionary<string, object> dictionary in get_dictionaries(inputs))
|
||||
foreach(string key in keys_list)
|
||||
if(dictionary.TryGetValue(key, out object value) && value is T typed)
|
||||
return typed;
|
||||
|
||||
@ -7,13 +7,13 @@ namespace ErrorsManager{
|
||||
// Tests.errors();
|
||||
// Tests.conversions();
|
||||
// Tests.alphabet();
|
||||
// Tests.bitwise();
|
||||
Tests.bitwise();
|
||||
// Tests.bitwise_sucesive();
|
||||
// Tests.bits();
|
||||
// Tests.reset();
|
||||
// Tests.ranges();
|
||||
// Tests.has();
|
||||
Tests.set();
|
||||
// Tests.set();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
3
VB/ErrorsManager.slnx
Normal file
3
VB/ErrorsManager.slnx
Normal file
@ -0,0 +1,3 @@
|
||||
<Solution>
|
||||
<Project Path="ErrorsManager.vbproj" />
|
||||
</Solution>
|
||||
625
VB/ErrorsManager.vb
Normal file
625
VB/ErrorsManager.vb
Normal file
@ -0,0 +1,625 @@
|
||||
Imports System
|
||||
Imports System.Collections.Generic
|
||||
Imports System.Text
|
||||
Imports System.Text.RegularExpressions
|
||||
Imports System.Linq
|
||||
|
||||
Namespace ErrorsManager
|
||||
Public Class ErrorsManager
|
||||
|
||||
Public Shared ReadOnly ALPHABET As Char() = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/=".ToCharArray()
|
||||
Public Shared ReadOnly ERRORS_MESSAGES As String() = New String(){
|
||||
"invalid_alphabet",
|
||||
"invalid_base",
|
||||
"invalid_alphabet_type",
|
||||
"too_short_alphabet",
|
||||
"repeated_characters_in_alphabet",
|
||||
"too_long_alphabet",
|
||||
"base_lower_than_2",
|
||||
"base_greater_than_128",
|
||||
"base_greater_than_alphabet"
|
||||
}
|
||||
|
||||
Public Shared ReadOnly RE_KEY As New Regex("^[a-zA-Z0-9_]+$", RegexOptions.IgnoreCase Or RegexOptions.Compiled)
|
||||
|
||||
Private _error As Integer = 0
|
||||
Private _alphabet As Char() = New Char(){}
|
||||
Private dictionary As New Dictionary(Of Char, Byte)()
|
||||
Private _base As Byte
|
||||
Private power As Byte
|
||||
|
||||
Public Sub New(Optional inputs As Object = Nothing)
|
||||
|
||||
set_alphabet(
|
||||
ErrorsManager.[get](Of Object)("alphabet", inputs, ALPHABET),
|
||||
ErrorsManager.[get](Of Integer)("base", inputs, 64)
|
||||
)
|
||||
|
||||
end Sub
|
||||
|
||||
Public Function set_alphabet(alphabet As Object, Optional _base As Integer = 64) As Integer
|
||||
|
||||
Dim original_length As Integer
|
||||
|
||||
_error = 0
|
||||
|
||||
If alphabet Is Nothing Then
|
||||
_alphabet = ALPHABET
|
||||
ElseIf TypeOf alphabet Is String Then
|
||||
_alphabet = DirectCast(alphabet, String).ToCharArray()
|
||||
ElseIf TypeOf alphabet Is IEnumerable(Of Char) Then
|
||||
_alphabet = DirectCast(alphabet, IEnumerable(Of Char)).ToArray()
|
||||
Else
|
||||
_error = _error Or (1 << 0)
|
||||
Return _error
|
||||
End If
|
||||
|
||||
original_length = _alphabet.Length
|
||||
|
||||
_alphabet = _alphabet.Distinct().ToArray()
|
||||
|
||||
If _alphabet.Length < 2 Then
|
||||
_error = _error Or (1 << 3)
|
||||
_alphabet = ALPHABET
|
||||
End If
|
||||
If _alphabet.Length <> original_length Then _error = _error Or (1 << 4)
|
||||
If _alphabet.Length > 128 Then _error = _error Or (1 << 5)
|
||||
|
||||
_error = _error Or (
|
||||
If(_base < 2, 1 << 0,
|
||||
If(_base > 128, 1 << 1,
|
||||
If(_base > _alphabet.Length, 1 << 2,
|
||||
0)))) << 6
|
||||
|
||||
If (_error >> 6) = 0 Then _alphabet = _alphabet.Take(_base).ToArray()
|
||||
|
||||
power = CByte(Math.Truncate(Math.Log(_alphabet.Take(128).Count(), 2)))
|
||||
Me._base = CByte(Math.Truncate(Math.Pow(2, power)))
|
||||
_alphabet = _alphabet.Take(Me._base).ToArray()
|
||||
|
||||
dictionary.Clear()
|
||||
For i As Byte = 0 To _alphabet.Length - 1
|
||||
dictionary(_alphabet(i)) = i
|
||||
Next
|
||||
|
||||
Return _error
|
||||
End Function
|
||||
|
||||
Public Function get_alphabet() As String
|
||||
Return New String(_alphabet)
|
||||
End Function
|
||||
|
||||
Public Function to_array(code As String) As Byte()
|
||||
Return code.Select(Function(character) dictionary(character)).ToArray()
|
||||
End Function
|
||||
|
||||
Public Function to_array(code As Byte()) As Byte()
|
||||
Return code
|
||||
End Function
|
||||
|
||||
Public Function to_array(code As Integer) As Byte()
|
||||
|
||||
Dim hexas As New List(Of Byte)()
|
||||
|
||||
While code > 0
|
||||
hexas.Add(CByte(code Mod _base))
|
||||
code \= _base
|
||||
End While
|
||||
|
||||
Return hexas.ToArray()
|
||||
End Function
|
||||
|
||||
Public Function to_string(code As Byte()) As String
|
||||
Return New String(code.Select(Function(hexa) _alphabet(hexa)).ToArray())
|
||||
End Function
|
||||
|
||||
Public Function to_string(code As Integer) As String
|
||||
|
||||
Dim hexas As New StringBuilder()
|
||||
|
||||
While code > 0
|
||||
hexas.Append(_alphabet(code Mod _base))
|
||||
code \= _base
|
||||
End While
|
||||
|
||||
Return hexas.ToString()
|
||||
End Function
|
||||
|
||||
Public Function to_string(code As String) As String
|
||||
Return code
|
||||
End Function
|
||||
|
||||
Public Function to_integer(code As Byte()) As Integer
|
||||
|
||||
Dim _integer As Integer = 0
|
||||
|
||||
For i As Integer = code.Length - 1 To 0 Step -1
|
||||
_integer = _integer * _base + code(i)
|
||||
Next
|
||||
|
||||
Return _integer
|
||||
End Function
|
||||
|
||||
Public Function to_integer(code As String) As Integer
|
||||
|
||||
Dim _integer As Integer = 0
|
||||
|
||||
For i As Integer = code.Length - 1 To 0 Step -1
|
||||
_integer = _integer * _base + dictionary(code(i))
|
||||
Next
|
||||
|
||||
Return _integer
|
||||
End Function
|
||||
|
||||
Public Function to_integer(code As Integer) As Integer
|
||||
Return code
|
||||
End Function
|
||||
|
||||
Public Function to_string_binary(code As Byte()) As String
|
||||
Return String.Concat(code.Reverse().Select(Function(hexa) Convert.ToString(hexa, 2).PadLeft(power, "0"c)))
|
||||
End Function
|
||||
|
||||
Public Function to_string_binary(code As Integer) As String
|
||||
|
||||
Dim binary As String = Convert.ToString(code, 2)
|
||||
Dim remainer As Integer = binary.Length Mod power
|
||||
|
||||
Return If(remainer = 0, binary, binary.PadLeft(binary.Length + power - remainer, "0"c))
|
||||
End Function
|
||||
|
||||
Public Function to_string_binary(code As String) As String
|
||||
Return String.Concat(code.Reverse().Select(Function(character) Convert.ToString(dictionary(character), 2).PadLeft(power, "0"c)))
|
||||
End Function
|
||||
|
||||
Public Function get_bits(code As String) As Integer
|
||||
Return If(code.Length = 0, 0, (code.Length - 1) * power + CInt(Math.Ceiling(Math.Log(dictionary(code.Last()) + 1, 2))))
|
||||
End Function
|
||||
|
||||
Public Function get_bits(code As Byte()) As Integer
|
||||
Return If(code.Length = 0, 0, (code.Length - 1) * power + CInt(Math.Ceiling(Math.Log(code.Last() + 1, 2))))
|
||||
End Function
|
||||
|
||||
Public Function get_bits(code As Integer) As Integer
|
||||
Return CInt(Math.Ceiling(Math.Log(code + 1, 2)))
|
||||
End Function
|
||||
|
||||
Public Sub get_from_bits(code As String, ByRef [from] As Integer, ByRef bits As Integer)
|
||||
get_from_bits(to_array(code), from, bits)
|
||||
End Sub
|
||||
|
||||
Public Sub get_from_bits(code As Byte(), ByRef [from] As Integer, ByRef bits As Integer)
|
||||
If [from] < 0 Then
|
||||
[from] = get_bits(code) + from
|
||||
If [from] < 0 Then [from] = 0
|
||||
End If
|
||||
If bits < 0 Then
|
||||
[from] += bits
|
||||
bits *= -1
|
||||
If [from] < 0 Then
|
||||
bits += [from]
|
||||
[from] = 0
|
||||
End If
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Public Sub get_from_bits(code As Integer, ByRef [from] As Integer, ByRef bits As Integer)
|
||||
get_from_bits(to_array(code), [from], bits)
|
||||
End Sub
|
||||
|
||||
Public Function clean(code As String) As String
|
||||
|
||||
Dim l As Integer = code.Length
|
||||
|
||||
While l > 0 AndAlso code(l - 1) = _alphabet(0)
|
||||
l -= 1
|
||||
End While
|
||||
|
||||
Return (
|
||||
If(code.Length = l, code,
|
||||
If(l = 0, _alphabet(0).ToString(),
|
||||
code.Substring(0, l))))
|
||||
End Function
|
||||
|
||||
Public Function clean(code As Byte()) As Byte()
|
||||
|
||||
Dim l As Integer = code.Length
|
||||
|
||||
While l > 0 AndAlso code(l - 1) = 0
|
||||
l -= 1
|
||||
End While
|
||||
|
||||
Return (
|
||||
If(code.Length = l, code,
|
||||
If(l = 0, New Byte(){0},
|
||||
code.Take(l).ToArray())))
|
||||
End Function
|
||||
|
||||
Public Function clean(code As Integer) As Integer
|
||||
Return code
|
||||
End Function
|
||||
|
||||
Public Function get_range(code As String, [from] As Integer, bits As Integer) As String
|
||||
Return to_string(get_range(to_array(code), [from], bits))
|
||||
End Function
|
||||
|
||||
Public Function get_range(code As Byte(), [from] As Integer, bits As Integer) As Byte()
|
||||
|
||||
Dim hexas As List(Of Byte)
|
||||
|
||||
get_from_bits(code, [from], bits)
|
||||
|
||||
If bits = 0 Then bits = get_bits(code) - [from]
|
||||
If bits <= 0 Then Return New Byte(){}
|
||||
|
||||
hexas = code.ToList()
|
||||
|
||||
If [from] > 0 Then
|
||||
|
||||
Dim shift As Byte = CByte([from] Mod power)
|
||||
Dim mask As Integer = CInt((1 << shift) - 1)
|
||||
|
||||
hexas = hexas.Skip([from] \ power).ToList()
|
||||
If shift <> 0 AndAlso hexas.Count() > 0 Then
|
||||
|
||||
Dim l = hexas.Count() - 2
|
||||
|
||||
For i As Integer = 0 To l
|
||||
hexas(i) = CByte((hexas(i) >> shift) Or ((hexas(i + 1) << (power - shift)) And mask))
|
||||
Next
|
||||
hexas(l + 1) = CByte(hexas(l + 1) >> shift)
|
||||
|
||||
End If
|
||||
|
||||
ElseIf bits > 0 Then
|
||||
|
||||
Dim shift As Byte = CByte(bits Mod power)
|
||||
|
||||
hexas = hexas.Take(CInt(Math.Ceiling(bits / CDbl(power)))).ToList()
|
||||
If shift <> 0 AndAlso hexas.Count() > 0 Then
|
||||
hexas(hexas.Count() - 1) = CByte(hexas.Last() And ((1 << shift) - 1))
|
||||
End If
|
||||
|
||||
End If
|
||||
|
||||
Return clean(hexas.ToArray())
|
||||
End Function
|
||||
|
||||
Public Function get_range(code As Integer, [from] As Integer, bits As Integer) As Integer
|
||||
|
||||
get_from_bits(code, [from], bits)
|
||||
|
||||
If [from] > 0 Then code = (code >> [from]) And ((1 << (31 - [from])) - 1)
|
||||
If bits <= 0 OrElse bits >= 31 Then Return code
|
||||
If [from] + bits > 31 Then bits = 31 - [from]
|
||||
|
||||
Return code And ((1 << bits) - 1)
|
||||
End Function
|
||||
|
||||
Public Function process(code As String, messages As IEnumerable(Of String), Optional blocks As Dictionary(Of Integer, Integer) = Nothing) As (Integer, String)()
|
||||
Return process(to_array(code), messages, blocks)
|
||||
End Function
|
||||
|
||||
Public Function process(code As Byte(), messages As IEnumerable(Of String), Optional blocks As Dictionary(Of Integer, Integer) = Nothing) As (Integer, String)()
|
||||
|
||||
Dim response As new List(Of (Integer, String))()
|
||||
Dim k As Integer = 0
|
||||
Dim l = code.Length * power
|
||||
Dim i As Integer = 0
|
||||
Dim m As Integer = messages.Count()
|
||||
|
||||
If blocks Is Nothing Then blocks = New Dictionary(Of Integer, Integer)()
|
||||
|
||||
While i < l
|
||||
If blocks.ContainsKey(i) AndAlso blocks(i) > 0 Then
|
||||
|
||||
Dim logarithm As Integer = CInt(Math.Log(blocks(i), 2))
|
||||
Dim _k As Integer
|
||||
|
||||
If logarithm = 0 Then logarithm = 1
|
||||
_k = to_integer(get_range(code, i, logarithm))
|
||||
|
||||
If _k > 0 Then
|
||||
_k = k + _k
|
||||
response.Add((_k, If(k < m, messages(k), "")))
|
||||
End If
|
||||
|
||||
k += blocks(i)
|
||||
i += logarithm
|
||||
|
||||
Else
|
||||
If (code(i \ power) And (1 << i Mod power)) <> 0 Then
|
||||
response.Add((k, If(k < m, messages(k), "")))
|
||||
End If
|
||||
k += 1
|
||||
i += 1
|
||||
End If
|
||||
End While
|
||||
|
||||
return response.ToArray()
|
||||
End Function
|
||||
|
||||
Public Function process(code As Integer, messages As IEnumerable(Of String), Optional blocks As Dictionary(Of Integer, Integer) = Nothing) As (Integer, String)()
|
||||
Return process(to_array(code), messages, blocks)
|
||||
End Function
|
||||
|
||||
Public Function bitwise(code As String, bits As Integer) As String
|
||||
Return to_string(get_range(to_array(code), 0, bits))
|
||||
End Function
|
||||
|
||||
Public Function bitwise(code As Byte(), bits As Integer) As Byte()
|
||||
If code.Length = 0 OrElse bits = 0 Then Return code
|
||||
|
||||
Dim shift As Byte = CByte(Math.Abs(bits) Mod power)
|
||||
Dim mask As Integer = _base - 1
|
||||
Dim hexas As List(Of Byte) = code.ToList()
|
||||
|
||||
If bits < 0 Then
|
||||
|
||||
hexas.RemoveRange(0, -bits \ power)
|
||||
|
||||
If shift <> 0 AndAlso hexas.Count() <> 0 Then
|
||||
|
||||
Dim l As Integer = hexas.Count() - 2
|
||||
|
||||
For i As Integer = 0 To l
|
||||
hexas(i) = CByte(((hexas(i) >> shift) Or (hexas(i + 1) << (power - shift))) And mask)
|
||||
Next
|
||||
hexas(hexas.Count - 1) = CByte((hexas.Last() >> shift) And mask)
|
||||
|
||||
End If
|
||||
|
||||
Else
|
||||
|
||||
If shift <> 0 Then
|
||||
|
||||
Dim last_hexa As Byte = hexas.Last() << shift
|
||||
|
||||
For i As Integer = hexas.Count() - 1 To 1 Step -1
|
||||
hexas(i) = CByte(((hexas(i) << shift) And mask) Or (hexas(i - 1) >> (power - shift)))
|
||||
Next
|
||||
hexas(0) = CByte((hexas(0) << shift) And mask)
|
||||
|
||||
If last_hexa >= _base Then hexas.Add(CByte(last_hexa >> power))
|
||||
|
||||
End If
|
||||
|
||||
For i As Integer = bits \ power To 1 Step -1
|
||||
hexas.Insert(0, 0)
|
||||
Next
|
||||
|
||||
End If
|
||||
|
||||
Return clean(hexas.ToArray())
|
||||
End Function
|
||||
|
||||
Public Function bitwise(code As Integer, bits As Integer) As Integer
|
||||
Return (
|
||||
If(bits > 0, code << bits,
|
||||
If(bits < 0, code >> -bits,
|
||||
code)))
|
||||
End Function
|
||||
|
||||
Public Function reset(code As String, [from] As Integer, Optional bits As Integer = 0, Optional reversed As Boolean = False) As String
|
||||
Return to_string(reset(to_array(code), [from], bits, reversed))
|
||||
End Function
|
||||
|
||||
Public Function reset(code As Byte(), [from] As Integer, Optional bits As Integer = 0, Optional reversed As Boolean = False) As Byte()
|
||||
|
||||
Dim hexas As List(Of Byte) = code.ToList()
|
||||
Dim hexa_from As Integer
|
||||
Dim hexa_to As Integer
|
||||
Dim l As Integer
|
||||
Dim from_mask As Byte
|
||||
Dim to_mask As Byte
|
||||
|
||||
get_from_bits(code, [from], bits)
|
||||
hexa_from = [from] \ power
|
||||
hexa_to = ([from] + bits) \ power
|
||||
|
||||
If reversed Then
|
||||
|
||||
l = [from] Mod power
|
||||
from_mask = CByte(((1 << power - l) - 1) << l)
|
||||
to_mask = Cbyte(((1 << ([from] + bits)) - 1) Mod power)
|
||||
|
||||
For i As Integer = 0 To hexas.Count() - 1
|
||||
If i < hexa_from AndAlso i > hexa_to Then hexas(i) = 0
|
||||
Next
|
||||
|
||||
If hexa_from = hexa_to Then
|
||||
hexas(hexa_to) = CByte(hexas(hexa_to) And (from_mask And to_mask))
|
||||
Else
|
||||
hexas(hexa_from) = CByte(hexas(hexa_from) And from_mask)
|
||||
If hexa_to < hexas.Count() Then hexas(hexa_to) = CByte(hexas(hexa_to) And to_mask)
|
||||
End If
|
||||
|
||||
Else
|
||||
|
||||
l = ([from] + bits) Mod power
|
||||
from_mask = CByte((1 << ([from] Mod power)) - 1)
|
||||
to_mask = CByte(((1 << (power - l)) - 1) << l)
|
||||
|
||||
If hexa_from = hexa_to Then
|
||||
hexas(hexa_to) = CByte(hexas(hexa_to) And (from_mask Or to_mask))
|
||||
Else
|
||||
hexas(hexa_from) = CByte(hexas(hexa_from) And from_mask)
|
||||
For i As Integer = hexa_from + 1 To hexa_to - 1
|
||||
If i < hexas.Count() Then hexas(i) = 0
|
||||
Next
|
||||
If hexa_to < hexas.Count() Then hexas(hexa_to) = CByte(hexas(hexa_to) And to_mask)
|
||||
End If
|
||||
|
||||
End If
|
||||
|
||||
Return clean(hexas.ToArray())
|
||||
End Function
|
||||
|
||||
Public Function reset(code As Integer, [from] As Integer, Optional bits As Integer = 0, Optional reversed As Boolean = False) As Integer
|
||||
|
||||
get_from_bits(code, [from], bits)
|
||||
If [from] + bits > 31 Then bits = 31 - [from]
|
||||
|
||||
Return code And (
|
||||
If(reversed, ((1 << bits) - 1) << [from],
|
||||
((1 << get_bits(code)) << [from] + bits Or ((1 << [from]) - 1))))
|
||||
End Function
|
||||
|
||||
Public Function has(code As String, [from] As Integer, bits As Integer) As Boolean
|
||||
Return get_range(to_array(code), [from], bits).Length > 0
|
||||
End Function
|
||||
|
||||
Public Function has(code As Byte(), [from] As Integer, bits As Integer) As Boolean
|
||||
Return get_range(code, [from], bits).Length > 0
|
||||
End Function
|
||||
|
||||
Public Function has(code As Integer, [from] As Integer, bits As Integer) As Boolean
|
||||
Return get_range(code, [from], bits) <> 0
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As String, code As String, Optional [from] As Integer = 0, Optional bits As Integer = 0) As String
|
||||
Return to_string([set](to_array([error]), to_array(code), [from], bits))
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As String, code As Byte(), Optional [from] As Integer = 0, Optional bits As Integer = 0) As String
|
||||
Return to_string([set](to_array([error]), code, [from], bits))
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As String, code As Integer, Optional [from] As Integer = 0, Optional bits As Integer = 0) As String
|
||||
Return to_string([set](to_array([error]), to_array(code), [from], bits))
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As Byte(), code As String, Optional [from] As Integer = 0, Optional bits As Integer = 0) As Byte()
|
||||
Return [set]([error], to_array(code), [from], bits)
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As Byte(), code As Byte(), Optional [from] As Integer = 0, Optional bits As Integer = 0) As Byte()
|
||||
|
||||
Dim l As Integer
|
||||
Dim m As Integer
|
||||
Dim n As Integer
|
||||
Dim results As New List(Of Byte)()
|
||||
|
||||
If bits <> 0 Then
|
||||
[error] = reset([error], [from], bits)
|
||||
m = [error].Length
|
||||
End If
|
||||
If [from] <> 0 Then
|
||||
code = bitwise(code, [from])
|
||||
n = code.Length
|
||||
End If
|
||||
l = If(m > n, m, n)
|
||||
|
||||
For i As Integer = 0 To l - 1
|
||||
results.Add(CByte(
|
||||
(If(i < m, [error](i), CByte(0))) Or
|
||||
(If(i < n, code(i), CByte(0)))
|
||||
))
|
||||
Next
|
||||
|
||||
Return clean(results.ToArray())
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As Byte(), code As Integer, Optional [from] As Integer = 0, Optional bits As Integer = 0) As Byte()
|
||||
Return [set]([error], to_array(code), [from], bits)
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As Integer, code As String, Optional [from] As Integer = 0, Optional bits As Integer = 0) As Integer
|
||||
Return [set]([error], to_integer(code), [from], bits)
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As Integer, code As Byte(), Optional [from] As Integer = 0, Optional bits As Integer = 0) As Integer
|
||||
Return [set]([error], to_integer(code), [from], bits)
|
||||
End Function
|
||||
|
||||
Public Function [set]([error] As Integer, code As Integer, Optional [from] As Integer = 0, Optional bits As Integer = 0) As Integer
|
||||
|
||||
If bits <> 0 Then [error] = reset([error], [from], bits)
|
||||
If [from] <> 0 Then [error] = bitwise([error], [from])
|
||||
|
||||
Return [error] Or code
|
||||
End Function
|
||||
|
||||
Public Shared Function get_keys(items As Object) As String()
|
||||
|
||||
Dim keys As New List(Of String)()
|
||||
|
||||
If TypeOf items Is String Then
|
||||
If RE_KEY.IsMatch(DirectCast(items, String)) Then keys.Add(DirectCast(items, String))
|
||||
ElseIf TypeOf items Is String() Then
|
||||
For Each item As String In DirectCast(items, String())
|
||||
If RE_KEY.IsMatch(item) AndAlso Not keys.Contains(item) Then keys.Add(item)
|
||||
Next
|
||||
ElseIf TypeOf items Is IEnumerable(Of Object) Then
|
||||
For Each item As Object In DirectCast(items, IEnumerable(Of Object))
|
||||
If item Is Nothing Then Continue For
|
||||
If TypeOf item Is String Then
|
||||
If RE_KEY.IsMatch(DirectCast(item, String)) AndAlso Not keys.Contains(DirectCast(item, String)) Then keys.Add(DirectCast(item, String))
|
||||
Else
|
||||
For Each key As String In get_keys(item)
|
||||
If Not keys.Contains(key) Then keys.Add(key)
|
||||
Next
|
||||
End If
|
||||
Next
|
||||
End If
|
||||
|
||||
Return keys.ToArray()
|
||||
End Function
|
||||
|
||||
Public Shared Function get_dictionaries(items As Object) As List(Of Dictionary(Of String, Object))
|
||||
|
||||
Dim dictionaries As New List(Of Dictionary(Of String, Object))()
|
||||
|
||||
If TypeOf items Is Dictionary(Of String, Object) Then
|
||||
dictionaries.Add(DirectCast(items, Dictionary(Of String, Object)))
|
||||
ElseIf TypeOf items Is IEnumerable(Of Object) Then
|
||||
For Each item As Object In DirectCast(items, IEnumerable(Of Object))
|
||||
dictionaries.AddRange(get_dictionaries(item))
|
||||
Next
|
||||
End If
|
||||
|
||||
Return dictionaries
|
||||
End Function
|
||||
|
||||
Public Shared Function [get](Of T)(keys As Object, inputs As Object, Optional _default As T = Nothing) As T
|
||||
|
||||
Dim keys_list As String() = get_keys(keys)
|
||||
|
||||
If keys_list.Length <> 0 Then
|
||||
For Each dictionary As Dictionary(Of String, Object) In get_dictionaries(inputs)
|
||||
For Each key As String In keys_list
|
||||
|
||||
Dim value As Object = Nothing
|
||||
|
||||
If dictionary.TryGetValue(key, value) AndAlso TypeOf value Is T Then Return DirectCast(value, T)
|
||||
|
||||
Next
|
||||
Next
|
||||
End If
|
||||
Return _default
|
||||
End Function
|
||||
|
||||
Public Shared Function Unique(Of T)(items As IEnumerable(Of T)) As T()
|
||||
Return items.Distinct().ToArray()
|
||||
End Function
|
||||
|
||||
Public Shared Function is_array(value As Object) As Boolean
|
||||
Return TypeOf value Is Array
|
||||
End Function
|
||||
|
||||
Public Shared Function is_integer(value As Object) As Boolean
|
||||
Return TypeOf value Is Integer
|
||||
End Function
|
||||
|
||||
Public Shared Sub for_each_enumerate(Of T)(items As IEnumerable(Of T), action As Action(Of T, Integer))
|
||||
|
||||
Dim index As Integer = 0
|
||||
|
||||
For Each item As T In items
|
||||
action(item, index)
|
||||
index += 1
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
End Namespace
|
||||
17
VB/ErrorsManager.vbproj
Executable file
17
VB/ErrorsManager.vbproj
Executable file
@ -0,0 +1,17 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<!-- <OutputType>Library</OutputType> -->
|
||||
<TargetFrameworks>net10.0;net462</TargetFrameworks>
|
||||
<!-- <Nullable>enable</Nullable> -->
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>ErrorsManager</RootNamespace>
|
||||
<AssemblyName>ErrorsManager</AssemblyName>
|
||||
<EnableDefaultCompileItems>false</EnableDefaultCompileItems>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ErrorsManager.vb" />
|
||||
<Compile Include="Program.vb" />
|
||||
<Compile Include="Tests.vb" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
20
VB/Program.vb
Executable file
20
VB/Program.vb
Executable file
@ -0,0 +1,20 @@
|
||||
Imports System
|
||||
|
||||
Namespace ErrorsManager
|
||||
Class Program
|
||||
Public Shared Sub Main(args As String())
|
||||
|
||||
' Tests.errors()
|
||||
' Tests.conversions()
|
||||
' Tests.alphabet()
|
||||
Tests.bitwise()
|
||||
' Tests.bitwise_sucesive()
|
||||
' Tests.bits()
|
||||
' Tests.reset()
|
||||
' Tests.ranges()
|
||||
' Tests.has()
|
||||
' Tests.set()
|
||||
|
||||
End Sub
|
||||
End Class
|
||||
End Namespace
|
||||
299
VB/Tests.vb
Normal file
299
VB/Tests.vb
Normal file
@ -0,0 +1,299 @@
|
||||
Imports System
|
||||
|
||||
Namespace ErrorsManager
|
||||
|
||||
Class FullError
|
||||
|
||||
Public _integer As Integer
|
||||
Public _string As String
|
||||
Public array As Byte()
|
||||
|
||||
Public Sub New(errors As ErrorsManager, _integer As Integer)
|
||||
Me._integer = _integer
|
||||
_string = errors.to_string(_integer)
|
||||
array = errors.to_array(_integer)
|
||||
End Sub
|
||||
|
||||
Public Sub New(errors As ErrorsManager, _string As String)
|
||||
_integer = errors.to_integer(_string)
|
||||
Me._string = _string
|
||||
array = errors.to_array(_string)
|
||||
End Sub
|
||||
|
||||
Public Sub New(errors As ErrorsManager, array As Byte())
|
||||
_integer = errors.to_integer(array)
|
||||
_string = errors.to_string(array)
|
||||
Me.array = array
|
||||
End Sub
|
||||
|
||||
Public Shared Function print(array As Byte()) As String
|
||||
Return "[" & String.Join(", ", array) & "]"
|
||||
End Function
|
||||
|
||||
End Class
|
||||
|
||||
Class Tests
|
||||
|
||||
Public Shared Sub errors()
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim [error] As New FullError(errors, 217934237)
|
||||
Dim reset As New FullError(errors, 0)
|
||||
|
||||
reset._integer = errors.reset([error]._integer, -5, 12)
|
||||
reset._string = errors.reset([error]._string, -5, 12)
|
||||
reset.array = errors.reset([error].array, -5, 12)
|
||||
|
||||
Console.WriteLine($"RESET: from {-5} bits {12}")
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary([error]._integer)} - {errors.to_string_binary(reset._integer)}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary([error]._string)} - {errors.to_string_binary(reset._string)}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary([error].array)} - {errors.to_string_binary(reset.array)}")
|
||||
Console.WriteLine()
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Function conversions(Optional tests As Integer = 10, Optional inputs As Object = Nothing) As ErrorsManager
|
||||
|
||||
Dim errors As New ErrorsManager(inputs)
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << 16))
|
||||
|
||||
Console.WriteLine($"INTEGER: {errors.to_integer([error]._integer)}, {errors.to_integer([error]._string)}, {errors.to_integer([error].array)}")
|
||||
Console.WriteLine($"STRING: {errors.to_string([error]._integer)}, {errors.to_string([error]._string)}, {errors.to_string([error].array)}")
|
||||
Console.WriteLine($"ARRAY: {FullError.print(errors.to_array([error]._integer))}, {FullError.print(errors.to_array([error]._string))}, {FullError.print(errors.to_array([error].array))}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
Return errors
|
||||
End Function
|
||||
|
||||
Public Shared Sub alphabet(Optional tests As Integer = 10)
|
||||
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim errors As ErrorsManager = conversions(1, New Dictionary(Of String, Object)() From {
|
||||
{"base", seed.Next(2, 128)}
|
||||
})
|
||||
|
||||
Console.WriteLine($"^^^ ALPHABET: {String.Join(", ", errors.get_alphabet())} ^^^")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub bitwise(Optional tests As Integer = 10)
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << seed.Next(0, 16)))
|
||||
Dim shifted As New FullError(errors, 0)
|
||||
Dim unshifted As New FullError(errors, 0)
|
||||
Dim bitwise As Integer = 10 - seed.Next(0, 20)
|
||||
|
||||
Console.WriteLine($"BITWISE: {bitwise}")
|
||||
|
||||
shifted._integer = errors.bitwise([error]._integer, bitwise)
|
||||
shifted._string = errors.bitwise([error]._string, bitwise)
|
||||
shifted.array = errors.bitwise([error].array, bitwise)
|
||||
unshifted._integer = errors.bitwise(shifted._integer, -bitwise)
|
||||
unshifted._string = errors.bitwise(shifted._string, -bitwise)
|
||||
unshifted.array = errors.bitwise(shifted.array, -bitwise)
|
||||
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary([error]._integer)} - {[error]._integer}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary([error]._string)} - {[error]._string}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary([error].array)} - {FullError.print([error].array)}")
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary(shifted._integer)} - {shifted._integer}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary(shifted._string)} - {shifted._string}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary(shifted.array)} - {FullError.print(shifted.array)}")
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary(unshifted._integer)} - {unshifted._integer}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary(unshifted._string)} - {unshifted._string}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary(unshifted.array)} - {FullError.print(unshifted.array)}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub bitwise_sucesive(Optional tests As Integer = 10)
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim [error] As New FullError(errors, New Random().Next(0, 1 << 16))
|
||||
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary([error]._integer)} - {[error]._integer}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary([error]._string)} - {[error]._string}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary([error].array)} - {FullError.print([error].array)}")
|
||||
Console.WriteLine()
|
||||
|
||||
For i As Integer = -tests To tests - 1
|
||||
|
||||
Dim shifted As New FullError(errors, 0)
|
||||
|
||||
Console.WriteLine($"BITWISE: {i}")
|
||||
|
||||
shifted._integer = errors.bitwise([error]._integer, i)
|
||||
shifted._string = errors.bitwise([error]._string, i)
|
||||
shifted.array = errors.bitwise([error].array, i)
|
||||
|
||||
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary(shifted._integer)} - {shifted._integer}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary(shifted._string)} - {shifted._string}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary(shifted.array)} - {FullError.print(shifted.array)}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub bits(Optional tests As Integer = 10)
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << seed.Next(0, 28)))
|
||||
Dim from_value As Integer = seed.Next(-15, 15)
|
||||
Dim bits_value As Integer = seed.Next(-13, 13)
|
||||
Dim from As Integer() = {from_value, from_value, from_value}
|
||||
Dim bits As Integer() = {bits_value, bits_value, bits_value}
|
||||
|
||||
errors.get_from_bits([error]._integer, from(0), bits(0))
|
||||
errors.get_from_bits([error]._string, from(1), bits(1))
|
||||
errors.get_from_bits([error].array, from(2), bits(2))
|
||||
|
||||
|
||||
Console.WriteLine($"CODE: {[error]._integer} - {[error]._string} - {FullError.print([error].array)}")
|
||||
Console.WriteLine($"ERROR: {errors.to_string_binary([error]._integer)} - {errors.get_bits([error]._integer)} - {errors.get_bits([error]._string)} - {errors.get_bits([error].array)}")
|
||||
Console.WriteLine($"FROM: {from_value} - {from(0)}, {from(1)}, {from(2)}")
|
||||
Console.WriteLine($"BITS: {bits_value} - {bits(0)}, {bits(1)}, {bits(2)}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub reset(Optional tests As Integer = 10)
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << 28))
|
||||
Dim [from] As Integer = seed.Next(-15, 15)
|
||||
Dim bits As Integer = seed.Next(-13, 13)
|
||||
Dim reset As New FullError(errors, 0)
|
||||
|
||||
reset._integer = errors.reset([error]._integer, [from], bits)
|
||||
reset._string = errors.reset([error]._string, [from], bits)
|
||||
reset.array = errors.reset([error].array, [from], bits)
|
||||
|
||||
Console.WriteLine($"RESET: from {[from]} bits {bits}")
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary([error]._integer)} - {errors.to_string_binary(reset._integer)}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary([error]._string)} - {errors.to_string_binary(reset._string)}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary([error].array)} - {errors.to_string_binary(reset.array)}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub ranges(Optional tests As Integer = 10)
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << 28))
|
||||
Dim [from] As Integer = seed.Next(-15, 15)
|
||||
Dim bits As Integer = seed.Next(-13, 13)
|
||||
Dim range As New FullError(errors, 0)
|
||||
|
||||
Console.WriteLine($"RANGE: from {[from]} bits {bits}")
|
||||
errors.get_from_bits([error]._string, [from], bits)
|
||||
Console.WriteLine($"REAL: from {[from]} bits {bits}")
|
||||
|
||||
range._integer = errors.get_range([error]._integer, [from], bits)
|
||||
range._string = errors.get_range([error]._string, [from], bits)
|
||||
range.array = errors.get_range([error].array, [from], bits)
|
||||
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary([error]._integer)} - {errors.to_string_binary(range._integer)}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary([error]._string)} - {errors.to_string_binary(range._string)}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary([error].array)} - {errors.to_string_binary(range.array)}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub has(Optional tests As Integer = 10)
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << 28))
|
||||
Dim [from] As Integer = seed.Next(-15, 15)
|
||||
Dim bits As Integer = seed.Next(-13, 13)
|
||||
|
||||
Console.WriteLine($"HAS: from {[from]} bits {bits}")
|
||||
Console.WriteLine($"INTEGER: {errors.has([error]._integer, [from], bits)} - {errors.to_string_binary([error]._integer)} - {errors.to_string_binary(errors.get_range([error]._integer, [from], bits))}")
|
||||
Console.WriteLine($"STRING: {errors.has([error]._string, [from], bits)} - {errors.to_string_binary([error]._string)} - {errors.to_string_binary(errors.get_range([error]._string, [from], bits))}")
|
||||
Console.WriteLine($"ARRAY: {errors.has([error].array, [from], bits)} - {errors.to_string_binary([error].array)} - {errors.to_string_binary(errors.get_range([error].array, [from], bits))}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
Public Shared Sub [set](Optional tests As Integer = 10)
|
||||
|
||||
Dim errors As New ErrorsManager()
|
||||
Dim seed As New Random()
|
||||
|
||||
For i As Integer = 0 To tests - 1
|
||||
|
||||
Dim [error] As New FullError(errors, seed.Next(0, 1 << 15))
|
||||
Dim value As New FullError(errors, seed.Next(0, 1 << 15))
|
||||
Dim [from] As Integer = seed.Next(-15, 15)
|
||||
Dim bits As Integer = seed.Next(-13, 13)
|
||||
Dim set_integer As New FullError(errors, 0)
|
||||
Dim set_string As New FullError(errors, 0)
|
||||
Dim set_array As New FullError(errors, 0)
|
||||
|
||||
set_integer._integer = errors.set([error]._integer, value._integer, [from], bits)
|
||||
set_string._integer = errors.set([error]._integer, value._string, [from], bits)
|
||||
set_array._integer = errors.set([error]._integer, value.array, [from], bits)
|
||||
set_integer._string = errors.set([error]._string, value._integer, [from], bits)
|
||||
set_string._string = errors.set([error]._string, value._string, [from], bits)
|
||||
set_array._string = errors.set([error]._string, value.array, [from], bits)
|
||||
set_integer.array = errors.set([error].array, value._integer, [from], bits)
|
||||
set_string.array = errors.set([error].array, value._string, [from], bits)
|
||||
set_array.array = errors.set([error].array, value.array, [from], bits)
|
||||
|
||||
Console.WriteLine($"SET: from {[from]} bits {bits} to {value}")
|
||||
Console.WriteLine($"ERROR: {errors.to_string_binary([error]._integer)} - VALUE: {errors.to_string_binary(value._integer)}")
|
||||
Console.WriteLine($"INTEGER: {errors.to_string_binary(set_integer._integer)} - {errors.to_string_binary(set_integer._string)} - {errors.to_string_binary(set_integer.array)}")
|
||||
Console.WriteLine($"STRING: {errors.to_string_binary(set_string._integer)} - {errors.to_string_binary(set_string._string)} - {errors.to_string_binary(set_string.array)}")
|
||||
Console.WriteLine($"ARRAY: {errors.to_string_binary(set_array._integer)} - {errors.to_string_binary(set_array._string)} - {errors.to_string_binary(set_array.array)}")
|
||||
Console.WriteLine()
|
||||
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
|
||||
End Namespace
|
||||
Loading…
Reference in New Issue
Block a user