DGL库的算法的声明文件Algorithms.inc_h://////////////////////////////////////////////////////////////////////////////////////////////////////////
//------------------------------------------------------------------------------
// Copyright (c) 2004
// Delhi泛型库--DGL(The Delphi Generic Library)
// Author: HouSisong
//------------------------------------------------------------------------------
// DGL库的算法的声明
// Create by HouSisong, 2004.09.01
// LastUpdate by HouSisong, 2005.03.27
//------------------------------------------------------------------------------
//Algorithms.inc_h ; Algorithms.inc_pas
{$ifndef __Algorithms_inc_h_}
{$define __Algorithms_inc_h_}
{$I DGLIntf.inc_h}
{$ifndef _DGL_Compare}
function _IsEqual(const Value0,Value1:_ValueType):boolean;
function _IsLess(const Value0,Value1:_ValueType):boolean;
{$endif}
{$ifndef _DGL_Compare_Value}
function _IsEqual_Value(const Value0,Value1:_ValueType):boolean;
function _IsLess_Value(const Value0,Value1:_ValueType):boolean;
{$endif}
type
TGenerateFunction = function (): _ValueType;
TGenerateFunctionOfObject = function (): _ValueType of object;
TTansfromFunction = function (const Value: _ValueType): _ValueType;
TTansfromFunctionOfObject = function (const Value: _ValueType): _ValueType of object;
TTansfromBinaryFunction = function (const Value0,Value1: _ValueType): _ValueType;
TTansfromBinaryFunctionOfObject = function (const Value0,Value1: _ValueType): _ValueType of object;
TVisitProc = procedure (const Value: _ValueType);
TVisitProcOfObject = procedure (const Value: _ValueType) of object;
TTestFunction = function (const Value:_ValueType):Boolean;
TTestFunctionOfObject = function (const Value:_ValueType):Boolean Of Object;
TTestBinaryFunction = function (const Value0,Value1:_ValueType):Boolean;
TTestBinaryFunctionOfObject = function (const Value0,Value1:_ValueType):Boolean Of Object;
TRandomGenerateFunction = function (const Range: Integer):integer; //0<=result<Range
TRandomGenerateFunctionOfObject = function (const Range: Integer):integer Of Object;
type
_TAlgorithms = class(TObject)
public
//返回两个迭代器之间的元素个数
class function Distance(const ItBegin,ItEnd:_IIterator):integer; overload;
//返回两个元素列是否相等
class function IsEquals(const ItBegin0,ItEnd0,ItBegin1,ItEnd1:_IIterator):boolean; overload;
//遍历
class procedure ForEach(const ItBegin,ItEnd:_IIterator;const VisitProc:TVisitProc); overload;
class procedure ForEach(const ItBegin,ItEnd:_IIterator;const VisitProc:TVisitProcOfObject); overload;
//查找 //失败返回位置等于ItEnd
class function Find(const ItBegin,ItEnd:_IIterator;const Value:_ValueType):_IIterator; overload;
class function FindIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunction):_IIterator; overload;
class function FindIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunctionOfObject):_IIterator; overload;
class function FindIf(const ItBegin,ItEnd:_IIterator;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunction):_IIterator; overload;
class function FindIf(const ItBegin,ItEnd:_IIterator;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunctionOfObject):_IIterator; overload;
//统计元素个数
class function Count(const ItBegin,ItEnd:_IIterator;const Value:_ValueType):integer; overload;
class function CountIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunction):integer;overload;
class function CountIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunctionOfObject):integer;overload;
//寻找子串
class function Search(const ItBegin,ItEnd,ItBeginSub,ItEndSub:_IIterator):_IIterator; overload;
class function Search(const ItBegin,ItEnd,ItBeginSub,ItEndSub:_IIterator;const TestBinaryFunction:TTestBinaryFunction):_IIterator; overload;
class function Search(const ItBegin,ItEnd,ItBeginSub,ItEndSub:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject):_IIterator; overload;
//最值
class function MinElement(const ItBegin,ItEnd:_IIterator):_IIterator; overload;
class function MinElement(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction):_IIterator; overload;
class function MinElement(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject):_IIterator; overload;
class function MaxElement(const ItBegin,ItEnd:_IIterator):_IIterator; overload;
class function MaxElement(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction):_IIterator; overload;
class function MaxElement(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject):_IIterator; overload;
//////////////////////////////////////////////
//交换值
class procedure SwapValue(const It0,It1:_IIterator);overload;
class procedure SwapValue(const It0:_IIterator;const Index0: integer;const It1:_IIterator;const Index1: integer);overload;
//将ItBeginSrc,ItEndSrc区间内的数据拷贝到ItBeginDest开始的区域中
class procedure Copy(const ItBeginSrc,ItEndSrc,ItBeginDest:_IIterator); overload;
//利用TansfromFunction函数进行转换
class procedure Tansfrom(const ItBegin,ItEnd:_IIterator;const TansfromFunction:TTansfromFunction); overload;
class procedure Tansfrom(const ItBegin,ItEnd:_IIterator;const TansfromFunction:TTansfromFunctionOfObject);overload;
class procedure Tansfrom(const ItBeginSrc,ItEndSrc,ItBeginDest:_IIterator;const TansfromFunction:TTansfromFunction); overload;
class procedure Tansfrom(const ItBeginSrc,ItEndSrc,ItBeginDest:_IIterator;const TansfromFunction:TTansfromFunctionOfObject);overload;
class procedure Tansfrom(const ItBeginSrc0,ItEndSrc0,ItBeginSrc1,ItBeginDest:_IIterator;const TansfromFunction:TTansfromBinaryFunction); overload;
class procedure Tansfrom(const ItBeginSrc0,ItEndSrc0,ItBeginSrc1,ItBeginDest:_IIterator;const TansfromFunction:TTansfromBinaryFunctionOfObject);overload;
//交换区间
class procedure SwapRanges(const ItBegin0,ItEnd0,ItBegin1:_IIterator);
//替换元素
class function Replace(const ItBegin,ItEnd:_IIterator;const OldValue,NewValue:_ValueType):integer; overload;
class function ReplaceIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunction;const NewValue:_ValueType):integer;overload;
class function ReplaceIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunctionOfObject;const NewValue:_ValueType):integer;overload;
//write ReplaceCopy() ?
//填充区间
class procedure Fill(const ItBegin,ItEnd:_IIterator;const Value:_ValueType);overload;
class procedure Fill(const ItBegin:_IIterator;const N:integer;const Value:_ValueType);overload;
class procedure Generate(const ItBegin,ItEnd:_IIterator;const GenerateFunction:TGenerateFunction);overload;
class procedure Generate(const ItBegin:_IIterator;const N:integer;const GenerateFunction:TGenerateFunction);overload;
class procedure Generate(const ItBegin,ItEnd:_IIterator;const GenerateFunction:TGenerateFunctionOfObject);overload;
class procedure Generate(const ItBegin:_IIterator;const N:integer;const GenerateFunction:TGenerateFunctionOfObject);overload;
//移除元素 //返回值(result)表示Value在[ItBegin,result)区间内被移除了
class function Remove(const ItBegin,ItEnd:_IIterator;const Value:_ValueType):_IIterator;overload;
class function RemoveIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunction):_IIterator;overload;
class function RemoveIf(const ItBegin,ItEnd:_IIterator;const TestFunction:TTestFunctionOfObject):_IIterator;overload;
/////////////////////////////////////////////////////
//反转
class procedure Reverse(const ItBegin,ItEnd:_IIterator); overload;
//随机重排
class procedure RandomShuffle(const ItBegin,ItEnd:_IIterator); overload;
class procedure RandomShuffle(const ItBegin,ItEnd:_IIterator;const RandomGenerateFunction:TRandomGenerateFunction); overload;
class procedure RandomShuffle(const ItBegin,ItEnd:_IIterator;const RandomGenerateFunction:TRandomGenerateFunctionOfObject); overload;
//排序
class procedure Sort(const ItBegin,ItEnd:_IIterator); overload;
class procedure Sort(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction); overload;
class procedure Sort(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject); overload;
class function IsSorted(const ItBegin,ItEnd:_IIterator):boolean; overload;
class function IsSorted(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction):boolean; overload;
class function IsSorted(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject):boolean; overload;
class function BinarySearch(const ItBegin,ItEnd:_IIterator;const Value:_ValueType):_IIterator; overload;
class function BinarySearch(const ItBegin,ItEnd:_IIterator;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunction):_IIterator; overload;
class function BinarySearch(const ItBegin,ItEnd:_IIterator;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunctionOfObject):_IIterator; overload;
class function LowerBound(const ItBegin,ItEnd:_IIterator;const Value:_ValueType):_IIterator; overload;
class function LowerBound(const ItBegin,ItEnd:_IIterator;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunction):_IIterator; overload;
class function LowerBound(const ItBegin,ItEnd:_IIterator;const Value:_ValueType;const TestBinaryFunction:TTestBinaryFunctionOfObject):_IIterator; overload;
//堆操作
class procedure MakeHeap(const ItBegin,ItEnd:_IIterator);overload;
class procedure MakeHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction);overload;
class procedure MakeHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject);overload;
class procedure PushHeap(const ItBegin,ItEnd:_IIterator);overload; //push value in [ItBegin,ItEnd-1) ,the Value at (ItEnd-1);
class procedure PushHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction);overload;
class procedure PushHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject);overload;
class procedure PopHeap(const ItBegin,ItEnd:_IIterator);overload;//pop max Value to (ItEnd-1);
class procedure PopHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction);overload;
class procedure PopHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject);overload;
class procedure SortHeap(const ItBegin,ItEnd:_IIterator);overload;
class procedure SortHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunction);overload;
class procedure SortHeap(const ItBegin,ItEnd:_IIterator;const TestBinaryFunction:TTestBinaryFunctionOfObject);overload;
end;
{$endif } // __Algorithms_inc_h_