开发者

dynamic array element deletion for multiple types

开发者 https://www.devze.com 2023-01-31 19:07 出处:网络
type TSomething = record name: string; value: integer; end; type TOtherSomething = record data: extended;
type TSomething = record
 name: string;
 value: integer;
end;    
type TOtherSomething = record
 data: extended;
 data2: extended;
 data3: array of TSomethingAlien;
end;
...
dynarray1: array of TSomething;
dynarray2: array of TOtherSomething;

Also, there's nasty procedure of deleting elements for each type:

procedure TForm1.DeleteSomething(N: integer);
begin
   if N > High(dynarray1) then Exit;
   if N < Low(dynarray1) then Exit;
   if N = High(dynarray1) then begin
     SetLength(dynarray1, Length(dynarray1) - 1);
     Exit;
   end;
   Finalize(dynarray1[N]) ;
   System.Move(dynarray1[N +1], dynarray1[N],(Length(dynarray1) - N  -1) * SizeOf(TSomething) + 1) ;
   setLength(dynarray1, Length(dynarray1) - 1) ;
end;                          

procedure TForm1.DeleteOtherSomething(N: integer);
... got the idea?

Binding the开发者_如何学JAVA procedure to global object wasn't smart, but compiler groaned, when a var Dyna: array of TSomething was in function arguments, so Dyna was used instead of addressing to dynarray1. So I even can't overload the function for multiple data types with var argument! SetLength(Dyna, length(dynarray1) type mismatch! SetLength(Dyna, length(Dyna) type mismatch!

What is wanted:

  1. Overloaded DeleteElement(var Arr: TMultipleTypes, N: element): see above, why can't reach this goal.

  2. single DeleteElement implementation that works with any type of data in array? Something like <template> type support?


Well, normal tlist class does most of this stuff, but is based on an array of pointers

The generic "tfplist" (requires 2.4.0+, see fgl unit) might be closer to what you want. But generics are still beta.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号