Advertisement
musifter

AoC Smalltalk Priority Queue/Heap

Dec 24th, 2022
2,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.34 KB | Source Code | 0 0
  1. " Heap class to implement priority queue "
  2. Object subclass: Heap [
  3.     | arr end |
  4.     Heap class >> new: size [
  5.         ^super new init: size
  6.     ]
  7.  
  8.     init: size [
  9.         arr := Array new: size.
  10.         end := 0.
  11.         ^self
  12.     ]
  13.  
  14.     isEmpty  [ ^(end  = 0) ]
  15.     notEmpty [ ^(end ~= 0) ]
  16.  
  17.     heapify: idx [
  18.         | left right smallest |
  19.         left  := 2 * idx.
  20.         right := 2 * idx + 1.
  21.  
  22.         smallest := idx.
  23.  
  24.         ((left <= end) and: [(arr at: idx) > (arr at: left)]) ifTrue: [
  25.             smallest := left
  26.         ].
  27.  
  28.         ((right <= end) and: [(arr at: smallest) > (arr at: right)]) ifTrue: [
  29.             smallest := right
  30.         ].
  31.  
  32.         (smallest ~= idx) ifTrue: [
  33.             arr swap: idx with: smallest.
  34.             self heapify: smallest.
  35.         ]
  36.     ]
  37.  
  38.     insert: item [
  39.         | i parent |
  40.         end := end + 1.
  41.         (end > arr size) ifTrue: [
  42.             stderr nextPutAll: ('Heap size (%1) exceeded!' % {arr size}); nl.
  43.             Error signal.
  44.         ].
  45.  
  46.         arr at: end put: item.
  47.  
  48.         i := end.
  49.         [(i ~= 1) and: [(arr at: (parent := i // 2)) > (arr at: i)]] whileTrue: [
  50.             arr swap: i with: parent.
  51.             i := parent.
  52.         ].
  53.         ^item
  54.     ]
  55.     next [
  56.         | top |
  57.         (end = 0) ifTrue: [ ^nil ].
  58.  
  59.         top := arr at: 1.
  60.         end := end - 1.
  61.  
  62.         (end > 0) ifTrue: [
  63.             arr at: 1 put: (arr at: end + 1).
  64.             self heapify: 1.
  65.         ].
  66.         ^top
  67.     ]
  68.  
  69.     priority [
  70.         (end = 0) ifTrue: [ ^nil ].
  71.         ^(arr at: 1) priority
  72.     ]
  73. ]
  74.  
  75. "
  76. | PriorityQueue implementation using Heap
  77. "
  78. Object subclass: PriorityItem [
  79.     | priority data |
  80.  
  81.     PriorityItem class >> new: pri data: anArray [ ^super new init: pri data: anArray ]
  82.     init: pri data: array [ priority := pri. data := array. ^self ]
  83.  
  84.     > state   [ ^priority > state priority ]
  85.     priority  [ ^priority ]
  86.     data      [ ^data     ]
  87. ]
  88.  
  89. Object subclass: PriorityQueue [
  90.     | heap |
  91.     PriorityQueue class >> new [ ^super new init ]
  92.     init  [ heap := Heap new: 100000. ^self ]
  93.  
  94.     isEmpty  [ ^heap isEmpty  ]
  95.     notEmpty [ ^heap notEmpty ]
  96.  
  97.     at: pri insert: data  [ heap insert: (PriorityItem new: pri data: data) ]
  98.  
  99.     next     [ ^heap next data ]
  100.     priority [ ^heap priority  ]
  101. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement