3v4l.org

run code in 300+ PHP versions simultaneously
<?php $matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16], // [17, 18, 19, 20] ]; $r = 1; function matrixRotation($matrix, $r) { $m = new Matrix($matrix); $m->rotate(); print_r($m->get()); } class Matrix { private $mx; private $m; private $n; private $a; // i [a,b] private $b; private $c; // j [c,d] private $d; public function get() { return $this->mx; } public function __construct($mx) { try { $this->set($mx); } catch(\Exception $e) { echo $e->getMessage(); // exit(1); } } private function set(array $mx) { $this->m = count($mx); $this->n = count($mx[0]); $this->a = 0; $this->b = m-1; $this->c = 0; $this->d = n-1; $bCtr = ($this->b)/2; $dCtr = ($this->d)/2;; $this->mx = $mx; } public function rotate($r) { $k = 0; while(1) { $this->a += $k; $this->b -= $k; $this->c += $k; $this->d -= $k; if ( $this->a === $this->b || $this->c === $this->d ) { break; } $mult = 2*($this->m-2*$k)+2*($this->n-2*$k)-4; $rm = $r % $mult; while($rm--) { rotateSquare(); } ++$k; } } private function rotateSquare($k) { $t = []; //transite for($i=0; $i<$this->m; ++$i) { for($j=0; $j<$this->n; ++$j) { $t[$i][$j] = [0,0]; } } $j=$this->c; for($i=$this->a; $i<$this->b; ++$i) { $t[$i][$j] = [-1,0]; } $i=$this->b; for($j=$this->c; $j<$this->d; ++$j) { $t[$i][$j] = [0,1]; } $j=$this->d; for($i=$this->b; $i>$this->a; --$i) { $t[$i][$j] = [1,0]; } $i=$this->a; for($j=$this->d; $j<$this->c; --$j) { $t[$i][$j] = [0,-1]; } $temp = []; for($i=0; $i<$this->m; ++$i) { for($j=0; $j<$this->n; ++$j) { $temp[$i + $t[$i][$j][0] ] [$j + $t[$i][$j][1] ] = $this->mx[$i][$j]; } } $this->mx = $this->ksort($temp); return $this; } private function ksort($temp) { foreach($temp as &$ar){ ksort($ar); } ksort($temp); return $temp; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SCuj4
function name:  (null)
number of ops:  3
compiled vars:  !0 = $matrix, !1 = $r
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    9     1        ASSIGN                                                   !1, 1
  126     2      > RETURN                                                   1

Function matrixrotation:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SCuj4
function name:  matrixRotation
number of ops:  14
compiled vars:  !0 = $matrix, !1 = $r, !2 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        NEW                                              $3      'Matrix'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $3
   13     6        INIT_METHOD_CALL                                         !2, 'rotate'
          7        DO_FCALL                                      0          
   14     8        INIT_FCALL                                               'print_r'
          9        INIT_METHOD_CALL                                         !2, 'get'
         10        DO_FCALL                                      0  $7      
         11        SEND_VAR                                                 $7
         12        DO_ICALL                                                 
   16    13      > RETURN                                                   null

End of function matrixrotation

Class Matrix:
Function get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SCuj4
function name:  get
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   FETCH_OBJ_R                                      ~0      'mx'
          1      > RETURN                                                   ~0
   31     2*     > RETURN                                                   null

End of function get

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 5
Branch analysis from position: 5
2 jumps found. (Code = 107) Position 1 = 6, Position 2 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SCuj4
function name:  __construct
number of ops:  10
compiled vars:  !0 = $mx, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
   35     1        INIT_METHOD_CALL                                         'set'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > JMP                                                      ->9
   36     5  E > > CATCH                                       last         'Exception'
   37     6    >   INIT_METHOD_CALL                                         !1, 'getMessage'
          7        DO_FCALL                                      0  $3      
          8        ECHO                                                     $3
   40     9    > > RETURN                                                   null

End of function __construct

