473,597 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Roll your own std::vector ???

I need std::vector like capability for several custom classes. I already
discussed this extensively in the thread named ArrayList without Boxing and
Unboxing. The solution was to simply create non-generic (non C++ template)
std::vector like capability for each of these custom classes. (Solution must
work in Visual Studio 2002).

Since I have already written one std::vector for a YeOlde C++ compiler (Borland
C++ 1.0) that had neither templates nor STL, I know how to do this. What I don't
know how to do is to directly re-allocate memory in the garbage collected C#.

I have written what I need in pseudocode, what is the correct C# syntax for
this?

int Size;
int Capacity;

bool AppendDataItem( DataItemType Data) {
if (Size == Capacity) {
(1) Capacity = Capacity * 2; // Or * 1.5
(2) Temp = MemoryPointer;
(3) MemoryPointer = Allocate(Capaci ty);
(4) Copy Data from Temp to MemoryPointer;
(5) DeAllocate(Temp );
(6) MemoryPointer[Size] = Data;
(7) Size++;
}
}

Dec 17 '06
82 3966
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
pV************* *****@newsfe19. lga...

| The commonly understood basic principle where an array name is
| one-and-the-same-thing as an array address.

In that case, that would appear to be the principle for all .NET types. The
variable that you see *is* the object that the variable points to. There is
no indirection on the surface, even though the underlying framework may
involve such.

I think the thing that you are having trouble with is this lack of
indirection; when it comes to C#, you need to try to forget about ->, & and
* and simply think of variables as being one and the same as the object
which they hold. For the sake of your comprehension, forget about pointers
and addresses :-)

| Yes that is what I was talking about, they are both pointers within the
| underlying architecture.

Maybe but that should not concern you; forget pointers :-)).

| As I said in my previous post, the contents of value types are copied on
| assignment, but those of reference types are simply "pointed to" by the
| second reference.
|
| Array.Copy(ia, temp, ia.Length);
| ia = temp;
|
| So the last statement contains two reference types, even though the
underlying
| type of array element may be a value type or a reference type ???

Array is a reference type, regardless of the type that it holds. See Jon's
post as well.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 17 '06 #11

"Joanna Carter [TeamB]" <jo****@not.for .spamwrote in message
news:eJ******** ******@TK2MSFTN GP03.phx.gbl...
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
pV************* *****@newsfe19. lga...

| The commonly understood basic principle where an array name is
| one-and-the-same-thing as an array address.

In that case, that would appear to be the principle for all .NET types. The
variable that you see *is* the object that the variable points to. There is
no indirection on the surface, even though the underlying framework may
involve such.

I think the thing that you are having trouble with is this lack of
indirection; when it comes to C#, you need to try to forget about ->, & and
* and simply think of variables as being one and the same as the object
which they hold. For the sake of your comprehension, forget about pointers
and addresses :-)
One can't forget about this completely otherwise ones makes the mistake of
taking a shallow copy to be one-and-the-same-thing as a deep copy. I think that
..NET may have simplified this somewhat in some ways, I am currently not sure of
exactly how they did this. The way that this problem is typically simplified is
to always provide all of the overhead of a deep copy just in case that is what
was wanted.
>
| Yes that is what I was talking about, they are both pointers within the
| underlying architecture.

Maybe but that should not concern you; forget pointers :-)).
Yet then one must still wonder about the shallow versus deep copy problem, and
avoiding the overhead of the deep copy, when it is not needed. The best solution
at this point in time for applications programming might be to simply always do
a deep copy, and make doing a shallow copy syntactically impossible. There are
cases on systems programming where this would be unacceptable.
>
| As I said in my previous post, the contents of value types are copied on
| assignment, but those of reference types are simply "pointed to" by the
| second reference.
|
| Array.Copy(ia, temp, ia.Length);
| ia = temp;
|
| So the last statement contains two reference types, even though the
underlying
| type of array element may be a value type or a reference type ???

Array is a reference type, regardless of the type that it holds. See Jon's
post as well.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Dec 17 '06 #12
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
ZY************* *****@newsfe19. lga...

| So it look like if I want to avoid the huge overhead of boxing and
unboxing that
| value types have, I must comprise all of my (hand-rolled std::vector like)
| arrays of value type which can include elemental types such as int, char,
| double, and also composite types such as struct, but not composite types
such as
| class. Is this right ???

Or you can simply use List<Tgeneric class under .NET 2.0 as this provides
a native typesafe, dynamic collection that doesn't use any boxing or
unboxing.

WYSIWYG. It says it is a list of a cetain type, it *is* a list of a certain
type - no casting required.

Why do you want to write your own version of something that already exists
and does the job, possibly better than your attempt ?

{
List<intintList = new List<int>();

intList.Add(123 ); // compiles

intList.Add("12 3"); // will not compile

...
}

Or if you want to have your own generic Vector class, then do something like
this :

