separate array into sub-arrays

Y

Yanhao Zhu

Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!


Thanks,
Yanhao Zhu
 
D

Daniel O'Connell

First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);
 
Y

Yanhao Zhu

Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays
when I separate the original one. When the array size is very large, I think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu


Daniel O'Connell said:
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

Yanhao Zhu said:
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!


Thanks,
Yanhao Zhu
 
J

Jay B. Harlow [MVP - Outlook]

Yanhao Zhu,
It sounds like you want to define a ProxyArray class.

The ProxyArray would be a class that holds a reference to your original
array, plus a start index & length into that array. Then all the methods on
the proxy would validate against this length and adjust by start index to
retrieve & update values on the actual array.

ProxyArray m01 = New ProxyArray(m,0,2) // m01 will hold 1st and 2nd
ProxyArray m23 = New ProxyArray(m,2,2) // m23 will hold srd and 4th

Note the ProxyArray does not hold any of the values, it holds a reference to
the actual array, plus a start index & a length.

I would consider having ProxyArray implement IList, with most methods
delegating to the actual Array.

Of course with Generics this will be much better ;-)

Hope this helps
Jay

Yanhao Zhu said:
Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays
when I separate the original one. When the array size is very large, I think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu


Daniel O'Connell said:
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

Yanhao Zhu said:
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!


Thanks,
Yanhao Zhu
 
D

Daniel O'Connell

I see what you mean, try to post more clearly, as it stands & means little
in the context of generic managed code.

Anyway, the solution Jay suggested would probably be ideal.
Yanhao Zhu said:
Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays
when I separate the original one. When the array size is very large, I think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu


Daniel O'Connell said:
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

Yanhao Zhu said:
Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!


Thanks,
Yanhao Zhu
 
Y

Yanhao Zhu

Hi, Jay,

Thank you very much! This is what I want. But I am just wondering if there
is way to do this without using the proxy class, say, sub-array is the same
type of original one. :)


Thanks again! You answer is good enough for me.

Have a good one,
Yanhao Zhu

Jay B. Harlow said:
Yanhao Zhu,
It sounds like you want to define a ProxyArray class.

The ProxyArray would be a class that holds a reference to your original
array, plus a start index & length into that array. Then all the methods on
the proxy would validate against this length and adjust by start index to
retrieve & update values on the actual array.

ProxyArray m01 = New ProxyArray(m,0,2) // m01 will hold 1st and 2nd
ProxyArray m23 = New ProxyArray(m,2,2) // m23 will hold srd and 4th

Note the ProxyArray does not hold any of the values, it holds a reference to
the actual array, plus a start index & a length.

I would consider having ProxyArray implement IList, with most methods
delegating to the actual Array.

Of course with Generics this will be much better ;-)

Hope this helps
Jay

Yanhao Zhu said:
Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays
when I separate the original one. When the array size is very large, I think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu


Daniel O'Connell said:
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd
items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th
items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!


Thanks,
Yanhao Zhu
 
Y

Yanhao Zhu

Yes, Thank you very much!
Daniel O'Connell said:
I see what you mean, try to post more clearly, as it stands & means little
in the context of generic managed code.

Anyway, the solution Jay suggested would probably be ideal.
Yanhao Zhu said:
Hi, Daniel,

My problem is that I don't want to allocate new space for the two sub-arrays
when I separate the original one. When the array size is very large, I think
it is better if the two sub-arrays can just reference the same memory
address as the original one.

Thanks a lot anyway!

Best,
Yanhao Zhu


Daniel O'Connell said:
First thing that comes to mind is Array.Copy:

int[] i1 = {1,2,3,4};
int[] i2=new int[2];
int[] i3= new int[2];
Array.Copy(i1,0,i2,0,2);
Array.Copy(i1,2,i3,0,2);

Hi, all,

If I have an array like
int[] m = new int[] { 0, 1, 2, 3 },
is there a way I can separate the array into two, like
int[] m01 = somefunction?(m,0,2) // m01 will hold 1st and 2nd
items
in array m
int[] m23 = somefunction?(m,2,2) // m23 will hold srd and 4th
items
in array m ,
and &m01 = &m, &m23 = &m + 2, always, (& is the get address of operation)

In another words, what I want to do is to separate the array into
sub-arrays, and the sub-arrays should reference the same items in the
original array.

Any idea will be appreciated!


Thanks,
Yanhao Zhu
 

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

Top