Author Topic: PCL: building an array and clearing it again  (Read 5533 times)

Offline Nocturnal

  • PixInsight Jedi Council Member
  • *******
  • Posts: 2727
    • http://www.carpephoton.com
PCL: building an array and clearing it again
« on: 2009 May 09 21:05:43 »

Hi,

I'm cracking my head open on how to use the Array<T> template. All I want is for one of my classes to have an array of objects as a member variable. Then I want to delete the entire array of objects and start over.

How can I do something like this:

Array<int> ia(5);

ia.Clear();
ia.NewSize(20);
ia.Clear();
ia.NewSize(15);

?

Should I do this instead?

Array<int> *ia = Array<int>(5);

ia = &Array<int>(5);

delete ia;
ia = new Array<int>(10);


But then I can't do ia[index] and as far as I can tell there's no ia->Member(index) method.

I'm trying to boil this down to a simple example but it's hard. The essence is that I need an array of objects that I can either resize or destroy and rebuild at will. The array is a member of a class.

The documentation isn't all that helpful :)

Code: [Select]
### TODO: Write a detailed description for Array.
Best,

    Sander
---
Edge HD 1100
QHY-8 for imaging, IMG0H mono for guiding, video cameras for occulations
ASI224, QHY5L-IIc
HyperStar3
WO-M110ED+FR-III/TRF-2008
Takahashi EM-400
PIxInsight, DeepSkyStacker, PHD, Nebulosity

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PCL: building an array and clearing it again
« Reply #1 on: 2009 May 09 23:33:34 »
Morning!

Having this declaration:

Code: [Select]
Array<int> ia( 5 );
what you want to do is as follows:

Code: [Select]
ia.Remove();
ia.Add( 0, 20 );
// ...
ia.Remove();
ia.Add( 0, 15 );

Alternatively (and slightly more efficiently because avoids clearing and initializing the new array to zeros):

Code: [Select]
ia = Array<int>( 20 );
// ...
ia = Array<int>( 15 );

That assignments are possible and extremely efficient because Array<T> is a reference counted class.

The new PCL that comes with v1.5 introduces a new set of lightweight containers that are more oriented to numerical tasks than Array. They are GenericVector<T> and GenericMatrix<T>. But Array<T> works nicely (and is compatible with GenericVector<T> so you keep your current work ).

Code: [Select]
The documentation isn't all that helpful smile
Touchée...  :P
« Last Edit: 2009 May 09 23:35:27 by Juan Conejero »
Juan Conejero
PixInsight Development Team
http://pixinsight.com/

Offline Nocturnal

  • PixInsight Jedi Council Member
  • *******
  • Posts: 2727
    • http://www.carpephoton.com
Re: PCL: building an array and clearing it again
« Reply #2 on: 2009 May 10 07:02:44 »
Thanks, I'll give that a try. I saw the 'Remove' method but the name suggests it takes out a single element rather than clearing the entire array.

*added*

and with the 'reference count' bit you mean that when I reassign a new array to my member variable the old one gets deleted properly so I don't have a memory leak?
« Last Edit: 2009 May 10 07:10:33 by Nocturnal »
Best,

    Sander
---
Edge HD 1100
QHY-8 for imaging, IMG0H mono for guiding, video cameras for occulations
ASI224, QHY5L-IIc
HyperStar3
WO-M110ED+FR-III/TRF-2008
Takahashi EM-400
PIxInsight, DeepSkyStacker, PHD, Nebulosity

Offline Juan Conejero

  • PTeam Member
  • PixInsight Jedi Grand Master
  • ********
  • Posts: 7111
    • http://pixinsight.com/
Re: PCL: building an array and clearing it again
« Reply #3 on: 2009 May 10 11:39:46 »
Quote
I saw the 'Remove' method but the name suggests it takes out a single element rather than clearing the entire array.

Array<T> has several overloaded versions of Remove():

Code: [Select]
void Array<T>::Remove( iterator i, size_type n = 1 ) // remove n items starting at i
void Array<T>::Remove( iterator i, iterator j ) // remove the sequence [i,j[
void Array<T>::Remove( const T& v ) // remove all occurences of v
void Array<T>::Remove( const T& v, BP p ) // same but using binary predicate p
void Array<T>::Remove() // remove all items in the array

Quote
and with the 'reference count' bit you mean that when I reassign a new array to my member variable the old one gets deleted properly so I don't have a memory leak?

Indeed. You can use Array<T> as a function return value and you can assign arrays directly. There are no memory leaks at all; all memory management occurs behind the scenes. The PCL (and the whole PI core application) uses reference counting and implicit data sharing techniques extensively.
Juan Conejero
PixInsight Development Team
http://pixinsight.com/