|
From: Francesco <xtr...@vi...> - 2005-04-21 20:48:13
|
Thanks to everybody for the in-depth explanations.
Francesco
On Apr 21, 2005, at 7:04 PM, Mat...@Ve... wrote:
> Alright. Once you have overloaded an operator, you are stuck with that
> operator. However, overloaded operators apply on a class by class
> basis. The
> HugeStuff type will not use the operator= that you defined for your
> class.
> Can you think of a way to refactor your operator= to operate in terms
> of the
> default operator= define for type HugeStuff?
>
> Also, memcpy should be very fast. If it's not, try rolling your own. It
> might work something like the below code. Also make sure you enable -O3
> -fexpensive-optimizations when you build the code. You might also need
> -mcpu
> and -mtune.
>
> Some routines benefit heavily from optimization. Copy-heavy routines
> seem to
> need help with this. When I wrote a crypto algorithm a while ago, it
> doubled
> the speed to optimize it fully.
>
> void copy(void* begin, void* end, void* dest)
> {
> for((char*) i=0; i<(char*)end; i++)
> {
> *(char*)dest=(char*)(begin+i);
> }
> }
>
> HTH!
> Matt
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...]On Behalf Of
> Francesco
> Sent: Thursday, April 21, 2005 3:02 AM
> To: Alfred P. Reaud
> Cc: Dev C++
> Subject: Re: [Dev-C++] Shallow copy
>
>
> Thanks for answering Alfred,
> I'm already using the STL but since I'm still in a learning stage I was
> trying to understand the "inner workings". Suppose I'm building ( just
> for
> my own pleasure :-) ) a simple competitor to the vector template. Or
> to make
> it even more banal, suppose I have the following code:
>
>
> class MyData
> {
> typeHugeStuff * mHugeStuffPtr;
> // ....
> public:
> // ....
> MyData const & operator=( MyData const & inMyData ); // the
> definition of this copies also the stuff pointed to by mHugeStuffPtr
> // .....
> };
>
>
> template< typename T >
> void MySwap( T * inDataPtr1, T * inDataPtr2 )
> {
> #ifdef BAD_SWAP
> // the following will swap in memory the two objects but will
> also
> deallocate/allocate/copy hugeStuff
> T tempData;
>
>
> tempData = *inDataPtr1;
> *inDataPtr1 = *inDataPtr2;
> *inDataPtr2 = tempData;
> #else
> // the following will just swap bitwise the members of the two
> objects without erasing/duplicating/whatever hugeStuff
> // but this bitwise copy seems to be slower than using the
> operator=
> (when it's not overloaded)
> // so I was wondering if it is possible to access the
> UNoverloaded
> operator= in objects that have overloaded it....
> char * aPtr;
>
>
> aPtr = (char*) malloc ( sizeof( T ) );
> memcpy( aPtr, inDataPtr1, sizeof( T ) );
> memcpy( inDataPtr1, inDataPtr2, sizeof( T ) );
> memcpy( inDataPtr2, aPtr, sizeof( T ) );
> free( aPtr );
> #endif
> }
>
>
> void main( void )
> {
> MyData * aDataPtr;
>
> aDataPtr = new MyData[ 100 ];
> FillUp( aDataPtr, 100 ); // filling up with data
> MySwap< MyData >( aDataPtr + 20, aDataPtr + 40 ); // just
> because.... :-)
> }
>
>
> Thanks in advance for any insight,
> Francesco
>
>
> On Apr 20, 2005, at 7:46 PM, Alfred P. Reaud wrote:
>
>
> Hi Francesco,
>
> Your array of class objects would be better served if it where a list
> or
> vector of class objects instead of an array. Then you can use the
> iterator
> features, including list and vector specific swap and sort member
> functions.
> I f you must use an array of class objects, then there's always the
> <algorithm> standard template library header. Algorithm has a rich
> set of
> functions that include swap, sort, and other useful data manipulation
> algorithms such as unique, etc. These functions are usually
> optimized, and
> work in most instances significantly better than manually coded swap
> or sort
> functions.
>
> I'm not actually sure what you mean by "deep copy", however, I'm
> assuming
> that you mean actual data copying. You shouldn't be copying data to
> swap
> it! You should be swapping pointers to the data. Maybe the issue is
> in
> using a class class_objects[array_size]; declaration vs. a class
> *class_objects; declaration, because the overloaded operator= may
> operate in
> a different fashion between a pointer and an array element (i.e.
> class_objects[7], is a pointer, but in machine code is actually a set
> of
> pointer arithmetic operations). The class operator= should already be
> configured to correctly do something like:
>
> class *obj_a, *obj_b, *temp;
> obj_a = new class; ...;
> temp = obj_a; obj_a = obj_b; obj_b = temp;
> delete(temp); ...
>
> so that should work correctly, ***** however *****
>
>
>
> list<class> obj_list;
> list<class>::iterator obj_iter;
> obj_list.push_back(obj_a); ...
> obj_iter = obj_list.begin();
> obj_list.sort() or obj_list.sort( compare_function );
> ...
>
> is easier, more elegant, and uses the optimized features of C++!
> Hopefully
> the above helps!
>
>
> Francesco <xtr...@vi...> wrote:
> Hi everybody,
> I'm a total beginner and I have the following problem:
> I have an array of objects instantiated from a class that has member
> pointers to dynamically allocated data. This class has also the usual
> overloaded operator=. I have to sort this array and I need to do some
> swapping. But swapping by "deep copy" seems useless to me in this case,
> thus is it possible somehow to override the overloaded operator= and
> use just for the swapping the normal operator =? I could use memcpy for
> a bitwise copy, but it seems to be significantly slower, especially
> for small objects.
> Thanks in advance,
> Francesco
> <snip>
>
>
>
>
> Alfred P. Reaud
> <image.tiff>
> ___________________________________________________________________
> The information contained in this message and any attachment may be
> proprietary, confidential, and privileged or subject to the work
> product doctrine and thus protected from disclosure. If the reader
> of this message is not the intended recipient, or an employee or
> agent responsible for delivering this message to the intended
> recipient, you are hereby notified that any dissemination,
> distribution or copying of this communication is strictly prohibited.
> If you have received this communication in error, please notify me
> immediately by replying to this message and deleting it and all
> copies and backups thereof. Thank you.
>
|