plugins/include/adt_array.inc
author David Anderson <dvander@alliedmods.net>
Fri Aug 15 05:22:26 2008 +0000 (2008-08-15)
changeset 2226 d10b38524be3
parent 2023 1bd756a9f8db
child 2289 44045ead61cd
permissions -rw-r--r--
Landed sourcepawn-1.2. The big changes:

1) JIT compilation/optimization now occurs per-function, and only when functions are first used. We're now officially a whole-method JIT rather than an AOT compiler (albiet, still a simple JIT). This has two implications: Functions are now much better abstracted internally, and loading a plugin is now much less expensive. If a function contains calls to other functions, THOSE functions are only compiled when they're invoked as well.

2) I've removed debug mode. We always show full backtraces now, as there was a very cheap way to implement this which really cleaned up everything. This is great for a number of reasons -- there's less code, the JIT is better designed, we don't need to relocate debug tables, and best of all we no longer have to tell users to enable debug mode at their own expense.
     1 /**
     2  * vim: set ts=4 :
     3  * =============================================================================
     4  * SourceMod (C)2004-2008 AlliedModders LLC.  All rights reserved.
     5  * =============================================================================
     6  *
     7  * This file is part of the SourceMod/SourcePawn SDK.
     8  *
     9  * This program is free software; you can redistribute it and/or modify it under
    10  * the terms of the GNU General Public License, version 3.0, as published by the
    11  * Free Software Foundation.
    12  * 
    13  * This program is distributed in the hope that it will be useful, but WITHOUT
    14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
    16  * details.
    17  *
    18  * You should have received a copy of the GNU General Public License along with
    19  * this program.  If not, see <http://www.gnu.org/licenses/>.
    20  *
    21  * As a special exception, AlliedModders LLC gives you permission to link the
    22  * code of this program (as well as its derivative works) to "Half-Life 2," the
    23  * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
    24  * by the Valve Corporation.  You must obey the GNU General Public License in
    25  * all respects for all other code used.  Additionally, AlliedModders LLC grants
    26  * this exception to all derivative works.  AlliedModders LLC defines further
    27  * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
    28  * or <http://www.sourcemod.net/license.php>.
    29  *
    30  * Version: $Id$
    31  */
    32  
    33 #if defined _adt_array_included
    34  #endinput
    35 #endif
    36 #define _adt_array_included
    37 
    38 /**
    39  * Given a maximum string size (including the null terminator), 
    40  * returns the number of cells required to fit that string.
    41  *
    42  * @param size			Number of bytes.
    43  * @return				Minimum number of cells required to fit the byte count.
    44  */
    45 stock ByteCountToCells(size)
    46 {
    47 	if (!size)
    48 	{
    49 		return 1;
    50 	}
    51 	
    52 	return (size + 3) / 4;
    53 }
    54 
    55 /**
    56  * Creates a dynamic global cell array.  While slower than a normal array,
    57  * it can be used globally AND dynamically, which is otherwise impossible.
    58  * 
    59  * The contents of the array are uniform; i.e. storing a string at index X 
    60  * and then retrieving it as an integer is NOT the same as StringToInt()!
    61  * The "blocksize" determines how many cells each array slot has; it cannot
    62  * be changed after creation.
    63  *
    64  * @param blocksize		The number of cells each member of the array can 
    65  *						hold.  For example, 32 cells is equivalent to:
    66  *						new Array[X][32]
    67  * @param startsize		Initial size of the array.  Note that data will 
    68  *						NOT be auto-intialized.
    69  * @return				New Handle to the array object.
    70  */
    71 native Handle:CreateArray(blocksize=1, startsize=0);
    72 
    73 /**
    74  * Clears an array of all entries.  This is the same as ResizeArray(0).
    75  *
    76  * @param array			Array Handle.
    77  * @noreturn
    78  * @error				Invalid Handle.
    79  */
    80 native ClearArray(Handle:array);
    81 
    82 /**
    83  * Clones an array, returning a new handle with the same size and data. This should NOT
    84  * be confused with CloneHandle. This is a completely new handle with the same data but
    85  * no relation to the original. You MUST close it.
    86  *
    87  * @param array			Array handle to be cloned
    88  * @return				New handle to the cloned array object
    89  * @error				Invalid Handle
    90  */
    91  native Handle:CloneArray(Handle:array);
    92 
    93 /**
    94  * Resizes an array.  If the size is smaller than the current size,
    95  * the array is truncated.
    96  *
    97  * @param array			Array Handle.
    98  * @param newsize		New size.
    99  * @noreturn
   100  * @error				Invalid Handle or out of memory.
   101  */
   102 native bool:ResizeArray(Handle:array, newsize);
   103 
   104 /**
   105  * Returns the array size.
   106  *
   107  * @param array			Array Handle.
   108  * @return				Number of elements in the array.
   109  * @error				Invalid Handle.
   110  */
   111 native GetArraySize(Handle:array);
   112 
   113 /**
   114  * Pushes a value onto the end of an array, adding a new index.
   115  *
   116  * This may safely be used even if the array has a blocksize
   117  * greater than 1.
   118  *
   119  * @param array			Array Handle.
   120  * @param value			Value to push.
   121  * @return				Index of the new entry.
   122  * @error				Invalid Handle or out of memory.
   123  */
   124 native PushArrayCell(Handle:array, any:value);
   125 
   126 /**
   127  * Pushes a string onto the end of an array, truncating it
   128  * if it is too big.
   129  *
   130  * @param array			Array Handle.
   131  * @param value			String to push.
   132  * @return				Index of the new entry.
   133  * @error				Invalid Handle or out of memory.
   134  */
   135 native PushArrayString(Handle:array, const String:value[]);
   136 
   137 /**
   138  * Pushes an array of cells onto the end of an array.  The cells
   139  * are pushed as a block (i.e. the entire array sits at the index),
   140  * rather than pushing each cell individually.
   141  *
   142  * @param array			Array Handle.
   143  * @param values		Block of values to copy.
   144  * @param size			If not set, the number of elements copied from the array
   145  *						will be equal to the blocksize.  If set higher than the 
   146  *						blocksize, the operation will be truncated.
   147  * @return				Index of the new entry.
   148  * @error				Invalid Handle or out of memory.
   149  */
   150 native PushArrayArray(Handle:array, const any:values[], size=-1);
   151 
   152 /**
   153  * Retrieves a cell value from an array.
   154  *
   155  * @param array			Array Handle.
   156  * @param index			Index in the array.
   157  * @param block			Optionally specify which block to read from
   158  *						(useful if the blocksize > 0).
   159  * @param asChar		Optionally read as a byte instead of a cell.
   160  * @return				Value read.
   161  * @error				Invalid Handle, invalid index, or invalid block.
   162  */
   163 native any:GetArrayCell(Handle:array, index, block=0, bool:asChar=false);
   164 
   165 /**
   166  * Retrieves a string value from an array.
   167  *
   168  * @param array			Array Handle.
   169  * @param index			Index in the array.
   170  * @param buffer		Buffer to copy to.
   171  * @param maxlength		Maximum size of the buffer.
   172  * @return				Number of characters copied.
   173  * @error				Invalid Handle or invalid index.
   174  */
   175 native GetArrayString(Handle:array, index, String:buffer[], maxlength);
   176 
   177 /**
   178  * Retrieves an array of cells from an array.
   179  *
   180  * @param array			Array Handle.
   181  * @param index			Index in the array.
   182  * @param buffer		Buffer to store the array in.
   183  * @param size			If not set, assumes the buffer size is equal to the
   184  *						blocksize.  Otherwise, the size passed is used.
   185  * @return				Number of cells copied.
   186  * @error				Invalid Handle or invalid index.
   187  */
   188 native GetArrayArray(Handle:array, index, any:buffer[], size=-1);
   189 
   190 /**
   191  * Sets a cell value in an array.
   192  *
   193  * @param array			Array Handle.
   194  * @param index			Index in the array.
   195  * @param value			Cell value to set.
   196  * @param block			Optionally specify which block to write to
   197  *						(useful if the blocksize > 0).
   198  * @param asChar		Optionally set as a byte instead of a cell.
   199  * @noreturn
   200  * @error				Invalid Handle, invalid index, or invalid block.
   201  */
   202 native any:SetArrayCell(Handle:array, index, any:value, block=0, bool:asChar=false);
   203 
   204 /**
   205  * Sets a string value in an array.
   206  *
   207  * @param array			Array Handle.
   208  * @param index			Index in the array.
   209  * @param value			String value to set.
   210  * @return				Number of characters copied.
   211  * @error				Invalid Handle or invalid index.
   212  */
   213 native SetArrayString(Handle:array, index, const String:buffer[]);
   214 
   215 /**
   216  * Sets an array of cells in an array.
   217  *
   218  * @param array			Array Handle.
   219  * @param index			Index in the array.
   220  * @param buffer		Array to copy.
   221  * @param size			If not set, assumes the buffer size is equal to the
   222  *						blocksize.  Otherwise, the size passed is used.
   223  * @return				Number of cells copied.
   224  * @error				Invalid Handle or invalid index.
   225  */
   226 native SetArrayArray(Handle:array, index, const any:values[], size=-1);
   227 
   228 /**
   229  * Shifts an array up.  All array contents after and including the given 
   230  * index are shifted up by one, and the given index is then "free."  
   231  * After shifting, the contents of the given index is undefined.
   232  *
   233  * @param array			Array Handle.
   234  * @param index			Index in the array to shift up from.
   235  * @noreturn
   236  * @error				Invalid Handle or invalid index.
   237  */
   238 native ShiftArrayUp(Handle:array, index);
   239 
   240 /**
   241  * Removes an array index, shifting the entire array down from that position 
   242  * on.  For example, if item 8 of 10 is removed, the last 3 items will then be
   243  * (6,7,8) instead of (7,8,9), and all indexes before 8 will remain unchanged.
   244  *
   245  * @param array			Array Handle.
   246  * @param index			Index in the array to remove at.
   247  * @noreturn
   248  * @error				Invalid Handle or invalid index.
   249  */
   250 native RemoveFromArray(Handle:array, index);
   251 
   252 /**
   253  * Swaps two items in the array.
   254  *
   255  * @param array			Array Handle.
   256  * @param index1		First index.
   257  * @param index2		Second index.
   258  * @noreturn
   259  * @error				Invalid Handle or invalid index.
   260  */
   261 native SwapArrayItems(Handle:array, index1, index2);
   262 
   263 /**
   264  * Returns the index for the first occurance of the provided string. If the string
   265  * cannot be located, -1 will be returned.
   266  *
   267  * @param array			Array Handle.
   268  * @param item			String to search for
   269  * @return				Array index, or -1 on failure
   270  * @error				Invalid Handle
   271  */
   272 native FindStringInArray(Handle:array, const String:item[]);
   273  
   274 /**
   275  * Returns the index for the first occurance of the provided value. If the value
   276  * cannot be located, -1 will be returned.
   277  *
   278  * @param array			Array Handle.
   279  * @param item			Value to search for
   280  * @return				Array index, or -1 on failure
   281  * @error				Invalid Handle
   282  */
   283 native FindValueInArray(Handle:array, any:item);