Passed
Push — master ( 65ca73...10de3a )
by Pol
02:10
created

FiniteGroup   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 0%

Importance

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