Lazarus

Programming => General => Topic started by: egsuh on September 04, 2020, 04:52:43 am

Title: Is there any commonly used cross-referencing approach?
Post by: egsuh on September 04, 2020, 04:52:43 am
Hi,

Let's say that I have two lists, and crossreference them (one to many relationship).
To do this, I thought of following algorithm. 

Code: Pascal  [Select][+][-]
  1. type
  2.     TObject2 = class;
  3.  
  4.     { TObject1 }
  5.  
  6.     TObject1 = class
  7.     private
  8.         FObject2 : TObject2;
  9.  
  10.         procedure setObj2(obj2: TObject2);
  11.     public
  12.         property Object2 : TObject2 read FObject2 write setObj2;
  13.  
  14.     end;
  15.  
  16.     { TObject2 }
  17.  
  18.     TObject2 = class
  19.         Object1s : array of TObject1;
  20.         procedure AddObj1 (obj1: TObject1);
  21.     end;
  22.  
  23. var
  24.   ObjectList1 : TFPGList<TObject1>;
  25.   ObjectList2 : TFPGList<TObject2>;
  26.  
  27.  
  28. implementation
  29.  
  30. { TObject2 }
  31.  
  32. procedure TObject2.AddObj1(obj1: TObject1);
  33. begin
  34.     SetLength(Object1s, Length(Object1s) + 1);
  35.     Object1s[High(Object1s)] := Obj1;
  36. end;
  37.  
  38. { TObject1 }
  39.  
  40. procedure TObject1.setObj2(obj2: TObject2);
  41. begin
  42.    FObject2 := obj2;
  43.    if obj2 <> nil then Obj2.AddObj1 (Self);
  44. end;
  45.  
  46.  

You will understand what I'm trying to do. This compiles well, but I have not run this yet. Just curious whether there are commonly used approach for this purpose, or any other built-in classes. 

I'm little bit nervous of modifying other object's data (obj2.Object1s) within property setting method (procedure TObject1.setObj2). 

Title: Re: Is there any commonly used cross-referencing approach?
Post by: ASerge on September 04, 2020, 05:13:27 pm
I still don't understand what you wanted. But this code does not correspond to "one to many relationship", because one Object1 can fall into the set of several (or all) Object2, when the owner changes. And Object2 can contain a set of Object1 by definition.
Title: Re: Is there any commonly used cross-referencing approach?
Post by: egsuh on September 05, 2020, 11:27:36 am
Quote
I still don't understand what you wanted. But this code does not correspond to "one to many relationship", because one Object1 can fall into the set of several (or all) Object2, when the owner changes. And Object2 can contain a set of Object1 by definition.

There are two arrays -- one of TObject1, and another of TObject2.
TObject1 has a pointer to any one of array members of TObject2.  But more than one TObject1 can point to the same TObject2. So TObject2 can have many TObject1s, but one TObject1 can point only one TObject2. So I said one to many relationship.

What I'm trying to do is to extract TObject1s that are pointing to one TObject2.
 
Let's assume that there are two arrays.

Obj1s : array of TObject1;
Obj2s : array of TObject2;

And let's assume.. 

Obj1s[1] := Obj2s [1];        // would be Obj1s[1].obj2 := Obj2s[1];  to write exactly
Obj1s[2] := Obj2s [1];
Obj1s[3] := Obj2s [1];
Obj1s[4] := Obj2s [2];
Obj1s[5] := Obj2s [2];
Obj1s[6] := Obj2s [3];

What I'd like to do is to extract Obj1s[1], Obj1s[2], Obj1s[3] from Obj2s[1]   later.
 


Title: Re: Is there any commonly used cross-referencing approach?
Post by: jamie on September 05, 2020, 06:58:57 pm
If I follow you the secondary objects needs to know who is pointing it It ?

 For that, you can use a Tlist in each object to keep track of who is pointing to it or use a dynamic array of objects that you can resize on the fly to contain them.. Of course you need to first verify the object is not already in the list before adding a new one to it.
TinyPortal © 2005-2018