Click to See Complete Forum and Search --> : assigning array subranges


michelle
01-26-2001, 04:04 PM
FORTRAN90 allows assigning array subranges. Is there a way to make C++ do
this?

Assuming a class called MyArray has been defined, something like:
main()
{
MyArray a(10, 10), b(5,5); // declare a 10x10 and a 5x5 array

a(3:5, 6:7) = b(1:3, 1:2);
}


I'm thinking that it would actually look more messy, like b(1,3,1,2), to
accomodate overloading the
function operator.

I've tried a couple of ways, but all I've gotten is an unhappy compiler.

Danny Kalev
01-26-2001, 11:31 PM
you can use memcpy (string.h) to assign array subranges. For example:

int arr[5]={0}; //initialize all elements to 0
int arr2[10]={1,1,1,1,1,1,1,1,1,1};


memcpy(&arr2[2], &arr[2], sizeof[int*3]);

this writes the elements arr[2], arr[3] and arr[4] into the
arr2[2],arr2[3] and arr2[4], respectively. As a result, arr2 becomes:
{1,1,0,0,0,1,1,1,1,1};
^ ^
| |
---------subarray inserted here


However, this technique is bug prone and messy, and it works only with
built-in types. In C++, you can use a vector class and call the assign()
member function or the copy algorithm to achieve the same effect. I
won't go through this right now because memcpy is the closest thing to
Fortarn's subrange copy but you should be aware that there are better
techniques.

Danny
michelle wrote:
>
> FORTRAN90 allows assigning array subranges. Is there a way to make C++ do
> this?
>
> Assuming a class called MyArray has been defined, something like:
> main()
> {
> MyArray a(10, 10), b(5,5); // declare a 10x10 and a 5x5 array
>
> a(3:5, 6:7) = b(1:3, 1:2);
> }
>
> I'm thinking that it would actually look more messy, like b(1,3,1,2), to
> accomodate overloading the
> function operator.
>
> I've tried a couple of ways, but all I've gotten is an unhappy compiler.

michelle
01-29-2001, 09:53 AM
at the heart of my data structure is a pointer to a built-in type, so memcpy
may work. i've been playing
around with using a vector class instead, but i haven't come up with anything
that's satisfactory. i can
make things work with 1-d arrays, but when i try to go to 2-d, i get performance
problems. can
you tell me more about using the copy algorithm? thanks.


Danny Kalev <dannykk@inter.net.il> wrote:
>you can use memcpy (string.h) to assign array subranges. For example:
>
>int arr[5]={0}; //initialize all elements to 0
>int arr2[10]={1,1,1,1,1,1,1,1,1,1};
>
>
>memcpy(&arr2[2], &arr[2], sizeof[int*3]);
>
>this writes the elements arr[2], arr[3] and arr[4] into the
>arr2[2],arr2[3] and arr2[4], respectively. As a result, arr2 becomes:
>{1,1,0,0,0,1,1,1,1,1};
> ^ ^
> | |
>---------subarray inserted here
>
>
>However, this technique is bug prone and messy, and it works only with
>built-in types. In C++, you can use a vector class and call the assign()
>member function or the copy algorithm to achieve the same effect. I
>won't go through this right now because memcpy is the closest thing to
>Fortarn's subrange copy but you should be aware that there are better
>techniques.
>
>Danny
>michelle wrote:
>>
>> FORTRAN90 allows assigning array subranges. Is there a way to make C++
do
>> this?
>>
>> Assuming a class called MyArray has been defined, something like:
>> main()
>> {
>> MyArray a(10, 10), b(5,5); // declare a 10x10 and a 5x5 array
>>
>> a(3:5, 6:7) = b(1:3, 1:2);
>> }
>>
>> I'm thinking that it would actually look more messy, like b(1,3,1,2),
to
>> accomodate overloading the
>> function operator.
>>
>> I've tried a couple of ways, but all I've gotten is an unhappy compiler.

Danny Kalev
01-29-2001, 10:41 AM
the copy algorithms takes three arguments: two iterators indicating the
beginning and the end of a sequence to be copied and a buffer into which
the elements are copied. This is similar to memcpy except that instead
of pointers you use iterators and instead of raw memory it copies
objects, including proper construction thereof. Here's an example that
shows how to copy a list object into a vector using the copy algorithm:

#include <algorithm>
#include<list>
#include<vector>
using namespace std;

int main()
{
list<int> li; vector <int> vi;
li.push_back(1); // fill the list with elements
li.push_back(2);
vi.reserve( li.size() ); // a vector must make room for copied
elements in advance
copy (li.begin(), li.end(), vi.begin() ); //copy list elements into
vector, starting at vector's beginning
}

Danny

michelle wrote:
>
> at the heart of my data structure is a pointer to a built-in type, so memcpy
> may work. i've been playing
> around with using a vector class instead, but i haven't come up with anything
> that's satisfactory. i can
> make things work with 1-d arrays, but when i try to go to 2-d, i get performance
> problems. can
> you tell me more about using the copy algorithm? thanks.
>
> Danny Kalev <dannykk@inter.net.il> wrote:
> >you can use memcpy (string.h) to assign array subranges. For example:
> >
> >int arr[5]={0}; //initialize all elements to 0
> >int arr2[10]={1,1,1,1,1,1,1,1,1,1};
> >
> >
> >memcpy(&arr2[2], &arr[2], sizeof[int*3]);
> >
> >this writes the elements arr[2], arr[3] and arr[4] into the
> >arr2[2],arr2[3] and arr2[4], respectively. As a result, arr2 becomes:
> >{1,1,0,0,0,1,1,1,1,1};
> > ^ ^
> > | |
> >---------subarray inserted here
> >
> >
> >However, this technique is bug prone and messy, and it works only with
> >built-in types. In C++, you can use a vector class and call the assign()
> >member function or the copy algorithm to achieve the same effect. I
> >won't go through this right now because memcpy is the closest thing to
> >Fortarn's subrange copy but you should be aware that there are better
> >techniques.
> >
> >Danny
> >michelle wrote:
> >>
> >> FORTRAN90 allows assigning array subranges. Is there a way to make C++
> do
> >> this?
> >>
> >> Assuming a class called MyArray has been defined, something like:
> >> main()
> >> {
> >> MyArray a(10, 10), b(5,5); // declare a 10x10 and a 5x5 array
> >>
> >> a(3:5, 6:7) = b(1:3, 1:2);
> >> }
> >>
> >> I'm thinking that it would actually look more messy, like b(1,3,1,2),
> to
> >> accomodate overloading the
> >> function operator.
> >>
> >> I've tried a couple of ways, but all I've gotten is an unhappy compiler.