⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 _algorithms_base_private.inc_pas

📁 delphi的范型代码库
💻 INC_PAS
📖 第 1 页 / 共 2 页
字号:
(*
 * DGL(The Delphi Generic Library)
 *
 * Copyright (c) 2004
 * HouSisong@263.net              
 *
 * This material is provided "as is", with absolutely no warranty expressed
 * or implied. Any use is at your own risk.
 *
 * Permission to use or copy this software for any purpose is hereby granted
 * without fee, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 *
 *)

//------------------------------------------------------------------------------
// DGL库的算法的实现 处理类函数和普通函数
// Create by HouSisong, 2005.10.13
//------------------------------------------------------------------------------
//_Algorithms_Base_Private.inc_pas


{$ifdef  _DGL_Algorithms_Object_Function}
class procedure _TAlgorithms.Tansfrom(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TansfromFunction:TTansfromFunctionOfObject);
{$else}
class procedure _TAlgorithms.Tansfrom(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TansfromFunction:TTansfromFunction);
{$endif}
var
  It  :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin

  It:=ItBegin;
  while (not It.IsEqual(ItEnd)) do
  begin
    {$ifdef  _DGL_ObjValue}
    _Assign(It.Value,TansfromFunction(It.Value));
    {$else}
    It.Value:=TansfromFunction(It.Value);
    {$endif}
    It.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class procedure _TAlgorithms.Tansfrom(const ItBeginSrc,ItEndSrc,ItBeginDest: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TansfromFunction:TTansfromFunctionOfObject);
{$else}
class procedure _TAlgorithms.Tansfrom(const ItBeginSrc,ItEndSrc,ItBeginDest: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TansfromFunction:TTansfromFunction);
{$endif}
var
  ItSrc  :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
  ItDest :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  ItSrc:=ItBeginSrc;
  ItDest:=ItBeginDest;
  while (not ItSrc.IsEqual(ItEndSrc)) do
  begin
    {$ifdef  _DGL_ObjValue}
    _Assign(ItDest.Value,TansfromFunction(ItSrc.Value));
    {$else}
    ItDest.Value:=TansfromFunction(ItSrc.Value);
    {$endif}
    ItSrc.Next();
    ItDest.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class procedure _TAlgorithms.Tansfrom(const ItBeginSrc0,ItEndSrc0,ItBeginSrc1,ItBeginDest: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TansfromFunction:TTansfromBinaryFunctionOfObject);
{$else}
class procedure _TAlgorithms.Tansfrom(const ItBeginSrc0,ItEndSrc0,ItBeginSrc1,ItBeginDest: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TansfromFunction:TTansfromBinaryFunction);
{$endif}
var
  ItSrc0  :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
  ItSrc1  :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
  ItDest  :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  ItSrc0:=ItBeginSrc0;
  ItSrc1:=ItBeginSrc1;
  ItDest:=ItBeginDest;
  while (not ItSrc0.IsEqual(ItEndSrc0)) do
  begin
    {$ifdef  _DGL_ObjValue}
    _Assign(ItDest.Value,TansfromFunction(ItSrc0.Value,ItSrc1.Value));
    {$else}
    ItDest.Value:=TansfromFunction(ItSrc0.Value,ItSrc1.Value);
    {$endif}
    ItSrc0.Next();
    ItSrc1.Next();
    ItDest.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class procedure _TAlgorithms.ForEach(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const VisitProc:TVisitProcOfObject);
{$else}
class procedure _TAlgorithms.ForEach(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const VisitProc:TVisitProc);
{$endif}
var
  It  :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  It:=ItBegin;
  while (not It.IsEqual(ItEnd)) do
  begin
    VisitProc(It.Value);
    It.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.FindIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunctionOfObject): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$else}
class function _TAlgorithms.FindIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunction): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$endif}
begin
  result:=ItBegin;
  while (not result.IsEqual(ItEnd)) do
  begin
    if TestFunction(result.Value) then
      exit
    else
      result.Next();
  end;
end;


{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.FindIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunctionOfObject): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$else}
class function _TAlgorithms.FindIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunction): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$endif}
begin
  result:=ItBegin;
  while (not result.IsEqual(ItEnd)) do
  begin
    if TestBinaryFunction(result.Value,Value) then
      exit
    else
      result.Next();
  end;
end;


