Server Help Forum Index Server Help
Community forums for Subgame, ASSS, and bots
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   StatisticsStatistics   RegisterRegister 
 ProfileProfile   Login to check your private messagesLogin to check your private messages   LoginLogin (SSL) 

Server Help | ASSS Wiki (0) | Shanky.com
Custom Vector

 
Post new topic   Reply to topic Printable version
 View previous topic  No cheats?? Post :: Post Since i cant make a poll ill just ask  View next topic  
Author Message
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Wed May 19, 2004 7:11 pm   Post maybe stupid    Post subject: Custom Vector Reply to topic Reply with quote

Hey, I'm trying to make my own vector (unlimited array) template. I haven't been able to find any tutorials on how to do this (except STL... but screw it if I'm gonna try to read that code), so please don't shout at me (Ekted) for how many newbish errors my code has. icon_razz.gif

I'm mainly just posting this so you guys can find the probably numerous mistakes I made. Thanks icon_smile.gif

Code: Show/Hide
template <class _AC> class SVector
{
   _AC **array;
   bool allocated;
   int count, space;

public:
   SVector()
   {
      count = space = 0;
      array = NULL;
      allocated = false;
   }
   SVector(SVector &source)
   {
      count = source.count;
      space = source.space;
      array = copy(source.array, false);
   }
   ~SVector()
   {
      clear();
   }
   void clear()
   {
      if (allocated)
      {
         for (int i = 0; i < count; i++)
            delete array[i];
         delete [] array;
         allocated = false;
      }
      count = space = 0;
      array = NULL;
   }
   _AC *at(int offset)
   {
      if (offset > count)
         return NULL;
      return array[offset];
   }
   _AC **copy(_AC **source, bool clear)
   {
      _AC **new_array = new _AC*[space];
      allocated = true;
      for (int i = 0; i < count; i++)
      {
         _AC *item = array[i];
         new_array[i] = new _AC(*item);
         if (clear)
            delete item;
      }
      if (clear)
         delete [] source;
      return new_array;
   }
   void append(_AC *item)
   {
      if (!allocated)
         allocate(1);
      if (space > count)
         array[count++] = new _AC(*item);
      else
      {
         space = ++count + EXTRA;
         array = copy(array, true);
      }
   }
   void allocate(int howmany)
   {
      space = howmany + EXTRA;
      if (allocated)
      {
         array = copy(array, true);
      }
      else
      {
         array = new _AC*[space];
         allocated = true;
         count = 0;
      }
   }
   int size() { return count; }
};

_________________
This help is informational only. No representation is made or warranty given as to its content. User assumes all risk of use. Cyan~Fire assumes no responsibility for any loss or delay resulting from such use.
Wise men STILL seek Him.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Thu May 20, 2004 9:16 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Sorry, no idea. I don't use templates. You are off the hook. icon_smile.gif
_________________
4,691 irradiated haggis!
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Thu May 20, 2004 5:09 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Yay.

If you don't use templates, how do you suggest doing what I'm doing?
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Dr Brain
Flip-flopping like a wind surfer


Age:39
Gender:Gender:Male
Joined: Dec 01 2002
Posts: 3502
Location: Hyperspace
Offline

PostPosted: Thu May 20, 2004 6:30 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

My guess is to use void pointers.

Ekted doesn't strike me as the C++ type of person.
_________________
Hyperspace Owner

Smong> so long as 99% deaths feel lame it will always be hyperspace to me
Back to top
View users profile Send private message Add User to Ignore List AIM Address Yahoo Messenger MSN Messenger
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Thu May 20, 2004 10:39 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

A vector is an array of values. This can be done easily in C, without all the ridiculous template coding, rampant angle brackets, or plethora of functions.
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Fri May 21, 2004 4:35 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

Yeah actually I was thinking in math class today that I mightn't as well use void pointers instead of a template. I'm no fan of templates myself, since they produce a ton of excess code.

Anyway, in conversion to void pointers, I've gotta learn how to use malloc right. When I allocate the memory for the objects that I'm gonna stash in the array, how do I tell the program how much it's gonna allocate? I can't use sizeof(), because that's generated on compile-time. Should I just pass the size as an argument?

News:
Well I started passing an object size as a parameter to the constructor... but VC++ complains about sizeof() being used in the header file where I'm using the vector. Huh??
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sun May 23, 2004 12:08 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

Nevermind, I just went with the template, couldn't figure out how to call a constructor with malloc... guess it wasn't made to.
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Mr Ekted
Movie Geek


Gender:Gender:Male
Joined: Feb 09 2004
Posts: 1379
Offline

PostPosted: Sun May 23, 2004 10:29 am   Post maybe stupid    Post subject: Reply to topic Reply with quote

When you use malloc() to create an object it means you are choosing to use straight C. There are no constructors in C. There is nothing "hidden" that happens without you explicitly calling it. That's one of the reasons I like it so much. I hate the fact that "a = b;" in C++ can result in millions of lines of code being executed.
Back to top
View users profile Send private message Add User to Ignore List
Cyan~Fire
I'll count you!
I'll count you!


Age:37
Gender:Gender:Male
Joined: Jul 14 2003
Posts: 4608
Location: A Dream
Offline

PostPosted: Sun May 23, 2004 12:10 pm   Post maybe stupid    Post subject: Reply to topic Reply with quote

OK, that's what I figured. And since I like constructors, I think I'll just stick with C++ stuff. Sorry icon_razz.gif
Back to top
View users profile Send private message Add User to Ignore List Visit posters website
Display posts from previous:   
Post new topic   Reply to topic    Server Help Forum Index -> Trash Talk All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum
View online users | View Statistics | View Ignored List


Software by php BB © php BB Group
Server Load: 55 page(s) served in previous 5 minutes.

phpBB Created this page in 0.570959 seconds : 33 queries executed (84.8%): GZIP compression disabled