软件的基本是要处理好”算法“及其基础(二)delphi系统原子函数及方法

软件的基本是要处理好”算法“及其基础(二)delphi系统原子函数及方法

 

        如果你没有深入到delphi的内心深处,可能你不常触及这些内容。本文将不断更新增加内容,敬请关注收藏......

序号函数、方法或记录作用引用单元
001Trunc(ADouble:Double)提取浮点类型的整数部分System.pas
 Frac(ADouble:Double)提取浮点类型的小数部分 
 Round(X: Single):Longint;返回:四舍六入五留双的序数 
002 序数相关System.pas
 Ord(var X):LongInt返回有序的数据类型参数对应的值的集合中的序数,不用Int64参数,返回值范围(0..MaxInt) 
 Low(var X):LongInt返回有序的数据类型参数对应的值的集合中的最小序数:(0..MaxInt) 
 High(var X):LongInt返回有序的数据类型参数对应的值的集合中的最大序数:(0..MaxInt) 
 Pred(var X):LongInt;  返回有序的数据类型参数对应的值的集合中的当前序数的前一个序数:(0..High(var X)-1) 
 Succ(var X):LongInt;   返回有序的数据类型参数对应的值的集合中的当前序数的后一个序数:(0..High(var X)-1) 
 Odd(AInt:Integer):Boolean;若参数为奇数返回真System.pas
 Sizeof(var X):MaxInt;类型或对象所占的内存字节数System.pas
003Inc(AInt:Integer; Incre:Integer)将整数AInt原子增加IncreSystem.pas
004Dec(AInt:Integer; Decre:Integer)将整数AInt原子减少DecreSystem.pas
005TGUID记录全局唯一标识符,可由记录类方法产生或其类操作进行比较System.pas
 UpCase(AString:String):String将字符串AString大写后返回 
 LowerCase(AString:String):String将字符串AString小写后返回 
 Copy ( Source : string; StartChar, Count : Integer ) : string;因涉及多字节字符可能返回乱码,请慎用请用System.SysUtils.TStringHelper 
 Chr(X: Byte): Char;返回字节X( (0..255),即UInt8)对应的ASCii码表的码值 
    
006TNotifyEvent标准事件Type TNotifyEvent = procedure(Sender: TObject) of object;System.Classes.pas
007TGetStrProc标准事件Type TGetStrProc = procedure(const S: string) of object;System.Classes.pas
008

  TProc

  TProc<T>

  TProc<T1,T2>

  TProc<T1,T2,T3>

  TProc<T1,T2,T3,T4>

 

  TFunc<TResult>

  TFunc<T,TResult>

  TFunc<T1,T2,TResult>

  TFunc<T1,T2,T3,TResult>

  TFunc<T1,T2,T3,T4,TResult>

 

  TPredicate<T>

 

 

 

通用匿名方法描述句法:

type
  TProc = reference to procedure;
  TProc<T> = reference to procedure (Arg1: T);
  TProc<T1,T2> = reference to procedure (Arg1: T1; Arg2: T2);
  TProc<T1,T2,T3> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3);
  TProc<T1,T2,T3,T4> = reference to procedure (Arg1: T1; Arg2: T2; Arg3: T3; Arg4: T4);

  TFunc<TResult> = reference to function: TResult;
  TFunc<T,TResult> = reference to function (Arg1: T): TResult;
  TFunc<T1,T2,TResult> = reference to function (Arg1: T1; Arg2: T2): TResult;
  TFunc<T1,T2,T3,TResult> = reference to function (Arg1: T1; Arg2: T2; Arg3: T3): TResult;
  TFunc<T1,T2,T3,T4,TResult> = reference to function (Arg1: T1; Arg2: T2; Arg3: T3; Arg4: T4): TResult;

  TPredicate<T> = reference to function (Arg1: T): Boolean;

 

案例:匿名函数的定义与赋值:constructor TStringList.Create;  //其中:

var
  CompareStringsP: function (const S1, S2: string): Integer of object;
  GetObjectP: function (Index: Integer): TObject of object;

  CompareStringsP := CompareStrings;

  GetObjectP := GetObject;  //其中:

function TStringList.CompareStrings(const S1, S2: string): Integer;
begin
  if UseLocale then
    if CaseSensitive then
      Result := AnsiCompareStr(S1, S2)
    else
      Result := AnsiCompareText(S1, S2)
  else
    if CaseSensitive then
      Result := CompareStr(S1, S2)
    else
      Result := CompareText(S1, S2);
end;

function TStringList.GetObject(Index: Integer): TObject;
begin
  if Cardinal(Index) >= Cardinal(FCount) then
    Error(@SListIndexError, Index);
  Result := FList[Index].FObject;
end;

案例:匿名过程的定义与赋值:

var LProc :TProc;   //:匿名过程的声明

LProc := Procedure

  var LProc_1:string; LProc_2:Integer; //:匿名过程内部的变量......

begin

    //......

end;       //:匿名过程的定义与赋值

//又如:

var LStr:string; LInt:Integer;
      LProc:TProc<string,Integer>;   //:匿名过程的声明

begin

  LStr:=''; LInt:=-1;
  LProc:=Procedure(AStr:string; AInt:Integer)  
    var LProc_1:string; LProc_2:Integer;//:匿名过程内部的变量......
  begin
      LProc_1:=AStr;  LProc_2:=AInt;
      ShowMessage(LProc_1 + LProc_2.toString);  //:UI输出结果
  end;     //:匿名过程的定义与赋值
  LProc(LStr,LInt);  //:传参(虚实替换)并执行上面赋值的匿名过程