Function set:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/SCuj4
function name:  set
number of ops:  29
compiled vars:  !0 = $mx, !1 = $bCtr, !2 = $dCtr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   RECV                                             !0      
   43     1        COUNT                                            ~4      !0
          2        ASSIGN_OBJ                                               'm'
          3        OP_DATA                                                  ~4
   44     4        FETCH_DIM_R                                      ~6      !0, 0
          5        COUNT                                            ~7      ~6
          6        ASSIGN_OBJ                                               'n'
          7        OP_DATA                                                  ~7
   46     8        ASSIGN_OBJ                                               'a'
          9        OP_DATA                                                  0
   47    10        FETCH_CONSTANT                                   ~10     'm'
         11        SUB                                              ~11     ~10, 1
         12        ASSIGN_OBJ                                               'b'
         13        OP_DATA                                                  ~11
   48    14        ASSIGN_OBJ                                               'c'
         15        OP_DATA                                                  0
   49    16        FETCH_CONSTANT                                   ~14     'n'
         17        SUB                                              ~15     ~14, 1
         18        ASSIGN_OBJ                                               'd'
         19        OP_DATA                                                  ~15
   51    20        FETCH_OBJ_R                                      ~16     'b'
         21        DIV                                              ~17     ~16, 2
         22        ASSIGN                                                   !1, ~17
   52    23        FETCH_OBJ_R                                      ~19     'd'
         24        DIV                                              ~20     ~19, 2
         25        ASSIGN                                                   !2, ~20
   54    26        ASSIGN_OBJ                                               'mx'
         27        OP_DATA                                                  !0
   55    28      > RETURN                                                   null

End of function set

Function rotate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 40
Branch analysis from position: 40
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 3
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 47) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 21
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 41
Branch analysis from position: 41
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 35
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 3
Branch analysis from position: 41
Branch analysis from position: 3
Branch analysis from position: 35
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 35
Branch analysis from position: 39
Branch analysis from position: 35
Branch analysis from position: 19
filename:       /in/SCuj4
function name:  rotate
number of ops:  42
compiled vars:  !0 = $r, !1 = $k, !2 = $mult, !3 = $rm
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
   58     1        ASSIGN                                                   !1, 0
   59     2      > JMP                                                      ->40
   60     3    >   ASSIGN_OBJ_OP                                 1          'a'
          4        OP_DATA                                                  !1
   61     5        ASSIGN_OBJ_OP                                 2          'b'
          6        OP_DATA                                                  !1
   62     7        ASSIGN_OBJ_OP                                 1          'c'
          8        OP_DATA                                                  !1
   63     9        ASSIGN_OBJ_OP                                 2          'd'
         10        OP_DATA                                                  !1
   65    11        FETCH_OBJ_R                                      ~9      'a'
         12        FETCH_OBJ_R                                      ~10     'b'
         13        IS_IDENTICAL                                     ~11     ~9, ~10
         14      > JMPNZ_EX                                         ~11     ~11, ->19
   66    15    >   FETCH_OBJ_R                                      ~12     'c'
         16        FETCH_OBJ_R                                      ~13     'd'
         17        IS_IDENTICAL                                     ~14     ~12, ~13
         18        BOOL                                             ~11     ~14
         19    > > JMPZ                                                     ~11, ->21
   68    20    > > JMP                                                      ->41
   70    21    >   FETCH_OBJ_R                                      ~15     'm'
         22        MUL                                              ~16     !1, 2
         23        SUB                                              ~17     ~15, ~16
         24        MUL                                              ~18     ~17, 2
         25        FETCH_OBJ_R                                      ~19     'n'
         26        MUL                                              ~20     !1, 2
         27        SUB                                              ~21     ~19, ~20
         28        MUL                                              ~22     ~21, 2
         29        ADD                                              ~23     ~18, ~22
         30        SUB                                              ~24     ~23, 4
         31        ASSIGN                                                   !2, ~24
   71    32        MOD                                              ~26     !0, !2
         33        ASSIGN                                                   !3, ~26
   72    34      > JMP                                                      ->37
   73    35    >   INIT_FCALL_BY_NAME                                       'rotateSquare'
         36        DO_FCALL                                      0          
   72    37    >   POST_DEC                                         ~29     !3
         38      > JMPNZ                                                    ~29, ->35
   75    39    >   PRE_INC                                                  !1
   59    40    > > JMPNZ                                                    1, ->3
   77    41    > > RETURN                                                   null

