Changeset 0e4e43d


Ignore:
Timestamp:
Apr 20, 2025, 9:48:18 AM (5 weeks ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
master
Children:
65b0402
Parents:
0eacfd4
Message:

proofread list API

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/theses/mike_brooks_MMath/list.tex

    r0eacfd4 r0e4e43d  
    129129
    130130
     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
     161The member routine @empty@ returns true if the sequence has no nodes and false otherwise.
     162\item
     163The 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
     165The 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
     167The 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
     169The 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
     171The member routine insertBef adds a node before the specified node or at the end (tail) if bef is nullptr.
     172\item
     173The member routine insertAft adds a node after the specified node or at the beginning (head) if aft is nullptr .
     174\item
     175The member routine addHead adds a node to the head or front of the sequence and returns a pointer to the node.
     176\item
     177The member routine addTail adds a node to the tail or end of the sequence and returns returns a pointer to the node.
     178\item
     179The member routine add is a synonym for addTail.
     180\item
     181The 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
     183The member routine drop is a synonym for dropHead.
     184\item
     185The 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
     187The member routine remove removes the specified node from the sequence (any location) and returns a pointer to the node.
     188\item
     189The 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
     191The 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
    131194
    132195\subsection{Iteration}
    133196
     197It 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.
     199Each example assumes its loop body prints the value in the current node.
     200
     201The manual method is low level but allows complete control of the iteration.
     202The list cursor (index) can be either a pointer or a reference to a node in the list.
     203The choice depends on how the programmer wants to access the fields: @it->f@ or @it.f@.
     204The following examples use a reference because the loop body manipulates the node values rather than the list pointers.
     205The end of iteration is denoted by the loop cursor having the null pointer, denoted @0p@ in \CFA.
     206
     207\noindent
     208Iterating forward and reverse through the entire list.
     209\begin{cquote}
     210\setlength{\tabcolsep}{15pt}
     211\begin{tabular}{@{}l|l@{}}
     212\begin{cfa}
     213for ( node & it = list`@first@; &it /* != 0p */ ; &it = &it`@next@ ) ...
     214for ( node & it = list`@last@; &it; &it = &it`@prev@ ) ...
     215\end{cfa}
     216&
     217\begin{cfa}
     2181, 2, 3, 4,
     2194, 3, 2, 1,
     220\end{cfa}
     221\end{tabular}
     222\end{cquote}
     223Iterating 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}
     228for ( node & it = @n2@; &it; &it = &it`@next@ ) ...
     229for ( node & it = @n3@; &it; &it = &it`@prev@ ) ...
     230\end{cfa}
     231&
     232\begin{cfa}
     2332, 3, 4,
     2343, 2, 1,
     235\end{cfa}
     236\end{tabular}
     237\end{cquote}
     238Iterating 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}
     243for ( node & it = @n2@; &it @!= &n4@; &it = &it`@next@ ) ...
     244for ( node & it = @n4@; &it @!= &n2@; &it = &it`@prev@ ) ...
     245\end{cfa}
     246&
     247\begin{cfa}
     2482, 3,
     2494, 3,
     250\end{cfa}
     251\end{tabular}
     252\end{cquote}
     253Iterating forward and reverse through the entire list using the shorthand start at the list head and pick a direction.
     254In 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}
     259for ( node & it = list`@head@; it`@next@; ) ...
     260for ( node & it = list`@head@; it`@prev@; ) ...
     261\end{cfa}
     262&
     263\begin{cfa}
     2641, 2, 3, 4,
     2654, 3, 2, 1,
     266\end{cfa}
     267\end{tabular}
     268\end{cquote}
     269Finally, there are convenience macros that look like @foreach@ in other programming languages.
     270Iterating forward and reverse through the entire list.
     271\begin{cquote}
     272\setlength{\tabcolsep}{15pt}
     273\begin{tabular}{@{}l|l@{}}
     274\begin{cfa}
     275FOREACH( list, it ) ...
     276FOREACH_REV( list, it ) ...
     277\end{cfa}
     278&
     279\begin{cfa}
     2801, 2, 3, 4,
     2814, 3, 2, 1,
     282\end{cfa}
     283\end{tabular}
     284\end{cquote}
     285Iterating 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}
     290FOREACH_COND( list, it, @it.v == 3@ ) ...
     291FOREACH_REV_COND( list, it, @it.v == 1@ ) ...
     292\end{cfa}
     293&
     294\begin{cfa}
     2951, 2,
     2964, 3, 2,
     297\end{cfa}
     298\end{tabular}
     299\end{cquote}
     300The 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
     307struct node {
     308        int v;
     309        inline dlink(node);
     310};
     311int 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 = &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}
    134326Many languages offer an iterator interface for collections, and a corresponding for-each loop syntax for consuming the items through implicit interface calls.
    135327\CFA does not yet have a general-purpose form of such a feature, though it has a form that addresses some use cases.
     
    187379by having a list operator @<=@ that just looks for equality, and @+=@ that moves to the next node, \etc.
    188380
    189 However, the list usage is contrived, because a list does use its data values for relational comparison, only links for equality comprison.
     381However, the list usage is contrived, because a list does use its data values for relational comparison, only links for equality comparison.
    190382Hence, the focus of a list iterator's stopping condition is fundamentally different.
    191383So, iteration of a linked list via the existing loop syntax is to ask whether this syntax can also do double-duty for iterating values.
     
    216408
    217409TODO: 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
    218412
    219413
Note: See TracChangeset for help on using the changeset viewer.