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