end;

System.SysUtils.pas
009Include

常量集合(常量数组)的赋值:案例:constructor TStringList.Create;

Include(FOverridden, sloCompareStrings);  //其中:

FOverridden: TOverridden = set of (sloCompareStrings, sloGetObject);

System.Classes.pas
010TMethod

方法记录(代码、返回数据)  及方法记录间的比较:

PMethod = ^TMethod;
  TMethod = record
    Code, Data: Pointer;
  public
    class operator Equal(const Left, Right: TMethod): Boolean; inline;
    class operator NotEqual(const Left, Right: TMethod): Boolean; inline;
    class operator GreaterThan(const Left, Right: TMethod): Boolean; inline;
    class operator GreaterThanOrEqual(const Left, Right: TMethod): Boolean; inline;
    class operator LessThan(const Left, Right: TMethod): Boolean; inline;
    class operator LessThanOrEqual(const Left, Right: TMethod): Boolean; inline;
  end;

案例:constructor TStringList.Create;

var
  CompareStringsP: function (const S1, S2: string): Integer of object;
  GetObjectP: function (Index: Integer): TObject of object;

  if TMethod(CompareStringsP).Code <> @TStringList.CompareStrings then
    Include(FOverridden, sloCompareStrings);

  if TMethod(GetObjectP).Code <> @TStringList.GetObject then
    Include(FOverridden, sloGetObject);  //其中:

function TStringList.CompareStrings(const S1, S2: string): Integer;
begin
  if UseLocale then
    if CaseSensitive then
      Result := AnsiCompareStr(S1, S2)
    else
      Result := AnsiCompareText(S1, S2)
  else
    if CaseSensitive then
      Result := CompareStr(S1, S2)
    else
      Result := CompareText(S1, S2);
end;

function TStringList.GetObject(Index: Integer): TObject;
begin
  if Cardinal(Index) >= Cardinal(FCount) then
    Error(@SListIndexError, Index);
  Result := FList[Index].FObject;
end;

System.pas
011CompareStr等各类型比较

字符串比较函数://其它自己去看

function CompareStr(const S1, S2: string): Integer;

System.SysUtils

//运行时刻库

//非原子单元,

//但很有用

//顺便提及

012TComparer

其中泛型比较类及其接口://下面是常见的简单的,其它Singleton及其子类客制化的自己去看

//uses System.SysUtils, System.TypInfo;

type
  IComparer<T> = interface
    function Compare(const Left, Right: T): Integer;
  end;

  IEqualityComparer<T> = interface
    function Equals(const Left, Right: T): Boolean;
    function GetHashCode(const Value: T): Integer;
  end;

  TComparison<T> = reference to function(const Left, Right: T): Integer;

  // Abstract base class for IComparer<T> implementations, and a provider
  // of default IComparer<T> implementations.
  TComparer<T> = class(TInterfacedObject, IComparer<T>)
  public
    class function Default: IComparer<T>;
    class function Construct(const Comparison: TComparison<T>): IComparer<T>;
    function Compare(const Left, Right: T): Integer; virtual; abstract;
  end;

  TEqualityComparison<T> = reference to function(const Left, Right: T): Boolean;
  THasher<T> = reference to function(const Value: T): Integer;

  // Abstract base class for IEqualityComparer<T> implementations, and a provider
  // of default IEqualityComparer<T> implementations.
  TEqualityComparer<T> = class(TInterfacedObject, IEqualityComparer<T>)
  public
    class function Default: IEqualityComparer<T>; static;

    class function Construct(const EqualityComparison: TEqualityComparison<T>;
      const Hasher: THasher<T>): IEqualityComparer<T>;

    function Equals(const Left, Right: T): Boolean;
      reintroduce; overload; virtual; abstract;
    function GetHashCode(const Value: T): Integer;
      reintroduce; overload; virtual; abstract;
  end;

案例:

while TComparer<TBitMap>.Construct(
                 function (const Left, Right: TBitMap): Integer
                 begin
                     Result := (Left.Handle - Right.Handle);//FMX.Graphics.pas
                end).Compare( LBitMapCompared,LBitMapComparing)
   = 0 do
                 begin
                         TThread.Synchronize(nil,
                           procedure
                           begin
                               ShowMessage('请先选择图片');
                           end); 
                         exit; //:不往下执行:但不能等待
                 end;//:确保被选赋值项和选择项,两者完美吻合

System.Generics.Defaults

系统泛型的默认单元

........................
099Power(const Base, Exponent: Extended): Extended;

返回任意Base>0的实数的Exponent指数的数值

请参考网友的数学库翻译:

https://blog.csdn.net/weixin_30664539/article/details/95452703

System.Math.pas
 (AInt div BInt):Integer;对2个整数进行除,取商,返回Integer 
 (AInt mod BInt):Integer;取2个整数相除的余数,返回Integer 
 (AReal BReal):real;2个实数或整数相除的结果,返回实数real 
........................

本博客相关:

  《软件的基本是要处理好”算法“及其基础(一)流-字-字符(包括某个数字、字母、符号和某个汉字等)-字符串-字节动态数组-字节-整数之间的转化关系和算法》https://blog.csdn.net/pulledup/article/details/104353336

 

喜欢的话,就在下面点个赞、收藏就好了,方便看下次的分享:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

专讲冷知识

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值