3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* // TODO: this structure class Slot { public $index; } */ class Show { private $possibleSlots; function __construct(array $possibleSlots) { $this->possibleSlots = $possibleSlots; } function getNextSlot() { return $this->possibleSlots.next(); } } class Schedule { public $matchings; function __construct(array $matchings) { $this->matchings = $matchings; } function slotAvalible($slot) { return array_key_exists($slot, $matchings); } function getShowAt($slot) { return $matchings[$slot]; } // simple recursive bipartite matching // applies the new matching or returns false if no possible matching function insertShow(Show $newShow) { // store the alternate matching so we can detect loops static $newMatching; // iterate over all possible slots $nextSlot = $newShow->getNextSlot(); if ($nextSlot) { // add to alternate matching $newMatching[$newShow] = $newSlot; // if there is no show set, we are done if ($this->slotAvalible($newSlot)) { $matchings[$newSlot] = $newMatching[$newSlot]; // clean up so we can reuse unset($newMatchings[$newSlot]); return true; } else { // trace path back $nextShow = $this->getShowAt($nextSlot); if (array_key_exists($nextShow, $newMatching)) { // this matching has already been considered so we would enter a loop unset($newMatching[$newShow]); return false; } // find alternate path from the next node in our alternate path $this->insertShow($nextShow); } } else { // no more slots were found for this show return false; } } } // everything below this line is a test $show1 = new Show(array(0,1,2)); $show2 = new Show(array(1)); $show3 = new Show(array(1,2)); $show4 = new Show(array(3)); // TODO: array cannot have objects as keys $initialMatching = array($show1=>1, $show3=>2, $show4=>3); $sched = new Schedule($initialMatching); $sched->insertShow($show2); print_r($sched->matchings); ?>
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VpOHL
function name:  (null)
number of ops:  32
compiled vars:  !0 = $show1, !1 = $show2, !2 = $show3, !3 = $show4, !4 = $initialMatching, !5 = $sched
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   NEW                                              $6      'Show'
          1        SEND_VAL_EX                                              <array>
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $6
   77     4        NEW                                              $9      'Show'
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $9
   78     8        NEW                                              $12     'Show'
          9        SEND_VAL_EX                                              <array>
         10        DO_FCALL                                      0          
         11        ASSIGN                                                   !2, $12
   79    12        NEW                                              $15     'Show'
         13        SEND_VAL_EX                                              <array>
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !3, $15
   82    16        INIT_ARRAY                                       ~18     1, !0
         17        ADD_ARRAY_ELEMENT                                ~18     2, !2
         18        ADD_ARRAY_ELEMENT                                ~18     3, !3
         19        ASSIGN                                                   !4, ~18
   84    20        NEW                                              $20     'Schedule'
         21        SEND_VAR_EX                                              !4
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !5, $20
   86    24        INIT_METHOD_CALL                                         !5, 'insertShow'
         25        SEND_VAR_EX                                              !1
         26        DO_FCALL                                      0          
   88    27        INIT_FCALL                                               'print_r'
         28        FETCH_OBJ_R                                      ~24     !5, 'matchings'
         29        SEND_VAL                                                 ~24
         30        DO_ICALL                                                 
   90    31      > RETURN                                                   1

Class Show:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VpOHL
function name:  __construct
number of ops:  4
compiled vars:  !0 = $possibleSlots
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   13     1        ASSIGN_OBJ                                               'possibleSlots'
          2        OP_DATA                                                  !0
   14     3      > RETURN                                                   null

End of function __construct

Function getnextslot:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VpOHL
function name:  getNextSlot
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   FETCH_OBJ_R                                      ~0      'possibleSlots'
          1        INIT_FCALL                                               'next'
          2        DO_ICALL                                         $1      
          3        CONCAT                                           ~2      ~0, $1
          4      > RETURN                                                   ~2
   18     5*     > RETURN                                                   null

End of function getnextslot

End of class Show.

Class Schedule:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VpOHL
function name:  __construct
number of ops:  4
compiled vars:  !0 = $matchings
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   27     1        ASSIGN_OBJ                                               'matchings'
          2        OP_DATA                                                  !0
   28     3      > RETURN                                                   null

End of function __construct

Function slotavalible:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VpOHL
function name:  slotAvalible
number of ops:  4
compiled vars:  !0 = $slot, !1 = $matchings
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   31     1        ARRAY_KEY_EXISTS                                 ~2      !0, !1
          2      > RETURN                                                   ~2
   32     3*     > RETURN                                                   null

End of function slotavalible

Function getshowat:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VpOHL
function name:  getShowAt
number of ops:  4
compiled vars:  !0 = $slot, !1 = $matchings
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   35     1        FETCH_DIM_R                                      ~2      !1, !0
          2      > RETURN                                                   ~2
   36     3*     > RETURN                                                   null

End of function getshowat

Function insertshow:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 30
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 26
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/VpOHL
function name:  insertShow
number of ops:  32
compiled vars:  !0 = $newShow, !1 = $newMatching, !2 = $nextSlot, !3 = $newSlot, !4 = $matchings, !5 = $newMatchings, !6 = $nextShow
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
   42     1        BIND_STATIC                                              !1
   45     2        INIT_METHOD_CALL                                         !0, 'getNextSlot'
          3        DO_FCALL                                      0  $7      
          4        ASSIGN                                                   !2, $7
   46     5      > JMPZ                                                     !2, ->30
   48     6    >   ASSIGN_DIM                                               !1, !0
          7        OP_DATA                                                  !3
   50     8        INIT_METHOD_CALL                                         'slotAvalible'
          9        SEND_VAR_EX                                              !3
         10        DO_FCALL                                      0  $10     
         11      > JMPZ                                                     $10, ->18
   51    12    >   FETCH_DIM_R                                      ~12     !1, !3
         13        ASSIGN_DIM                                               !4, !3
         14        OP_DATA                                                  ~12
   53    15        UNSET_DIM                                                !5, !3
   54    16      > RETURN                                                   <true>
         17*       JMP                                                      ->29
   57    18    >   INIT_METHOD_CALL                                         'getShowAt'
         19        SEND_VAR_EX                                              !2
         20        DO_FCALL                                      0  $13     
         21        ASSIGN                                                   !6, $13
   58    22        ARRAY_KEY_EXISTS                                         !6, !1
         23      > JMPZ                                                     ~15, ->26
   60    24    >   UNSET_DIM                                                !1, !0
   61    25      > RETURN                                                   <false>
   64    26    >   INIT_METHOD_CALL                                         'insertShow'
         27        SEND_VAR_EX                                              !6
         28        DO_FCALL                                      0          
         29      > JMP                                                      ->31
   68    30    > > RETURN                                                   <false>
   70    31    > > RETURN                                                   null

End of function insertshow

End of class Schedule.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.44 ms | 1409 KiB | 17 Q