End of function rotate

Function rotatesquare:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 4
Branch analysis from position: 17
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 22
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 34
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 46
Branch analysis from position: 53
1 jumps found. (Code = 42) Position 1 = 62
Branch analysis from position: 62
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 58
Branch analysis from position: 65
1 jumps found. (Code = 42) Position 1 = 89
Branch analysis from position: 89
2 jumps found. (Code = 44) Position 1 = 92, Position 2 = 68
Branch analysis from position: 92
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 68
1 jumps found. (Code = 42) Position 1 = 85
Branch analysis from position: 85
2 jumps found. (Code = 44) Position 1 = 88, Position 2 = 70
Branch analysis from position: 88
2 jumps found. (Code = 44) Position 1 = 92, Position 2 = 68
Branch analysis from position: 92
Branch analysis from position: 68
Branch analysis from position: 70
2 jumps found. (Code = 44) Position 1 = 88, Position 2 = 70
Branch analysis from position: 88
Branch analysis from position: 70
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 65, Position 2 = 58
Branch analysis from position: 65
Branch analysis from position: 58
Branch analysis from position: 46
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 46
Branch analysis from position: 53
Branch analysis from position: 46
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 34
Branch analysis from position: 41
Branch analysis from position: 34
Branch analysis from position: 22
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 22
Branch analysis from position: 29
Branch analysis from position: 22
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 6
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 17, Position 2 = 4
Branch analysis from position: 17
Branch analysis from position: 4
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 6
Branch analysis from position: 13
Branch analysis from position: 6
filename:       /in/SCuj4
function name:  rotateSquare
number of ops:  100
compiled vars:  !0 = $k, !1 = $t, !2 = $i, !3 = $j, !4 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
   80     1        ASSIGN                                                   !1, <array>
   82     2        ASSIGN                                                   !2, 0
          3      > JMP                                                      ->14
   83     4    >   ASSIGN                                                   !3, 0
          5      > JMP                                                      ->10
   84     6    >   FETCH_DIM_W                                      $8      !1, !2
          7        ASSIGN_DIM                                               $8, !3
          8        OP_DATA                                                  <array>
   83     9        PRE_INC                                                  !3
         10    >   FETCH_OBJ_R                                      ~11     'n'
         11        IS_SMALLER                                               !3, ~11
         12      > JMPNZ                                                    ~12, ->6
   82    13    >   PRE_INC                                                  !2
         14    >   FETCH_OBJ_R                                      ~14     'm'
         15        IS_SMALLER                                               !2, ~14
         16      > JMPNZ                                                    ~15, ->4
   88    17    >   FETCH_OBJ_R                                      ~16     'c'
         18        ASSIGN                                                   !3, ~16
   89    19        FETCH_OBJ_R                                      ~18     'a'
         20        ASSIGN                                                   !2, ~18
         21      > JMP                                                      ->26
   90    22    >   FETCH_DIM_W                                      $20     !1, !2
         23        ASSIGN_DIM                                               $20, !3
         24        OP_DATA                                                  <array>
   89    25        PRE_INC                                                  !2
         26    >   FETCH_OBJ_R                                      ~23     'b'
         27        IS_SMALLER                                               !2, ~23
         28      > JMPNZ                                                    ~24, ->22
   92    29    >   FETCH_OBJ_R                                      ~25     'b'
         30        ASSIGN                                                   !2, ~25
   93    31        FETCH_OBJ_R                                      ~27     'c'
         32        ASSIGN                                                   !3, ~27
         33      > JMP                                                      ->38
   94    34    >   FETCH_DIM_W                                      $29     !1, !2
         35        ASSIGN_DIM                                               $29, !3
         36        OP_DATA                                                  <array>
   93    37        PRE_INC                                                  !3
         38    >   FETCH_OBJ_R                                      ~32     'd'
         39        IS_SMALLER                                               !3, ~32
         40      > JMPNZ                                                    ~33, ->34
   96    41    >   FETCH_OBJ_R                                      ~34     'd'
         42        ASSIGN                                                   !3, ~34
   97    43        FETCH_OBJ_R                                      ~36     'b'
         44        ASSIGN                                                   !2, ~36
         45      > JMP                                                      ->50
   98    46    >   FETCH_DIM_W                                      $38     !1, !2
         47        ASSIGN_DIM                                               $38, !3
         48        OP_DATA                                                  <array>
   97    49        PRE_DEC                                                  !2
         50    >   FETCH_OBJ_R                                      ~41     'a'
         51        IS_SMALLER                                               ~41, !2
         52      > JMPNZ                                                    ~42, ->46
  100    53    >   FETCH_OBJ_R                                      ~43     'a'
         54        ASSIGN                                                   !2, ~43
  101    55        FETCH_OBJ_R                                      ~45     'd'
         56        ASSIGN                                                   !3, ~45
         57      > JMP                                                      ->62
  102    58    >   FETCH_DIM_W                                      $47     !1, !2
         59        ASSIGN_DIM                                               $47, !3
         60        OP_DATA                                                  <array>
  101    61        PRE_DEC                                                  !3
         62    >   FETCH_OBJ_R                                      ~50     'c'
         63        IS_SMALLER                                               !3, ~50
         64      > JMPNZ                                                    ~51, ->58
  105    65    >   ASSIGN                                                   !4, <array>
  106    66        ASSIGN                                                   !2, 0
         67      > JMP                                                      ->89
  107    68    >   ASSIGN                                                   !3, 0
         69      > JMP                                                      ->85
  108    70    >   FETCH_DIM_R                                      ~55     !1, !2
         71        FETCH_DIM_R                                      ~56     ~55, !3
         72        FETCH_DIM_R                                      ~57     ~56, 0
         73        ADD                                              ~58     !2, ~57
  109    74        FETCH_DIM_R                                      ~60     !1, !2
         75        FETCH_DIM_R                                      ~61     ~60, !3
         76        FETCH_DIM_R                                      ~62     ~61, 1
         77        ADD                                              ~63     !3, ~62
         78        FETCH_OBJ_R                                      ~65     'mx'
         79        FETCH_DIM_R                                      ~66     ~65, !2
         80        FETCH_DIM_R                                      ~67     ~66, !3
  108    81        FETCH_DIM_W                                      $59     !4, ~58
  109    82        ASSIGN_DIM                                               $59, ~63
         83        OP_DATA                                                  ~67
  107    84        PRE_INC                                                  !3
         85    >   FETCH_OBJ_R                                      ~69     'n'
         86        IS_SMALLER                                               !3, ~69
         87      > JMPNZ                                                    ~70, ->70
  106    88    >   PRE_INC                                                  !2
         89    >   FETCH_OBJ_R                                      ~72     'm'
         90        IS_SMALLER                                               !2, ~72
         91      > JMPNZ                                                    ~73, ->68
  113    92    >   INIT_METHOD_CALL                                         'ksort'
         93        SEND_VAR_EX                                              !4
         94        DO_FCALL                                      0  $75     
         95        ASSIGN_OBJ                                               'mx'
         96        OP_DATA                                                  $75
  115    97        FETCH_THIS                                       ~76     
         98      > RETURN                                                   ~76
  116    99*     > RETURN                                                   null

End of function rotatesquare

Function ksort:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
2 jumps found. (Code = 126) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/SCuj4
function name:  ksort
number of ops:  13
compiled vars:  !0 = $temp, !1 = $ar
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  118     0  E >   RECV                                             !0      
  119     1      > FE_RESET_RW                                      $2      !0, ->7
          2    > > FE_FETCH_RW                                              $2, !1, ->7
  120     3    >   INIT_FCALL                                               'ksort'
          4        SEND_REF                                                 !1
          5        DO_ICALL                                                 
  119     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $2
  122     8        INIT_FCALL                                               'ksort'
          9        SEND_REF                                                 !0
         10        DO_ICALL                                                 
  124    11      > RETURN                                                   !0
  125    12*     > RETURN                                                   null

End of function ksort

End of class Matrix.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.8 ms | 1420 KiB | 17 Q