ds@1952: /** ds@1952: * vim: set ts=4 : ds@1952: * ============================================================================= ds@1952: * SourceMod (C)2004-2008 AlliedModders LLC. All rights reserved. ds@1952: * ============================================================================= ds@1952: * ds@1952: * This file is part of the SourceMod/SourcePawn SDK. ds@1952: * ds@1952: * This program is free software; you can redistribute it and/or modify it under ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the ds@1952: * Free Software Foundation. ds@1952: * ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more ds@1952: * details. ds@1952: * ds@1952: * You should have received a copy of the GNU General Public License along with ds@1952: * this program. If not, see . ds@1952: * ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants ds@1952: * this exception to all derivative works. AlliedModders LLC defines further ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), ds@1952: * or . ds@1952: * ds@1952: * Version: $Id$ ds@1952: */ ds@1952: ds@1952: #if defined _adt_array_included ds@1952: #endinput ds@1952: #endif ds@1952: #define _adt_array_included ds@1952: ds@1952: /** ds@1952: * Given a maximum string size (including the null terminator), ds@1952: * returns the number of cells required to fit that string. ds@1952: * ds@1952: * @param size Number of bytes. ds@1952: * @return Minimum number of cells required to fit the byte count. ds@1952: */ ds@1952: stock ByteCountToCells(size) ds@1952: { ds@1952: if (!size) ds@1952: { ds@1952: return 1; ds@1952: } ds@1952: ds@1952: return (size + 3) / 4; ds@1952: } ds@1952: ds@1952: /** ds@1952: * Creates a dynamic global cell array. While slower than a normal array, ds@1952: * it can be used globally AND dynamically, which is otherwise impossible. ds@1952: * ds@1952: * The contents of the array are uniform; i.e. storing a string at index X ds@1952: * and then retrieving it as an integer is NOT the same as StringToInt()! ds@1952: * The "blocksize" determines how many cells each array slot has; it cannot ds@1952: * be changed after creation. ds@1952: * ds@1952: * @param blocksize The number of cells each member of the array can ds@1952: * hold. For example, 32 cells is equivalent to: ds@1952: * new Array[X][32] ds@1952: * @param startsize Initial size of the array. Note that data will ds@1952: * NOT be auto-intialized. ds@1952: * @return New Handle to the array object. ds@1952: */ ds@1952: native Handle:CreateArray(blocksize=1, startsize=0); ds@1952: ds@1952: /** ds@1952: * Clears an array of all entries. This is the same as ResizeArray(0). ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @noreturn ds@1952: * @error Invalid Handle. ds@1952: */ ds@1952: native ClearArray(Handle:array); ds@1952: ds@1952: /** ds@1952: * Clones an array, returning a new handle with the same size and data. This should NOT ds@1952: * be confused with CloneHandle. This is a completely new handle with the same data but ds@1952: * no relation to the original. You MUST close it. ds@1952: * ds@1952: * @param array Array handle to be cloned ds@2023: * @return New handle to the cloned array object ds@2023: * @error Invalid Handle ds@1952: */ ds@1952: native Handle:CloneArray(Handle:array); ds@1952: ds@1952: /** ds@1952: * Resizes an array. If the size is smaller than the current size, ds@1952: * the array is truncated. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param newsize New size. ds@1952: * @noreturn ds@1952: * @error Invalid Handle or out of memory. ds@1952: */ ds@1952: native bool:ResizeArray(Handle:array, newsize); ds@1952: ds@1952: /** ds@1952: * Returns the array size. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @return Number of elements in the array. ds@1952: * @error Invalid Handle. ds@1952: */ ds@1952: native GetArraySize(Handle:array); ds@1952: ds@1952: /** ds@1952: * Pushes a value onto the end of an array, adding a new index. ds@1952: * ds@1952: * This may safely be used even if the array has a blocksize ds@1952: * greater than 1. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param value Value to push. ds@1952: * @return Index of the new entry. ds@1952: * @error Invalid Handle or out of memory. ds@1952: */ ds@1952: native PushArrayCell(Handle:array, any:value); ds@1952: ds@1952: /** ds@1952: * Pushes a string onto the end of an array, truncating it ds@1952: * if it is too big. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param value String to push. ds@1952: * @return Index of the new entry. ds@1952: * @error Invalid Handle or out of memory. ds@1952: */ ds@1952: native PushArrayString(Handle:array, const String:value[]); ds@1952: ds@1952: /** ds@1952: * Pushes an array of cells onto the end of an array. The cells ds@1952: * are pushed as a block (i.e. the entire array sits at the index), ds@1952: * rather than pushing each cell individually. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param values Block of values to copy. ds@1952: * @param size If not set, the number of elements copied from the array ds@1952: * will be equal to the blocksize. If set higher than the ds@1952: * blocksize, the operation will be truncated. ds@1952: * @return Index of the new entry. ds@1952: * @error Invalid Handle or out of memory. ds@1952: */ ds@2023: native PushArrayArray(Handle:array, const any:values[], size=-1); ds@1952: ds@1952: /** ds@1952: * Retrieves a cell value from an array. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array. ds@1952: * @param block Optionally specify which block to read from ds@1952: * (useful if the blocksize > 0). ds@1952: * @param asChar Optionally read as a byte instead of a cell. ds@1952: * @return Value read. ds@1952: * @error Invalid Handle, invalid index, or invalid block. ds@1952: */ ds@1952: native any:GetArrayCell(Handle:array, index, block=0, bool:asChar=false); ds@1952: ds@1952: /** ds@1952: * Retrieves a string value from an array. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array. ds@1952: * @param buffer Buffer to copy to. ds@1952: * @param maxlength Maximum size of the buffer. ds@1952: * @return Number of characters copied. ds@1952: * @error Invalid Handle or invalid index. ds@1952: */ ds@1952: native GetArrayString(Handle:array, index, String:buffer[], maxlength); ds@1952: ds@1952: /** ds@1952: * Retrieves an array of cells from an array. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array. ds@1952: * @param buffer Buffer to store the array in. ds@1952: * @param size If not set, assumes the buffer size is equal to the ds@1952: * blocksize. Otherwise, the size passed is used. ds@1952: * @return Number of cells copied. ds@1952: * @error Invalid Handle or invalid index. ds@1952: */ ds@2023: native GetArrayArray(Handle:array, index, any:buffer[], size=-1); ds@1952: ds@1952: /** ds@1952: * Sets a cell value in an array. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array. ds@1952: * @param value Cell value to set. ds@1952: * @param block Optionally specify which block to write to ds@1952: * (useful if the blocksize > 0). ds@1952: * @param asChar Optionally set as a byte instead of a cell. ds@1952: * @noreturn ds@1952: * @error Invalid Handle, invalid index, or invalid block. ds@1952: */ ds@1952: native any:SetArrayCell(Handle:array, index, any:value, block=0, bool:asChar=false); ds@1952: ds@1952: /** ds@1952: * Sets a string value in an array. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array. ds@1952: * @param value String value to set. ds@1952: * @return Number of characters copied. ds@1952: * @error Invalid Handle or invalid index. ds@1952: */ ds@1952: native SetArrayString(Handle:array, index, const String:buffer[]); ds@1952: ds@1952: /** ds@1952: * Sets an array of cells in an array. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array. ds@1952: * @param buffer Array to copy. ds@1952: * @param size If not set, assumes the buffer size is equal to the ds@1952: * blocksize. Otherwise, the size passed is used. ds@1952: * @return Number of cells copied. ds@1952: * @error Invalid Handle or invalid index. ds@1952: */ ds@2023: native SetArrayArray(Handle:array, index, const any:values[], size=-1); ds@1952: ds@1952: /** ds@1952: * Shifts an array up. All array contents after and including the given ds@1952: * index are shifted up by one, and the given index is then "free." ds@1952: * After shifting, the contents of the given index is undefined. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array to shift up from. ds@1952: * @noreturn ds@1952: * @error Invalid Handle or invalid index. ds@1952: */ ds@1952: native ShiftArrayUp(Handle:array, index); ds@1952: ds@1952: /** ds@1952: * Removes an array index, shifting the entire array down from that position ds@1952: * on. For example, if item 8 of 10 is removed, the last 3 items will then be ds@1952: * (6,7,8) instead of (7,8,9), and all indexes before 8 will remain unchanged. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index Index in the array to remove at. ds@1952: * @noreturn ds@1952: * @error Invalid Handle or invalid index. ds@1952: */ ds@1952: native RemoveFromArray(Handle:array, index); ds@1952: ds@1952: /** ds@1952: * Swaps two items in the array. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param index1 First index. ds@1952: * @param index2 Second index. ds@1952: * @noreturn ds@1952: * @error Invalid Handle or invalid index. ds@1952: */ ds@1952: native SwapArrayItems(Handle:array, index1, index2); ds@1952: ds@1952: /** ds@1952: * Returns the index for the first occurance of the provided string. If the string ds@1952: * cannot be located, -1 will be returned. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param item String to search for ds@1952: * @return Array index, or -1 on failure ds@1952: * @error Invalid Handle ds@1952: */ ds@1952: native FindStringInArray(Handle:array, const String:item[]); ds@1952: ds@1952: /** ds@1952: * Returns the index for the first occurance of the provided value. If the value ds@1952: * cannot be located, -1 will be returned. ds@1952: * ds@1952: * @param array Array Handle. ds@1952: * @param item Value to search for ds@1952: * @return Array index, or -1 on failure ds@1952: * @error Invalid Handle ds@1952: */ ds@1952: native FindValueInArray(Handle:array, any:item);