Home » U++ Library support » U++ Core » Arrys vs Vectors
Arrys vs Vectors [message #30260] |
Tue, 21 December 2010 10:43 |
281264
Messages: 272 Registered: June 2010 Location: Spain
|
Experienced Member |
|
|
Hi,
Some queries regarding the appropriateness of Arrys or Vectors. Please consider a generic scenario:
1.- In the manual it is mentioned that Arrays are significantly slower than Vectors. In a real application, is this noticeable? Does it have an strong influence in the application’s performance?.
2.- Programming wise, are there important differences? For example, are Arrays more complex to program and debug?.
3.- Debugging: If we store a class in Array, how to visualize the data of the classes stored in the Array with the debugger? Apparently this is not possible, which penalizes Array in comparison to Vectors (please correct me if I am wrong).
4.- Memory consumption wise: manual says that for small size of Array, this is more memory consuming than its Vector counterpart and improvements appears as Arrys sizes grows. Any hints about this? Any real figures from real applications?
5.- Pointers: apparently if you need pointers to classes, then Arrays is the only option available. What other limitations Arrys have compared with Vectors and vice versa?.
6.- In general (perhaps this is a non sense question) what is your preferred container?.
Thank you.
Best wishes,
Javier
|
|
|
Re: Arrys vs Vectors [message #30264 is a reply to message #30260] |
Tue, 21 December 2010 11:20 |
|
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
1) in general, no. if OTOH you use the containers in a 'high performance scenario, loops, creation, destruction, adding, it might be of interest to use Vector, especially for intrinsic types. but for GUI applications as storage for some user data structure, it has no perceptible difference.
2) the most important difference is, that Vector holds the mem space for the objects itself in a sequence and creates the objects (calls constructor) there, while Array creates the objects somewhere else on the heap, and stores only the pointers to them. thats why Adding to Vector can render any previous referenece to an object inside it as invalid, thats also why the objects need to be Moveable<>. see manual. Arrays dont need those precautions. in Vector, you cant Detach an element, and Attach() it in another Vector, in Array, you can, whithout even touching the element itself (handling over pointers in Array).
programming with both is quite the same, infact, Upp has MACROS which apply to a variety of Containers, because they have a huge subset of common Methods, like GetCount(), Add(), etc.
debugging is not really easy, since Upp doesnt yet (AFAIK) support expressions, so things like looking at vec[2] are not possible. but you can look at v[0] indirectly. inspectiong them in terms of count, allocated mem etc is no problem.
3) explained above, they both lack the same. maybe try to hassle with the Vector<void*> vector inside the Array, '*(MyClass*)arr.vector[12]' or sth.
4) depends on use case and whether it really matters, best is to check the implementations, they are not too hard to understand, knowing the basics from manual.
5) correctly speaking, if you need references in which ever way to classes, beeing it pointers, or refs, that should not get invalid once you manipulate the container, (adding is dangerous, needs growing and 'moving' objects in Vector), Array is the only choice. it also grows, but only moves the pointers to the objects, which remain in same place.
6) Vector<> for intrinsic types, Buffers, 'oldschool' arrays (MyType ar[12] replacement, Arrays, if type is quite complex, or need to 'move' them around, by detaching/ataching somewhere else.
|
|
|
|
|
Re: Arrys vs Vectors [message #30268 is a reply to message #30267] |
Tue, 21 December 2010 12:08 |
281264
Messages: 272 Registered: June 2010 Location: Spain
|
Experienced Member |
|
|
Data are geometric objects (that require to be painted with OpenGL in a loop). For example:
template<class T>
class Point3D:MoveableAndDeepCopyOption<Point3D<T>>{
private:
T x,y,z,w;
unsigned int id,layer;
bool picked;
void Copy(const Point3D<T>& src){
x=src.x;
y=src.y;
z=src.z;
w=src.w;
id=src.id;
picked=src.picked;
layer=src.layer;
}
public:
Point3D (){
x=T();
y=T();
z=T();
w=static_cast<T>(1.0);
id=0;picked=0;
}
Point3D (T a, T b, T c,unsigned int e, bool f, unsigned int g, T d=static_cast<T>(1.0)){
x=a;y=b;z=c;w=d;
id=e;
picked=f;
layer=g;
}
Point3D (Point3D<T> &point){
x=point.get_x();y=point.get_y();z=point.get_z();w=point.get_w();
id=point.get_id();
picked=point.get_picked();
layer=point.get_layer();
}
Point3D (const Point3D<T> &src,int){Copy(src);}
void set_x(T a){x=a;}
void set_y(T a){y=a;}
void set_z(T a){z=a;}
void set_w(T a){w=a;}
void set_id(unsigned int a){id=a;}
void set_picked(bool a){picked=a;}
void set_layer(unsigned int a){layer=a;}
T get_x()const{return x;}
T get_y()const{return y;}
T get_z()const{return z;}
T get_w()const{return w;}
unsigned int get_id(){return id;}
bool get_picked(){return picked;}
unsigned int get_layer(){return layer;}
void operator*(const T& obj){
x=x*obj;
y=y*obj;
z=z*obj;
w=w*obj;
}
Point3D<T> operator+(const Point3D<T> &obj){
return Point3D<T>(x+obj.get_x(),y+obj.get_y(),z+obj.get_z(),w+obj.get_w());
}
void Serialize(Stream& s){
s%x%y%z%w%id%picked%layer;
}
String ToString()const{
String s;
s<<"x:"<<x<<","<<"y:"<<y<<","<<"z:"<<z<<","<<"w:"<<w<<","<<"id:"<<id<<","<<"picked:"<<picked<<", "<<"layer:"<<layer;
return s;
}
};
I am using other geometrical structures, for example line3D, nurbs lines and surfaces.
Let us take a line. Here is the question: the two points that form part of a line, is it better to reference them by using pointers (hence Array has to be used to store the point3D) or by emulating their content or by storing a simple identifier (an unsigned int which looks the easiest way to do it)?.
Remark: I have tried to debug an Arry<int> array and added an integer array.Add(1): when debugging U++ crashes when clicking on the .vector, what is it going on wrong? It is weird, is it not?
Thanks,
Javier
|
|
|
Re: Arrys vs Vectors [message #30270 is a reply to message #30268] |
Tue, 21 December 2010 12:36 |
|
kohait00
Messages: 939 Registered: July 2009 Location: Germany
|
Experienced Contributor |
|
|
i'd recommend to use Vector here, see Core/Gtypes.h for Size and Point implementations, they also are templates, and are only Moveable<>, no need for you to MoveableAndDeepCopyOption<> them. it is a lightweight pointer class as i can see. it is best copied. i suppose T is double or float..
picked is misleading... Upp has own usage for pick stuff, think of a different name for it.
also, use the initializer list in constructors, and the implicit logic of classes (aka implicit copy constructor).
think of what you really need to have getters/setters to. better make the members public in this case.
template<class T>
class Point3D : Moveable<Point3D<T> >
{
public:
T x,y,z,w;
unsigned int id,layer;
bool taken;
public:
Point3D ()
: x(T())
, y(T())
, z(T())
, w((T)1.0)
, id(0)
, layer(-1)
, taken(0)
{}
Point3D (const T& a, const T& b, const T& c, unsigned int e, bool f, unsigned int g, const T& d=(T)1.0)
: x(a)
, y(b)
, z(c)
, w(d)
, id(e)
, layer(g)
, taken(f)
{}
//use implicit copy constructor
//this one should probaly be Point3D<T> operator*(const T& o) const, since yours is modifying object
void operator*(const T& o){
x*=o;
y*=o;
z*=o;
w*=o;
}
Point3D<T> operator+(const Point3D<T> &o){
return Point3D<T>(x+o.x(),y+o.y(),z+o.z(),w+o.w());
}
void Serialize(Stream& s){
s%x%y%z%w%id%taken%layer;
}
String ToString()const{
String s;
s<<"x:"<<x<<","<<"y:"<<y<<","<<"z:"<<z<<","<<"w:"<<w<<","<<"id:"<<id<<","<<"taken:"<<taken<<", "<<"layer:"<<layer;
return s;
}
};
GUI_APP_MAIN
{
Vector<Point3D<double> > v;
v.Add();
Vector<Point3D<double> > vw;
vw <<= v;
Point3D<double> p;
p = v[0];
}
|
|
|
|
|
Re: Arrys vs Vectors [message #30281 is a reply to message #30260] |
Tue, 21 December 2010 18:20 |
281264
Messages: 272 Registered: June 2010 Location: Spain
|
Experienced Member |
|
|
Thanks.
I am afraid that I’ll need to use Array instead of Vector for I a reference between geometric objects (for example, a Line2D needs to point to its two constituent points Point2D).
An additional question, please. Have a look to these two classes:
template<class T>
class Point2D:Moveable<Point2D<T>>{
private:
T x,y;
public:
Point2D():x(T()),y(T()){}
Point2D(const T &a, const T &b): x(a),y(b){}
T get_x()const{return x;}
T get_y()const{return y;}
void set_x(T a){x=a;}
void set_y(T a){y=a;}
Point2D<T> operator+(const Point2D<T> &obj){
return Point3D<T>(x+obj.x,y+obj.get_y());
}
};
and
template<class T>
class Line2D:Moveable<Line2D<T>>{
private:
Point2D<T> *p1,*p2;
public:
Line2D():p1(),p2(){}
Line2D(const Point2D<T> &point1, const Point2D<T> &point2):
p1(&point1),
p2(&point2)
{}
Point2D<T> get_p1() const{return *p1;}
Point2D<T> get_p2() const{return *p2;}
void set_p1(const Point2D<T> &p){p1=&p;}
void set_p2(const Point2D<T> &p){p2=&p;}
};
The compiler is claiming about the constructor in Point2D; it says that it cannot assign const pointers to *p1 and *p2. Why is this?. I would appreciate your advice.
Thanks.
Cheers,
Javier
|
|
|
|
|
|
|
|
|
Re: Arrys vs Vectors [message #30338 is a reply to message #30277] |
Sat, 25 December 2010 10:57 |
|
mirek
Messages: 14039 Registered: November 2005
|
Ultimate Member |
|
|
281264 wrote on Tue, 21 December 2010 11:23 | Roger. DeepCopyOption is not needed. Your hints are also correct, thanks.
But, what about my question regarding Array<int> and the debugger? U++ is crashing when clicking in the container during debug.
|
Sorry about that. I would like to fix it, can you describe the exact way how to crash it?
Quote: |
It is frustrating to see that the only way to inspect an Array is through a function.
|
Well, over years, I have rather got used to debug things trough .logs. Especially if larger data (-> containers) or GUI are in play, logging IMO is much more productive approach.
I believe it worth to try for you, just place DDUMPC(container) here and there and see how it goes...
Another option is to use msdev.exe (Visual C++ environment) for debugging. I know Tom does this all the time. You can configure theide to use msdev as 'external' debugger. And today, it is free. But I doubt it is any helpfil with containers either.
Certainly, improving debugger would be nice to
Hm, now thinking about it, maybe something like:
template <class T>
class Array : public MoveableAndDeepCopyOption< Array<T> > {
protected:
#ifdef _DEBUG
Vector<T *> vector;
#else
Vector<void *> vector;
#endif
might be helpful...
Mirek
|
|
|
|
|
|
Re: Arrys vs Vectors [message #30389 is a reply to message #30380] |
Mon, 27 December 2010 13:29 |
|
mirek
Messages: 14039 Registered: November 2005
|
Ultimate Member |
|
|
281264 wrote on Mon, 27 December 2010 05:51 | Thank you for your answers and advises.
Regarding DUMPC et al. I am getting used to them and find DUMPC and Logfile very useful. It works very well, thanks. Remark: By the way,
**Query: what are DUMPCC, DUMPCCC and DDUMPC for?. So far I only know DUMP and DUMPC.
|
DUMPCC is for dumpning container of containers...
Quote: |
Another question more related to C++: the usage of pointers and Arrays of pointers as class member variables is difficult to me for things like s%*p1…. In Serialize function does not work.
|
Well, the U++ way is to try to avoid pointers as much as possible...
Why not use proper objects instead?
Quote: |
**Query: if I have an Array<ptr*> as a variable private member of a class, does it work? Is it any limitation in the NTL containers for being used as containers of pointers to classes as member variables of a class?
|
No. Just you have to keep in mind that the object then in container is pointer...
Also, if you need to store pointers, Vector<ptr*> is most likely the better option.
Quote: |
Regarding DeepCopyOptionness I am using it in all my classes for I find it very useful.
**Query: Does it penalize or increases the complexity of the code in any way whatsoever?
|
DeepCopy _Option_ is definitely fine as it is only used explicitly.
Still, I surprised you are using that so much. I found using myself the deep-copy-option quite rarely.
Quote: |
**Query: Is there any way to preserve the U++ personal configuration between installations?
|
I guess once you get accustomed, you will start recompiling theide based on svn - then you just replace theide.exe and keep configuration.
I guess the "U++ installation" should perhaps rather be considered to be "U++ starter pack"
Mirek
[Updated on: Mon, 27 December 2010 13:30] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Fri Sep 20 14:57:26 CEST 2024
Total time taken to generate the page: 0.02984 seconds
|