Changeset 0e4e43d
- Timestamp:
- Apr 20, 2025, 9:48:18 AM (5 weeks ago)
- Branches:
- master
- Children:
- 65b0402
- Parents:
- 0eacfd4
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
doc/theses/mike_brooks_MMath/list.tex
r0eacfd4 r0e4e43d 129 129 130 130 131 \section{List API} 132 133 134 \begin{cfa} 135 void insert_after( tE & list_pos, tE & to_insert ) { 136 void insert_before( tE & list_pos, tE &to_insert ) { 137 tE & remove( tE & list_pos ) { 138 tE & ?`first( dlist(tE, tLinks) &lst ) { 139 tE & ?`last( dlist(tE, tLinks) &lst ) { 140 bool ?`isEmpty( dlist(tE, tLinks) & lst ) { 141 bool ?`isListed( tE & e ) { 142 tE & ?`elems( dlist(tE, tLinks) & lst ) { 143 tE & ?`head( dlist(tE, tLinks) & lst ) { 144 bool ?`moveNext( tE && refx ) { 145 bool ?`next( tE && refx ) { // alternate name 146 bool ?`movePrev( tE && refx ) { 147 bool ?`prev( tE && refx ) { // alternate name 148 bool ?`hasNext( tE & e ) { 149 bool ?`hasPrev( tE & e ) { 150 tE & ?`next( tE & e ) { 151 tE & ?`prev( tE & e ) { 152 void insert_first( dlist(tE, tLinks) &lst, tE & e ) { 153 void insert_last( dlist(tE, tLinks) &lst, tE & e ) { 154 void insert( dlist(tE, tLinks) &lst, tE & e ) { // alternate name 155 tE & try_pop_front( dlist(tE, tLinks) &lst ) { 156 tE & try_pop_back( dlist(tE, tLinks) &lst ) { 157 \end{cfa} 158 159 \begin{itemize} 160 \item 161 The member routine @empty@ returns true if the sequence has no nodes and false otherwise. 162 \item 163 The member routine head returns a pointer to the head or first node of the sequence without removing it or nullptr if the sequence has no nodes. 164 \item 165 The member routine tail returns a pointer to the tail or last node of the sequence without removing it or nullptr if the sequence has no nodes. 166 \item 167 The member routine succ returns a pointer to the successor node after the specified node (toward the tail) or nullptr if the specified node is the last node in the sequence. 168 \item 169 The member routine pred returns a pointer to the predecessor node before the specified node (toward the head) or nullptr if the specified node is the first node in the sequence. 170 \item 171 The member routine insertBef adds a node before the specified node or at the end (tail) if bef is nullptr. 172 \item 173 The member routine insertAft adds a node after the specified node or at the beginning (head) if aft is nullptr . 174 \item 175 The member routine addHead adds a node to the head or front of the sequence and returns a pointer to the node. 176 \item 177 The member routine addTail adds a node to the tail or end of the sequence and returns returns a pointer to the node. 178 \item 179 The member routine add is a synonym for addTail. 180 \item 181 The member routine dropHead removes a node from the head or front of the sequence and returns a pointer to it or nullptr if the sequence has no nodes. 182 \item 183 The member routine drop is a synonym for dropHead. 184 \item 185 The member routine dropTail removes a node from the tail or end of the sequence and returns a pointer to it or nullptr if the sequence has no nodes. 186 \item 187 The member routine remove removes the specified node from the sequence (any location) and returns a pointer to the node. 188 \item 189 The member routine transfer transfers all nodes from the "from" list to the tail or end of the specified sequence; the "from" list is empty after the transfer. 190 \item 191 The member routine split transfer the "from" list up to node "n" to the end of the specified sequence; the "from" list becomes the list after node "n". Node "n" must be in the "from" list. 192 \end{itemize} 193 131 194 132 195 \subsection{Iteration} 133 196 197 It is possible to iterate through a list manually or using a set of standard macros. 198 \VRef[Figure]{f:IteratorExample} shows the iterator template, managing a list of nodes, used throughout the following iterator examples. 199 Each example assumes its loop body prints the value in the current node. 200 201 The manual method is low level but allows complete control of the iteration. 202 The list cursor (index) can be either a pointer or a reference to a node in the list. 203 The choice depends on how the programmer wants to access the fields: @it->f@ or @it.f@. 204 The following examples use a reference because the loop body manipulates the node values rather than the list pointers. 205 The end of iteration is denoted by the loop cursor having the null pointer, denoted @0p@ in \CFA. 206 207 \noindent 208 Iterating forward and reverse through the entire list. 209 \begin{cquote} 210 \setlength{\tabcolsep}{15pt} 211 \begin{tabular}{@{}l|l@{}} 212 \begin{cfa} 213 for ( node & it = list`@first@; &it /* != 0p */ ; &it = &it`@next@ ) ... 214 for ( node & it = list`@last@; ⁢ &it = &it`@prev@ ) ... 215 \end{cfa} 216 & 217 \begin{cfa} 218 1, 2, 3, 4, 219 4, 3, 2, 1, 220 \end{cfa} 221 \end{tabular} 222 \end{cquote} 223 Iterating forward and reverse from a starting node through the remaining list. 224 \begin{cquote} 225 \setlength{\tabcolsep}{15pt} 226 \begin{tabular}{@{}l|l@{}} 227 \begin{cfa} 228 for ( node & it = @n2@; ⁢ &it = &it`@next@ ) ... 229 for ( node & it = @n3@; ⁢ &it = &it`@prev@ ) ... 230 \end{cfa} 231 & 232 \begin{cfa} 233 2, 3, 4, 234 3, 2, 1, 235 \end{cfa} 236 \end{tabular} 237 \end{cquote} 238 Iterating forward and reverse from a starting node to an ending node through the remaining list. 239 \begin{cquote} 240 \setlength{\tabcolsep}{15pt} 241 \begin{tabular}{@{}l|l@{}} 242 \begin{cfa} 243 for ( node & it = @n2@; &it @!= &n4@; &it = &it`@next@ ) ... 244 for ( node & it = @n4@; &it @!= &n2@; &it = &it`@prev@ ) ... 245 \end{cfa} 246 & 247 \begin{cfa} 248 2, 3, 249 4, 3, 250 \end{cfa} 251 \end{tabular} 252 \end{cquote} 253 Iterating forward and reverse through the entire list using the shorthand start at the list head and pick a direction. 254 In this case, @next@ and @prev@ return a boolean, like \CC @while ( cin >> i )@. 255 \begin{cquote} 256 \setlength{\tabcolsep}{15pt} 257 \begin{tabular}{@{}l|l@{}} 258 \begin{cfa} 259 for ( node & it = list`@head@; it`@next@; ) ... 260 for ( node & it = list`@head@; it`@prev@; ) ... 261 \end{cfa} 262 & 263 \begin{cfa} 264 1, 2, 3, 4, 265 4, 3, 2, 1, 266 \end{cfa} 267 \end{tabular} 268 \end{cquote} 269 Finally, there are convenience macros that look like @foreach@ in other programming languages. 270 Iterating forward and reverse through the entire list. 271 \begin{cquote} 272 \setlength{\tabcolsep}{15pt} 273 \begin{tabular}{@{}l|l@{}} 274 \begin{cfa} 275 FOREACH( list, it ) ... 276 FOREACH_REV( list, it ) ... 277 \end{cfa} 278 & 279 \begin{cfa} 280 1, 2, 3, 4, 281 4, 3, 2, 1, 282 \end{cfa} 283 \end{tabular} 284 \end{cquote} 285 Iterating forward and reverse through the entire list or until a predicate is triggered. 286 \begin{cquote} 287 \setlength{\tabcolsep}{15pt} 288 \begin{tabular}{@{}l|l@{}} 289 \begin{cfa} 290 FOREACH_COND( list, it, @it.v == 3@ ) ... 291 FOREACH_REV_COND( list, it, @it.v == 1@ ) ... 292 \end{cfa} 293 & 294 \begin{cfa} 295 1, 2, 296 4, 3, 2, 297 \end{cfa} 298 \end{tabular} 299 \end{cquote} 300 The ability to provide a language-level @foreach@ is a future \CFA project. 301 302 \begin{figure} 303 \begin{cfa} 304 #include <fstream.hfa> 305 #include <list.hfa> 306 307 struct node { 308 int v; 309 inline dlink(node); 310 }; 311 int main() { 312 dlist(node) list; 313 node n1 = { 1 }, n2 = { 2 }, n3 = { 3 }, n4 = { 4 }; 314 insert( list, n1 ); insert( list, n2 ); insert( list, n3 ); insert( list, n4 ); 315 sout | nlOff; 316 for ( node & it = list`first; ⁢ &it = &it`next ) @sout | it.v | ","; sout | nl;@ 317 // other iterator examples 318 remove( n1 ); remove( n2 ); remove( n3 ); remove( n4 ); 319 } 320 \end{cfa} 321 \caption{Iterator Example} 322 \label{f:IteratorExample} 323 \end{figure} 324 325 \begin{comment} 134 326 Many languages offer an iterator interface for collections, and a corresponding for-each loop syntax for consuming the items through implicit interface calls. 135 327 \CFA does not yet have a general-purpose form of such a feature, though it has a form that addresses some use cases. … … 187 379 by having a list operator @<=@ that just looks for equality, and @+=@ that moves to the next node, \etc. 188 380 189 However, the list usage is contrived, because a list does use its data values for relational comparison, only links for equality comp rison.381 However, the list usage is contrived, because a list does use its data values for relational comparison, only links for equality comparison. 190 382 Hence, the focus of a list iterator's stopping condition is fundamentally different. 191 383 So, iteration of a linked list via the existing loop syntax is to ask whether this syntax can also do double-duty for iterating values. … … 216 408 217 409 TODO: deal with spontaneous simultaneity, like a single-axis req, put into an array: which ``axis'' is @&req++@ navigating: array-adjacency vs link dereference. It should sick according to how you got it in the first place: navigating dlist(req, req.pri) vs navigating array(req, 42). (prob. future work) 410 \end{comment} 411 218 412 219 413
Note: See TracChangeset
for help on using the changeset viewer.