View Single Post
Author Message
API
Veteran Member
Join Date: May 2006
Old 05-13-2007 , 22:51   [EXTENSION] SHVector with example usage
Reply With Quote #1

Hey guys,

SHVector is an extension I made to expose the CVector class to SourceMod. Basically, a vector is like an array of data, without a set size and without many datatype limitations. You can have handles, floats, ints, and bools if you use the TYPE_CELL vector type, and there is TYPE_STRING for a string vector.

Credit
PimpinJuice, BAILOPAN, and everyone in IRC that kept me entertained.

Installation
1. Extract 'shvector.ext.dll' and 'shvector.ext.so' to the 'addons/sourcemod/extensions' folder.
2. Extract 'shvector.inc' to the 'addons/sourcemod/scripting/include' folder.

Functions
There are 19 different functions that you can use in your plugins.
They are documented and called as follows.
Code:
/**
 * Creates a SHVector
 * @param type      The type of vector to create (TYPE_CELL or TYPE_STRING)
 * @return          The SHVector handle of the result
 */
native Handle:SHVectorCreate(type=TYPE_CELL);

/**
 * Frees the vector from the memory
 * @param vector    The vector to free from the memory
 * @noreturn
 */
native SHVectorFree_Cell(Handle:vector);
native SHVectorFree_String(Handle:vector);

/**
 * Gets the vector value at the provided position
 * @param vector    The vector to use
 * @param pos       The position in the vector to get the value of
 * @return          The value
 */
native any:SHVectorAt_Cell(Handle:vector,pos);
native SHVectorAt_String(Handle:vector,pos,String:storeto[],maxlen);

/**
 * Sets the vector value at the provided position
 * @param vector    The vector to use
 * @param pos       The position in the vector to set the value of
 * @param value     The value to set as
 * @return          Returns 0 if there was a failure 
 */
native SHVectorSetAt_Cell(Handle:vector,pos,any:value);
native SHVectorSetAt_String(Handle:vector,pos,String:value[]);

/**
 * Clear the whole vector provided
 * @param vector    The vector to use
 * @noreturn 
 */
native SHVectorClear_Cell(Handle:vector);
native SHVectorClear_String(Handle:vector);

/**
 * Is the vector empty?
 * @param vector    The vector to use
 * @return          Returns true if the vector is empty
 */
native bool:SHVectorIsEmpty_Cell(Handle:vector);
native bool:SHVectorIsEmpty_String(Handle:vector);

/**
 * Swap the contents of two pos's in a vector
 * @param vector  The Vector to use
 * @param pos1    One of the pos's
 * @param pos2    The other pos  
 * @return        Returns 0 if there was a failure
 */
native SHVectorSwap_Cell(Handle:vector,pos1,pos2);
native SHVectorSwap_String(Handle:vector,pos1,pos2);

/**
 * Insert a new item into a vector
 * @param vector    The vector to use
 * @param where     Where to put the item (ITER_FRONT or ITER_BACK) 
 * @param value     The value to insert 
 * @return          Returns 0 if there was a failure
 */
native SHVectorInsert_Cell(Handle:vector,where,any:value);
native SHVectorInsert_String(Handle:vector,where,String:value[]);

/**
 * Erase an item from the vector
 * @param vector    The vector to use
 * @param pos       The position to delete from the vector
 * @return          Returns 0 if there was a failure
 */
native SHVectorErase_Cell(Handle:vector,pos);
native SHVectorErase_String(Handle:vector,pos);

/**
 * The amount of items in the vector
 * @param vector    The vector to use
 * @return          Returns the amount of items in the vector
 */
native SHVectorSize_Cell(Handle:vector);
native SHVectorSize_String(Handle:vector);
Here is a small example on usage of the functions, although it is not showing everything.

Code:
#include <sourcemod>
#include <shvector>

public Plugin:myinfo = 
{
	name = "SHVector Test",
	author = "PimpinJuice",
	description = "Testing out the SHVector extension",
	version = "1.0.0.0",
	url = "http://pimpinjuice.net/"
};

public OnPluginStart()
{
    // Create a vector
    new Handle:shVec=SHVectorCreate(TYPE_CELL); // supports int,float,bool, and handle
    SHVectorInsert_Cell(shVec,ITER_BACK,2.5); // Put 2.5 at the back of the vector, size should now be 1.
    PrintToServer("The size of the vector is now %d",SHVectorSize_Cell(shVec));
    // Lets add an int and a bool
    SHVectorInsert_Cell(shVec,ITER_BACK,5);
    SHVectorInsert_Cell(shVec,ITER_BACK,true);
    // Right now the vector reads 2.5,5,true, in that order, but I want it to say true,5,2.5
    SHVectorSwap_Cell(shVec,0,2);
    // Lets see our full vector and our size
    PrintToServer("size: %d\n0 == %d\n1 == %d\n2 == %f",SHVectorSize_Cell(shVec),SHVectorAt_Cell(shVec,0),SHVectorAt_Cell(shVec,1),SHVectorAt_Cell(shVec,2));
    // Lets give our system the memory back
    SHVectorFree_Cell(shVec);
}
The output, as expected, will follow as:
Quote:
The size of the vector is now 1
size: 3
0 == 1
1 == 5
2 == 2.500000
Note: I have included the source code, feel free to modify and take code from it, but credit me.

Have fun.
Attached Files
File Type: zip shvector_source.zip (10.9 KB, 3373 views)
File Type: zip shvector_1_0_0.zip (56.9 KB, 5658 views)
__________________
API is offline
Send a message via AIM to API