Completed
Push — master ( 831fe8...777178 )
by Pol
05:31
created

Hilbert::hilbert_to_point()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 25
Code Lines 22

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 25
loc 25
ccs 0
cts 25
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 22
nc 5
nop 2
crap 30
1
<?php
2
3
namespace drupol\phpermutations;
4
5
/**
6
 * Class Hilbert.
7
 *
8
 * @package drupol\phpermutations
9
 */
10
class Hilbert {
0 ignored issues
show
Coding Style introduced by
The property $hilbert_map is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $rev_map is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
11
12
  var $hilbert_map = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $hilbert_map.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
13
    'a' => array(
14
      "0, 0" => array(0, 'd'),
15
      "0, 1" => array(1, 'a'),
16
      "1, 0" => array(3, 'b'),
17
      "1, 1" => array(2, 'a'),
18
    ),
19
    'b' => array(
20
      "0, 0" => array(2, 'b'),
21
      "0, 1" => array(1, 'b'),
22
      "1, 0" => array(3, 'a'),
23
      "1, 1" => array(0, 'c'),
24
    ),
25
    'c' => array(
26
      "0, 0" => array(2, 'c'),
27
      "0, 1" => array(3, 'd'),
28
      "1, 0" => array(1, 'c'),
29
      "1, 1" => array(0, 'b'),
30
    ),
31
    'd' => array(
32
      "0, 0" => array(0, 'a'),
33
      "0, 1" => array(3, 'c'),
34
      "1, 0" => array(1, 'd'),
35
      "1, 1" => array(2, 'd'),
36
    ),
37
  );
38
39
  var $rev_map = Array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $rev_map.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
40
    'a' => Array(
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
41
      Array(3, 'd'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
42
      Array(1, 'a'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
43
      Array(0, 'a'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
44
      Array(2, 'c'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
45
    ),
46
    'b' => Array(
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
47
      Array(0, 'c'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
48
      Array(2, 'b'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
49
      Array(3, 'b'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
50
      Array(1, 'd'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
51
    ),
52
    'c' => Array(
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
53
      Array(0, 'b'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
54
      Array(1, 'c'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
55
      Array(3, 'c'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
56
      Array(2, 'a'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
57
    ),
58
    'd' => Array(
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
59
      Array(3, 'a'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
60
      Array(2, 'd'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
61
      Array(0, 'd'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
62
      Array(1, 'b'),
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
63
    ),
64
  );
65
66
  /**
67
   * @var int
68
   */
69
  protected $recursion;
70
71
  /**
72
   * Hilbert constructor.
73
   *
74
   * @param null $recursion
75
   */
76
  public function __construct($recursion = NULL) {
1 ignored issue
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
77
    if (is_null($recursion)) {
78
      $recursion = 28;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $recursion. This often makes code more readable.
Loading history...
79
    }
80
    $this->setRecursion($recursion);
81
  }
82
83
  /**
84
   * @param $recursion
85
   *
86
   * @return $this
87
   */
88
  public function setRecursion($recursion) {
89
    $this->recursion = $recursion;
90
    return $this;
91
  }
92
93
  /**
94
   * @return int
95
   */
96
  public function getRecursion() {
97
    return $this->recursion;
98
  }
99
100
101 View Code Duplication
  function point_to_hilbert($x, $y, $order = 16, $map = "hilbert_map_1", $mode = "hilbert") {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $x. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $y. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style Naming introduced by
The method point_to_hilbert is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $current_square is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $quad_x is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $quad_y is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $quad_position is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
Method name "Hilbert::point_to_hilbert" is not in camel caps format
Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for point_to_hilbert.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
102
    $current_square = 'a';
103
    $position = 0;
104
    foreach (range($order - 1, 0, -1) as $i) {
105
      $position <<= 2;
106
      $quad_x = $x & (1 << $i) ? 1 : 0;
107
      $quad_y = $y & (1 << $i) ? 1 : 0;
108
      list($quad_position, $current_square) = $this->{$map}[$current_square]["$quad_x, $quad_y"];
109
      $position |= $quad_position;
110
    }
111
    return $position;
112
  }
113
114 View Code Duplication
  function hilbert_to_point($hilbert, $order) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Naming introduced by
The method hilbert_to_point is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $current_square is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
Method name "Hilbert::hilbert_to_point" is not in camel caps format
Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for hilbert_to_point.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
115
    $current_square = "a";
116
    $amount = 1 << $order - 1;
117
    $x = $y = 0;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $x. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $y. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
118
    for ($i = 2 * $order; $i > 0; $i -= 2) {
119
      list ($position, $current_square) = $this->rev_map[$current_square][$hilbert >> $i - 2];
120
      switch ($position) {
121
        case 1:
122
          $x += $amount;
123
          break;
124
        case 2:
125
          $y += $amount;
126
          break;
127
        case 3:
128
          $y += $amount;
129
          $x += $amount;
130
          break;
131
        default:
132
          break;
133
      }
134
      $amount /= 2;
135
      $hilbert &= (1 << ($i - 2)) - 1;
136
    }
137
    return [$x, $y];
138
  }
139
140
141 View Code Duplication
  function xy2d($x, $y, $order = 16) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $x. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $y. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Coding Style Naming introduced by
The variable $current_square is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $quad_x is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $quad_y is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style Naming introduced by
The variable $quad_position is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for xy2d.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
142
    $current_square = 'a';
143
    $position = 0;
144
145
    foreach (range($order - 1, 0, -1) as $i) {
146
      $position <<= 2;
147
      $quad_x = $x & (1 << $i) ? 1 : 0;
148
      $quad_y = $y & (1 << $i) ? 1 : 0;
149
      list($quad_position, $current_square) = $this->hilbert_map[$current_square]["$quad_x, $quad_y"];
150
      $position |= $quad_position;
151
    }
152
153
    return $position;
154
  }
155
156 View Code Duplication
  function d2xy($hilbert, $order = 16) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Naming introduced by
The variable $current_square is not named in camelCase.

This check marks variable names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for d2xy.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
157
    $current_square = "a";
158
    $amount = 1 << $order - 1;
159
    $x = $y = 0;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $x. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $y. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
160
    for ($i = 2 * $order; $i > 0; $i -= 2) {
161
      list ($position, $current_square) = $this->rev_map[$current_square][$hilbert >> $i - 2];
162
      switch ($position) {
163
        case 1:
164
          $x += $amount;
165
          break;
166
        case 2:
167
          $y += $amount;
168
          break;
169
        case 3:
170
          $y += $amount;
171
          $x += $amount;
172
          break;
173
        default:
174
          break;
175
      }
176
      $amount /= 2;
177
      $hilbert &= (1 << ($i - 2)) - 1;
178
    }
179
    return [$x, $y];
180
  }
181
182
  function test_pth() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style Naming introduced by
The method test_pth is not named in camelCase.

This check marks method names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
Method name "Hilbert::test_pth" is not in camel caps format
Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for test_pth.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
183
    foreach (range(7, 0, -1) as $x) {
184
      foreach (range(7, 0, -1) as $y) {
185
        $sort[] = $points["$x, $y"] = $this->point_to_hilbert($x, $y, 3);
186
      }
187
    }
188
    array_multisort($points, $sort);
189
    foreach ($points as $k => $v) {
190
      echo $k . "\n";
191
    }
192
  }
193
194
}
195