Completed
Push — master ( 652abf...65ca73 )
by Pol
09:59 queued 05:18
created

FiniteGroup   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 86.05%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 18
cbo 1
dl 0
loc 175
ccs 37
cts 43
cp 0.8605
rs 10
c 2
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A current() 0 4 1
A next() 0 5 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A count() 0 4 1
A setSize() 0 5 1
A getSize() 0 4 1
A gcd() 0 4 2
A toArray() 0 10 2
A computeGroup() 0 10 3
A order() 0 11 2
1
<?php
2
3
namespace drupol\phpermutations\Iterators;
4
5
use drupol\phpermutations\Combinatorics;
6
7
/**
8
 * Class FiniteGroup.
9
 *
10
 * The finite group is an abelian finite cyclic group.
11
 *
12
 * @package drupol\phpermutations\Iterators
13
 */
14
class FiniteGroup extends Combinatorics implements \Iterator, \Countable
15
{
16
17
  /**
18
   * The group size.
19
   *
20
   * @var int
21
   */
22
    protected $size;
23
24
    /**
25
     * The group.
26
     *
27
     * @var int[]
28
     */
29
    protected $group;
30
31
    /**
32
     * The key.
33
     *
34
     * @var int
35
     */
36
    protected $key;
37
38
    /**
39
     * Combinatorics constructor.
40
     */
41 2
    public function __construct()
42
    {
43 2
        parent::__construct([], null);
44 2
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function current()
50
    {
51 2
        return current($this->group);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 2
    public function next()
58
    {
59 2
        $this->key++;
60 2
        next($this->group);
61 2
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 2
    public function key()
67
    {
68 2
        return $this->key;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74 2
    public function valid()
75
    {
76 2
        return isset($this->group[$this->key()]);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 2
    public function rewind()
83
    {
84 2
        $this->key = 0;
85 2
    }
86
87
    /**
88
     * Count elements of an object.
89
     *
90
     * @return int
91
     *   The number of element.
92
     */
93 2
    public function count()
94
    {
95 2
        return count($this->group);
96
    }
97
98
    /**
99
     * Convert the iterator into an array.
100
     *
101
     * @return array
102
     *   The elements.
103
     */
104 2
    public function toArray()
105
    {
106 2
        $data = [];
107
108 2
        for ($this->rewind(); $this->valid(); $this->next()) {
109 2
            $data[] = $this->current();
110
        }
111
112 2
        return $data;
113
    }
114
115
    /**
116
     * Set the group size.
117
     *
118
     * @param int $size
119
     *   The size.
120
     */
121 2
    public function setSize($size)
122
    {
123 2
        $this->size = $size;
124 2
        $this->computeGroup();
125 2
    }
126
127
    /**
128
     * Get the group size.
129
     *
130
     * @return int
131
     *   The size.
132
     */
133 2
    public function getSize()
134
    {
135 2
        return intval($this->size);
136
    }
137
138
    /**
139
     * Clean out the group from unwanted values.
140
     */
141 2
    private function computeGroup()
142
    {
143 2
        $this->group = [];
144
145 2
        foreach (range(1, $this->getSize() - 1) as $number) {
146 2
            if ($this->gcd($number, $this->getSize() - 1) == 1) {
147 2
                $this->group[] = $number;
148
            }
149
        }
150 2
    }
151
152
    /**
153
     * Get the greater common divisor between two numbers.
154
     *
155
     * @param int $a
156
     *   The first number.
157
     * @param int $b
158
     *   The second number.
159
     *
160
     * @return int
161
     *   The greater common divisor between $a and $b.
162
     */
163 2
    private function gcd($a, $b)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $a. 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 $b. 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...
164
    {
165 2
        return $b ? $this->gcd($b, $a % $b) : $a;
166
    }
167
168
    /**
169
     * Get the order.
170
     *
171
     * @param int $generator
172
     *   The generator.
173
     *
174
     * @return int
175
     *   The order.
176
     */
177
    public function order($generator)
178
    {
179
        $result = [];
180
181
        foreach (range(1, $this->getSize() - 1) as $number) {
182
            $value = pow($generator, $number) % $this->getSize();
183
            $result[$value] = $value;
184
        }
185
186
        return count($result);
187
    }
188
}
189