[fpc-pascal] sorting and merging array of records

Tomas Hajny XHajT03 at hajny.biz
Thu Jan 12 18:42:08 CET 2012


On Thu, January 12, 2012 17:23, waldo kitty wrote:
> On 1/12/2012 07:20, Tomas Hajny wrote:
>> On Thu, January 12, 2012 03:34, waldo kitty wrote:
> [TRIM]
>>> without looking at the "code to copy" if i want to override the insert
>>> method, it almost seems that there's a bug if it just throws away the
>>> record we're trying to insert... it would seem that if the code locates
>>> a "duplicate" record, it would properly dispose of unwanted data...
>>> unless i perform the
>>
>> No. There are two tasks. One is dynamic allocation of the object, the
>> other is insertion. Although you perform both tasks on one line in your
>> program, these are two distinct tasks. The insert code cannot know
>> whether
>> you still may need the (previously allocated) object or not in the very
>> general case. The insert code doesn't throw anything away - you do it by
>> not storing the result of the "New (PTLERec, Init (..." to some variable
>> and only send it as a parameter to a method which may or may not store
>> it
>> somewhere else.
>
> AHH! i think i understand this now... you are saying that instead of doing
> this...
>
> aTLEColl^.insert(New(PTLERec, Init(MySatName, MySatData1, MySatData2,
> MyCatNbr,
> MyEpoch)));
>
> i should do it in two steps like this...
>
> aTLERec := New(PTLERec, Init(MySatName, MySatData1, MySatData2, MyCatNbr,
> MyEpoch));
> aTLEColl^.insert(aTLERec);
>
> but the the problem still comes of how do i know if the record was
> inserted into
> the list or not? insert doesn't seem to return a true or false on
> success... i
> guess this is where it is required(?) to override the insert method? does
> everyone have to override the insert method if they are in duplicates :=
> FALSE
> mode? how do they handle the throwing away of the unneeded data??

There are multiple options. You can search before (as suggested by you)
and only call Insert if not finding the result, you can override the
Insert call as suggested by me, you can add another insertion method with
a different signature (e.g. a function instead of a procedure providing
information whether the record was inserted or not as its result), you
could also search for the record after calling Insert and compare the
pointer returned by At for the found index returned by Search to your
original pointer (i.e. checking that both pointers refer to the same
address, not just that they contain the same data), you could possibly
also ignore the lost memory altogether in certain scenarios (assuming that
the memory is deallocated anyway when the program is finished with its
work, the amount of data is limited to sensible values compared to overall
amount of memory in your machine and the program does just some one-time
processing and thus does not live for very long), etc.


 .
 .
> yeah, that went over like a lead balloon... i'm digging into the insert
> method
> override, instead... just gotta figure out how to access the epoch in the
> current record and the one in the passed record for the decision making
> comparison...

PROCEDURE TTLECollection.Insert (Item: Pointer);
VAR I: Sw_Integer;
BEGIN
   If NOT Search(KeyOf(Item), I) OR Duplicates Then   { Item valid }
     AtInsert(I, Item)                                { Insert the item }
   else
     if PTLERec (At (I))^.Epoch > PTLERec (Item)^.Epoch then
       begin
         FreeItem (At (I));
         AtPut (I, Item);
       end
     else
      FreeItem (Item); (* or "Dispose (PTLERec (Item), Done)" instead *)
END;

Tomas





More information about the fpc-pascal mailing list