Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » Array : swapping element with a pointer
Array : swapping element with a pointer [message #33756] Sat, 10 September 2011 21:30 Go to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
Hi,

I'd need something like this :

Array[A] array;

A * a = new A;

array.Swap(someIndex, a);



where Array::Swap() should be so :

void Array<A>::Swap(int idx, A*& elem);


This would allow swapping contents of single elements of different arrays without insertion/deletion steps; example :

Array<A> arr1, arr2;

arr1.Add(new A);
arr1.Add(new A);

arr2.Add(new A);
arr2.Add(new A);

A *a = NULL;
arr1.Swap(1, a);
arr2.Swap(1, a);
arr1.Swap(1, a);


That one because I guess removing and then inserting an element in a 10000 elements array just to swap a couple of them would be quite slow....

A more nice way would be some sort of Detach() Set() that don't drop the element but replace it with a dummy (maybe null...) pointer. That would be :

Array<A> arr1, arr2;

arr1.Add(new A);
arr1.Add(new A);

arr2.Add(new A);
arr2.Add(new A);

A *a = arr1.NoDropDetach(1);
A *b = arr2.NoDropDetach(1);
arr1.NoInsertSet(1, b);
arr2.NoInsertSet(1, a);


Max
Re: Array : swapping element with a pointer [message #33757 is a reply to message #33756] Sat, 10 September 2011 22:05 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
Maybe something added to Array class :

void Swap(int i, T*&ptr) { T *tmp = ptr; ptr = vector[i]; vector[i] = tmp; }


I'd need for ArrayMap too... I wonder if it's automatic or not, adding it to Array class.
Will try....

Max
Re: Array : swapping element with a pointer [message #33758 is a reply to message #33757] Sat, 10 September 2011 22:11 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
That one should do the job for ArrayMap too :

void Swap(int i, T*&ptr) { value.Swap(i, ptr); }


It would be possible to add both to Core ?

Max

Re: Array : swapping element with a pointer [message #33764 is a reply to message #33757] Sun, 11 September 2011 15:37 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
mdelfede wrote on Sat, 10 September 2011 16:05

Maybe something added to Array class :

void Swap(int i, T*&ptr) { T *tmp = ptr; ptr = vector[i]; vector[i] = tmp; }


I'd need for ArrayMap too... I wonder if it's automatic or not, adding it to Array class.
Will try....

Max



Interesting idea. Not yet sure whether I like it, but maybe this would make it nicer:

T *Replace(int i, T *ptr) { T *tmp = vector[i]; vector[i] = ptr; return tmp; }
Re: Array : swapping element with a pointer [message #33765 is a reply to message #33764] Sun, 11 September 2011 15:51 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
The Replace() solution is really a nice idea indeed Smile

Max
Re: Array : swapping element with a pointer [message #33780 is a reply to message #33765] Mon, 12 September 2011 19:51 Go to previous message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
Thank you for the new

T* Swap(int i, T*ptr)


addition, very handy to speed up element replacement !

Max
Previous Topic: Why this one leaks ?
Next Topic: [SOLVED] String.GetCount with umlaut
Goto Forum:
  


Current Time: Sat Apr 20 16:21:35 CEST 2024

Total time taken to generate the page: 0.03384 seconds