home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Programming / lsEstuff / nmList.e < prev    next >
Encoding:
Text File  |  1999-11-02  |  4.3 KB  |  219 lines

  1. OPT MODULE
  2.  
  3. ->
  4. ->the new listclass (991017)
  5. ->nodes and the list inherits methods from lobject.
  6. ->if nodes with methods is a problem, dont use this one!
  7. ->be SURE to use only nodes inherited from nm!!
  8.  
  9. ->kinda skitbra !! skulle jag vilja säga.
  10. ->extremt trevlig klass som man kan bygga vidare på
  11. ->eller bara använda som den e..
  12. ->används till tamejfan nästan allt i leifoo-lådan :)
  13.  
  14.  
  15. MODULE 'leifoo/nm'
  16.  
  17. EXPORT OBJECT nmList OF nm
  18.    id
  19.    PRIVATE
  20.    first:PTR TO nm
  21.    last:PTR TO nm
  22. ENDOBJECT
  23.  
  24. EXPORT OBJECT nmList_travObj
  25.    node :PTR TO nm
  26.    list:PTR TO nmList
  27. ENDOBJECT
  28.  
  29. PROC printObject() OF nmList
  30.    DEF n:PTR TO nm
  31.    WriteF('list')
  32.    SUPER self.printObject()
  33.    n := self.first()
  34.    WriteF('nodes :\n')
  35.    WHILE n
  36.       n.printObject()
  37.       n := n.next
  38.    ENDWHILE
  39. ENDPROC
  40.  
  41.  
  42. PROC end() OF nmList IS self.clear()
  43.  
  44. PROC clear() OF nmList
  45.    DEF node:PTR TO nm
  46.    DEF next
  47.    node := self.first()
  48.    WHILE node
  49.       next := node.next
  50.       END node
  51.       node := next
  52.    ENDWHILE
  53. ENDPROC
  54.  
  55. PROC getObjectName() OF nmList IS 'nmList'
  56.  
  57. PROC getObjectSize() OF nmList IS SIZEOF nmList
  58.  
  59. PROC first() OF nmList IS self.first
  60.  
  61. PROC last() OF nmList IS self.last
  62.  
  63.  
  64. PROC addFirst(nm:PTR TO nm) OF nmList
  65.    DEF next:PTR TO nm
  66.    next:=self.first
  67.  
  68.    self.first:=nm
  69.    IF self.last=NIL THEN self.last:=nm
  70.    nm.prev:=NIL
  71.    nm.next:=next
  72.    IF next THEN next.prev:=nm
  73. ENDPROC nm
  74.  
  75. PROC addLast(nm:PTR TO nm) OF nmList
  76.    DEF prev:PTR TO nm
  77.    prev:=self.last
  78.  
  79.    self.last:=nm
  80.    IF self.first=NIL THEN self.first:=nm
  81.    nm.next:=NIL
  82.    nm.prev:=prev
  83.    IF prev THEN prev.next:=nm
  84. ENDPROC nm
  85.  
  86. PROC remFirst() OF nmList
  87.    DEF remed:PTR TO nm, next:PTR TO nm
  88.    remed:=NIL
  89.    IF self.first
  90.       remed:=self.first
  91.       self.first:=remed.next
  92.       IF remed.next=NIL
  93.          self.last:=NIL
  94.       ELSE
  95.          next:=remed.next
  96.          next.prev:=NIL
  97.       ENDIF
  98.    ENDIF
  99. ENDPROC remed
  100.  
  101. PROC remLast() OF nmList
  102.    DEF remed:PTR TO nm, prev:PTR TO nm
  103.    remed:=NIL
  104.    IF self.last
  105.       remed:=self.last
  106.       self.last:=remed.prev
  107.       IF remed.prev=NIL
  108.          self.first:=NIL
  109.       ELSE
  110.          prev:=remed.prev
  111.          prev.next:=NIL
  112.       ENDIF
  113.    ENDIF
  114. ENDPROC remed
  115.  
  116. PROC remove(nm:PTR TO nm) OF nmList
  117.    DEF prev:PTR TO nm, next:PTR TO nm
  118.    IF nm.prev=NIL THEN RETURN self.remFirst()
  119.    IF nm.next=NIL THEN RETURN self.remLast()
  120.    prev:=nm.prev
  121.    next:=nm.next
  122.    prev.next:=next
  123.    next.prev:=prev
  124. ENDPROC nm
  125.  
  126. PROC insert(nm:PTR TO nm, afterthis:PTR TO nm) OF nmList
  127.    DEF next:PTR TO nm
  128.    IF afterthis.next=NIL THEN RETURN self.addLast(nm)
  129.    next:=afterthis.next
  130.    afterthis.next:=nm
  131.    nm.prev:=afterthis
  132.    nm.next:=next
  133.    next.prev:=nm
  134. ENDPROC nm
  135.  
  136. PROC countNodes(firstnode=NIL) OF nmList
  137.    DEF nm:REG PTR TO nm, count:REG
  138.    count:=NIL
  139.    nm := IF firstnode = NIL THEN self.first ELSE firstnode
  140.    WHILE nm
  141.       count++
  142.       nm:=nm.next
  143.    ENDWHILE
  144. ENDPROC count
  145.  
  146. PROC listInsert(nmList:PTR TO nmList, afterthis:PTR TO nm) OF nmList
  147.    DEF nmL_last:PTR TO nm
  148.    DEF nmL_first:PTR TO nm
  149.    nmL_first := nmList.first
  150.    nmL_first.prev := afterthis
  151.    nmL_last := nmList.last
  152.    nmL_last.next := afterthis.next
  153.    afterthis.next := nmList.first
  154.    self.last := nmList.last
  155. ENDPROC
  156.  
  157. PROC listAddFirst(nmList:PTR TO nmList) OF nmList
  158.    DEF thatlast:PTR TO nm
  159.    DEF thisfirst:PTR TO nm
  160.    thisfirst.prev := nmList.last
  161.    thatlast := nmList.last
  162.    thatlast.next := self.first
  163.    self.first := nmList.first
  164. ENDPROC
  165.  
  166. PROC listAddLast(nmList:PTR TO nmList) OF nmList
  167.    DEF thatfirst:PTR TO nm
  168.    thatfirst := nmList.first
  169.    thatfirst.prev := self.last
  170.    self.last.next := thatfirst
  171.    self.last := nmList.last
  172. ENDPROC
  173.  
  174. PROC delete(nm:PTR TO nm) OF nmList
  175.    self.remove(nm)
  176.    END nm
  177. ENDPROC
  178.  
  179. PROC travNodes(proc, obj=NIL) OF nmList
  180.    DEF n:PTR TO nm
  181.    DEF to:PTR TO nmList_travObj
  182.    to := obj
  183.    IF to = NIL THEN NEW to
  184.    to.list := self
  185.    n := self.first
  186.    WHILE n
  187.       to.node := n
  188.       proc(to)
  189.       n := n.next
  190.    ENDWHILE
  191.    IF obj = NIL THEN END to
  192. ENDPROC
  193.  
  194. PROC replace(oldnode:PTR TO nm, newnode:PTR TO nm) OF nmList
  195.    DEF prevnode:PTR TO nm
  196.    DEF nextnode:PTR TO nm
  197.  
  198.    prevnode := oldnode.prev
  199.    nextnode := oldnode.next
  200.  
  201.    newnode.prev := prevnode
  202.    newnode.next := nextnode
  203.  
  204.    IF prevnode = NIL
  205.       self.first := newnode
  206.    ELSE
  207.       prevnode.next := newnode
  208.    ENDIF
  209.  
  210.    IF nextnode = NIL
  211.       self.last := newnode
  212.    ELSE
  213.       nextnode.prev := newnode
  214.    ENDIF
  215.  
  216. ENDPROC oldnode
  217.  
  218.  
  219.