Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. vString

vString

Scheduled Pinned Locked Moved C / C++ / MFC
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tyler Elric
    wrote on last edited by
    #1

    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.

    T Richard Andrew x64R 2 Replies Last reply
    0
    • T Tyler Elric

      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.

      T Offline
      T Offline
      Tyler Elric
      wrote on last edited by
      #2

      ** 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.

      1 Reply Last reply
      0
      • T Tyler Elric

        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.

        Richard Andrew x64R Online
        Richard Andrew x64R Online
        Richard Andrew x64
        wrote on last edited by
        #3

        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.

        T 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          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.

          T Offline
          T Offline
          Tyler Elric
          wrote on last edited by
          #4

          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? (x

          Inside 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);
                                      }
                              }
          
          N 1 Reply Last reply
          0
          • T Tyler Elric

            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? (x

            Inside 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);
                                        }
                                }
            
            N Offline
            N Offline
            Niklas L
            wrote on last edited by
            #5

            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. ?

            T 1 Reply Last reply
            0
            • N Niklas L

              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. ?

              T Offline
              T Offline
              Tyler Elric
              wrote on last edited by
              #6

              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.

              N 1 Reply Last reply
              0
              • T Tyler Elric

                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.

                N Offline
                N Offline
                Niklas L
                wrote on last edited by
                #7

                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.

                T 1 Reply Last reply
                0
                • N Niklas L

                  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.

                  T Offline
                  T Offline
                  Tyler Elric
                  wrote on last edited by
                  #8

                  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" ?

                  N 1 Reply Last reply
                  0
                  • T Tyler Elric

                    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" ?

                    N Offline
                    N Offline
                    Niklas L
                    wrote on last edited by
                    #9

                    Tyler Elric wrote:

                    Is this what your'e referring to as "Move semantics" ?

                    Yes it is.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups