[fpc-pascal] sorting and merging array of records
waldo kitty
wkitty42 at windstream.net
Wed Jan 11 09:27:48 CET 2012
On 1/10/2012 16:41, Sven Barth wrote:
> On 10.01.2012 22:02, waldo kitty wrote:
>> TList? hummm... better? smaller? faster? easier to use? small memory
>> footprint? i'm game to try things as long as it works in the end ;)
>
> TList is basically an array wrapped in a class (though I would suggest TFPList
> as that is faster than TList (TList is based on TFPList)). Both can be found in
> unit Classes.
>
> The following you need to know: T(FP)List works on pointers, thus you need to
> 1. declare a pointer type to your record type (e.g. Pthree_line_data =
> ^three_line_data)
> 2. allocate a Pthree_line_data variable if you want to add a new entry to the list
i tried this but could only get so far and then not further so i backed up and
punted the ball... now i'm trying this with a sortedcollection and while i can
apparently insert items, i haven't figured out how to access them and print
their contents... unless their contents are garbage like the following prints out...
program satsort;
uses {$IFDEF DEBUG}heaptrc,{$ENDIF}objects,math,crt,dos;
type
vector = array [1..4] of double;
cat_nbr = string[5];
sat_name = string[25];
line_data = string[69];
two_line = array [1..2] of line_data;
PTLERec = ^TTLERec;
TTLERec = record
satname : sat_name;
satdata : two_line;
catnbr : cat_nbr;
epoch : double;
end;
TTLERecObj = object(TObject)
TLEData : PTLERec;
constructor Init;
destructor Done; virtual;
end;
constructor TTLERecObj.Init;
begin
new(TLEData);
end;
destructor TTLERecObj.Done;
begin
dispose(TLEData);
end;
type
PTLECollection = ^TTLECollection;
TTLECollection = object(TSortedCollection)
TLERec : TTLERecObj;
function Compare(Key1, Key2 : Pointer): longint; virtual;
end;
function TTLECollection.Compare(key1, key2: pointer): longint;
begin
if PTLERec(key1)^.catnbr = PTLERec(key2)^.catnbr then
compare := 0
else if PTLERec(key1)^.catnbr < PTLERec(key2)^.catnbr then
compare := -1
else
compare := 1;
end;
const
data_type : byte = 3;
// max_sats = {$IFDEF FPC}65536{$ELSE}250{$ENDIF};
var
data_drive,data_dir,
work_drive,work_dir : string;
UTC_offset : double;
DST : boolean;
fsat,fobs : text;
obs_name : string[25];
piss : vector;
gigo : char;
my_sats : word;
aTLERec : PTLERec;
aTLECollection : PTLECollection;
[...]
Function Input_Satellite_List(var aInputFile: TextFile) : word;
var
data : PTLERec;
sat_cnt : word;
i,x : longint;
begin
sat_cnt := 0;
while not EOF(aInputFile) do
begin
new(data);
Readln(aInputFile,data^.satname);
Readln(aInputFile,data^.satdata[1]);
Readln(aInputFile,data^.satdata[2]);
data^.catnbr := Copy(data^.satdata[1],3,5);
data^.epoch := Real_Value(data^.satdata[1],19,14);
inc(sat_cnt);
aTLECollection^.insert(data);
dispose(data);
{$IFDEF DEBUG}
writeln(PTLERec(aTLECollection)^.satname);
writeln(PTLERec(aTLECollection)^.satdata[1]);
writeln(PTLERec(aTLECollection)^.satdata[2]);
writeln(PTLERec(aTLECollection)^.catnbr);
writeln(PTLERec(aTLECollection)^.epoch);
{$ENDIF}
end; {while not EOF}
Input_Satellite_List := sat_cnt;
end; {Procedure Input Satellite List}
More information about the fpc-pascal
mailing list