plugins/include/adt_array.inc
author David Anderson <dvander@alliedmods.net>
Wed Jul 25 20:23:34 2007 +0000 (2007-07-25)
changeset 1169 47a8892d2c81
parent 1156 a80f4ebe389f
child 1330 ab94316bb52b
permissions -rw-r--r--
added amb416 - SlapPlayer()
     1 /**
     2  * vim: set ts=4 :
     3  * ===============================================================
     4  * SourceMod (C)2004-2007 AlliedModders LLC.  All rights reserved.
     5  * ===============================================================
     6  *
     7  *  This file is part of the SourceMod/SourcePawn SDK.  This file may only be used 
     8  * or modified under the Terms and Conditions of its License Agreement, which is found 
     9  * in LICENSE.txt.  The Terms and Conditions for making SourceMod extensions/plugins 
    10  * may change at any time.  To view the latest information, see:
    11  *   http://www.sourcemod.net/license.php
    12  *
    13  * Version: $Id$
    14  */
    15  
    16 #if defined _adt_array_included
    17  #endinput
    18 #endif
    19 #define _adt_array_included
    20 
    21 /**
    22  * Given a maximum string size (including the null terminator), 
    23  * returns the number of cells required to fit that string.
    24  *
    25  * @param size			Number of bytes.
    26  * @return				Minimum number of cells required to fit the byte count.
    27  */
    28 stock ByteCountToCells(size)
    29 {
    30 	if (!size)
    31 	{
    32 		return 1;
    33 	}
    34 	
    35 	return (size + 3) / 4;
    36 }
    37 
    38 /**
    39  * Creates a dynamic global cell array.  While slower than a normal array,
    40  * it can be used globally AND dynamically, which is otherwise impossible.
    41  * 
    42  * The contents of the array are uniform; i.e. storing a string at index X 
    43  * and then retrieving it as an integer is NOT the same as StringToInt()!
    44  * The "blocksize" determines how many cells each array slot has; it cannot
    45  * be changed after creation.
    46  *
    47  * @param blocksize		The number of cells each member of the array can 
    48  *						hold.  For example, 32 cells is equivalent to:
    49  *						new Array[X][32]
    50  * @param startsize		Initial size of the array.  Note that data will 
    51  *						NOT be auto-intialized.
    52  * @return				New Handle to the array object.
    53  */
    54 native Handle:CreateArray(blocksize=1, startsize=0);
    55 
    56 /**
    57  * Clears an array of all entries.  This is the same as ResizeArray(0).
    58  *
    59  * @param array			Array Handle.
    60  * @noreturn
    61  * @error				Invalid Handle.
    62  */
    63 native ClearArray(Handle:array);
    64 
    65 /**
    66  * Resizes an array.  If the size is smaller than the current size,
    67  * the array is truncated.
    68  *
    69  * @param array			Array Handle.
    70  * @param newsize		New size.
    71  * @noreturn
    72  * @error				Invalid Handle or out of memory.
    73  */
    74 native bool:ResizeArray(Handle:array, newsize);
    75 
    76 /**
    77  * Returns the array size.
    78  *
    79  * @param array			Array Handle.
    80  * @return				Number of elements in the array.
    81  * @error				Invalid Handle.
    82  */
    83 native GetArraySize(Handle:array);
    84 
    85 /**
    86  * Pushes a value onto the end of an array, adding a new index.
    87  *
    88  * This may safely be used even if the array has a blocksize
    89  * greater than 1.
    90  *
    91  * @param array			Array Handle.
    92  * @param value			Value to push.
    93  * @return				Index of the new entry.
    94  * @error				Invalid Handle or out of memory.
    95  */
    96 native PushArrayCell(Handle:array, any:value);
    97 
    98 /**
    99  * Pushes a string onto the end of an array, truncating it
   100  * if it is too big.
   101  *
   102  * @param array			Array Handle.
   103  * @param value			String to push.
   104  * @return				Index of the new entry.
   105  * @error				Invalid Handle or out of memory.
   106  */
   107 native PushArrayString(Handle:array, const String:value[]);
   108 
   109 /**
   110  * Pushes an array of cells onto the end of an array.  The cells
   111  * are pushed as a block (i.e. the entire array sits at the index),
   112  * rather than pushing each cell individually.
   113  *
   114  * @param array			Array Handle.
   115  * @param values		Block of values to copy.
   116  * @param size			If not set, the number of elements copied from the array
   117  *						will be equal to the blocksize.  If set higher than the 
   118  *						blocksize, the operation will be truncated.
   119  * @return				Index of the new entry.
   120  * @error				Invalid Handle or out of memory.
   121  */
   122 native PushArrayArray(Handle:array, const values[], size=-1);
   123 
   124 /**
   125  * Retrieves a cell value from an array.
   126  *
   127  * @param array			Array Handle.
   128  * @param index			Index in the array.
   129  * @param block			Optionally specify which block to read from
   130  *						(useful if the blocksize > 0).
   131  * @param asChar		Optionally read as a byte instead of a cell.
   132  * @return				Value read.
   133  * @error				Invalid Handle, invalid index, or invalid block.
   134  */
   135 native any:GetArrayCell(Handle:array, index, block=0, bool:asChar=false);
   136 
   137 /**
   138  * Retrieves a string value from an array.
   139  *
   140  * @param array			Array Handle.
   141  * @param index			Index in the array.
   142  * @param buffer		Buffer to copy to.
   143  * @param maxlength		Maximum size of the buffer.
   144  * @return				Number of characters copied.
   145  * @error				Invalid Handle or invalid index.
   146  */
   147 native GetArrayString(Handle:array, index, String:buffer[], maxlength);
   148 
   149 /**
   150  * Retrieves an array of cells from an array.
   151  *
   152  * @param array			Array Handle.
   153  * @param index			Index in the array.
   154  * @param buffer		Buffer to store the array in.
   155  * @param size			If not set, assumes the buffer size is equal to the
   156  *						blocksize.  Otherwise, the size passed is used.
   157  * @return				Number of cells copied.
   158  * @error				Invalid Handle or invalid index.
   159  */
   160 native GetArrayArray(Handle:array, index, buffer[], size=-1);
   161 
   162 /**
   163  * Sets a cell value in an array.
   164  *
   165  * @param array			Array Handle.
   166  * @param index			Index in the array.
   167  * @param value			Cell value to set.
   168  * @param block			Optionally specify which block to write to
   169  *						(useful if the blocksize > 0).
   170  * @param asChar		Optionally set as a byte instead of a cell.
   171  * @noreturn
   172  * @error				Invalid Handle, invalid index, or invalid block.
   173  */
   174 native any:SetArrayCell(Handle:array, index, any:value, block=0, bool:asChar=false);
   175 
   176 /**
   177  * Sets a string value in an array.
   178  *
   179  * @param array			Array Handle.
   180  * @param index			Index in the array.
   181  * @param value			String value to set.
   182  * @return				Number of characters copied.
   183  * @error				Invalid Handle or invalid index.
   184  */
   185 native SetArrayString(Handle:array, index, const String:buffer[]);
   186 
   187 /**
   188  * Sets an array of cells in an array.
   189  *
   190  * @param array			Array Handle.
   191  * @param index			Index in the array.
   192  * @param buffer		Array to copy.
   193  * @param size			If not set, assumes the buffer size is equal to the
   194  *						blocksize.  Otherwise, the size passed is used.
   195  * @return				Number of cells copied.
   196  * @error				Invalid Handle or invalid index.
   197  */
   198 native SetArrayArray(Handle:array, index, const values[], size=-1);
   199 
   200 /**
   201  * Shifts an array up.  All array contents after and including the given 
   202  * index are shifted up by one, and the given index is then "free."  
   203  * After shifting, the contents of the given index is undefined.
   204  *
   205  * @param array			Array Handle.
   206  * @param index			Index in the array to shift up from.
   207  * @noreturn
   208  * @error				Invalid Handle or invalid index.
   209  */
   210 native ShiftArrayUp(Handle:array, index);
   211 
   212 /**
   213  * Removes an array index, shifting the entire array down from that position 
   214  * on.  For example, if item 8 of 10 is removed, the last 3 items will then be
   215  * (6,7,8) instead of (7,8,9), and all indexes before 8 will remain unchanged.
   216  *
   217  * @param array			Array Handle.
   218  * @param index			Index in the array to remove at.
   219  * @noreturn
   220  * @error				Invalid Handle or invalid index.
   221  */
   222 native RemoveFromArray(Handle:array, index);
   223 
   224 /**
   225  * Swaps two items in the array.
   226  *
   227  * @param array			Array Handle.
   228  * @param index1		First index.
   229  * @param index2		Second index.
   230  * @noreturn
   231  * @error				Invalid Handle or invalid index.
   232  */
   233 native SwapArrayItems(Handle:array, index1, index2);