{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.ReplaceIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunctionOfObject;const NewValue:_ValueType):integer;
{$else}
class function _TAlgorithms.ReplaceIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunction;const NewValue:_ValueType):integer;
{$endif}
var
  It: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  It:=ItBegin;
  result:=0;
  while (not It.IsEqual(ItEnd)) do
  begin
    if (TestFunction(It.Value)) then
    begin
      It.Value:=NewValue;
      inc(result);
    end;
    It.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.ReplaceCopyIf(const ItBeginSrc,ItEndSrc,ItBeginDest: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunctionOfObject;const NewValue:_ValueType):integer;
{$else}
class function _TAlgorithms.ReplaceCopyIf(const ItBeginSrc,ItEndSrc,ItBeginDest: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunction;const NewValue:_ValueType):integer;
{$endif}
var
  ItSrc: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
  ItDest: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  ItSrc:=ItBeginSrc;
  ItDest:=ItBeginDest;
  result:=0;
  while (not ItSrc.IsEqual(ItEndSrc)) do
  begin
    if (TestFunction(ItSrc.Value)) then
    begin
      ItDest.Value:=NewValue;
      inc(result);
    end
    else
      ItDest.Value:=ItSrc.Value;
    ItSrc.Next();
    ItDest.Next();
  end;
end;



{$ifdef  _DGL_Algorithms_Object_Function}
class procedure _TAlgorithms.Generate(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const GenerateFunction:TGenerateFunctionOfObject);
{$else}
class procedure _TAlgorithms.Generate(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const GenerateFunction:TGenerateFunction);
{$endif}
var
  It: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  It:=ItBegin;
  while (not It.IsEqual(ItEnd)) do
  begin
    It.Value:=GenerateFunction();
    It.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class procedure _TAlgorithms.Generate(const ItBegin: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const N:integer;const GenerateFunction:TGenerateFunctionOfObject);
{$else}
class procedure _TAlgorithms.Generate(const ItBegin: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const N:integer;const GenerateFunction:TGenerateFunction);
{$endif}
var
  It: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
  i: integer;
begin
  It:=ItBegin;
  for i:=0 to N-1 do
  begin
    It.Value:=GenerateFunction();
    It.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.RemoveIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunctionOfObject): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$else}
class function _TAlgorithms.RemoveIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunction): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$endif}
var
  ItRead: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  result:=FindIf(ItBegin,ItEnd,TestFunction);
  if result.IsEqual(ItEnd) then exit;

  ItRead:=result;
  ItRead.Next;
  while (not ItRead.IsEqual(ItEnd)) do
  begin
    if not TestFunction(ItRead.Value) then
    begin
      Result.Value:=ItRead.Value;
      Result.Next;
    end;
    ItRead.Next();
  end;
end;


{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.CountIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunctionOfObject):integer;
{$else}
class function _TAlgorithms.CountIf(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestFunction:TTestFunction):integer;
{$endif}
var
  It :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  result:=0;
  It:=ItBegin;
  while (not It.IsEqual(ItEnd)) do
  begin
    if TestFunction(It.Value) then
      inc(result);
    It.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.MinElement(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestBinaryFunction:TTestBinaryFunctionOfObject): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$else}
class function _TAlgorithms.MinElement(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestBinaryFunction:TTestBinaryFunction): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$endif}
var
  It :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  if ItBegin.IsEqual(ItEnd) then
  begin
    result:=ItEnd;
    exit;
  end;

  result:=ItBegin;
  It:=ItBegin;
  It.Next;
  while (not It.IsEqual(ItEnd)) do
  begin
    if TestBinaryFunction(It.Value,result.Value) then
      result.Assign(It);
    It.Next();
  end;
end;

{$ifdef  _DGL_Algorithms_Object_Function}
class function _TAlgorithms.MaxElement(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestBinaryFunction:TTestBinaryFunctionOfObject): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$else}
class function _TAlgorithms.MaxElement(const ItBegin,ItEnd: {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;const TestBinaryFunction:TTestBinaryFunction): {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
{$endif}
var
  It :  {$ifdef _DGL_VectorItType}_IVectorIterator{$else}_IIterator{$endif} ;
begin
  if ItBegin.IsEqual(ItEnd) then
  begin
    result:=ItEnd;
    exit;
  end;

  result:=ItBegin;
  It:=ItBegin;
  It.Next;
  while (not It.IsEqual(ItEnd)) do
  begin
    if TestBinaryFunction(result.Value,It.Value) then
      result.Assign(It);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -