Angelscript/docs/manual/doc_datatypes_arrays.html

228 lines
12 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.18"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>AngelScript: array</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function() { init_search(); });
/* @license-end */
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><img alt="Logo" src="aslogo_small.png"/></td>
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">AngelScript
</div>
</td>
<td> <div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.18 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('doc_datatypes_arrays.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="PageDoc"><div class="header">
<div class="headertitle">
<div class="title">array </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><dl class="section note"><dt>Note</dt><dd>Arrays are only available in the scripts if the application <a class="el" href="doc_addon_array.html">registers the support for them</a>. The syntax for using arrays may differ for the application you're working with so consult the application's manual for more details.</dd></dl>
<p>It is possible to declare array variables with the array identifier followed by the type of the elements within angle brackets.</p>
<p>Example:</p>
<pre>
array&lt;int&gt; a, b, c;
array&lt;Foo@&gt; d;
</pre><p><code>a</code>, <code>b</code>, and <code>c</code> are now arrays of integers, and <code>d</code> is an array of handles to objects of the Foo type.</p>
<p>When declaring arrays it is possible to define the initial size of the array by passing the length as a parameter to the constructor. The elements can also be individually initialized by specifying an initialization list. Example:</p>
<pre>
array&lt;int&gt; a; // A zero-length array of integers
array&lt;int&gt; b(3); // An array of integers with 3 elements
array&lt;int&gt; c(3, 1); // An array of integers with 3 elements, all set to 1 by default
array&lt;int&gt; d = {5,6,7}; // An array of integers with 3 elements with specific values
</pre><p>Multidimensional arrays are supported as arrays of arrays, for example:</p>
<pre>
array&lt;array&lt;int&gt;&gt; a; // An empty array of arrays of integers
array&lt;array&lt;int&gt;&gt; b = {{1,2},{3,4}} // A 2 by 2 array with initialized values
array&lt;array&lt;int&gt;&gt; c(10, array&lt;int&gt;(10)); // A 10 by 10 array of integers with uninitialized values
</pre><p>Each element in the array is accessed with the indexing operator. The indices are zero based, i.e. the range of valid indices are from 0 to length - 1.</p>
<pre>
a[0] = some_value;
</pre><p>When the array stores <a class="el" href="doc_script_handle.html">handles</a> the elements are assigned using the <a class="el" href="doc_expressions.html#handle">handle assignment</a>.</p>
<pre>
// Declare an array with initial length 1
array&lt;Foo@&gt; arr(1);</pre><pre> // Set the first element to point to a new instance of Foo
@arr[0] = Foo();
</pre><p>Arrays can also be created and initialized within expressions as <a class="el" href="doc_expressions.html#anonobj">anonymous objects</a>.</p>
<pre>
// Call a function that expects an array of integers as input
foo({1,2,3,4});</pre><pre> // If the function has multiple overloads supporting different types with
// initialization lists it is necessary to explicitly inform the array type
foo2(array&lt;int&gt; = {1,2,3,4});
</pre><h1><a class="anchor" id="doc_datatypes_arrays_addon"></a>
Supporting array object</h1>
<p>The array object supports a number of operators and has several class methods to facilitate the manipulation of strings.</p>
<p>The array object is a <a class="el" href="doc_datatypes_obj.html">reference type</a> even if the elements are not, so it's possible to use handles to the array object when passing it around to avoid costly copies.</p>
<h2><a class="anchor" id="doc_datatypes_array_addon_ops"></a>
Operators</h2>
<p><b>= assignment</b></p>
<p>The assignment operator performs a shallow copy of the content.</p>
<p><b>[] index operator</b></p>
<p>The index operator returns the reference of an element allowing it to be inspected or modified. If the index is out of range, then an exception will be raised.</p>
<p><b>==, != equality</b></p>
<p>Performs a value comparison on each of the elements in the two arrays and returns true if all match the used operator.</p>
<h2><a class="anchor" id="doc_datatypes_array_addon_mthd"></a>
Methods</h2>
<p><b>uint length() const</b></p>
<p>Returns the length of the array.</p>
<p><b>void resize(uint)</b></p>
<p>Sets the new length of the array.</p>
<p><b>void reverse()</b></p>
<p>Reverses the order of the elements in the array.</p>
<p><b>void insertAt(uint index, const T&amp; in value)</b><br />
<b>void insertAt(uint index, const array&lt;T&gt;&amp; arr)</b><br />
</p>
<p>Inserts a new element, or another array of elements, into the array at the specified index.</p>
<p><b>void insertLast(const T&amp; in)</b></p>
<p>Appends an element at the end of the array.</p>
<p><b>void removeAt(uint index)</b></p>
<p>Removes the element at the specified index.</p>
<p><b>void removeLast()</b></p>
<p>Removes the last element of the array.</p>
<p><b>void removeRange(uint start, uint count)</b></p>
<p>Removes <em>count</em> elements starting from <em>start</em>.</p>
<p><b>void sortAsc()</b><br />
<b>void sortAsc(uint startAt, uint count)</b><br />
</p>
<p>Sorts the elements in the array in ascending order. For object types, this will use the type's opCmp method.</p>
<p>The second variant will sort only the elements starting at index <em>startAt</em> and the following <em>count</em> elements.</p>
<p><b>void sortDesc()</b><br />
<b>void sortDesc(uint startAt, uint count)</b><br />
</p>
<p>These does the same thing as sortAsc except sorts the elements in descending order.</p>
<p><b>void sort(const less &amp;in compareFunc, uint startAt = 0, uint count = uint(-1))</b><br />
</p>
<p>This method takes as input a callback function to use for comparing two elements when sorting the array.</p>
<p>The callback function should take as parameters two references of the same type of the array elements and it should return a bool. The return value should be true if the first argument should be placed before the second argument.</p>
<pre>
array&lt;int&gt; arr = {3,2,1};
arr.sort(function(a,b) { return a &lt; b; });
</pre><p>The example shows how to use the sort method with a callback to an <a class="el" href="doc_script_anonfunc.html">anonymous function</a>.</p>
<p>Here's another example where the callback function is declared explicitly:</p>
<pre>
bool lessForInt(const int &amp;in a, const int &amp;in b)
{
return a &lt; b;
}
bool lessForHandle(const obj @&amp;in a, const obj @&amp;in b)
{
return a &lt; b;
}
void sortArrayOfInts(array&lt;int&gt; @arr) { arr.sort(lessForInt); }
void sortArrayOfHandles(array&lt;obj@&gt; @arr) { arr.sort(lessForHandle); }
</pre><p><b>int find(const T&amp; in)</b><br />
<b>int find(uint startAt, const T&amp; in)</b><br />
</p>
<p>These will return the index of the first element that has the same value as the wanted value.</p>
<p>For object types, this will use the type's opEquals or opCmp method to compare the value. For arrays of handles any null handle will be skipped.</p>
<p>If no match is found the methods will return a negative value.</p>
<p><b>int findByRef(const T&amp; in)</b><br />
<b>int findByRef(uint startAt, const T&amp; in)</b><br />
</p>
<p>These will search for a matching address. These are especially useful for arrays of handles where specific instances of objects are desired, and not just objects that happen to have equal value.</p>
<p>If no match is found the methods will return a negative value.</p>
<h2><a class="anchor" id="doc_datatypes_array_addon_example"></a>
Script example</h2>
<pre>
int main()
{
array&lt;int&gt; arr = {1,2,3}; // 1,2,3
arr.insertLast(0); // 1,2,3,0
arr.insertAt(2,4); // 1,2,4,3,0
arr.removeAt(1); // 1,4,3,0</pre><pre> arr.sortAsc(); // 0,1,3,4</pre><pre> int sum = 0;
for( uint n = 0; n &lt; arr.length(); n++ )
sum += arr[n];</pre><pre> return sum;
}
</pre> </div></div><!-- contents -->
</div><!-- PageDoc -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Sat Dec 5 2020 23:20:25 for AngelScript by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.18 </li>
</ul>
</div>
</body>
</html>