public class Vector<T: IEnumerable<T>
{
private List<Titems = new List<T>();

public int Size
{
get { return items.Count; }
}

public bool Empty
{
get { return items.Count == 0; }
}

public void PushBack(T item)
{
items.Add(item) ;
}

public T this[int index]
{
get { return items[index]; }
}

.... etc

#region IEnumerable members

public IEnumerator GetEnumerator()
{
return items.GetEnumer ator();
}

#endregion

#region IEnumerable<Tme mbers

public IEnumerator<TGe tEnumerator()
{
return items.GetEnumer ator();
}

#endregion
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 17 '06 #13
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
FF************* ****@newsfe22.l ga...

| One can't forget about this completely otherwise ones makes the mistake of
| taking a shallow copy to be one-and-the-same-thing as a deep copy. I think
that
| .NET may have simplified this somewhat in some ways, I am currently not
sure of
| exactly how they did this. The way that this problem is typically
simplified is
| to always provide all of the overhead of a deep copy just in case that is
what
| was wanted.

You don't need to think about pointers and addresses to separate shallow and
deep copy semantics.

All types support shallow cloning by means of the derived protected
System.Object.M emberwiseClone( ) method which simply copies the contents of
instance fields following the semantics of the field type.

| Yet then one must still wonder about the shallow versus deep copy problem,
and
| avoiding the overhead of the deep copy, when it is not needed. The best
solution
| at this point in time for applications programming might be to simply
always do
| a deep copy, and make doing a shallow copy syntactically impossible. There
are
| cases on systems programming where this would be unacceptable.

Deep cloning is usually only available if the type supports ICloneable, but
is still at the discretion of the implementer as to how deep that cloning
goes.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 17 '06 #14

"Joanna Carter [TeamB]" <jo****@not.for .spamwrote in message
news:Oh******** ******@TK2MSFTN GP03.phx.gbl...
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
ZY************* *****@newsfe19. lga...

| So it look like if I want to avoid the huge overhead of boxing and
unboxing that
| value types have, I must comprise all of my (hand-rolled std::vector like)
| arrays of value type which can include elemental types such as int, char,
| double, and also composite types such as struct, but not composite types
such as
| class. Is this right ???

Or you can simply use List<Tgeneric class under .NET 2.0 as this provides
a native typesafe, dynamic collection that doesn't use any boxing or
unboxing.
Of even better use the actual std::vector itself that is now available with the
currently released version of visual studio, that way I further still reduce my
learning curve. There are two reason why I don't want to upgrade yet, (1) Price,
(2) The exams refer to the older version.
>
WYSIWYG. It says it is a list of a cetain type, it *is* a list of a certain
type - no casting required.

Why do you want to write your own version of something that already exists
and does the job, possibly better than your attempt ?

{
List<intintList = new List<int>();

intList.Add(123 ); // compiles

intList.Add("12 3"); // will not compile

...
}

Or if you want to have your own generic Vector class, then do something like
this :

public class Vector<T: IEnumerable<T>
{
private List<Titems = new List<T>();

public int Size
{
get { return items.Count; }
}

public bool Empty
{
get { return items.Count == 0; }
}

public void PushBack(T item)
{
items.Add(item) ;
}

public T this[int index]
{
get { return items[index]; }
}

... etc

#region IEnumerable members

public IEnumerator GetEnumerator()
{
return items.GetEnumer ator();
}

#endregion

#region IEnumerable<Tme mbers

public IEnumerator<TGe tEnumerator()
{
return items.GetEnumer ator();
}

#endregion
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Dec 18 '06 #15
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
XB************* ***@newsfe14.lg a...

| Of even better use the actual std::vector itself that is now available
with the
| currently released version of visual studio, that way I further still
reduce my
| learning curve. There are two reason why I don't want to upgrade yet, (1)
Price,
| (2) The exams refer to the older version.

I think you will find that List<Tshould give you most of the functionality
you need. (1)You can get a copy of Visual Studio 2005 Express for free (2)
are you more interested in writing code and achieving results or passing
exams ? :-)

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 18 '06 #16

Are you doing all this work because you've profiled your app or done
tests to confirm that boxing/unboxing is an unacceptable performance
hit or is all this extra work and overhead just because you think
boxing is going to be a problem?

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Sun, 17 Dec 2006 08:42:52 -0600, "Peter Olcott"
<No****@SeeScre en.comwrote:
>I need std::vector like capability for several custom classes. I already
discussed this extensively in the thread named ArrayList without Boxing and
Unboxing. The solution was to simply create non-generic (non C++ template)
std::vector like capability for each of these custom classes. (Solution must
work in Visual Studio 2002).

Since I have already written one std::vector for a YeOlde C++ compiler (Borland
C++ 1.0) that had neither templates nor STL, I know how to do this. What I don't
know how to do is to directly re-allocate memory in the garbage collected C#.

I have written what I need in pseudocode, what is the correct C# syntax for
this?

int Size;
int Capacity;

bool AppendDataItem( DataItemType Data) {
if (Size == Capacity) {
(1) Capacity = Capacity * 2; // Or * 1.5
(2) Temp = MemoryPointer;
(3) MemoryPointer = Allocate(Capaci ty);
(4) Copy Data from Temp to MemoryPointer;
(5) DeAllocate(Temp );
(6) MemoryPointer[Size] = Data;
(7) Size++;
}
}

Dec 18 '06 #17
Systems programming is entirely different than applications programming. When
you are amortizing development costs over millions of users, you just don't test
something to see if its good enough. In this case you shoot for the ballpark of
as good as possible, right from the very beginning.

"Samuel R. Neff" <sa********@nom ail.comwrote in message
news:e9******** *************** *********@4ax.c om...
>
Are you doing all this work because you've profiled your app or done
tests to confirm that boxing/unboxing is an unacceptable performance
hit or is all this extra work and overhead just because you think
boxing is going to be a problem?

Sam

------------------------------------------------------------
We're hiring! B-Line Medical is seeking Mid/Sr. .NET
Developers for exciting positions in medical product
development in MD/DC. Work with a variety of technologies
in a relaxed team environment. See ads on Dice.com.

On Sun, 17 Dec 2006 08:42:52 -0600, "Peter Olcott"
<No****@SeeScre en.comwrote:
>>I need std::vector like capability for several custom classes. I already
discussed this extensively in the thread named ArrayList without Boxing and
Unboxing. The solution was to simply create non-generic (non C++ template)
std::vector like capability for each of these custom classes. (Solution must
work in Visual Studio 2002).

Since I have already written one std::vector for a YeOlde C++ compiler
(Borland
C++ 1.0) that had neither templates nor STL, I know how to do this. What I
don't
know how to do is to directly re-allocate memory in the garbage collected C#.

I have written what I need in pseudocode, what is the correct C# syntax for
this?

int Size;
int Capacity;

bool AppendDataItem( DataItemType Data) {
if (Size == Capacity) {
(1) Capacity = Capacity * 2; // Or * 1.5
(2) Temp = MemoryPointer;
(3) MemoryPointer = Allocate(Capaci ty);
(4) Copy Data from Temp to MemoryPointer;
(5) DeAllocate(Temp );
(6) MemoryPointer[Size] = Data;
(7) Size++;
}
}


Dec 18 '06 #18

"Joanna Carter [TeamB]" <jo****@not.for .spamwrote in message
news:Oo******** *****@TK2MSFTNG P06.phx.gbl...
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
XB************* ***@newsfe14.lg a...

| Of even better use the actual std::vector itself that is now available
with the
| currently released version of visual studio, that way I further still
reduce my
| learning curve. There are two reason why I don't want to upgrade yet, (1)
Price,
| (2) The exams refer to the older version.

I think you will find that List<Tshould give you most of the functionality
you need. (1)You can get a copy of Visual Studio 2005 Express for free (2)
are you more interested in writing code and achieving results or passing
exams ? :-)
Both simultaneously without any tradeoffs if possible.
(1) Where can I get this free copy?
(2) This free copy probably is not licensed for commercial use, and even if it
is, would most likely be missing the code optimizers.
>
Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer


