数组切割(50分)

  • 主题发起人 主题发起人 heimwz
  • 开始时间 开始时间
H

heimwz

Unregistered / Unconfirmed
GUEST, unregistred user!
VB里面有个LBound和UBound函数,请问相对应的DELPHI中的函数是什么呢
 
深度历险里的例子
procedure DynArrayDelete(var A; elSize: Longint; index, Count: Integer);
procedure DynArrayInsert(var A; elSize: Longint; index, Count: Integer);

procedure DynArrayCopy(var ADst; const ASrc; elSize: Longint; IndexDst, IndexSrc, Count: Integer);
procedure DynArrayAppend(var ADst; const ASrc; elSize: Longint; IndexSrc, Count: Integer);

implementation

procedure DynArraySetZero(var A);
var
P: PLongint;
begin
P := PLongint(A);
Dec(P);
P^ := 0;
Dec(P);
P^ := 0;
end;

procedure DynArrayDelete(var A; elSize: Longint; index, Count: Integer);
var
len, MaxDelete: Integer;
P : PLongint;
begin
P := PLongint(A);
if P = nil then Exit;

len := PLongint(PChar(P) - 4)^;

if index >= len then Exit;

MaxDelete := len - index;
Count := Min(Count, MaxDelete);

if Count = 0 then Exit; // nothing to delete

Dec(len, Count);
MoveMemory(PChar(P) + index * elSize, PChar(P) + (index + Count) * elSize, (len - index) * elSize);

Dec(P);
Dec(P);
ReallocMem(P, len * elSize + Sizeof(Longint) * 2);
Inc(P);
P^ := len; // new length
Inc(P);

PLongint(A) := P;
end;

procedure DynArrayInsert(var A; elSize: Longint; index, Count: Integer);
const
PNull: Pointer = nil;
var
len, I: Integer;
P : PLongint;
begin
P := PLongint(A);
if P <> nil then
begin
len := PLongint(PChar(P) - 4)^;
if (index > len) or (Count = 0) then Exit; // nothing to insert

Dec(P);
Dec(P);
end else len := 0;

ReallocMem(P, (len + Count) * elSize + Sizeof(Longint) * 2); // &amp;yen;&amp;yacute; realloc
Inc(P);
P^ := len + Count;
Inc(P);

MoveMemory(PChar(P) + (index + Count) * elSize, PChar(P) + index * elSize, (len - index) * elSize); // &amp;copy;&amp;sup1;&amp;laquo;á&amp;reg;&amp;iquest;
for I := index to index + Count - 1 do
System.Move(PNull, PChar(Integer(P) + I * elSize)^, elSize);

PLongint(A) := P;
end;

procedure DynArrayCopy(var ADst; const ASrc; elSize: Longint; IndexDst, IndexSrc, Count: Integer);
var
PDst, PSrc : PLongint;
LenDst, LenSrc: Integer;
begin
PDst := PLongint(ADst);
PSrc := PLongint(ASrc);
if (PSrc = nil) then Exit;

if PDst <> nil then
LenDst := PLongint(PChar(PDst) - 4)^
else LenDst := 0;

LenSrc := PLongint(PChar(PSrc) - 4)^;

Count := Min(LenSrc - IndexSrc, Count); // src array ¤&amp;pound;¨&amp;not;&amp;reg;&amp;Eacute;, &amp;acute;&amp;icirc;¤&amp;Ouml; count

if IndexDst + Count - 1 > LenDst then // dst array &amp;ordf;&amp;Aring;&amp;para;&amp;iexcl;¤&amp;pound;¨&amp;not;
begin
DynArrayInsert(ADst, elSize, IndexDst, (IndexDst + Count) - LenDst); // &amp;cedil;&amp;Eacute;¨&amp;not;, ¤&amp;ordm;&amp;reg;e¤&amp;pound;&amp;ordm;&amp;THORN;, &amp;laquo;&amp;Yacute;·|&amp;acute;N&amp;raquo;/±&amp;frac14;¤F
PDst := PLongint(ADst);
end;

MoveMemory(PChar(PDst) + IndexDst * elSize, PChar(PSrc) + IndexSrc * elSize, Count * elSize); // &amp;frac12;&amp;AElig;&amp;raquo;s
end;

procedure DynArrayAppend(var ADst; const ASrc; elSize: Longint; IndexSrc, Count: Integer);
var
P: PLongint;
begin
P := PLongint(ADst);
if (P = nil) then Exit;

DynArrayCopy(ADst, ASrc, elSize, PLongint(PChar(P) - 4)^, IndexSrc, Count);
end;

end.
 
low()和high()
 
多人接受答案了。
 
后退
顶部