| Conditions | 19 |
| Paths | 56 |
| Total Lines | 83 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 35 | public static function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) |
||
| 36 | { |
||
| 37 | // Common code for intializing a Reed-Solomon control block (char or int symbols) |
||
| 38 | // Copyright 2004 Phil Karn, KA9Q |
||
| 39 | // May be used under the terms of the GNU Lesser General Public License (LGPL) |
||
| 40 | |||
| 41 | $rs = null; |
||
| 42 | |||
| 43 | // Check parameter ranges |
||
| 44 | if($symsize < 0 || $symsize > 8) return $rs; |
||
| 45 | if($fcr < 0 || $fcr >= (1<<$symsize)) return $rs; |
||
| 46 | if($prim <= 0 || $prim >= (1<<$symsize)) return $rs; |
||
| 47 | if($nroots < 0 || $nroots >= (1<<$symsize)) return $rs; // Can't have more roots than symbol values! |
||
| 48 | if($pad < 0 || $pad >= ((1<<$symsize) -1 - $nroots)) return $rs; // Too much padding |
||
| 49 | |||
| 50 | $rs = new QRrsItem(); |
||
| 51 | $rs->mm = $symsize; |
||
| 52 | $rs->nn = (1<<$symsize)-1; |
||
| 53 | $rs->pad = $pad; |
||
| 54 | |||
| 55 | $rs->alpha_to = array_fill(0, $rs->nn+1, 0); |
||
| 56 | $rs->index_of = array_fill(0, $rs->nn+1, 0); |
||
| 57 | |||
| 58 | // PHP style macro replacement ;) |
||
| 59 | $NN =& $rs->nn; |
||
| 60 | $A0 =& $NN; |
||
| 61 | |||
| 62 | // Generate Galois field lookup tables |
||
| 63 | $rs->index_of[0] = $A0; // log(zero) = -inf |
||
| 64 | $rs->alpha_to[$A0] = 0; // alpha**-inf = 0 |
||
| 65 | $sr = 1; |
||
| 66 | |||
| 67 | for($i=0; $i<$rs->nn; $i++) { |
||
| 68 | $rs->index_of[$sr] = $i; |
||
| 69 | $rs->alpha_to[$i] = $sr; |
||
| 70 | $sr <<= 1; |
||
| 71 | if($sr & (1<<$symsize)) { |
||
| 72 | $sr ^= $gfpoly; |
||
| 73 | } |
||
| 74 | $sr &= $rs->nn; |
||
| 75 | } |
||
| 76 | |||
| 77 | if($sr != 1){ |
||
| 78 | // field generator polynomial is not primitive! |
||
| 79 | $rs = NULL; |
||
| 80 | return $rs; |
||
| 81 | } |
||
| 82 | |||
| 83 | /* Form RS code generator polynomial from its roots */ |
||
| 84 | $rs->genpoly = array_fill(0, $nroots+1, 0); |
||
| 85 | |||
| 86 | $rs->fcr = $fcr; |
||
| 87 | $rs->prim = $prim; |
||
| 88 | $rs->nroots = $nroots; |
||
| 89 | $rs->gfpoly = $gfpoly; |
||
| 90 | |||
| 91 | /* Find prim-th root of 1, used in decoding */ |
||
| 92 | for($iprim=1;($iprim % $prim) != 0;$iprim += $rs->nn) |
||
| 93 | ; // intentional empty-body loop! |
||
| 94 | |||
| 95 | $rs->iprim = (int)($iprim / $prim); |
||
| 96 | $rs->genpoly[0] = 1; |
||
| 97 | |||
| 98 | for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) { |
||
| 99 | $rs->genpoly[$i+1] = 1; |
||
| 100 | |||
| 101 | // Multiply rs->genpoly[] by @**(root + x) |
||
| 102 | for ($j = $i; $j > 0; $j--) { |
||
| 103 | if ($rs->genpoly[$j] != 0) { |
||
| 104 | $rs->genpoly[$j] = $rs->genpoly[$j-1] ^ $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[$j]] + $root)]; |
||
| 105 | } else { |
||
| 106 | $rs->genpoly[$j] = $rs->genpoly[$j-1]; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | // rs->genpoly[0] can never be zero |
||
| 110 | $rs->genpoly[0] = $rs->alpha_to[$rs->modnn($rs->index_of[$rs->genpoly[0]] + $root)]; |
||
| 111 | } |
||
| 112 | |||
| 113 | // convert rs->genpoly[] to index form for quicker encoding |
||
| 114 | for ($i = 0; $i <= $nroots; $i++) |
||
| 115 | $rs->genpoly[$i] = $rs->index_of[$rs->genpoly[$i]]; |
||
| 116 | |||
| 117 | return $rs; |
||
| 118 | } |
||
| 162 |