| match agentPrefs projectPrefs |
match := [:offers :choices | | n ranks next held free a b old |
 n := offers size.
 ranks := choices collect: [:row | | inverse |
  inverse := Array new: n.
  1 to: n do: [:i | inverse at: (row at: i) put: i]. inverse].
 next := Array new: n withAll: 1.
 held := Array new: n withAll: 0.
 free := (1 to: n) reversed asOrderedCollection.
 [free isEmpty] whileFalse: [
  a := free removeLast.
  b := (offers at: a) at: (next at: a).
  next at: a put: (next at: a) + 1.
  old := held at: b.
  (old = 0 or: [((ranks at: b) at: a) < ((ranks at: b) at: old)])
   ifTrue: [held at: b put: a.
    old = 0 ifFalse: [free addLast: old]]
   ifFalse: [free addLast: a]].
 held].
agentPrefs := #(#(1 2 3) #(1 2 3) #(3 2 1)).
projectPrefs := #(#(2 1 3) #(1 2 3) #(3 1 2)).
match value: agentPrefs value: projectPrefs
