0

I have a number of Objects that represent Polygons. I have a set of Pointers each to its own polygon.

Iterating through each pair of pointer I merge the polygons if they overlap into a new polygon. The two pointers in the pair I now let point to the merged polygon, in order to be able to merge it again and have the change in both pointers.

Now my problem arises pretty quickly with three objects:

  1. A* points to A, B* to B and C* to C as you would expect.
  2. I merge A* and B* and now both point to AB
  3. Then I merge A* and C* and now both point to ABC.

My problem is now B* does not know that AB was again merged and still points to AB, this is not what I want.

What I want is the behaviour not the pointer, so I am free for any suggestions. The only limitation is sadly that I am not allowed to use C++11 or newer.

7
  • Can you include the code doing the merge? I can only guess but I would suspect that when you are performing the merge you are creating a new object and setting the pointer to reference this, rather than updating one of the existing objects with the merged data.
    – Graeme
    Commented Jan 22, 2016 at 14:38
  • Wouldn't that make it necessary that I always merge into the same object? If A* and B* both point to the merged into A, then if I merge A* and C* and don't know in which I merged I could merge into C, and not A* and C* point to the same object, but B* still to a wrong A.
    – Dragonseel
    Commented Jan 22, 2016 at 14:44
  • Can you use boost? A lot of the pointer stuff in C++11 has been in boost for a decade. (P.S. If you need to edit the question, you mean "or newer". I have the reverse problem; when I compose German, I think of the German word, and my fingers type the English.) Commented Jan 22, 2016 at 14:44
  • In this example, do the original Polygons continue to exist? If not, 'boost::shared_ptr<Poly> pA, pB, pC; *pA += *pB, pB = pA; *pA += *pC, pC = pA;` should do it. Commented Jan 22, 2016 at 14:47
  • 1
    If you update the 2 pointers you are working with at one time A & C, and change where they are pointing (to your 'new' object) then there is nothing updating where B is pointing, so it will continue to point to the object created when you merged A & B. Rather than create new objects, update one and destroy the other, then when that object changes all references to it will see the same data.
    – Graeme
    Commented Jan 22, 2016 at 14:47

2 Answers 2

0

When mergin A and C, merge all polygons that belongs to the same polygon A (and C) belongs.

That means, when merging A*->AB you should also merge B*->AB.

3
  • How do A* and C* know that B* also points to one of the merged polygons?
    – Dragonseel
    Commented Jan 22, 2016 at 14:44
  • You mean like storing a list of which pointers have been merged into a object? That sounds effective and would not need a library or something.
    – Dragonseel
    Commented Jan 22, 2016 at 14:54
  • yes exactly. The problem is in the algorithm used, not on the type of pointers. Commented Jan 22, 2016 at 14:56
0

I'd sugegst this by only "merging in the forward direction".

A* points to A and B* points to B. Good, this doesn't change. When you merge A and B, make both point to AB. :

A* -> A -> AB <- B <- B*

and now with C*/C :

A* -> A -> AB <- B <- B*
            |
            V
C* -> C -> ABC
2
  • But AB is not again a pointer but an Object. So when AB and C get merged, the pointer B* does not automatically get changed. So I would'n know if B points to a correct polygon that I have to use later or to a merged one, and if merged I have no clue which one.
    – Dragonseel
    Commented Jan 22, 2016 at 15:43
  • @Dragonseel: Obviously the Object (Polygon) AB would contain a pointer; you're right that it isn't a pointer itself.
    – MSalters
    Commented Jan 22, 2016 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.