pointless object copies

J

jsnX

hi,

i would like to make some code that operates on sets of points - does
convex hulls and other operations. but it looks like i will need a lot
of pointless object copies:

======================================================================================
// point cloud class
class pc : public std::vector<vec2d>
{
// utility methods for operating on lists of points

static pc&
convex_hull(const pc& _1, const pc& _2)
{
// return the convex hull of these two clouds somehow
}
};

pc a = get_a();
pc b = get_b();
pc c = get_c();

pc hull_ab = pc::convex_hull(a, b); // pointless copies from
a and b
pc hull_bc = pc::convex_hull(b, c); // pointless copies from
b and c

pc::iterator alpha = b.begin();
pc::iterator omega = b.end();
pc b_on_both;

for(; alpha != omega; ++alpha )
if ( hull_ab.has(*alpha) && hull_bc.has(*alpha) )
b_on_both.push_back(*alpha); // pointless copies due
to operator=


======================================================================================

how do i avoid all these copies? obviously, there is a way - using
pointers. i could just make pc inherit from std::vector<vec2d*>. but in
books and on the web, i am told that containers of pointers are evil,
so i don't want to implement my class with one! is this a case where
'garbage collection' or 'reference counting' is necessary?

_jsnX
 
V

Victor Bazarov

jsnX said:
i would like to make some code that operates on sets of points - does
convex hulls and other operations. but it looks like i will need a lot
of pointless object copies:

======================================================================================
// point cloud class
class pc : public std::vector<vec2d>
{
// utility methods for operating on lists of points

static pc&
convex_hull(const pc& _1, const pc& _2)
{
// return the convex hull of these two clouds somehow
}
};

pc a = get_a();
pc b = get_b();
pc c = get_c();

pc hull_ab = pc::convex_hull(a, b); // pointless copies from
a and b

Copies? Where? The arguments are passed by reference to const. That
means that inside the function the _1 and _2 are *aliases* to the real
objects 'a' and 'b' here.
pc hull_bc = pc::convex_hull(b, c); // pointless copies from
b and c

Same question: what copies?
pc::iterator alpha = b.begin();
pc::iterator omega = b.end();
pc b_on_both;

for(; alpha != omega; ++alpha )
if ( hull_ab.has(*alpha) && hull_bc.has(*alpha) )
b_on_both.push_back(*alpha); // pointless copies due
to operator=

Well, to store objects in a container, you have to make a copy. If you
don't want to store a separate (read: independent, self-sufficient) piece
of information, declare a vector of pointers. It's dangerous since
pointers tend to easily become invalid, it's cumbersome because pointers
need care in handling, but it prevents "unnecessary" copying.
======================================================================================

how do i avoid all these copies? obviously, there is a way - using
pointers. i could just make pc inherit from std::vector<vec2d*>. but in
books and on the web, i am told that containers of pointers are evil,
so i don't want to implement my class with one! is this a case where
'garbage collection' or 'reference counting' is necessary?

You could have a container of smart (or shared) pointer objects, see
boost::shared_ptr. They can be stored in a container, they are relatively
safe and the count references IIRC.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top