vString
-
Okay, I'm new here and new to the community...don't kill me if I seem lazy? I swear I'm not... x) ANYWAYS. Here is a template-abusive custom vector class. It implements buffering, even. (: And Here and here is a custom vString class ( IDK what the 'v' stands for, sorry. Just seemed right. ) I have beat my brains out a few times trying to debug this... Would anybody be willing to help me debug / better organize this code? { Or point out huge problems with it } It just seems really unburly to me, and I'm having a hard time deciding if I should rely on the Vector<>::Add functionality for my vString::Insert() functions ( Append / Prepend are just convenience wrapping functions. I intend to make the vString class operate like jQuery. $().Append($().html("")).Prepend().Remove().Insert() etc ) I will also add support for using glibmm's regex library, but for now, I need to get the basics working out for me. Any help would be much appreciated! :)
The roof's on fire, but keeps out the rain, so clench your fists and enjoy the pain.
-
Okay, I'm new here and new to the community...don't kill me if I seem lazy? I swear I'm not... x) ANYWAYS. Here is a template-abusive custom vector class. It implements buffering, even. (: And Here and here is a custom vString class ( IDK what the 'v' stands for, sorry. Just seemed right. ) I have beat my brains out a few times trying to debug this... Would anybody be willing to help me debug / better organize this code? { Or point out huge problems with it } It just seems really unburly to me, and I'm having a hard time deciding if I should rely on the Vector<>::Add functionality for my vString::Insert() functions ( Append / Prepend are just convenience wrapping functions. I intend to make the vString class operate like jQuery. $().Append($().html("")).Prepend().Remove().Insert() etc ) I will also add support for using glibmm's regex library, but for now, I need to get the basics working out for me. Any help would be much appreciated! :)
The roof's on fire, but keeps out the rain, so clench your fists and enjoy the pain.
** Also, when you see something like
if(sizeof(T)<=sizeof(T*)){
} else {
}
I'm aware that a T* is almost always going to be 4 bytes long, but I felt the need to place things this way. I'm also aware that most of my code is lacking heavily in comments. This is because I usually work on my own, and make code that seems self explanatory to me.
-
Okay, I'm new here and new to the community...don't kill me if I seem lazy? I swear I'm not... x) ANYWAYS. Here is a template-abusive custom vector class. It implements buffering, even. (: And Here and here is a custom vString class ( IDK what the 'v' stands for, sorry. Just seemed right. ) I have beat my brains out a few times trying to debug this... Would anybody be willing to help me debug / better organize this code? { Or point out huge problems with it } It just seems really unburly to me, and I'm having a hard time deciding if I should rely on the Vector<>::Add functionality for my vString::Insert() functions ( Append / Prepend are just convenience wrapping functions. I intend to make the vString class operate like jQuery. $().Append($().html("")).Prepend().Remove().Insert() etc ) I will also add support for using glibmm's regex library, but for now, I need to get the basics working out for me. Any help would be much appreciated! :)
The roof's on fire, but keeps out the rain, so clench your fists and enjoy the pain.
Since you are new, you may not be aware of the following points to keep in mind: 1. Nobody is going to download your code from a link in order to look at it 2. You need to have a very specific question, and post a very short snippet of the relevant code 3. Your request of "debug/organize my code" is far to broad and general for a forum such as this
The difficult we do right away... ...the impossible takes slightly longer.
-
Since you are new, you may not be aware of the following points to keep in mind: 1. Nobody is going to download your code from a link in order to look at it 2. You need to have a very specific question, and post a very short snippet of the relevant code 3. Your request of "debug/organize my code" is far to broad and general for a forum such as this
The difficult we do right away... ...the impossible takes slightly longer.
Oh, okay. My apologies!
class Range{
private:
int mStart,mSize,myID;
public:
Range(int aStart,int aSize):mStart(aStart),mSize(aSize){}
Range(Range& r):mStart(r.mStart),mSize(r.mSize){}
Range(const Range& r):mStart(r.mStart),mSize(r.mSize){} //needed to return 'by value'
bool operator () (int x){
return x>=0? (xInside Vector<> Class:
Range All( int size = -1 ) { if(size==-1) size = arraySize; const Range ret(0,size); return ret; }
And the problematic code:
T& Add(const T& nElement, int indice = -1){ Allocate(); ++arraySize; Range ran = All(); indice = ran\[indice\]; if ( indice < 0 ) indice = arraySize + indice; if(sizeof(T)<=sizeof(T\*)){ if(static\_cast(indice+1)!=arraySize) memcpy(&(((T\*)tElements)\[indice+1\]),&(((T\*)tElements)\[indice\]),sizeof(T)\*(arraySize-indice-1)); memcpy(&(((T\*)tElements)\[indice\]),&nElement,sizeof(T)); return (((T\*)tElements)\[indice\]); } else { memcpy( &(tElements\[indice+1\]),&(tElements\[indice\]),sizeof(T\*)\*(arraySize-indice)); if(construct) return \*(tElements\[indice\] = new T(nElement)); else{ T\* nPtr = (T\*)new char\[sizeof(T)\]; memcpy(nPtr,&nElement,sizeof(T)); return \*(tElements\[indice\] = nPtr); } }
-
Oh, okay. My apologies!
class Range{
private:
int mStart,mSize,myID;
public:
Range(int aStart,int aSize):mStart(aStart),mSize(aSize){}
Range(Range& r):mStart(r.mStart),mSize(r.mSize){}
Range(const Range& r):mStart(r.mStart),mSize(r.mSize){} //needed to return 'by value'
bool operator () (int x){
return x>=0? (xInside Vector<> Class:
Range All( int size = -1 ) { if(size==-1) size = arraySize; const Range ret(0,size); return ret; }
And the problematic code:
T& Add(const T& nElement, int indice = -1){ Allocate(); ++arraySize; Range ran = All(); indice = ran\[indice\]; if ( indice < 0 ) indice = arraySize + indice; if(sizeof(T)<=sizeof(T\*)){ if(static\_cast(indice+1)!=arraySize) memcpy(&(((T\*)tElements)\[indice+1\]),&(((T\*)tElements)\[indice\]),sizeof(T)\*(arraySize-indice-1)); memcpy(&(((T\*)tElements)\[indice\]),&nElement,sizeof(T)); return (((T\*)tElements)\[indice\]); } else { memcpy( &(tElements\[indice+1\]),&(tElements\[indice\]),sizeof(T\*)\*(arraySize-indice)); if(construct) return \*(tElements\[indice\] = new T(nElement)); else{ T\* nPtr = (T\*)new char\[sizeof(T)\]; memcpy(nPtr,&nElement,sizeof(T)); return \*(tElements\[indice\] = nPtr); } }
-
I'm going to be straight, this code is a mess, and as you've already noticed, error prone. Why not use std::vector<> and a couple of iterators? Problem solved. ?
Well, I would BUT there are some problems I've found with std::vector<>: 1) Deep copies -- Every time I resize the vector, a constructor is called, or an operator = () is called. I can't remember which. Either way, if an object in an std::vector<> requires deep copies to be made, lots of time is just wasted allocating, releasing, and assigning memory. 2) For instance, take my vString class. If I had used std::vector for it's base type, things might be a little more complicated, and time consuming. ( With #1 in mind )
The roof's on fire, but it keeps out the rain. So clench your fists, and enjoy the pain.
-
Well, I would BUT there are some problems I've found with std::vector<>: 1) Deep copies -- Every time I resize the vector, a constructor is called, or an operator = () is called. I can't remember which. Either way, if an object in an std::vector<> requires deep copies to be made, lots of time is just wasted allocating, releasing, and assigning memory. 2) For instance, take my vString class. If I had used std::vector for it's base type, things might be a little more complicated, and time consuming. ( With #1 in mind )
The roof's on fire, but it keeps out the rain. So clench your fists, and enjoy the pain.
-
Sorry for the late reply, but are you using an old compiler? If not, implement move semantics in your classes. It will take care of things for you.
I use g++/gcc ( MinGW ) -- not Visual Studio -- http://msdn.microsoft.com/en-us/library/dd293665.aspx[^] Is this what your'e referring to as "Move semantics" ?
-
I use g++/gcc ( MinGW ) -- not Visual Studio -- http://msdn.microsoft.com/en-us/library/dd293665.aspx[^] Is this what your'e referring to as "Move semantics" ?