P
Peter Olcott
Arne Vajhøj said:Both ArrayList and List<> has a constructor with an int argument
specifying initial capacity.
Arne
These can refer to any type of structure?
Which of these is closest to std::vector?
Arne Vajhøj said:Both ArrayList and List<> has a constructor with an int argument
specifying initial capacity.
Arne
Peter said:Can allocating an ordinary array, be done through the managed heap? If it can
not be done using the managed heap, then wouldn't allocating unmanaged memory be
considered a poor practice in terms of good .NET design?
There are two different facets to my problem, one requiring the array to
dynamically grow, and the other most time critical one, knows its size in
advance.
Even the one that is required to dynamically grow, must do this
relatively quickly, thus can not take the boxing / unboxing overhead. I wanted
to see if I could adapt my system to .NET using my current compiler, the answer
is no. The next level question is can my system be adapted to .NET at all, the
answer is yes if I use generics.
Bruce Wood said:Who said anything about unmanaged memory? I'm talking about using a
regular, boring managed array. Fixed-size arrays in .NET can be of any
type, including value types, and so required no boxing and unboxing in
order to store value types. There is no need to use unmanaged arrays.
The use a dynamic structure for the first and a vanilla fixed-size
array for the second. Using a fixed-size array will certainly bring
speed benefits.
No, the answer is even better than that. The answer is that you can
achieve a high-speed dynamic structure in .NET 1.1, but you have to
program it yourself. Remember that ArrayList is just a wrapper around
an array that, when you attempt to add one element too many,
reallocates the array and copies the contents. You can write your own
ArrayList specific to your value type that will have no boxing /
unboxing overhead. The only thing that .NET 2.0 gives you is the
ability to use the dynamic data structures provided by the .NET
Framework without incurring boxing / unboxing overhead.
Peter said:These can refer to any type of structure?
Which of these is closest to std::vector?
Peter said:Can allocating an ordinary array, be done through the managed heap? If it can
not be done using the managed heap, then wouldn't allocating unmanaged memory be
considered a poor practice in terms of good .NET design?
Peter said:Actually it must be any array of struct of scalar types, 8-bit, 16-bit, and
32-bit integers, all unsigned.
struct MyType {
uint One;
ushort Two;
byte Three;
};
How do I go about creating the equivalent of a std::vector<MyType> that does not
ever have boxing and unboxing overhead?
Arne Vajhøj said:Peter said:Actually it must be any array of struct of scalar types, 8-bit, 16-bit, and
32-bit integers, all unsigned.
struct MyType {
uint One;
ushort Two;
byte Three;
};
How do I go about creating the equivalent of a std::vector<MyType> that does
not ever have boxing and unboxing overhead?
If you know the size:
MyType[] myarray = new MyType[noelm];
If you don't know the size and are on .NET 2.0:
List<MyType> mylist = new List<MyType>();
Arne
Peter said:Arne Vajhøj said:If you know the size:
MyType[] myarray = new MyType[noelm];
If you don't know the size and are on .NET 2.0:
List<MyType> mylist = new List<MyType>();
Arne
Bruce said that this could be done using .NET 1.1you can achieve a high-speed dynamic structure in .NET 1.1, but you have to
program it yourself.
Arne Vajhøj said:Peter said:Arne Vajhøj said:If you know the size:
MyType[] myarray = new MyType[noelm];
If you don't know the size and are on .NET 2.0:
List<MyType> mylist = new List<MyType>();
Arne
Bruce said that this could be done using .NET 1.1you can achieve a high-speed dynamic structure in .NET 1.1, but you have to
program it yourself.
You can not make a fast generic one in 1.1, but you can make a fast
one specific for your type.
Arne
Peter said:That is all that I need. How hard would this be? I already wrote a complete
std::vector from scratch, (for a C++ compiler lacking this capability) does it
require this same amount of programming?
It also must dynamically re-allocate when it needs to grow in size.Arne Vajhøj said:Probably less.
If you only need a constructor + an Add method + an indexer
then it can not be many lines of code.
Arne
Peter said:It also must dynamically re-allocate when it needs to grow in size.
Peter said:It also must dynamically re-allocate when it needs to grow in size.
Peter said:It also must dynamically re-allocate when it needs to grow in size.
Peter said:It also must dynamically re-allocate when it needs to grow in size.
Peter said:public MyValueType this[uint index] // does this eliminate the possibility of
index < 0 ?
{
get { return this._vector[index]; }
set { this._vector[index] = value; }
}
Bruce Wood said:Peter said:public MyValueType this[uint index] // does this eliminate the possibility
of
index < 0 ?
{
get { return this._vector[index]; }
set { this._vector[index] = value; }
}
Yes, but you will have to mark the method as non-CLS compliant because
some .NET languages don't support unsigned types. No biggie unless
you're planning on calling the code from another .NET language.
And no, C# has no "inline" indication. That is left entirely up to the
compiler and the JITter.
Yes, C# generics offer similar functionality to C++ templates, although
the former are compiler constructs while the latter are
text-substitution constructs, which introduces some subtle differences.
Nonetheless, you can think of them as the "same thing" for basic usage
purposes.
Peter said:Bruce Wood said:Peter said:public MyValueType this[uint index] // does this eliminate the possibility
of
index < 0 ?
{
get { return this._vector[index]; }
set { this._vector[index] = value; }
}
Yes, but you will have to mark the method as non-CLS compliant because
some .NET languages don't support unsigned types. No biggie unless
you're planning on calling the code from another .NET language.
And no, C# has no "inline" indication. That is left entirely up to the
compiler and the JITter.
Yes, C# generics offer similar functionality to C++ templates, although
the former are compiler constructs while the latter are
text-substitution constructs, which introduces some subtle differences.
Nonetheless, you can think of them as the "same thing" for basic usage
purposes.
Now all that remains is to see if your get/set code mentioned above is placed
inline.
Bruce Wood said:Peter said:Bruce Wood said:Peter Olcott wrote:
public MyValueType this[uint index] // does this eliminate the
possibility
of
index < 0 ?
{
get { return this._vector[index]; }
set { this._vector[index] = value; }
}
Yes, but you will have to mark the method as non-CLS compliant because
some .NET languages don't support unsigned types. No biggie unless
you're planning on calling the code from another .NET language.
And no, C# has no "inline" indication. That is left entirely up to the
compiler and the JITter.
Yes, C# generics offer similar functionality to C++ templates, although
the former are compiler constructs while the latter are
text-substitution constructs, which introduces some subtle differences.
Nonetheless, you can think of them as the "same thing" for basic usage
purposes.
Now all that remains is to see if your get/set code mentioned above is placed
inline.
I know that the compiler inlines getters and setters that do nothing
but fetch and assign a corresponding field. I don't see why an indexer
would be any different.
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.