Методы расширения FillRandom для массива и матрицы
Метод a.RandomElement
This commit is contained in:
Mikhalkovich Stanislav 2020-03-12 19:41:09 +03:00
parent c6186a6b9f
commit d31977a285
10 changed files with 105 additions and 20 deletions

View file

@ -15,7 +15,7 @@ internal static class RevisionClass
public const string Major = "3"; public const string Major = "3";
public const string Minor = "6"; public const string Minor = "6";
public const string Build = "2"; public const string Build = "2";
public const string Revision = "2379"; public const string Revision = "2383";
public const string MainVersion = Major + "." + Minor; public const string MainVersion = Major + "." + Minor;
public const string FullVersion = Major + "." + Minor + "." + Build + "." + Revision; public const string FullVersion = Major + "." + Minor + "." + Build + "." + Revision;

View file

@ -1,4 +1,4 @@
%MINOR%=6 %MINOR%=6
%REVISION%=2379 %REVISION%=2383
%COREVERSION%=2 %COREVERSION%=2
%MAJOR%=3 %MAJOR%=3

View file

@ -1 +1 @@
3.6.2.2379 3.6.2.2383

View file

@ -1 +1 @@
!define VERSION '3.6.2.2379' !define VERSION '3.6.2.2383'

View file

@ -10610,6 +10610,22 @@ begin
Self[i, j] := f(i, j); Self[i, j] := f(i, j);
end; end;
/// Заполняет элементы двумерного массива случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array [,] of integer; a,b: integer); extensionmethod;
begin
for var i := 0 to Self.RowCount - 1 do
for var j := 0 to Self.ColCount - 1 do
Self[i, j] := Random(a,b);
end;
/// Заполняет элементы двумерного массива случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array [,] of real; a,b: real); extensionmethod;
begin
for var i := 0 to Self.RowCount - 1 do
for var j := 0 to Self.ColCount - 1 do
Self[i, j] := a + Random*(b-a);
end;
/// Применяет действие к каждому элементу двумерного массива /// Применяет действие к каждому элементу двумерного массива
procedure &ForEach<T>(Self: array [,] of T; act: T -> ()); extensionmethod; procedure &ForEach<T>(Self: array [,] of T; act: T -> ()); extensionmethod;
begin begin
@ -10787,6 +10803,28 @@ end;
// Статические методы - в методы расширения: BinarySearch, ConvertAll, Find, FindIndex, FindAll, // Статические методы - в методы расширения: BinarySearch, ConvertAll, Find, FindIndex, FindAll,
// FindLast, FindLastIndex, IndexOf, Contains, LastIndexOf, Reverse, Sort // FindLast, FindLastIndex, IndexOf, Contains, LastIndexOf, Reverse, Sort
// Дополнения март 2020: RandomElement, FillRandom
/// Заполняет массив случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array of integer; a,b: integer); extensionmethod;
begin
for var i:=0 to Self.Length-1 do
Self[i] := Random(a,b);
end;
/// Заполняет массив случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array of real; a,b: real); extensionmethod;
begin
for var i:=0 to Self.Length-1 do
Self[i] := a + Random*(b-a);
end;
/// Возвращает случайный элемент массива
function RandomElement<T>(Self: array of T): T; extensionmethod;
begin
Result := Self[Random(Self.Length)];
end;
/// Перемешивает элементы массива случайным образом /// Перемешивает элементы массива случайным образом
function Shuffle<T>(Self: array of T): array of T; extensionmethod; function Shuffle<T>(Self: array of T): array of T; extensionmethod;
begin begin

View file

@ -0,0 +1,9 @@
function f0(x: byte) := x;
procedure p1(o: object) := exit;
procedure p1(o: word) := exit;
begin
p1(f0);
Assert(True)
end.

View file

@ -1262,6 +1262,8 @@ namespace PascalABCCompiler.TreeConverter
} }
else if (sem_type.IsEnum) else if (sem_type.IsEnum)
return new PascalABCCompiler.SyntaxTree.named_type_reference(get_idents_from_dot_string(sem_type.name, sem_type.location), sem_type.location); return new PascalABCCompiler.SyntaxTree.named_type_reference(get_idents_from_dot_string(sem_type.name, sem_type.location), sem_type.location);
else if (sem_type is compiled_type_node ctn1 && ctn1.compiled_type == typeof(System.IntPtr))
return new PascalABCCompiler.SyntaxTree.named_type_reference(get_idents_from_dot_string("System.IntPtr", sem_type.location), sem_type.location);
else else
return new PascalABCCompiler.SyntaxTree.named_type_reference(get_idents_from_dot_string(sem_type.PrintableName, sem_type.location), sem_type.location); return new PascalABCCompiler.SyntaxTree.named_type_reference(get_idents_from_dot_string(sem_type.PrintableName, sem_type.location), sem_type.location);
} }

