MODULE: VectorClass v1.0 - Dynamically-sized arrays

Started by monkey0506, Thu 13/07/2006 06:45:10

Previous topic - Next topic

monkey0506

UPDATE 20 MARCH 2009: In addition to the download links being long broken, this module is now officially deprecated. See the new and improved Stack module which provides similar functionality (still vectorized!), in a generic manner.

Quote from: VectorClass manual[/url]/*****************************
Description:

Provides vector (dynamic array) classes for  use  with  integers,  Strings,  and
floating point decimals. Based on a script by HeirOfNorton.
*****************************/

Vectors are dynamically sized arrays. They are useful when you don't know the size of array demanded. You can't create a #define based on a variable so the alternative in this situation would be creating a very large array, or limiting the size of the array.

With vectors you don't have to worry about setting the size as it can change dynamically as needed.

They can also save memory if you use a vector instead of a large array that may not be entirely used...

For example, as a make-up project for my seminary class last school year, I used AGS to make a very simple program to place the 20 students into 3 groups randomly. Supposing we had gained more students, and had I used a vector in designing the program and made it so new students could be added and students could be removed...it would be easier to just allow the user to increase the size of the array (vector) rather than having to recompile...considering when I made it I only made it with the 20 spaces for the 20 existing students.

I've put three vector classes (iVector (integer vector), sVector (String vector), and fVector (float vector)) into one module:

Requires AGS v2.71 or higher!

Download the VectorClass v1.0 module here
Mirror Thanks Neole!

And I exported the classes to their own modules (Require AGS v2.71 or higher!):

Download the IntVector v1.0 module here
IntVector Mirror Thanks Neole!
Download the StringVector v1.01 module here
StringVector Mirror Thanks Neole!
Download the FloatVector v1.01 module here
FloatVector Mirror Thanks Neole!

If the downloads don't work, try going to my website and clicking the appropriate link...it should work from there.

Thanks to HeirOfNorton for the idea and his code which I referred to in writing these modules.

Edit:

13 July 2006 - Uploaded StringVector v1.01 which is compatible with the new VectorClass module.
13 July 2006 - Uploaded FloatVector v1.01 which is compatible with the new VectorClass module.
15 August 2006 - I'm changing my host, so you may need to right-click and choose "Save as..." (in my experience trying to link directly to the files opens it as if it were a text file). If that doesn't work, feel free to PM me and let me know. I'll try and have this issue resolved as quickly as possible. Thanks.

scotch

Vector is slightly confusing, though, IMO, even though I've used std::vectors in C++ and java.util.Vectors in Java, in game programming Vector is more frequently a 2/3/4 dimensional vector for computing positions and directions and so on, also "dynamically sized array" is at least descriptive to non coders.

Nice module by the way. I think stuff like this should become part of AGS script some day.

HeirOfNorton

#2
That's really neat, Monkey. I almost did String and Float vectors, as well, but I got too lazy.  ;) I am also glad that someone was able to decipher my messy and uncommented code.

Great work.  I like how you used delimiters to set apart each of the values. (I forgot all about String.Contains to do that myself.) I would mention that your version will take more memory -- but since it's all dynamically allocated, I don't see how that will ever be a problem -- and that GetValue may be a bit slower, but again, I doubt that anyone would ever notice it.

I think I'll use your version from now on.   :)

HoN

monkey0506

#3
Your module was fairly easy to figure out once I deciphered all the bitwise operator stuff. I understand how it works, but I've never actually managed to make practical application of it before so it confused me a bit, so I plugged in a few numbers, displayed a few values and then figured out what it was you had done.

Yeah, the integer vector probably would run slower as it allocates about 8x as much memory...but the method you used wouldn't work for floats or Strings...

And I figured with the delimiters that people couldn't use a String as part of an integer or float, so I was safe there, and what are the odds that someone is actually going to try and include "\\StringVectorStart:" or "\\StringVectorEnd:" into the String values? Plus it makes it easier for me to find since I included a key into that ("\\StringVectorStart:%d\\", this.Size).

Thanks for your comments! :D

SMF spam blocked by CleanTalk