Kernel-Dag.st 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. Smalltalk createPackage: 'Kernel-Dag'!
  2. Object subclass: #DagNode
  3. instanceVariableNames: ''
  4. package: 'Kernel-Dag'!
  5. !DagNode commentStamp!
  6. I am the abstract root class of any directed acyclic graph.
  7. Concrete classes should implement `dagChildren` and `dagChildren:`
  8. to get / set direct successor nodes (aka child nodes / subnodes).!
  9. !DagNode methodsFor: 'accessing'!
  10. allDagChildren
  11. | allNodes |
  12. allNodes := self dagChildren asSet.
  13. self dagChildren do: [ :each |
  14. allNodes addAll: each allDagChildren ].
  15. ^ allNodes
  16. !
  17. dagChildren
  18. self subclassResponsibility
  19. !
  20. dagChildren: aCollection
  21. self subclassResponsibility
  22. ! !
  23. !DagNode methodsFor: 'testing'!
  24. isDagNode
  25. ^ true
  26. ! !
  27. DagNode subclass: #DagParentNode
  28. instanceVariableNames: 'nodes'
  29. package: 'Kernel-Dag'!
  30. !DagParentNode commentStamp!
  31. I am `DagNode` that stores a collection of its children,
  32. lazy initialized to empty array.
  33. I can `addDagChild:` to add a child.!
  34. !DagParentNode methodsFor: 'accessing'!
  35. addDagChild: aDagNode
  36. self dagChildren add: aDagNode
  37. !
  38. dagChildren
  39. ^ nodes ifNil: [ nodes := Array new ]
  40. !
  41. dagChildren: aCollection
  42. nodes := aCollection
  43. ! !
  44. DagNode subclass: #DagSink
  45. instanceVariableNames: 'nodes'
  46. package: 'Kernel-Dag'!
  47. !DagSink commentStamp!
  48. I am `DagNode` with no direct successors.
  49. Sending `dagChildren:` with empty collection is legal.!
  50. !DagSink methodsFor: 'accessing'!
  51. dagChildren
  52. ^ #()
  53. !
  54. dagChildren: aCollection
  55. aCollection ifNotEmpty: [ self error: 'A DagSink cannot have children.' ]
  56. ! !
  57. !Object methodsFor: '*Kernel-Dag'!
  58. isDagNode
  59. ^ false
  60. ! !