View file

@ -1153,23 +1153,21 @@ namespace PascalABCCompiler.TreeConverter
} }
else else
{ {
if (is_alone_method_defined) //issue #2161 - SSM 12.03.2020
//issue #348
if (formal_param_type == SystemLibrary.SystemLibrary.object_type && factparams[i].type is delegated_methods)
{ {
//issue #348 possible_type_convertions ptci = new possible_type_convertions();
if (formal_param_type == SystemLibrary.SystemLibrary.object_type && factparams[i].type is delegated_methods) ptci.first = null;
{ ptci.second = null;
possible_type_convertions ptci = new possible_type_convertions(); ptci.from = factparams[i].type;
ptci.first = null; ptci.to = formal_param_type;
ptci.second = null; tc.AddElement(ptci);
ptci.from = factparams[i].type; factparams[i] = syntax_tree_visitor.CreateDelegateCall((factparams[i].type as delegated_methods).proper_methods[0]);
ptci.to = formal_param_type; return tc;
tc.AddElement(ptci);
factparams[i] = syntax_tree_visitor.CreateDelegateCall((factparams[i].type as delegated_methods).proper_methods[0]);
return tc;
}
else
error = new CanNotConvertTypes(factparams[i], factparams[i].type, formal_param_type, locg);
} }
if (is_alone_method_defined) // если мы сюда попали, то ошибка более явная
error = new CanNotConvertTypes(factparams[i], factparams[i].type, formal_param_type, locg);
return null; return null;
} }

View file

@ -382,7 +382,7 @@ namespace PascalABCCompiler.TreeRealization
{ {
get get
{ {
return full_name; return name;
} }
} }

View file

@ -10610,6 +10610,22 @@ begin
Self[i, j] := f(i, j); Self[i, j] := f(i, j);
end; end;
/// Заполняет элементы двумерного массива случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array [,] of integer; a,b: integer); extensionmethod;
begin
for var i := 0 to Self.RowCount - 1 do
for var j := 0 to Self.ColCount - 1 do
Self[i, j] := Random(a,b);
end;
/// Заполняет элементы двумерного массива случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array [,] of real; a,b: real); extensionmethod;
begin
for var i := 0 to Self.RowCount - 1 do
for var j := 0 to Self.ColCount - 1 do
Self[i, j] := a + Random*(b-a);
end;
/// Применяет действие к каждому элементу двумерного массива /// Применяет действие к каждому элементу двумерного массива
procedure &ForEach<T>(Self: array [,] of T; act: T -> ()); extensionmethod; procedure &ForEach<T>(Self: array [,] of T; act: T -> ()); extensionmethod;
begin begin
@ -10787,6 +10803,28 @@ end;
// Статические методы - в методы расширения: BinarySearch, ConvertAll, Find, FindIndex, FindAll, // Статические методы - в методы расширения: BinarySearch, ConvertAll, Find, FindIndex, FindAll,
// FindLast, FindLastIndex, IndexOf, Contains, LastIndexOf, Reverse, Sort // FindLast, FindLastIndex, IndexOf, Contains, LastIndexOf, Reverse, Sort
// Дополнения март 2020: RandomElement, FillRandom
/// Заполняет массив случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array of integer; a,b: integer); extensionmethod;
begin
for var i:=0 to Self.Length-1 do
Self[i] := Random(a,b);
end;
/// Заполняет массив случайными значениями в диапазоне от a до b
procedure FillRandom(Self: array of real; a,b: real); extensionmethod;
begin
for var i:=0 to Self.Length-1 do
Self[i] := a + Random*(b-a);
end;
/// Возвращает случайный элемент массива
function RandomElement<T>(Self: array of T): T; extensionmethod;
begin
Result := Self[Random(Self.Length)];
end;
/// Перемешивает элементы массива случайным образом /// Перемешивает элементы массива случайным образом
function Shuffle<T>(Self: array of T): array of T; extensionmethod; function Shuffle<T>(Self: array of T): array of T; extensionmethod;
begin begin