home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / lsEstuff / old_lists / xl.e < prev    next >
Encoding:
Text File  |  1999-10-13  |  4.4 KB  |  228 lines

  1. OPT MODULE
  2.  
  3. OPT EXPORT
  4.  
  5. MODULE 'exec/lists'
  6. MODULE '*/mymods/myfile'
  7.  
  8. OBJECT xn
  9.    next:PTR TO LONG
  10.    prev:PTR TO LONG
  11. ENDOBJECT
  12.  
  13.  
  14. OBJECT xl
  15.    next:PTR TO LONG
  16.    prev:PTR TO LONG
  17.    id
  18.    PRIVATE
  19.    first:PTR TO LONG
  20.    last:PTR TO LONG
  21. ENDOBJECT
  22.  
  23. PROC first() OF xl IS self.first
  24.  
  25. PROC last() OF xl IS self.last
  26.  
  27.  
  28. PROC addFirst(xn:PTR TO xn) OF xl
  29.    DEF next:PTR TO xn
  30.    next:=self.first
  31.  
  32.    self.first:=xn
  33.    IF self.last=NIL THEN self.last:=xn
  34.    xn.prev:=NIL
  35.    xn.next:=next
  36.    IF next THEN next.prev:=xn
  37. ENDPROC xn
  38.  
  39. PROC addLast(xn:PTR TO xn) OF xl
  40.    DEF prev:PTR TO xn
  41.    prev:=self.last
  42.  
  43.    self.last:=xn
  44.    IF self.first=NIL THEN self.first:=xn
  45.    xn.next:=NIL
  46.    xn.prev:=prev
  47.    IF prev THEN prev.next:=xn
  48. ENDPROC xn
  49.  
  50. PROC remFirst() OF xl
  51.    DEF remed:PTR TO xn, next:PTR TO xn
  52.    remed:=NIL
  53.    IF self.first
  54.       remed:=self.first
  55.       self.first:=remed.next
  56.       IF remed.next=NIL
  57.          self.last:=NIL
  58.       ELSE
  59.          next:=remed.next
  60.          next.prev:=NIL
  61.       ENDIF
  62.    ENDIF
  63. ENDPROC remed
  64.  
  65. PROC remLast() OF xl
  66.    DEF remed:PTR TO xn, prev:PTR TO xn
  67.    remed:=NIL
  68.    IF self.last
  69.       remed:=self.last
  70.       self.last:=remed.prev
  71.       IF remed.prev=NIL
  72.          self.first:=NIL
  73.       ELSE
  74.          prev:=remed.prev
  75.          prev.next:=NIL
  76.       ENDIF
  77.    ENDIF
  78. ENDPROC remed
  79.  
  80. PROC remove(xn:PTR TO xn) OF xl
  81.    DEF prev:PTR TO xn, next:PTR TO xn
  82.    IF xn.prev=NIL THEN RETURN self.remFirst()
  83.    IF xn.next=NIL THEN RETURN self.remLast()
  84.    prev:=xn.prev
  85.    next:=xn.next
  86.    prev.next:=next
  87.    next.prev:=prev
  88. ENDPROC xn
  89.  
  90. PROC insert(xn:PTR TO xn, afterthis:PTR TO xn) OF xl
  91.    DEF next:PTR TO xn
  92.    IF afterthis.next=NIL THEN RETURN self.addLast(xn)
  93.    next:=afterthis.next
  94.    afterthis.next:=xn
  95.    xn.prev:=afterthis
  96.    xn.next:=next
  97.    next.prev:=xn
  98. ENDPROC xn
  99.  
  100. PROC countNodes(firstnode=NIL) OF xl
  101.    DEF xn:REG PTR TO xn, count:REG
  102.    count:=NIL
  103.    xn:=IF firstnode = NIL THEN self.first ELSE firstnode
  104.    WHILE xn
  105.       count++
  106.       xn:=xn.next
  107.    ENDWHILE
  108. ENDPROC count
  109.  
  110. PROC removeFastDisposeAll(nodesize) OF xl
  111.    DEF xn:PTR TO xn
  112.    xn:=self.first
  113.    WHILE xn
  114.       self.removeFastDispose(xn, nodesize)
  115.       xn:=xn.next
  116.    ENDWHILE
  117. ENDPROC
  118.  
  119.  
  120. PROC removeFastDispose(node, nodesize) OF xl
  121.    self.remove(node)
  122.    FastDispose(node, nodesize)
  123. ENDPROC
  124.  
  125. PROC addLastFastNew(nodesize) OF xl
  126. ENDPROC self.addLast(FastNew(nodesize))
  127.  
  128. PROC cloneFastNew(tolist:PTR TO xl, nodesize) OF xl
  129.    DEF n1:PTR TO xn
  130.    DEF n2:PTR TO xn
  131.    n1 := self.first
  132.    WHILE n1
  133.       n2 := FastNew(nodesize)
  134.       CopyMem(n1, n2, nodesize)
  135.       tolist.addLast(n2)
  136.       n1 := n1.next
  137.    ENDWHILE
  138. ENDPROC self.first
  139.  
  140. PROC fromExecList(lh:PTR TO lh) OF xl
  141.    self.first := lh.head
  142.    self.last := lh.tailpred
  143. ENDPROC
  144.  
  145. PROC looseNodes() OF xl
  146.    self.first := NIL
  147.    self.last := NIL
  148. ENDPROC
  149.  
  150. PROC listInsert(xl:PTR TO xl, afterthis:PTR TO xn) OF xl
  151.    afterthis.next := xl.first
  152.    self.last := xl.last
  153. ENDPROC
  154.  
  155. PROC listAddFirst(xl:PTR TO xl) OF xl
  156.    DEF thatlast:PTR TO xn
  157.    thatlast := xl.last
  158.    thatlast.next := self.first
  159.    self.first := xl.first
  160. ENDPROC
  161.  
  162. PROC listAddLast(xl:PTR TO xl) OF xl
  163.    DEF thatfirst:PTR TO xn
  164.    thatfirst := xl.first
  165.    thatfirst.prev := self.last
  166.    self.last := xl.last
  167. ENDPROC
  168.  
  169. EXPORT OBJECT xl_CPObj
  170.    node:PTR TO LONG
  171.    list:PTR TO LONG
  172. ENDOBJECT
  173.  
  174.  
  175. PROC travNodes(proc, cpobj=NIL:PTR TO xl_CPObj) OF xl
  176.    DEF n:PTR TO xn
  177.    DEF cp:xl_CPObj
  178.    IF cpobj = NIL THEN cpobj := cp
  179.    n := self.first
  180.    cpobj.list := self
  181.    WHILE n
  182.       cpobj.node := n
  183.       proc(cpobj)
  184.       n := n.next
  185.    ENDWHILE
  186. ENDPROC
  187.  
  188. PROC travNodesRev(proc, cpobj=NIL:PTR TO xl_CPObj) OF xl
  189.    DEF n:PTR TO xn
  190.    DEF cp:xl_CPObj
  191.    IF cpobj = NIL THEN cpobj := cp
  192.    n := self.last
  193.    cpobj.list := self
  194.    WHILE n
  195.       cpobj.node := n
  196.       proc(cpobj)
  197.       n := n.prev
  198.    ENDWHILE
  199. ENDPROC
  200.  
  201. /* only to use with xn-inherited objects! (no methods) */
  202. EXPORT PROC saveToDisk(xl:PTR TO xl, name, nodesize)
  203.    DEF mem
  204.    DEF memptr:PTR TO LONG
  205.    DEF memsize
  206.    DEF nrof
  207.    DEF n:PTR TO xn
  208.    nodesize := nodesize - SIZEOF xn
  209.    IF nodesize < 1 THEN RETURN NIL
  210.    nrof := xl.countNodes()
  211.    IF nrof = NIL THEN RETURN NIL
  212.    memsize := memsize * nrof
  213.    memsize := memsize + 8 -> two LONGs describing the block
  214.    mem := FastNew(memsize)
  215.    memptr := mem
  216.    memptr[]++ := nrof
  217.    memptr[]++ := nodesize
  218.    n := xl.first
  219.    WHILE n
  220.       CopyMem(n + (SIZEOF xn), memptr, nodesize)
  221.       memptr := memptr + nodesize
  222.       n := n.next
  223.    ENDWHILE
  224.    writefile(name, mem, memsize)
  225.    FastDispose(mem, memsize)
  226. ENDPROC
  227.  
  228.