Dec 18 '06 #19
"Peter Olcott" <No****@SeeScre en.coma écrit dans le message de news:
mG************* ****@newsfe19.l ga...

| Systems programming is entirely different than applications programming.
When
| you are amortizing development costs over millions of users, you just
don't test
| something to see if its good enough. In this case you shoot for the
ballpark of
| as good as possible, right from the very beginning.

There is a well known anti-pattern called "Premature Optimisation", beware
of it, prove that something is too slow before wasting time and effort
trying to speed it up.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Dec 18 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
5940
by: Jason Heyes | last post by:
To my understanding, std::vector does not use reference counting to avoid the overhead of copying and initialisation. Where can I get a reference counted implementation of std::vector? Thanks.
18
2855
by: Janina Kramer | last post by:
hi ng, i'm working on a multiplayer game for a variable number of players and on the client side, i'm using a std::vector<CPlayer> to store informatik about the players. CPlayer is a class that contains another std::vector<CPosition>. Because one of the players is the client itself (and the size of the vector<CPlayer> doesn't change during a game), i thought i could store a std::vector<CPlayer>::iterator "localplayer" that points to the...
20
17778
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
17
3342
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling this uovec at the moment (for Unit-Offset VECtor). I want the class to respond correctly to all usage of STL containers and algorithms so that it is a transparent replacement for std:vector. The options seems to be:
8
5102
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
32
69668
by: zl2k | last post by:
hi, c++ user Suppose I constructed a large array and put it in the std::vector in a function and now I want to return it back to where the function is called. I can do like this: std::vector<int> fun(){ //build the vector v; return v; }
56
5744
by: Peter Olcott | last post by:
I am trying to refer to the same std::vector in a class by two different names, I tried a union, and I tried a reference, I can't seem to get the syntax right. Can anyone please help? Thanks
9
8890
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter; Any ideas why is tihs happening? The code is as follows: template <class T> struct StdVectorStorage { std::vector<T>* _storage;
13
2949
by: jubelbrus | last post by:
Hi I'm trying to do the following. #include <vector> #include <boost/thread/mutex.hpp> #include <boost/shared_ptr.hpp> #include <boost/tuple/tuple.hpp> class {
0
7971
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7893
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8040
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8259
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5847
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
1495
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1243
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.