home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / misc / emu / AROSdev.lha / AROS / rom / exec / insert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-09  |  2.4 KB  |  102 lines

  1. /*
  2.     (C) 1995-96 AROS - The Amiga Replacement OS
  3.     $Id: insert.c,v 1.10 1997/01/03 12:29:07 digulla Exp $
  4.  
  5.     Desc:
  6.     Lang: english
  7. */
  8. #include "exec_intern.h"
  9. #include <exec/lists.h>
  10. #include <proto/exec.h>
  11.  
  12. /*****************************************************************************
  13.  
  14.     NAME */
  15.  
  16.     AROS_LH3I(void, Insert,
  17.  
  18. /*  SYNOPSIS */
  19.     AROS_LHA(struct List *, list, A0),
  20.     AROS_LHA(struct Node *, node, A1),
  21.     AROS_LHA(struct Node *, pred, A2),
  22.  
  23. /*  LOCATION */
  24.     struct ExecBase *, SysBase, 39, Exec)
  25.  
  26. /*  FUNCTION
  27.     Insert Node node after pred in list.
  28.  
  29.     INPUTS
  30.     list - The list to insert the node into
  31.     node - This node is to be inserted
  32.     pred - Insert after this node. If this is NULL, node is inserted
  33.         as the first node (same as AddHead()).
  34.  
  35.     RESULT
  36.  
  37.     NOTES
  38.  
  39.     EXAMPLE
  40.     struct List * list;
  41.     struct Node * pred, * node;
  42.  
  43.     // Insert Node node as second node in list
  44.     pred = GetHead (list);
  45.     Insert (list, node, pred);
  46.  
  47.     BUGS
  48.  
  49.     SEE ALSO
  50.     AddHead(), AddTail(), Enqueue(), RemHead(), Remove(), RemTail(),
  51.     "AROS: Exec Lists".
  52.  
  53.     INTERNALS
  54.  
  55.     HISTORY
  56.     26-08-95    digulla created after EXEC-Routine
  57.     26-10-95    digulla adjusted to new calling scheme
  58.  
  59. ******************************************************************************/
  60. {
  61.     AROS_LIBFUNC_INIT
  62.     assert (node);
  63.     assert (list);
  64.  
  65.     /* If we have a node to insert behind... */
  66.     if (pred)
  67.     {
  68.     /*
  69.         Our successor is the successor of the node we add ourselves
  70.         behind and our predecessor is just the node itself.
  71.     */
  72.     node->ln_Succ = pred->ln_Succ;
  73.     node->ln_Pred = pred;
  74.  
  75.     /*
  76.         We are the predecessor of the successor of our predecessor
  77.         (What ? blblblb... ;) and of our predecessor itself.
  78.         Note that here the sequence is quite important since
  79.         we need ln_Succ in the first expression and change it in
  80.         the second.
  81.     */
  82.     pred->ln_Succ->ln_Pred = node;
  83.     pred->ln_Succ = node;
  84.     }
  85.     else
  86.     {
  87.     /*
  88.         add at the top of the list. I do not use AddHead() here but
  89.         write the code twice for two reasons: 1. The code is small and
  90.         quite prone to errors and 2. If I would call AddHead(), it
  91.         would take almost as long to call the function as the execution
  92.         would take yielding 100% overhead.
  93.     */
  94.     node->ln_Succ           = list->lh_Head;
  95.     node->ln_Pred           = (struct Node *)&list->lh_Head;
  96.     list->lh_Head->ln_Pred = node;
  97.     list->lh_Head           = node;
  98.     }
  99.     AROS_LIBFUNC_EXIT
  100. } /* Insert */
  101.  
  102.