Debian Codegraph @

doc/ref/chap30.html

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8"?>
2
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
5
6 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
7 <head>
8 <title>GAP (ref) - Chapter 30: Collections</title>
9 <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
10 <meta name="generator" content="GAPDoc2HTML" />
11 <link rel="stylesheet" type="text/css" href="manual.css" />
12 <script src="manual.js" type="text/javascript"></script>
13 <script type="text/javascript">overwriteStyle();</script>
14 </head>
15 <body class="chap30" onload="jscontent()">
16
17
18 <div class="chlinktop"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
19
20 <div class="chlinkprevnexttop">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap29.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap31.html">[Next Chapter]</a>&nbsp; </div>
21
22 <p id="mathjaxlink" class="pcenter"><a href="chap30_mj.html">[MathJax on]</a></p>
23 <p><a id="X8050A8037984E5B6" name="X8050A8037984E5B6"></a></p>
24 <div class="ChapSects"><a href="chap30.html#X8050A8037984E5B6">30 <span class="Heading">Collections</span></a>
25 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X8084F03A78ABD4F8">30.1 <span class="Heading">IsCollection (Filter)</span></a>
26 </span>
27 <div class="ContSSBlock">
28 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X79C9FC7F86E2738C">30.1-1 IsCollection</a></span>
29 </div></div>
30 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X85D8D8F684B02DDF">30.2 <span class="Heading">Collection Families</span></a>
31 </span>
32 <div class="ContSSBlock">
33 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X84E5A67E87D8DD66">30.2-1 CollectionsFamily</a></span>
34 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X856AC2DF7F7CBAAF">30.2-2 IsCollectionFamily</a></span>
35 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X864BB3748546F63F">30.2-3 ElementsFamily</a></span>
36 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X78C38017804B2EA7">30.2-4 CategoryCollections</a></span>
37 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X85ABC1B4829778C7">30.2-5 DeclareCategoryCollections</a></span>
38 </div></div>
39 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X7C3722DF8736FFDB">30.3 <span class="Heading">Lists and Collections</span></a>
40 </span>
41 <div class="ContSSBlock">
42 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X877128A77826DD69">30.3-1 IsListOrCollection</a></span>
43 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7EF8910F82B45EC7">30.3-2 Enumerator</a></span>
44 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X80CD7DDC7D0C60D5">30.3-3 EnumeratorSorted</a></span>
45 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X85E149177AC547C3">30.3-4 EnumeratorByFunctions</a></span>
46 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7F12F40E87F3C3A7">30.3-5 List</a></span>
47 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X82CE157A7FAD8036">30.3-6 SortedList</a></span>
48 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7E399AC97FD98217">30.3-7 SSortedList</a></span>
49 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X8289FCCC8274C89D">30.3-8 AsList</a></span>
50 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7BCA5C6181391007">30.3-9 AsSortedList</a></span>
51 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X856D927378C33548">30.3-10 AsSSortedList</a></span>
52 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X79B130FC7906FB4C">30.3-11 Elements</a></span>
53 </div></div>
54 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X79AD18737E70B414">30.4 <span class="Heading">Attributes and Properties for Collections</span></a>
55 </span>
56 <div class="ContSSBlock">
57 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7969C48780C5C1BC">30.4-1 IsEmpty</a></span>
58 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X808A4061809A6E67">30.4-2 IsFinite</a></span>
59 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7E3402D6799D3C24">30.4-3 IsTrivial</a></span>
60 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7F192373850B85B9">30.4-4 IsNonTrivial</a></span>
61 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X78EF6A137E8F66B0">30.4-5 IsWholeFamily</a></span>
62 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X858ADA3B7A684421">30.4-6 Size</a></span>
63 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X865507568182424E">30.4-7 Representative</a></span>
64 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X8026085680270D37">30.4-8 RepresentativeSmallest</a></span>
65 </div></div>
66 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X7F8FEA3278239ADE">30.5 <span class="Heading">Operations for Collections</span></a>
67 </span>
68 <div class="ContSSBlock">
69 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X79CA175481F8105F">30.5-1 IsSubset</a></span>
70 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X851069107CACF98E">30.5-2 <span class="Heading">Intersection</span></a>
71 </span>
72 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X799F0E2F7A502DBA">30.5-3 <span class="Heading">Union</span></a>
73 </span>
74 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X825AC0F07E010B07">30.5-4 Difference</a></span>
75 </div></div>
76 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X82D39CF980FDBFFA">30.6 <span class="Heading">Membership Test for Collections</span></a>
77 </span>
78 <div class="ContSSBlock">
79 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X84B7FA8C7C94400F"><code>30.6-1 \in</code></a></span>
80 </div></div>
81 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X8151A51884B7EE2C">30.7 <span class="Heading">Random Elements</span></a>
82 </span>
83 <div class="ContSSBlock">
84 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7FF906E57D6936F8">30.7-1 Random</a></span>
85 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X811B5BD47DC5356B">30.7-2 PseudoRandom</a></span>
86 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7EBA01EB83BC65A9">30.7-3 RandomList</a></span>
87 </div></div>
88 <div class="ContSect"><span class="tocline"><span class="nocss">&nbsp;</span><a href="chap30.html#X85A3F00985453F95">30.8 <span class="Heading">Iterators</span></a>
89 </span>
90 <div class="ContSSBlock">
91 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X83ADF8287ED0668E">30.8-1 Iterator</a></span>
92 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X8688C20B828FC129">30.8-2 IteratorSorted</a></span>
93 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X87168A827E5B28E4">30.8-3 IsIterator</a></span>
94 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X8055FC557B5D899E">30.8-4 IsDoneIterator</a></span>
95 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X879F62F77D1D1179">30.8-5 NextIterator</a></span>
96 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X858A28667D137C4B">30.8-6 IteratorList</a></span>
97 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X7DB80BE68271247E">30.8-7 TrivialIterator</a></span>
98 <span class="ContSS"><br /><span class="nocss">&nbsp;&nbsp;</span><a href="chap30.html#X82677D8F817D6701">30.8-8 IteratorByFunctions</a></span>
99 </div></div>
100 </div>
101
102 <h3>30 <span class="Heading">Collections</span></h3>
103
104 <p>A <em>collection</em> in <strong class="pkg">GAP</strong> consists of elements in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). The most important kinds of collections are <em>homogeneous lists</em> (see <a href="chap21.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>) and <em>domains</em> (see <a href="chap12.html#X7BAF69417BB925F6"><span class="RefLink">12.4</span></a>). Note that a list is never a domain, and a domain is never a list. A list is a collection if and only if it is nonempty and homogeneous.</p>
105
106 <p>Basic operations for collections are <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>) and <code class="func">Enumerator</code> (<a href="chap30.html#X7EF8910F82B45EC7"><span class="RefLink">30.3-2</span></a>); for <em>finite</em> collections, <code class="func">Enumerator</code> (<a href="chap30.html#X7EF8910F82B45EC7"><span class="RefLink">30.3-2</span></a>) admits to delegate the other operations for collections (see <a href="chap30.html#X79AD18737E70B414"><span class="RefLink">30.4</span></a> and <a href="chap30.html#X7F8FEA3278239ADE"><span class="RefLink">30.5</span></a>) to functions for lists (see <a href="chap21.html#X7B256AE5780F140A"><span class="RefLink">21</span></a>). Obviously, special methods depending on the arguments are needed for the computation of e.g. the intersection of two <em>infinite</em> domains.</p>
107
108 <p><a id="X8084F03A78ABD4F8" name="X8084F03A78ABD4F8"></a></p>
109
110 <h4>30.1 <span class="Heading">IsCollection (Filter)</span></h4>
111
112 <p><a id="X79C9FC7F86E2738C" name="X79C9FC7F86E2738C"></a></p>
113
114 <h5>30.1-1 IsCollection</h5>
115
116 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsCollection</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
117 <p>tests whether an object is a collection.</p>
118
119 <p>Some of the functions for lists and collections are described in the chapter about lists, mainly in Section <a href="chap21.html#X7DF510F7848CBBFD"><span class="RefLink">21.20</span></a>. In the current chapter, we describe those functions for which the <q>collection aspect</q> seems to be more important than the <q>list aspect</q>.</p>
120
121 <p><a id="X85D8D8F684B02DDF" name="X85D8D8F684B02DDF"></a></p>
122
123 <h4>30.2 <span class="Heading">Collection Families</span></h4>
124
125 <p><a id="X84E5A67E87D8DD66" name="X84E5A67E87D8DD66"></a></p>
126
127 <h5>30.2-1 CollectionsFamily</h5>
128
129 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CollectionsFamily</code>( <var class="Arg">Fam</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
130 <p>For a family <var class="Arg">Fam</var>, <code class="func">CollectionsFamily</code> returns the family of all collections over <var class="Arg">Fam</var>, that is, of all dense lists and domains that consist of objects in <var class="Arg">Fam</var>.</p>
131
132 <p>The <code class="func">NewFamily</code> (<a href="chap13.html#X7FB4123E7E22137D"><span class="RefLink">13.1-2</span></a>) call in the standard method of <code class="func">CollectionsFamily</code> is executed with second argument <code class="func">IsCollection</code> (<a href="chap30.html#X79C9FC7F86E2738C"><span class="RefLink">30.1-1</span></a>), since every object in the collections family must be a collection, and with third argument the collections categories of the involved categories in the implied filter of <var class="Arg">Fam</var>.</p>
133
134 <p>Note that families (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>) are used to describe relations between objects. Important such relations are that between an element <span class="SimpleMath">e</span> and each collection of elements that lie in the same family as <span class="SimpleMath">e</span>, and that between two collections whose elements lie in the same family. Therefore, all collections of elements in the family <var class="Arg">Fam</var> form the new family <code class="code">CollectionsFamily( <var class="Arg">Fam</var> )</code>.</p>
135
136 <p><a id="X856AC2DF7F7CBAAF" name="X856AC2DF7F7CBAAF"></a></p>
137
138 <h5>30.2-2 IsCollectionFamily</h5>
139
140 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsCollectionFamily</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
141 <p>is <code class="keyw">true</code> if <var class="Arg">Fam</var> is a family of collections, and <code class="keyw">false</code> otherwise.</p>
142
143 <p><a id="X864BB3748546F63F" name="X864BB3748546F63F"></a></p>
144
145 <h5>30.2-3 ElementsFamily</h5>
146
147 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; ElementsFamily</code>( <var class="Arg">Fam</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
148 <p>If <var class="Arg">Fam</var> is a collections family (see <code class="func">IsCollectionFamily</code> (<a href="chap30.html#X856AC2DF7F7CBAAF"><span class="RefLink">30.2-2</span></a>)) then <code class="func">ElementsFamily</code> returns the family from which <var class="Arg">Fam</var> was created by <code class="func">CollectionsFamily</code> (<a href="chap30.html#X84E5A67E87D8DD66"><span class="RefLink">30.2-1</span></a>). The way a collections family is created, it always has its elements family stored. If <var class="Arg">Fam</var> is not a collections family then an error is signalled.</p>
149
150
151 <div class="example"><pre>
152 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fam:= FamilyObj( (1,2) );;</span>
153 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">collfam:= CollectionsFamily( fam );;</span>
154 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">fam = collfam; fam = ElementsFamily( collfam );</span>
155 false
156 true
157 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">collfam = FamilyObj( [ (1,2,3) ] );</span>
158 true
159 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">collfam = FamilyObj( Group( () ) );</span>
160 true
161 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">collfam = CollectionsFamily( collfam );</span>
162 false
163 </pre></div>
164
165 <p><a id="X78C38017804B2EA7" name="X78C38017804B2EA7"></a></p>
166
167 <h5>30.2-4 CategoryCollections</h5>
168
169 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; CategoryCollections</code>( <var class="Arg">filter</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
170 <p>Let <var class="Arg">filter</var> be a filter that is <code class="keyw">true</code> for all elements of a family <var class="Arg">Fam</var>, by the construction of <var class="Arg">Fam</var>. Then <code class="func">CategoryCollections</code> returns the <em>collections category</em> of <var class="Arg">filter</var>. This is a category that is <code class="keyw">true</code> for all elements in <code class="code">CollectionsFamily( <var class="Arg">Fam</var> )</code>.</p>
171
172 <p>For example, the construction of <code class="func">PermutationsFamily</code> (<a href="chap42.html#X819628B083B3939B"><span class="RefLink">42.1-3</span></a>) guarantees that each of its elements lies in the filter <code class="func">IsPerm</code> (<a href="chap42.html#X7AA69C6686FC49EA"><span class="RefLink">42.1-1</span></a>), and each collection of permutations (permutation group or dense list of permutations) lies in the category <code class="code">CategoryCollections( IsPerm )</code>. <code class="code">CategoryCollections( IsPerm )</code>. Note that this works only if the collections category is created <em>before</em> the collections family. So it is necessary to construct interesting collections categories immediately after the underlying category has been created.</p>
173
174 <p><a id="X85ABC1B4829778C7" name="X85ABC1B4829778C7"></a></p>
175
176 <h5>30.2-5 DeclareCategoryCollections</h5>
177
178 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; DeclareCategoryCollections</code>( <var class="Arg">name</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
179 <p>Calls <code class="func">CategoryCollections</code> (<a href="chap30.html#X78C38017804B2EA7"><span class="RefLink">30.2-4</span></a>) on the category that is bound to the global variable with name <var class="Arg">name</var> to obtain its collections category, and binds the latter to the global variable with name <code class="code">nname</code>. This name is defined as follows: If <var class="Arg">name</var> is of the form <code class="code"><var class="Arg">Something</var>Collection</code> then <code class="code">nname</code> is set to <code class="code"><var class="Arg">Something</var>CollColl</code>, if <var class="Arg">name</var> is of the form <code class="code"><var class="Arg">Something</var>Coll</code> then <code class="code">nname</code> is set to <code class="code"><var class="Arg">Something</var>CollColl</code>, otherwise we set <code class="code">nname</code> to <code class="code"><var class="Arg">name</var>Collection</code>.</p>
180
181 <p><a id="X7C3722DF8736FFDB" name="X7C3722DF8736FFDB"></a></p>
182
183 <h4>30.3 <span class="Heading">Lists and Collections</span></h4>
184
185 <p>The following functions take a <em>list or collection</em> as argument, and return a corresponding <em>list</em>. They differ in whether or not the result is mutable or immutable (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>), guaranteed to be sorted, or guaranteed to admit list access in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)).</p>
186
187 <p><a id="X877128A77826DD69" name="X877128A77826DD69"></a></p>
188
189 <h5>30.3-1 IsListOrCollection</h5>
190
191 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsListOrCollection</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
192 <p>Several functions are defined for both lists and collections, for example <code class="func">Intersection</code> (<a href="chap30.html#X851069107CACF98E"><span class="RefLink">30.5-2</span></a>), <code class="func">Iterator</code> (<a href="chap30.html#X83ADF8287ED0668E"><span class="RefLink">30.8-1</span></a>), and <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>). <code class="func">IsListOrCollection</code> is a supercategory of <code class="func">IsList</code> (<a href="chap21.html#X7C4CC4EA8299701E"><span class="RefLink">21.1-1</span></a>) and <code class="func">IsCollection</code> (<a href="chap30.html#X79C9FC7F86E2738C"><span class="RefLink">30.1-1</span></a>) (that is, all lists and collections lie in this category), which is used to describe the arguments of functions such as the ones listed above.</p>
193
194 <p><a id="X7EF8910F82B45EC7" name="X7EF8910F82B45EC7"></a></p>
195
196 <h5>30.3-2 Enumerator</h5>
197
198 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Enumerator</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
199 <p><code class="func">Enumerator</code> returns an immutable list <span class="SimpleMath">enum</span>. If the argument is a list (which may contain holes), then <code class="code">Length( </code><span class="SimpleMath">enum</span><code class="code"> )</code> is the length of this list, and <span class="SimpleMath">enum</span> contains the elements (and holes) of this list in the same order. If the argument is a collection that is not a list, then <code class="code">Length( </code><span class="SimpleMath">enum</span><code class="code"> )</code> is the number of different elements of <var class="Arg">C</var>, and <span class="SimpleMath">enum</span> contains the different elements of the collection in an unspecified order, which may change for repeated calls of <code class="func">Enumerator</code>. <span class="SimpleMath">enum[pos]</span> may not execute in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <span class="SimpleMath">enum</span> in memory is as small as is feasible.</p>
200
201 <p>For lists, the default method is <code class="func">Immutable</code> (<a href="chap12.html#X7F0ABF2C870B0CBB"><span class="RefLink">12.6-3</span></a>). For collections that are not lists, there is no default method.</p>
202
203 <p><a id="X80CD7DDC7D0C60D5" name="X80CD7DDC7D0C60D5"></a></p>
204
205 <h5>30.3-3 EnumeratorSorted</h5>
206
207 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; EnumeratorSorted</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
208 <p><code class="func">EnumeratorSorted</code> returns an immutable list <span class="SimpleMath">enum</span>. The argument must be a collection or a list <var class="Arg">listorcoll</var> which may contain holes but whose elements lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). <code class="code">Length( </code><span class="SimpleMath">enum</span><code class="code"> )</code> is the number of different elements of the argument, and <span class="SimpleMath">enum</span> contains the different elements in sorted order, w.r.t. <code class="code">&lt;</code>. <span class="SimpleMath">enum[pos]</span> may not execute in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <span class="SimpleMath">enum</span> in memory is as small as is feasible.</p>
209
210
211 <div class="example"><pre>
212 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Enumerator( [ 1, 3,, 2 ] );</span>
213 [ 1, 3,, 2 ]
214 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">enum:= Enumerator( Rationals );; elm:= enum[ 10^6 ];</span>
215 -69/907
216 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Position( enum, elm );</span>
217 1000000
218 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( enum ); IsSortedList( enum );</span>
219 false
220 false
221 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsConstantTimeAccessList( enum );</span>
222 false
223 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">EnumeratorSorted( [ 1, 3,, 2 ] );</span>
224 [ 1, 2, 3 ]
225 </pre></div>
226
227 <p><a id="X85E149177AC547C3" name="X85E149177AC547C3"></a></p>
228
229 <h5>30.3-4 EnumeratorByFunctions</h5>
230
231 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; EnumeratorByFunctions</code>( <var class="Arg">D</var>, <var class="Arg">record</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
232 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; EnumeratorByFunctions</code>( <var class="Arg">Fam</var>, <var class="Arg">record</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
233 <p><code class="func">EnumeratorByFunctions</code> returns an immutable, dense, and duplicate-free list <span class="SimpleMath">enum</span> for which <code class="func">IsBound</code> (<a href="chap21.html#X79EC565A7DCEC938"><span class="RefLink">21.5-1</span></a>), element access via <code class="func"><span>\</span>[<span>\</span>]</code> (<a href="chap21.html#X8297BBCD79642BE6"><span class="RefLink">21.2-1</span></a>), <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>), and <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>) are computed via prescribed functions.</p>
234
235 <p>Let <var class="Arg">record</var> be a record with at least the following components.</p>
236
237
238 <dl>
239 <dt><strong class="Mark"><code class="code">ElementNumber</code></strong></dt>
240 <dd><p>a function taking two arguments <var class="Arg">enum</var> and <var class="Arg">pos</var>, which returns <code class="code"><var class="Arg">enum</var>[ <var class="Arg">pos</var> ]</code> (see <a href="chap21.html#X7B202D147A5C2884"><span class="RefLink">21.2</span></a>); it can be assumed that the argument <var class="Arg">pos</var> is a positive integer, but <var class="Arg">pos</var> may be larger than the length of <var class="Arg">enum</var> (in which case an error must be signalled); note that the result must be immutable since <var class="Arg">enum</var> itself is immutable,</p>
241
242 </dd>
243 <dt><strong class="Mark"><code class="code">NumberElement</code></strong></dt>
244 <dd><p>a function taking two arguments <var class="Arg">enum</var> and <var class="Arg">elm</var>, which returns <code class="code">Position( <var class="Arg">enum</var>, <var class="Arg">elm</var> )</code> (see <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>)); it cannot be assumed that <var class="Arg">elm</var> is really contained in <var class="Arg">enum</var> (and <code class="keyw">fail</code> must be returned if not); note that for the three argument version of <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>), the method that is available for duplicate-free lists suffices.</p>
245
246 </dd>
247 </dl>
248 <p>Further (data) components may be contained in <var class="Arg">record</var> which can be used by these function.</p>
249
250 <p>If the first argument is a domain <var class="Arg">D</var> then <var class="Arg">enum</var> lists the elements of <var class="Arg">D</var> (in general <var class="Arg">enum</var> is <em>not</em> sorted), and methods for <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>), <code class="func">IsBound</code> (<a href="chap21.html#X79EC565A7DCEC938"><span class="RefLink">21.5-1</span></a>), and <code class="func">PrintObj</code> (<a href="chap6.html#X815BF22186FD43C9"><span class="RefLink">6.3-5</span></a>) may use <var class="Arg">D</var>.</p>
251
252 <p>If one wants to describe the result without creating a domain then the elements are given implicitly by the functions in <var class="Arg">record</var>, and the first argument must be a family <var class="Arg">Fam</var> which will become the family of <var class="Arg">enum</var>; if <var class="Arg">enum</var> is not homogeneous then <var class="Arg">Fam</var> must be <code class="code">ListsFamily</code>, otherwise it must be the collections family of any element in <var class="Arg">enum</var>. In this case, additionally the following component in <var class="Arg">record</var> is needed.</p>
253
254
255 <dl>
256 <dt><strong class="Mark"><code class="code">Length</code></strong></dt>
257 <dd><p>a function taking the argument <var class="Arg">enum</var>, which returns the length of <var class="Arg">enum</var> (see <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>)).</p>
258
259 </dd>
260 </dl>
261 <p>The following components are optional; they are used if they are present but default methods are installed for the case that they are missing.</p>
262
263
264 <dl>
265 <dt><strong class="Mark"><code class="code">IsBound\[\]</code></strong></dt>
266 <dd><p>a function taking two arguments <var class="Arg">enum</var> and <var class="Arg">k</var>, which returns <code class="code">IsBound( <var class="Arg">enum</var>[ <var class="Arg">k</var> ] )</code> (see <a href="chap21.html#X7B202D147A5C2884"><span class="RefLink">21.2</span></a>); if this component is missing then <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>) is used for computing the result,</p>
267
268 </dd>
269 <dt><strong class="Mark"><code class="code">Membership</code></strong></dt>
270 <dd><p>a function taking two arguments <var class="Arg">elm</var> and <var class="Arg">enum</var>, which returns <code class="keyw">true</code> is <var class="Arg">elm</var> is an element of <var class="Arg">enum</var>, and <code class="keyw">false</code> otherwise (see <a href="chap21.html#X7B202D147A5C2884"><span class="RefLink">21.2</span></a>); if this component is missing then <code class="code">NumberElement</code> is used for computing the result,</p>
271
272 </dd>
273 <dt><strong class="Mark"><code class="code">AsList</code></strong></dt>
274 <dd><p>a function taking one argument <var class="Arg">enum</var>, which returns a list with the property that the access to each of its elements will take roughly the same time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)); if this component is missing then <code class="func">ConstantTimeAccessList</code> (<a href="chap21.html#X7B55FB967CDEF468"><span class="RefLink">21.17-6</span></a>) is used for computing the result,</p>
275
276 </dd>
277 <dt><strong class="Mark"><code class="code">ViewObj</code> and <code class="code">PrintObj</code></strong></dt>
278 <dd><p>two functions that print what one wants to be printed when <code class="code">View( <var class="Arg">enum</var> )</code> or <code class="code">Print( <var class="Arg">enum</var> )</code> is called (see <a href="chap6.html#X8074A8387C9DB9A8"><span class="RefLink">6.3</span></a>), if the <code class="code">ViewObj</code> component is missing then the <code class="code">PrintObj</code> method is used as a default.</p>
279
280 </dd>
281 </dl>
282 <p>If the result is known to have additional properties such as being strictly sorted (see <code class="func">IsSSortedList</code> (<a href="chap21.html#X80CDAF45782E8DCB"><span class="RefLink">21.17-4</span></a>)) then it can be useful to set these properties after the construction of the enumerator, before it is used for the first time. And in the case that a new sorted enumerator of a domain is implemented via <code class="func">EnumeratorByFunctions</code>, and this construction is installed as a method for the operation <code class="func">Enumerator</code> (<a href="chap30.html#X7EF8910F82B45EC7"><span class="RefLink">30.3-2</span></a>), then it should be installed also as a method for <code class="func">EnumeratorSorted</code> (<a href="chap30.html#X80CD7DDC7D0C60D5"><span class="RefLink">30.3-3</span></a>).</p>
283
284 <p>Note that it is <em>not</em> checked that <code class="func">EnumeratorByFunctions</code> really returns a dense and duplicate-free list. <code class="func">EnumeratorByFunctions</code> does <em>not</em> make a shallow copy of <var class="Arg">record</var>, this record is changed in place, see <a href="chap79.html#X82E86CF37B123FD4"><span class="RefLink">79.1</span></a>.</p>
285
286 <p>It would be easy to implement a slightly generalized setup for enumerators that need not be duplicate-free (where the three argument version of <code class="func">Position</code> (<a href="chap21.html#X79975EC6783B4293"><span class="RefLink">21.16-1</span></a>) is supported), but the resulting overhead for the methods seems not to be justified.</p>
287
288 <p><a id="X7F12F40E87F3C3A7" name="X7F12F40E87F3C3A7"></a></p>
289
290 <h5>30.3-5 List</h5>
291
292 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; List</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
293 <p>For a collection <var class="Arg">C</var> (see <a href="chap30.html#X8050A8037984E5B6"><span class="RefLink">30</span></a>) that is not a list, <code class="func">List</code> returns a new mutable list <var class="Arg">new</var> such that <code class="code">Length( <var class="Arg">new</var> )</code> is the number of different elements of <var class="Arg">C</var>, and <var class="Arg">new</var> contains the different elements of <var class="Arg">C</var> in an unspecified order which may change for repeated calls. <code class="code"><var class="Arg">new</var>[<var class="Arg">pos</var>]</code> executes in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <var class="Arg">new</var> is proportional to its length. The generic method for this case is <code class="code">ShallowCopy( Enumerator( <var class="Arg">C</var> ) )</code>.</p>
294
295 <p>Developers who wish to adapt this for custom list or collection types need to install suitable methods for the operation <code class="code">ListOp</code>.</p>
296
297
298 <div class="example"><pre>
299 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= List( Group( (1,2,3) ) );</span>
300 [ (), (1,3,2), (1,2,3) ]
301 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l ); IsSortedList( l ); IsConstantTimeAccessList( l );</span>
302 true
303 false
304 true
305 </pre></div>
306
307 <p>(See also <code class="func">List</code> (<a href="chap21.html#X86CB7DCE8510F977"><span class="RefLink">21.20-18</span></a>).)</p>
308
309 <p><a id="X82CE157A7FAD8036" name="X82CE157A7FAD8036"></a></p>
310
311 <h5>30.3-6 SortedList</h5>
312
313 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SortedList</code>( <var class="Arg">listorcoll</var>[, <var class="Arg">func</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
314 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SortedListBy</code>( <var class="Arg">listorcoll</var>, <var class="Arg">func</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
315 <p><code class="func">SortedList</code> returns a new mutable and dense list <var class="Arg">new</var>. The argument must be a collection or list <var class="Arg">listorcoll</var> which may contain holes but whose elements lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). <code class="code">Length( <var class="Arg">new</var> )</code> is the number of elements of <var class="Arg">listorcoll</var>, and <var class="Arg">new</var> contains the elements in sorted order, w.r.t. <code class="code">&lt;</code> or <var class="Arg">func</var> if it is specified. For details, please refer to <code class="func">Sort</code> (<a href="chap21.html#X7FE4975F8166884D"><span class="RefLink">21.18-1</span></a>).</p>
316
317 <p><code class="func">SortedListBy</code> also returns a new mutable and dense list <var class="Arg">new</var>. The first argument must be a collection or list <var class="Arg">listorcoll</var> which may contain holes but whose elements lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). <code class="code">Length( <var class="Arg">new</var> )</code> is the number of elements of <var class="Arg">listorcoll</var>, and <var class="Arg">new</var> has elements in an order such that <code class="code">func(new[i]) &lt;= func(new[i+1])</code> for all relevant <var class="Arg">i</var>. <var class="Arg">func</var> must thus be a function on one argument which returns values that can be compared. Each <code class="code">func(listorcoll[i])</code> is computed just once and stored, making this more efficient than using the two-argument version of <code class="func">SortedList</code> in many cases.</p>
318
319 <p>In both cases, <code class="code"><var class="Arg">new</var>[<var class="Arg">pos</var>]</code> executes in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <var class="Arg">new</var> in memory is proportional to its length.</p>
320
321
322 <div class="example"><pre>
323 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= SortedList( Group( (1,2,3) ) );</span>
324 [ (), (1,2,3), (1,3,2) ]
325 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l ); IsSortedList( l ); IsConstantTimeAccessList( l );</span>
326 true
327 true
328 true
329 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SortedList( [ 1, 2, 1,, 3, 2 ] );</span>
330 [ 1, 1, 2, 2, 3 ]
331 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SortedListBy(SymmetricGroup(3), Order);</span>
332 [ (), (1,3), (2,3), (1,2), (1,2,3), (1,3,2) ]
333 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SortedListBy( [ 3, 1,,, -6, 5, -2,, -5, 3, 3,,, -1 ], x -&gt; x^2 );</span>
334 [ 1, -1, -2, 3, 3, 3, 5, -5, -6 ]
335 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:=SortedListBy( [ 2, 1,, 1, -5,, 3, -5 ], x -&gt; x^3);</span>
336 [ -5, -5, 1, 1, 2, 3 ]
337 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l ); IsSortedList( l ); IsConstantTimeAccessList( l );</span>
338 true
339 true
340 true
341 </pre></div>
342
343 <p><a id="X7E399AC97FD98217" name="X7E399AC97FD98217"></a></p>
344
345 <h5>30.3-7 SSortedList</h5>
346
347 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; SSortedList</code>( <var class="Arg">listorcoll</var>[, <var class="Arg">fun</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
348 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Set</code>( <var class="Arg">C</var>[, <var class="Arg">fun</var>] )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
349 <p><code class="func">SSortedList</code> (<q>strictly sorted list</q>) returns a new dense, mutable, and duplicate free list <var class="Arg">new</var>. The argument must be a collection or list <var class="Arg">listorcoll</var> which may contain holes.</p>
350
351 <p>If the optional argument <var class="Arg">fun</var> is not given then <code class="code">Length( <var class="Arg">new</var> )</code> is the number of different elements of <var class="Arg">listorcoll</var>, and <var class="Arg">new</var> contains the different elements in strictly sorted order, w.r.t. <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>). For that, any two entries of <var class="Arg">listorcoll</var> must be comparable via <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>). (Typically, the entries lie in the same family, see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>.)</p>
352
353 <p>If <var class="Arg">fun</var> is given then it must be a unary function. In this case, <var class="Arg">fun</var> is applied to all elements of <var class="Arg">listorcoll</var>, <var class="Arg">new</var> contains the different return values in strictly sorted order, and <code class="code">Length( <var class="Arg">new</var> )</code> is the number of different such values. For that, any two return values must be comparable via <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>).</p>
354
355 <p><code class="code"><var class="Arg">new</var>[<var class="Arg">pos</var>]</code> executes in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <var class="Arg">new</var> in memory is proportional to its length.</p>
356
357 <p><code class="func">Set</code> is simply a synonym for <code class="func">SSortedList</code>.</p>
358
359
360 <div class="example"><pre>
361 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= SSortedList( Group( (1,2,3) ) );</span>
362 [ (), (1,2,3), (1,3,2) ]
363 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l ); IsSSortedList( l ); IsConstantTimeAccessList( l );</span>
364 true
365 true
366 true
367 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SSortedList( Group( (1,2,3) ), Order );</span>
368 [ 1, 3 ]
369 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SSortedList( [ 1, 2, 1,, 3, 2 ] );</span>
370 [ 1, 2, 3 ]
371 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">SSortedList( [ 1, 2, 1,, 3, 2 ], x -&gt; x^2 );</span>
372 [ 1, 4, 9 ]
373 </pre></div>
374
375 <p><a id="X8289FCCC8274C89D" name="X8289FCCC8274C89D"></a></p>
376
377 <h5>30.3-8 AsList</h5>
378
379 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AsList</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
380 <p><code class="func">AsList</code> returns a immutable list <var class="Arg">imm</var>. If the argument is a list (which may contain holes), then <code class="code">Length( <var class="Arg">imm</var> )</code> is the <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>) value of this list, and <var class="Arg">imm</var> contains the elements (and holes) of the list in the same order. If the argument is a collection that is not a list, then <code class="code">Length( <var class="Arg">imm</var> )</code> is the number of different elements of this collection, and <var class="Arg">imm</var> contains the different elements of the collection in an unspecified order, which may change for repeated calls of <code class="func">AsList</code>. <code class="code"><var class="Arg">imm</var>[<var class="Arg">pos</var>]</code> executes in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <var class="Arg">imm</var> in memory is proportional to its length.</p>
381
382 <p>If you expect to do many element tests in the resulting list, it might be worth to use a sorted list instead, using <code class="func">AsSSortedList</code> (<a href="chap30.html#X856D927378C33548"><span class="RefLink">30.3-10</span></a>).</p>
383
384
385 <div class="example"><pre>
386 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= AsList( [ 1, 3, 3,, 2 ] );</span>
387 [ 1, 3, 3,, 2 ]
388 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l ); IsSortedList( l ); IsConstantTimeAccessList( l );</span>
389 false
390 false
391 true
392 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AsList( Group( (1,2,3), (1,2) ) );</span>
393 [ (), (2,3), (1,3,2), (1,3), (1,2,3), (1,2) ]
394 </pre></div>
395
396 <p><a id="X7BCA5C6181391007" name="X7BCA5C6181391007"></a></p>
397
398 <h5>30.3-9 AsSortedList</h5>
399
400 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AsSortedList</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
401 <p><code class="func">AsSortedList</code> returns a dense and immutable list <var class="Arg">imm</var>. The argument must be a collection or list <var class="Arg">listorcoll</var> which may contain holes but whose elements lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). <code class="code">Length( <var class="Arg">imm</var> )</code> is the number of elements of the argument, and <var class="Arg">imm</var> contains the elements in sorted order, w.r.t. <code class="code">&lt;=</code>. <code class="code"><var class="Arg">new</var>[<var class="Arg">pos</var>]</code> executes in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <var class="Arg">imm</var> in memory is proportional to its length.</p>
402
403 <p>The only difference to the operation <code class="func">SortedList</code> (<a href="chap30.html#X82CE157A7FAD8036"><span class="RefLink">30.3-6</span></a>) is that <code class="func">AsSortedList</code> returns an <em>immutable</em> list.</p>
404
405
406 <div class="example"><pre>
407 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= AsSortedList( [ 1, 3, 3,, 2 ] );</span>
408 [ 1, 2, 3, 3 ]
409 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l ); IsSortedList( l ); IsConstantTimeAccessList( l );</span>
410 false
411 true
412 true
413 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsSSortedList( l );</span>
414 false
415 </pre></div>
416
417 <p><a id="X856D927378C33548" name="X856D927378C33548"></a></p>
418
419 <h5>30.3-10 AsSSortedList</h5>
420
421 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AsSSortedList</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
422 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; AsSet</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
423 <p><code class="func">AsSSortedList</code> (<q>as strictly sorted list</q>) returns a dense, immutable, and duplicate free list <var class="Arg">imm</var>. The argument must be a collection or list <var class="Arg">listorcoll</var> which may contain holes but whose elements lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). <code class="code">Length( <var class="Arg">imm</var> )</code> is the number of different elements of <var class="Arg">listorcoll</var>, and <var class="Arg">imm</var> contains the different elements in strictly sorted order, w.r.t. <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>). <code class="code"><var class="Arg">imm</var>[<var class="Arg">pos</var>]</code> executes in constant time (see <code class="func">IsConstantTimeAccessList</code> (<a href="chap21.html#X7C84E16A85C99C8C"><span class="RefLink">21.1-6</span></a>)), and the size of <var class="Arg">imm</var> in memory is proportional to its length.</p>
424
425 <p>Because the comparisons required for sorting can be very expensive for some kinds of objects, you should use <code class="func">AsList</code> (<a href="chap30.html#X8289FCCC8274C89D"><span class="RefLink">30.3-8</span></a>) instead if you do not require the result to be sorted.</p>
426
427 <p>The only difference to the operation <code class="func">SSortedList</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) is that <code class="func">AsSSortedList</code> returns an <em>immutable</em> list.</p>
428
429 <p><code class="func">AsSet</code> is simply a synonym for <code class="func">AsSSortedList</code>.</p>
430
431 <p>In general a function that returns a set of elements is free, in fact encouraged, to return a domain instead of the proper set of its elements. This allows one to keep a given structure, and moreover the representation by a domain object is usually more space efficient. <code class="func">AsSSortedList</code> must of course <em>not</em> do this, its only purpose is to create the proper set of elements.</p>
432
433
434 <div class="example"><pre>
435 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= AsSSortedList( l );</span>
436 [ 1, 2, 3 ]
437 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsMutable( l ); IsSSortedList( l ); IsConstantTimeAccessList( l );</span>
438 false
439 true
440 true
441 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">AsSSortedList( Group( (1,2,3), (1,2) ) );</span>
442 [ (), (2,3), (1,2), (1,2,3), (1,3,2), (1,3) ]
443 </pre></div>
444
445 <p><a id="X79B130FC7906FB4C" name="X79B130FC7906FB4C"></a></p>
446
447 <h5>30.3-11 Elements</h5>
448
449 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Elements</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
450 <p><code class="func">Elements</code> does the same as <code class="func">AsSSortedList</code> (<a href="chap30.html#X856D927378C33548"><span class="RefLink">30.3-10</span></a>), that is, the return value is a strictly sorted list of the elements in the list or collection <var class="Arg">C</var>.</p>
451
452 <p><code class="func">Elements</code> is only supported for backwards compatibility. In many situations, the sortedness of the <q>element list</q> for a collection is in fact not needed, and one can save a lot of time by asking for a list that is <em>not</em> necessarily sorted, using <code class="func">AsList</code> (<a href="chap30.html#X8289FCCC8274C89D"><span class="RefLink">30.3-8</span></a>). If one is really interested in the strictly sorted list of elements in <var class="Arg">C</var> then one should use <code class="func">AsSet</code> (<a href="chap30.html#X856D927378C33548"><span class="RefLink">30.3-10</span></a>) or <code class="func">AsSSortedList</code> (<a href="chap30.html#X856D927378C33548"><span class="RefLink">30.3-10</span></a>) instead.</p>
453
454 <p><a id="X79AD18737E70B414" name="X79AD18737E70B414"></a></p>
455
456 <h4>30.4 <span class="Heading">Attributes and Properties for Collections</span></h4>
457
458 <p><a id="X7969C48780C5C1BC" name="X7969C48780C5C1BC"></a></p>
459
460 <h5>30.4-1 IsEmpty</h5>
461
462 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsEmpty</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
463 <p><code class="func">IsEmpty</code> returns <code class="keyw">true</code> if the collection or list <var class="Arg">listorcoll</var> is <em>empty</em> (that is it contains no elements), and <code class="keyw">false</code> otherwise.</p>
464
465 <p><a id="X808A4061809A6E67" name="X808A4061809A6E67"></a></p>
466
467 <h5>30.4-2 IsFinite</h5>
468
469 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsFinite</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
470 <p><code class="func">IsFinite</code> returns <code class="keyw">true</code> if the collection <var class="Arg">C</var> is finite, and <code class="keyw">false</code> otherwise.</p>
471
472 <p>The default method for <code class="func">IsFinite</code> checks the size (see <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>)) of <var class="Arg">C</var>.</p>
473
474 <p>Methods for <code class="func">IsFinite</code> may call <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>), but methods for <code class="func">Size</code> (<a href="chap30.html#X858ADA3B7A684421"><span class="RefLink">30.4-6</span></a>) must <em>not</em> call <code class="func">IsFinite</code>.</p>
475
476 <p><a id="X7E3402D6799D3C24" name="X7E3402D6799D3C24"></a></p>
477
478 <h5>30.4-3 IsTrivial</h5>
479
480 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsTrivial</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
481 <p><code class="func">IsTrivial</code> returns <code class="keyw">true</code> if the collection <var class="Arg">C</var> consists of exactly one element.</p>
482
483 <p><a id="X7F192373850B85B9" name="X7F192373850B85B9"></a></p>
484
485 <h5>30.4-4 IsNonTrivial</h5>
486
487 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsNonTrivial</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
488 <p><code class="func">IsNonTrivial</code> returns <code class="keyw">true</code> if the collection <var class="Arg">C</var> is empty or consists of at least two elements (see <code class="func">IsTrivial</code> (<a href="chap30.html#X7E3402D6799D3C24"><span class="RefLink">30.4-3</span></a>)).</p>
489
490
491 <div class="example"><pre>
492 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsEmpty( [] ); IsEmpty( [ 1 .. 100 ] ); IsEmpty( Group( (1,2,3) ) );</span>
493 true
494 false
495 false
496 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsFinite( [ 1 .. 100 ] ); IsFinite( Integers );</span>
497 true
498 false
499 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsTrivial( Integers ); IsTrivial( Group( () ) );</span>
500 false
501 true
502 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsNonTrivial( Integers ); IsNonTrivial( Group( () ) );</span>
503 true
504 false
505 </pre></div>
506
507 <p><a id="X78EF6A137E8F66B0" name="X78EF6A137E8F66B0"></a></p>
508
509 <h5>30.4-5 IsWholeFamily</h5>
510
511 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsWholeFamily</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;property&nbsp;)</td></tr></table></div>
512 <p><code class="func">IsWholeFamily</code> returns <code class="keyw">true</code> if the collection <var class="Arg">C</var> contains the whole family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>) of its elements.</p>
513
514
515 <div class="example"><pre>
516 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsWholeFamily( Integers )</span>
517 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ; # all rationals and cyclotomics lie in the family</span>
518 false
519 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsWholeFamily( Integers mod 3 )</span>
520 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ; # all finite field elements in char. 3 lie in this family</span>
521 false
522 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsWholeFamily( Integers mod 4 );</span>
523 true
524 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsWholeFamily( FreeGroup( 2 ) );</span>
525 true
526 </pre></div>
527
528 <p><a id="X858ADA3B7A684421" name="X858ADA3B7A684421"></a></p>
529
530 <h5>30.4-6 Size</h5>
531
532 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Size</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
533 <p><code class="func">Size</code> returns the size of the list or collection <var class="Arg">listorcoll</var>, which is either an integer or <code class="func">infinity</code> (<a href="chap18.html#X8511B8DF83324C27"><span class="RefLink">18.2-1</span></a>). If the argument is a list then the result is its length (see <code class="func">Length</code> (<a href="chap21.html#X780769238600AFD1"><span class="RefLink">21.17-5</span></a>)).</p>
534
535 <p>The default method for <code class="func">Size</code> checks the length of an enumerator of <var class="Arg">listorcoll</var>.</p>
536
537 <p>Methods for <code class="func">IsFinite</code> (<a href="chap30.html#X808A4061809A6E67"><span class="RefLink">30.4-2</span></a>) may call <code class="func">Size</code>, but methods for <code class="func">Size</code> must not call <code class="func">IsFinite</code> (<a href="chap30.html#X808A4061809A6E67"><span class="RefLink">30.4-2</span></a>).</p>
538
539
540 <div class="example"><pre>
541 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Size( [1,2,3] ); Size( Group( () ) ); Size( Integers );</span>
542 3
543 1
544 infinity
545 </pre></div>
546
547 <p><a id="X865507568182424E" name="X865507568182424E"></a></p>
548
549 <h5>30.4-7 Representative</h5>
550
551 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Representative</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
552 <p><code class="func">Representative</code> returns a <em>representative</em> of the collection <var class="Arg">C</var>.</p>
553
554 <p>Note that <code class="func">Representative</code> is free in choosing a representative if there are several elements in <var class="Arg">C</var>. It is not even guaranteed that <code class="func">Representative</code> returns the same representative if it is called several times for one collection. The main difference between <code class="func">Representative</code> and <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>) is that <code class="func">Representative</code> is free to choose a value that is cheap to compute, while <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>) must make an effort to randomly distribute its answers.</p>
555
556 <p>If <var class="Arg">C</var> is a domain then there are methods for <code class="func">Representative</code> that try to fetch an element from any known generator list of <var class="Arg">C</var>, see <a href="chap31.html#X7E651AC287AFDCC1"><span class="RefLink">31</span></a>. Note that <code class="func">Representative</code> does not try to <em>compute</em> generators of <var class="Arg">C</var>, thus <code class="func">Representative</code> may give up and signal an error if <var class="Arg">C</var> has no generators stored at all.</p>
557
558 <p><a id="X8026085680270D37" name="X8026085680270D37"></a></p>
559
560 <h5>30.4-8 RepresentativeSmallest</h5>
561
562 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RepresentativeSmallest</code>( <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;attribute&nbsp;)</td></tr></table></div>
563 <p>returns the smallest element in the collection <var class="Arg">C</var>, w.r.t. the ordering <code class="func">\&lt;</code> (<a href="chap31.html#X7EF67D047F03CA6F"><span class="RefLink">31.11-1</span></a>). While the operation defaults to comparing all elements, better methods are installed for some collections.</p>
564
565
566 <div class="example"><pre>
567 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Representative( Rationals );</span>
568 0
569 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Representative( [ -1, -2 .. -100 ] );</span>
570 -1
571 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RepresentativeSmallest( [ -1, -2 .. -100 ] );</span>
572 -100
573 </pre></div>
574
575 <p><a id="X7F8FEA3278239ADE" name="X7F8FEA3278239ADE"></a></p>
576
577 <h4>30.5 <span class="Heading">Operations for Collections</span></h4>
578
579 <p><a id="X79CA175481F8105F" name="X79CA175481F8105F"></a></p>
580
581 <h5>30.5-1 IsSubset</h5>
582
583 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsSubset</code>( <var class="Arg">C1</var>, <var class="Arg">C2</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
584 <p><code class="func">IsSubset</code> returns <code class="keyw">true</code> if <var class="Arg">C2</var>, which must be a collection, is a <em>subset</em> of <var class="Arg">C1</var>, which also must be a collection, and <code class="keyw">false</code> otherwise.</p>
585
586 <p><var class="Arg">C2</var> is considered a subset of <var class="Arg">C1</var> if and only if each element of <var class="Arg">C2</var> is also an element of <var class="Arg">C1</var>. That is <code class="func">IsSubset</code> behaves as if implemented as <code class="code">IsSubsetSet( AsSSortedList( <var class="Arg">C1</var> ), AsSSortedList( <var class="Arg">C2</var> ) )</code>, except that it will also sometimes, but not always, work for infinite collections, and that it will usually work much faster than the above definition. Either argument may also be a proper set (see <a href="chap21.html#X80ABC25582343910"><span class="RefLink">21.19</span></a>).</p>
587
588
589 <div class="example"><pre>
590 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsSubset( Rationals, Integers );</span>
591 true
592 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsSubset( Integers, [ 1, 2, 3 ] );</span>
593 true
594 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsSubset( Group( (1,2,3,4) ), [ (1,2,3) ] );</span>
595 false
596 </pre></div>
597
598 <p><a id="X851069107CACF98E" name="X851069107CACF98E"></a></p>
599
600 <h5>30.5-2 <span class="Heading">Intersection</span></h5>
601
602 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Intersection</code>( <var class="Arg">C1</var>, <var class="Arg">C2</var>, <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
603 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Intersection</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
604 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Intersection2</code>( <var class="Arg">C1</var>, <var class="Arg">C2</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
605 <p>In the first form <code class="func">Intersection</code> returns the intersection of the collections <var class="Arg">C1</var>, <var class="Arg">C2</var>, etc. In the second form <var class="Arg">list</var> must be a <em>nonempty</em> list of collections and <code class="func">Intersection</code> returns the intersection of those collections. Each argument or element of <var class="Arg">list</var> respectively may also be a homogeneous list that is not a proper set, in which case <code class="func">Intersection</code> silently applies <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) to it first.</p>
606
607 <p>The result of <code class="func">Intersection</code> is the set of elements that lie in every of the collections <var class="Arg">C1</var>, <var class="Arg">C2</var>, etc. If the result is a list then it is mutable and new, i.e., not identical to any of <var class="Arg">C1</var>, <var class="Arg">C2</var>, etc.</p>
608
609 <p>Methods can be installed for the operation <code class="func">Intersection2</code> that takes only two arguments. <code class="func">Intersection</code> calls <code class="func">Intersection2</code>.</p>
610
611 <p>Methods for <code class="func">Intersection2</code> should try to maintain as much structure as possible, for example the intersection of two permutation groups is again a permutation group.</p>
612
613
614 <div class="example"><pre>
615 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># this is one of the rare cases where the intersection of two</span>
616 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># infinite domains works ('CF' is a shorthand for 'CyclotomicField'):</span>
617 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection( CyclotomicField(9), CyclotomicField(12) );</span>
618 CF(3)
619 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">D12 := Group( (2,6)(3,5), (1,2)(3,6)(4,5) );;</span>
620 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection( D12, Group( (1,2), (1,2,3,4,5) ) );</span>
621 Group([ (1,5)(2,4) ])
622 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection( D12, [ (1,3)(4,6), (1,2)(3,4) ] )</span>
623 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ; # note that the second argument is not a proper set</span>
624 [ (1,3)(4,6) ]
625 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># although the result is mathematically a group it is returned as a</span>
626 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput"># proper set because the second argument is not regarded as a group:</span>
627 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection( D12, [ (), (1,2)(3,4), (1,3)(4,6), (1,4)(5,6) ] );</span>
628 [ (), (1,3)(4,6) ]
629 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection( Group( () ), [1,2,3] );</span>
630 [ ]
631 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection( [2,4,6,8,10], [3,6,9,12,15], [5,10,15,20,25] )</span>
632 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ; # two or more lists or collections as arguments are legal</span>
633 [ ]
634 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Intersection( [ [1,2,4], [2,3,4], [1,3,4] ] )</span>
635 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ; # or one list of lists or collections</span>
636 [ 4 ]
637 </pre></div>
638
639 <p><a id="X799F0E2F7A502DBA" name="X799F0E2F7A502DBA"></a></p>
640
641 <h5>30.5-3 <span class="Heading">Union</span></h5>
642
643 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Union</code>( <var class="Arg">C1</var>, <var class="Arg">C2</var>, <var class="Arg">...</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
644 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Union</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
645 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Union2</code>( <var class="Arg">C1</var>, <var class="Arg">C2</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
646 <p>In the first form <code class="func">Union</code> returns the union of the collections <var class="Arg">C1</var>, <var class="Arg">C2</var>, etc. In the second form <var class="Arg">list</var> must be a list of collections and <code class="func">Union</code> returns the union of those collections. Each argument or element of <var class="Arg">list</var> respectively may also be a homogeneous list that is not a proper set, in which case <code class="func">Union</code> silently applies <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) to it first.</p>
647
648 <p>The result of <code class="func">Union</code> is the set of elements that lie in any of the collections <var class="Arg">C1</var>, <var class="Arg">C2</var>, etc. If the result is a list then it is mutable and new, i.e., not identical to any of <var class="Arg">C1</var>, <var class="Arg">C2</var>, etc.</p>
649
650 <p>Methods can be installed for the operation <code class="func">Union2</code> that takes only two arguments. <code class="func">Union</code> calls <code class="func">Union2</code>.</p>
651
652
653 <div class="example"><pre>
654 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Union( [ (1,2,3), (1,2,3,4) ], Group( (1,2,3), (1,2) ) );</span>
655 [ (), (2,3), (1,2), (1,2,3), (1,2,3,4), (1,3,2), (1,3) ]
656 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Union( [2,4,6,8,10], [3,6,9,12,15], [5,10,15,20,25] )</span>
657 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ; # two or more lists or collections as arguments are legal</span>
658 [ 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 20, 25 ]
659 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Union( [ [1,2,4], [2,3,4], [1,3,4] ] )</span>
660 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> ; # or one list of lists or collections</span>
661 [ 1 .. 4 ]
662 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Union( [ ] );</span>
663 [ ]
664 </pre></div>
665
666 <p>When computing the Union of lists or sets of small integers and ranges, every attempt is made to return the result as a range and to avoid expanding ranges provided as input.</p>
667
668 <p><a id="X825AC0F07E010B07" name="X825AC0F07E010B07"></a></p>
669
670 <h5>30.5-4 Difference</h5>
671
672 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Difference</code>( <var class="Arg">C1</var>, <var class="Arg">C2</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
673 <p><code class="func">Difference</code> returns the set difference of the collections <var class="Arg">C1</var> and <var class="Arg">C2</var>. Either argument may also be a homogeneous list that is not a proper set, in which case <code class="func">Difference</code> silently applies <code class="func">Set</code> (<a href="chap30.html#X7E399AC97FD98217"><span class="RefLink">30.3-7</span></a>) to it first.</p>
674
675 <p>The result of <code class="func">Difference</code> is the set of elements that lie in <var class="Arg">C1</var> but not in <var class="Arg">C2</var>. Note that <var class="Arg">C2</var> need not be a subset of <var class="Arg">C1</var>. The elements of <var class="Arg">C2</var>, however, that are not elements of <var class="Arg">C1</var> play no role for the result. If the result is a list then it is mutable and new, i.e., not identical to <var class="Arg">C1</var> or <var class="Arg">C2</var>.</p>
676
677
678 <div class="example"><pre>
679 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Difference( [ (1,2,3), (1,2,3,4) ], Group( (1,2,3), (1,2) ) );</span>
680 [ (1,2,3,4) ]
681 </pre></div>
682
683 <p><a id="X82D39CF980FDBFFA" name="X82D39CF980FDBFFA"></a></p>
684
685 <h4>30.6 <span class="Heading">Membership Test for Collections</span></h4>
686
687 <p><a id="X84B7FA8C7C94400F" name="X84B7FA8C7C94400F"></a></p>
688
689 <h5><code>30.6-1 \in</code></h5>
690
691 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; \in</code>( <var class="Arg">obj</var>, <var class="Arg">C</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
692 <p>returns <code class="keyw">true</code> if the object <var class="Arg">obj</var> lies in the collection <var class="Arg">C</var>, and <code class="keyw">false</code> otherwise.</p>
693
694 <p>The infix version of the command</p>
695
696 <p><var class="Arg">obj</var> <code class="keyw">in</code> <var class="Arg">C</var></p>
697
698 <p>calls the operation <code class="func">\in</code>, for which methods can be installed.</p>
699
700
701 <div class="example"><pre>
702 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">13 in Integers; [ 1, 2 ] in Integers;</span>
703 true
704 false
705 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">g:= Group( (1,2) );; (1,2) in g; (1,2,3) in g;</span>
706 true
707 false
708 </pre></div>
709
710 <p><a id="X8151A51884B7EE2C" name="X8151A51884B7EE2C"></a></p>
711
712 <h4>30.7 <span class="Heading">Random Elements</span></h4>
713
714 <p>The method used by <strong class="pkg">GAP</strong> to obtain random elements may depend on the type object.</p>
715
716 <p>Most methods which produce random elements in <strong class="pkg">GAP</strong> use a global random number generator (see <code class="func">GlobalMersenneTwister</code> (<a href="chap14.html#X7AC96008820FAF1F"><span class="RefLink">14.7-4</span></a>)). This random number generator is (deliberately) initialized to the same values when <strong class="pkg">GAP</strong> is started, so different runs of <strong class="pkg">GAP</strong> with the same input will always produce the same result, even if random calculations are involved.</p>
717
718 <p>See <code class="func">Reset</code> (<a href="chap14.html#X86FFFBC9790F9742"><span class="RefLink">14.7-3</span></a>) for a description of how to reset the random number generator to a previous state.</p>
719
720 <p><a id="X7FF906E57D6936F8" name="X7FF906E57D6936F8"></a></p>
721
722 <h5>30.7-1 Random</h5>
723
724 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Random</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
725 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Random</code>( <var class="Arg">from</var>, <var class="Arg">to</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
726 <p><code class="func">Random</code> returns a (pseudo-)random element of the dense, nonempty list or nonempty collection <var class="Arg">listorcoll</var>. The behaviour for non-dense or empty lists, and for empty collections (see <code class="func">IsDenseList</code> (<a href="chap21.html#X870AA9D8798C93DD"><span class="RefLink">21.1-2</span></a>), <code class="func">IsEmpty</code> (<a href="chap30.html#X7969C48780C5C1BC"><span class="RefLink">30.4-1</span></a>)) is undefined.</p>
727
728 <p>As lists or ranges are restricted in length (<span class="SimpleMath">2^28-1</span> or <span class="SimpleMath">2^60-1</span> depending on your system), the second form returns a random integer in the range <var class="Arg">from</var> to <var class="Arg">to</var> (inclusive) for arbitrary integers <var class="Arg">from</var> and <var class="Arg">to</var>. The behaviour in the case that <var class="Arg">from</var> is larger than <var class="Arg">to</var> is undefined.</p>
729
730 <p>See Section <a href="chap14.html#X85361FAE8088C006"><span class="RefLink">14.7</span></a> for more about computing random elements, in particular for <code class="func">Random</code> (<a href="chap14.html#X821004F286282D49"><span class="RefLink">14.7-2</span></a>) methods that take a random source as the first argument.</p>
731
732 <p>The distribution of elements returned by <code class="func">Random</code> depends on the argument. For a dense, nonempty list the distribution is uniform (all elements are equally likely). The same holds usually for finite collections that are not lists. For infinite collections some reasonable distribution is used.</p>
733
734 <p>See the chapters of the various collections to find out which distribution is being used.</p>
735
736 <p>For some collections ensuring a reasonable distribution can be difficult and require substantial runtime (for example for large finite groups). If speed is more important than a guaranteed distribution, the operation <code class="func">PseudoRandom</code> (<a href="chap30.html#X811B5BD47DC5356B"><span class="RefLink">30.7-2</span></a>) should be used instead.</p>
737
738 <p>Note that <code class="func">Random</code> is of course <em>not</em> an attribute.</p>
739
740
741 <div class="example"><pre>
742 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Random([1..6]);</span>
743 6
744 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Random(1, 2^100);</span>
745 866227015645295902682304086250
746 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">g:= Group( (1,2,3) );; Random( g ); Random( g );</span>
747 (1,3,2)
748 ()
749 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">Random(Rationals);</span>
750 -4
751 </pre></div>
752
753 <p><a id="X811B5BD47DC5356B" name="X811B5BD47DC5356B"></a></p>
754
755 <h5>30.7-2 PseudoRandom</h5>
756
757 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; PseudoRandom</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
758 <p><code class="func">PseudoRandom</code> returns a pseudo random element of the list or collection <var class="Arg">listorcoll</var>, which can be roughly described as follows. For a list, <code class="func">PseudoRandom</code> returns the same as <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>). For collections that are not lists, the elements returned by <code class="func">PseudoRandom</code> are <em>not</em> necessarily equally distributed, even for finite collections; the idea is that <code class="func">Random</code> (<a href="chap30.html#X7FF906E57D6936F8"><span class="RefLink">30.7-1</span></a>) returns elements according to a reasonable distribution, <code class="func">PseudoRandom</code> returns elements that are cheap to compute but need not satisfy this strong condition, and <code class="func">Representative</code> (<a href="chap30.html#X865507568182424E"><span class="RefLink">30.4-7</span></a>) returns arbitrary elements, probably the same element for each call.</p>
759
760 <p><a id="X7EBA01EB83BC65A9" name="X7EBA01EB83BC65A9"></a></p>
761
762 <h5>30.7-3 RandomList</h5>
763
764 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; RandomList</code>( [<var class="Arg">rs</var>, ]<var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
765 <p>For a dense list <var class="Arg">list</var>, <code class="func">RandomList</code> returns a (pseudo-)random element with equal distribution.</p>
766
767 <p>The random source <var class="Arg">rs</var> (see <a href="chap14.html#X85361FAE8088C006"><span class="RefLink">14.7</span></a>) is used to choose a random number. If <var class="Arg">rs</var> is absent, this function uses the <code class="func">GlobalMersenneTwister</code> (<a href="chap14.html#X7AC96008820FAF1F"><span class="RefLink">14.7-4</span></a>) to produce the random elements (a source of high quality random numbers).</p>
768
769
770 <div class="example"><pre>
771 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RandomList( [ 1 .. 6 ] );</span>
772 3
773 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">elms:= AsList( Group( (1,2,3) ) );;</span>
774 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RandomList( elms ); RandomList( elms );</span>
775 (1,3,2)
776 (1,2,3)
777 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">rs:= RandomSource( IsMersenneTwister, 1 );</span>
778 &lt;RandomSource in IsMersenneTwister&gt;
779 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">RandomList( rs, elms );</span>
780 (1,3,2)
781 </pre></div>
782
783 <p><a id="X85A3F00985453F95" name="X85A3F00985453F95"></a></p>
784
785 <h4>30.8 <span class="Heading">Iterators</span></h4>
786
787 <p><a id="X83ADF8287ED0668E" name="X83ADF8287ED0668E"></a></p>
788
789 <h5>30.8-1 Iterator</h5>
790
791 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; Iterator</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
792 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsStandardIterator</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;filter&nbsp;)</td></tr></table></div>
793 <p>Iterators provide a possibility to loop over the elements of a (countable) collection or list <var class="Arg">listorcoll</var>, without repetition. For many collections <span class="SimpleMath">C</span>, an iterator of <span class="SimpleMath">C</span> need not store all elements of <span class="SimpleMath">C</span>, for example it is possible to construct an iterator of some infinite domains, such as the field of rational numbers.</p>
794
795 <p><code class="func">Iterator</code> returns a mutable <em>iterator</em> <span class="SimpleMath">iter</span> for its argument. If this argument is a list (which may contain holes), then <span class="SimpleMath">iter</span> iterates over the elements (but not the holes) of this list in the same order (see <code class="func">IteratorList</code> (<a href="chap30.html#X858A28667D137C4B"><span class="RefLink">30.8-6</span></a>) for details). If this argument is a collection but not a list then <span class="SimpleMath">iter</span> iterates over the elements of this collection in an unspecified order, which may change for repeated calls of <code class="func">Iterator</code>. Because iterators returned by <code class="func">Iterator</code> are mutable (see <a href="chap12.html#X7F0C119682196D65"><span class="RefLink">12.6</span></a>), each call of <code class="func">Iterator</code> for the same argument returns a <em>new</em> iterator. Therefore <code class="func">Iterator</code> is not an attribute (see <a href="chap13.html#X7C701DBF7BAE649A"><span class="RefLink">13.5</span></a>).</p>
796
797 <p>The only operations for iterators are <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>), <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>), and <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>). In particular, it is only possible to access the next element of the iterator with <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>) if there is one, and this can be checked with <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>) For an iterator <span class="SimpleMath">iter</span>, <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>) returns a mutable iterator <span class="SimpleMath">new</span> that iterates over the remaining elements independent of <span class="SimpleMath">iter</span>; the results of <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>) for <span class="SimpleMath">iter</span> and <span class="SimpleMath">new</span> are equal, and if <span class="SimpleMath">iter</span> is mutable then also the results of <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>) for <span class="SimpleMath">iter</span> and <span class="SimpleMath">new</span> are equal; note that <code class="code">=</code> is not defined for iterators, so the equality of two iterators cannot be checked with <code class="code">=</code>.</p>
798
799 <p>When <code class="func">Iterator</code> is called for a <em>mutable</em> collection <span class="SimpleMath">C</span> then it is not defined whether <span class="SimpleMath">iter</span> respects changes to <span class="SimpleMath">C</span> occurring after the construction of <span class="SimpleMath">iter</span>, except if the documentation explicitly promises a certain behaviour. The latter is the case if the argument is a mutable list (see <code class="func">IteratorList</code> (<a href="chap30.html#X858A28667D137C4B"><span class="RefLink">30.8-6</span></a>) for subtleties in this case).</p>
800
801 <p>It is possible to have <code class="keyw">for</code>-loops run over mutable iterators instead of lists.</p>
802
803 <p>In some situations, one can construct iterators with a special succession of elements, see <code class="func">IteratorByBasis</code> (<a href="chap61.html#X855625D47979005D"><span class="RefLink">61.6-6</span></a>) for the possibility to loop over the elements of a vector space w.r.t. a given basis.</p>
804
805 <p>For lists, <code class="func">Iterator</code> is implemented by <code class="func">IteratorList</code> (<a href="chap30.html#X858A28667D137C4B"><span class="RefLink">30.8-6</span></a>). For collections <span class="SimpleMath">C</span> that are not lists, the default method is <code class="code">IteratorList( Enumerator( </code><span class="SimpleMath">C</span><code class="code"> ) )</code>. Better methods depending on <span class="SimpleMath">C</span> should be provided if possible.</p>
806
807 <p>For random access to the elements of a (possibly infinite) collection, <em>enumerators</em> are used. See <a href="chap21.html#X7EA3ACE27E43D174"><span class="RefLink">21.23</span></a> for the facility to compute a list from <span class="SimpleMath">C</span>, which provides a (partial) mapping from <span class="SimpleMath">C</span> to the positive integers.</p>
808
809 <p>The filter <code class="func">IsStandardIterator</code> means that the iterator is implemented as a component object and has components <code class="code">IsDoneIterator</code> and <code class="code">NextIterator</code> which are bound to the methods of the operations of the same name for this iterator.</p>
810
811
812 <div class="example"><pre>
813 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">iter:= Iterator( GF(5) );</span>
814 &lt;iterator&gt;
815 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [];;</span>
816 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">for i in iter do Add( l, i ); od; l;</span>
817 [ 0*Z(5), Z(5)^0, Z(5), Z(5)^2, Z(5)^3 ]
818 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">iter:= Iterator( [ 1, 2, 3, 4 ] );; l:= [];;</span>
819 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">for i in iter do</span>
820 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> new:= ShallowCopy( iter );</span>
821 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> for j in new do Add( l, j ); od;</span>
822 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od; l;</span>
823 [ 2, 3, 4, 3, 4, 4 ]
824 </pre></div>
825
826 <p><a id="X8688C20B828FC129" name="X8688C20B828FC129"></a></p>
827
828 <h5>30.8-2 IteratorSorted</h5>
829
830 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IteratorSorted</code>( <var class="Arg">listorcoll</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
831 <p><code class="func">IteratorSorted</code> returns a mutable iterator. The argument must be a collection or a list that is not necessarily dense but whose elements lie in the same family (see <a href="chap13.html#X846063757EC05986"><span class="RefLink">13.1</span></a>). It loops over the different elements in sorted order.</p>
832
833 <p>For a collection <span class="SimpleMath">C</span> that is not a list, the generic method is <code class="code">IteratorList( EnumeratorSorted( </code><var class="Arg">C</var><code class="code"> ) )</code>.</p>
834
835 <p><a id="X87168A827E5B28E4" name="X87168A827E5B28E4"></a></p>
836
837 <h5>30.8-3 IsIterator</h5>
838
839 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsIterator</code>( <var class="Arg">obj</var> )</td><td class="tdright">(&nbsp;category&nbsp;)</td></tr></table></div>
840 <p>Every iterator lies in the category <code class="code">IsIterator</code>.</p>
841
842 <p><a id="X8055FC557B5D899E" name="X8055FC557B5D899E"></a></p>
843
844 <h5>30.8-4 IsDoneIterator</h5>
845
846 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IsDoneIterator</code>( <var class="Arg">iter</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
847 <p>If <var class="Arg">iter</var> is an iterator for the list or collection <span class="SimpleMath">C</span> then <code class="code">IsDoneIterator( <var class="Arg">iter</var> )</code> is <code class="keyw">true</code> if all elements of <span class="SimpleMath">C</span> have been returned already by <code class="code">NextIterator( <var class="Arg">iter</var> )</code>, and <code class="keyw">false</code> otherwise.</p>
848
849 <p><a id="X879F62F77D1D1179" name="X879F62F77D1D1179"></a></p>
850
851 <h5>30.8-5 NextIterator</h5>
852
853 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; NextIterator</code>( <var class="Arg">iter</var> )</td><td class="tdright">(&nbsp;operation&nbsp;)</td></tr></table></div>
854 <p>Let <var class="Arg">iter</var> be a mutable iterator for the list or collection <span class="SimpleMath">C</span>. If <code class="code">IsDoneIterator( <var class="Arg">iter</var> )</code> is <code class="keyw">false</code> then <code class="func">NextIterator</code> is applicable to <var class="Arg">iter</var>, and the result is the next element of <span class="SimpleMath">C</span>, according to the succession defined by <var class="Arg">iter</var>.</p>
855
856 <p>If <code class="code">IsDoneIterator( <var class="Arg">iter</var> )</code> is <code class="keyw">true</code> then it is not defined what happens when <code class="func">NextIterator</code> is called for <var class="Arg">iter</var>; that is, it may happen that an error is signalled or that something meaningless is returned, or even that <strong class="pkg">GAP</strong> crashes.</p>
857
858
859 <div class="example"><pre>
860 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">iter:= Iterator( [ 1, 2, 3, 4 ] );</span>
861 &lt;iterator&gt;
862 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">sum:= 0;;</span>
863 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">while not IsDoneIterator( iter ) do</span>
864 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> sum:= sum + NextIterator( iter );</span>
865 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
866 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">IsDoneIterator( iter ); sum;</span>
867 true
868 10
869 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">ir:= Iterator( Rationals );;</span>
870 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">l:= [];; for i in [1..20] do Add( l, NextIterator( ir ) ); od; l;</span>
871 [ 0, 1, -1, 1/2, 2, -1/2, -2, 1/3, 2/3, 3/2, 3, -1/3, -2/3, -3/2, -3,
872 1/4, 3/4, 4/3, 4, -1/4 ]
873 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">for i in ir do</span>
874 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> if DenominatorRat( i ) &gt; 10 then break; fi;</span>
875 <span class="GAPprompt">&gt;</span> <span class="GAPinput"> od;</span>
876 <span class="GAPprompt">gap&gt;</span> <span class="GAPinput">i;</span>
877 1/11
878 </pre></div>
879
880 <p><a id="X858A28667D137C4B" name="X858A28667D137C4B"></a></p>
881
882 <h5>30.8-6 IteratorList</h5>
883
884 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IteratorList</code>( <var class="Arg">list</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
885 <p><code class="func">IteratorList</code> returns a new iterator that allows iteration over the elements of the list <var class="Arg">list</var> (which may have holes) in the same order.</p>
886
887 <p>If <var class="Arg">list</var> is mutable then it is in principle possible to change <var class="Arg">list</var> after the call of <code class="func">IteratorList</code>. In this case all changes concerning positions that have not yet been reached in the iteration will also affect the iterator. For example, if <var class="Arg">list</var> is enlarged then the iterator will iterate also over the new elements at the end of the changed list.</p>
888
889 <p><em>Note</em> that changes of <var class="Arg">list</var> will also affect all shallow copies of <var class="Arg">list</var>.</p>
890
891 <p><a id="X7DB80BE68271247E" name="X7DB80BE68271247E"></a></p>
892
893 <h5>30.8-7 TrivialIterator</h5>
894
895 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; TrivialIterator</code>( <var class="Arg">elm</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
896 <p>is a mutable iterator for the collection <code class="code">[ <var class="Arg">elm</var> ]</code> that consists of exactly one element <var class="Arg">elm</var> (see <code class="func">IsTrivial</code> (<a href="chap30.html#X7E3402D6799D3C24"><span class="RefLink">30.4-3</span></a>)).</p>
897
898 <p><a id="X82677D8F817D6701" name="X82677D8F817D6701"></a></p>
899
900 <h5>30.8-8 IteratorByFunctions</h5>
901
902 <div class="func"><table class="func" width="100%"><tr><td class="tdleft"><code class="func">&#8227; IteratorByFunctions</code>( <var class="Arg">record</var> )</td><td class="tdright">(&nbsp;function&nbsp;)</td></tr></table></div>
903 <p><code class="func">IteratorByFunctions</code> returns a (mutable) iterator <var class="Arg">iter</var> for which <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>), <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>), and <code class="func">ShallowCopy</code> (<a href="chap12.html#X846BC7107C352031"><span class="RefLink">12.7-1</span></a>) are computed via prescribed functions.</p>
904
905 <p>Let <var class="Arg">record</var> be a record with at least the following components.</p>
906
907
908 <dl>
909 <dt><strong class="Mark"><code class="code">NextIterator</code></strong></dt>
910 <dd><p>a function taking one argument <var class="Arg">iter</var>, which returns the next element of <var class="Arg">iter</var> (see <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>)); for that, the components of <var class="Arg">iter</var> are changed,</p>
911
912 </dd>
913 <dt><strong class="Mark"><code class="code">IsDoneIterator</code></strong></dt>
914 <dd><p>a function taking one argument <var class="Arg">iter</var>, which returns the <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>) value of <var class="Arg">iter</var>,</p>
915
916 </dd>
917 <dt><strong class="Mark"><code class="code">ShallowCopy</code></strong></dt>
918 <dd><p>a function taking one argument <var class="Arg">iter</var>, which returns a record for which <code class="func">IteratorByFunctions</code> can be called in order to create a new iterator that is independent of <var class="Arg">iter</var> but behaves like <var class="Arg">iter</var> w.r.t. the operations <code class="func">NextIterator</code> (<a href="chap30.html#X879F62F77D1D1179"><span class="RefLink">30.8-5</span></a>) and <code class="func">IsDoneIterator</code> (<a href="chap30.html#X8055FC557B5D899E"><span class="RefLink">30.8-4</span></a>).</p>
919
920 </dd>
921 <dt><strong class="Mark"><code class="code">ViewObj</code> and <code class="code">PrintObj</code></strong></dt>
922 <dd><p>two functions that print what one wants to be printed when <code class="code">View( <var class="Arg">iter</var> )</code> or <code class="code">Print( <var class="Arg">item</var> )</code> is called (see <a href="chap6.html#X8074A8387C9DB9A8"><span class="RefLink">6.3</span></a>), if the <code class="code">ViewObj</code> component is missing then the <code class="code">PrintObj</code> method is used as a default.</p>
923
924 </dd>
925 </dl>
926 <p>Further (data) components may be contained in <var class="Arg">record</var> which can be used by these function.</p>
927
928 <p><code class="func">IteratorByFunctions</code> does <em>not</em> make a shallow copy of <var class="Arg">record</var>, this record is changed in place (see Section  <a href="chap79.html#X82E86CF37B123FD4"><span class="RefLink">79.1</span></a>).</p>
929
930 <p>Iterators constructed with <code class="func">IteratorByFunctions</code> are in the filter <code class="func">IsStandardIterator</code> (<a href="chap30.html#X83ADF8287ED0668E"><span class="RefLink">30.8-1</span></a>).</p>
931
932
933 <div class="chlinkprevnextbot">&nbsp;<a href="chap0.html">[Top of Book]</a>&nbsp; <a href="chap0.html#contents">[Contents]</a>&nbsp; &nbsp;<a href="chap29.html">[Previous Chapter]</a>&nbsp; &nbsp;<a href="chap31.html">[Next Chapter]</a>&nbsp; </div>
934
935
936 <div class="chlinkbot"><span class="chlink1">Goto Chapter: </span><a href="chap0.html">Top</a> <a href="chap1.html">1</a> <a href="chap2.html">2</a> <a href="chap3.html">3</a> <a href="chap4.html">4</a> <a href="chap5.html">5</a> <a href="chap6.html">6</a> <a href="chap7.html">7</a> <a href="chap8.html">8</a> <a href="chap9.html">9</a> <a href="chap10.html">10</a> <a href="chap11.html">11</a> <a href="chap12.html">12</a> <a href="chap13.html">13</a> <a href="chap14.html">14</a> <a href="chap15.html">15</a> <a href="chap16.html">16</a> <a href="chap17.html">17</a> <a href="chap18.html">18</a> <a href="chap19.html">19</a> <a href="chap20.html">20</a> <a href="chap21.html">21</a> <a href="chap22.html">22</a> <a href="chap23.html">23</a> <a href="chap24.html">24</a> <a href="chap25.html">25</a> <a href="chap26.html">26</a> <a href="chap27.html">27</a> <a href="chap28.html">28</a> <a href="chap29.html">29</a> <a href="chap30.html">30</a> <a href="chap31.html">31</a> <a href="chap32.html">32</a> <a href="chap33.html">33</a> <a href="chap34.html">34</a> <a href="chap35.html">35</a> <a href="chap36.html">36</a> <a href="chap37.html">37</a> <a href="chap38.html">38</a> <a href="chap39.html">39</a> <a href="chap40.html">40</a> <a href="chap41.html">41</a> <a href="chap42.html">42</a> <a href="chap43.html">43</a> <a href="chap44.html">44</a> <a href="chap45.html">45</a> <a href="chap46.html">46</a> <a href="chap47.html">47</a> <a href="chap48.html">48</a> <a href="chap49.html">49</a> <a href="chap50.html">50</a> <a href="chap51.html">51</a> <a href="chap52.html">52</a> <a href="chap53.html">53</a> <a href="chap54.html">54</a> <a href="chap55.html">55</a> <a href="chap56.html">56</a> <a href="chap57.html">57</a> <a href="chap58.html">58</a> <a href="chap59.html">59</a> <a href="chap60.html">60</a> <a href="chap61.html">61</a> <a href="chap62.html">62</a> <a href="chap63.html">63</a> <a href="chap64.html">64</a> <a href="chap65.html">65</a> <a href="chap66.html">66</a> <a href="chap67.html">67</a> <a href="chap68.html">68</a> <a href="chap69.html">69</a> <a href="chap70.html">70</a> <a href="chap71.html">71</a> <a href="chap72.html">72</a> <a href="chap73.html">73</a> <a href="chap74.html">74</a> <a href="chap75.html">75</a> <a href="chap76.html">76</a> <a href="chap77.html">77</a> <a href="chap78.html">78</a> <a href="chap79.html">79</a> <a href="chap80.html">80</a> <a href="chap81.html">81</a> <a href="chap82.html">82</a> <a href="chap83.html">83</a> <a href="chap84.html">84</a> <a href="chap85.html">85</a> <a href="chap86.html">86</a> <a href="chap87.html">87</a> <a href="chapBib.html">Bib</a> <a href="chapInd.html">Ind</a> </div>
937
938 <hr />
939 <p class="foot">generated by <a href="https://www.math.rwth-aachen.de/~Frank.Luebeck/GAPDoc">GAPDoc2HTML</a></p>
940 </body>
941 </html>