@@ -19,106 +19,106 @@ |
||
19 | 19 | */ |
20 | 20 | class FixedBitString extends BitString |
21 | 21 | { |
22 | - /** |
|
23 | - * @param string $bits the bit string, i.e., a string of 1's and 0's |
|
24 | - * @param int|null $length length of the bit string in bits; |
|
25 | - * <tt>null</tt> for taking the length of <tt>$bits</tt>; |
|
26 | - * if less or greater than <tt>strlen($bits)</tt>, the bits get zero-padded or truncated on |
|
27 | - * the right to be exactly <tt>$length</tt> bits (and a warning is issued is case of |
|
28 | - * truncation) |
|
29 | - * @return FixedBitString |
|
30 | - * @throws \InvalidArgumentException if <tt>$length</tt> is a non-positive number (PostgreSQL forbids it) |
|
31 | - */ |
|
32 | - public static function fromString($bits, $length = null) |
|
33 | - { |
|
34 | - $bits = (string)$bits; |
|
35 | - $bitsLen = strlen($bits); |
|
22 | + /** |
|
23 | + * @param string $bits the bit string, i.e., a string of 1's and 0's |
|
24 | + * @param int|null $length length of the bit string in bits; |
|
25 | + * <tt>null</tt> for taking the length of <tt>$bits</tt>; |
|
26 | + * if less or greater than <tt>strlen($bits)</tt>, the bits get zero-padded or truncated on |
|
27 | + * the right to be exactly <tt>$length</tt> bits (and a warning is issued is case of |
|
28 | + * truncation) |
|
29 | + * @return FixedBitString |
|
30 | + * @throws \InvalidArgumentException if <tt>$length</tt> is a non-positive number (PostgreSQL forbids it) |
|
31 | + */ |
|
32 | + public static function fromString($bits, $length = null) |
|
33 | + { |
|
34 | + $bits = (string)$bits; |
|
35 | + $bitsLen = strlen($bits); |
|
36 | 36 | |
37 | - if ($length === null) { |
|
38 | - return new FixedBitString($bits, $bitsLen); |
|
39 | - } |
|
40 | - elseif ($length <= 0) { |
|
41 | - throw new \InvalidArgumentException('length is non-positive'); |
|
42 | - } |
|
43 | - elseif ($length == $bitsLen) { |
|
44 | - return new FixedBitString($bits, $bitsLen); |
|
45 | - } |
|
46 | - elseif ($length > $bitsLen) { |
|
47 | - return new FixedBitString(str_pad($bits, $length, '0'), $length); |
|
48 | - } |
|
49 | - else { // $length < $bitsLen |
|
50 | - trigger_error("Bit string truncated to the length of $length", E_USER_WARNING); |
|
51 | - return new FixedBitString(substr($bits, 0, $length), $length); |
|
52 | - } |
|
53 | - } |
|
37 | + if ($length === null) { |
|
38 | + return new FixedBitString($bits, $bitsLen); |
|
39 | + } |
|
40 | + elseif ($length <= 0) { |
|
41 | + throw new \InvalidArgumentException('length is non-positive'); |
|
42 | + } |
|
43 | + elseif ($length == $bitsLen) { |
|
44 | + return new FixedBitString($bits, $bitsLen); |
|
45 | + } |
|
46 | + elseif ($length > $bitsLen) { |
|
47 | + return new FixedBitString(str_pad($bits, $length, '0'), $length); |
|
48 | + } |
|
49 | + else { // $length < $bitsLen |
|
50 | + trigger_error("Bit string truncated to the length of $length", E_USER_WARNING); |
|
51 | + return new FixedBitString(substr($bits, 0, $length), $length); |
|
52 | + } |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Creates a bit string as the two's complement of a given integer represented on a given number of bits. |
|
57 | - * |
|
58 | - * The least significant bit is the rightmost one in the resulting bit string. |
|
59 | - * |
|
60 | - * Overflows are not detected - if `$length` is not sufficient for representing the whole `$int`, only the `$length` |
|
61 | - * least significant bits of the representation are used quietly, without any notice. |
|
62 | - * |
|
63 | - * @param int $int integer to represent |
|
64 | - * @param int $length number of bits |
|
65 | - * @return FixedBitString |
|
66 | - * @throws \InvalidArgumentException if <tt>$length</tt> is a non-positive number |
|
67 | - */ |
|
68 | - public static function fromInt($int, $length) |
|
69 | - { |
|
70 | - if ($length <= 0) { |
|
71 | - throw new \InvalidArgumentException('length <= 0'); |
|
72 | - } |
|
55 | + /** |
|
56 | + * Creates a bit string as the two's complement of a given integer represented on a given number of bits. |
|
57 | + * |
|
58 | + * The least significant bit is the rightmost one in the resulting bit string. |
|
59 | + * |
|
60 | + * Overflows are not detected - if `$length` is not sufficient for representing the whole `$int`, only the `$length` |
|
61 | + * least significant bits of the representation are used quietly, without any notice. |
|
62 | + * |
|
63 | + * @param int $int integer to represent |
|
64 | + * @param int $length number of bits |
|
65 | + * @return FixedBitString |
|
66 | + * @throws \InvalidArgumentException if <tt>$length</tt> is a non-positive number |
|
67 | + */ |
|
68 | + public static function fromInt($int, $length) |
|
69 | + { |
|
70 | + if ($length <= 0) { |
|
71 | + throw new \InvalidArgumentException('length <= 0'); |
|
72 | + } |
|
73 | 73 | |
74 | - $bin = decbin($int); |
|
74 | + $bin = decbin($int); |
|
75 | 75 | |
76 | - $padBit = ($int >= 0 ? '0' : '1'); |
|
77 | - $padded = str_pad($bin, $length, $padBit, STR_PAD_LEFT); |
|
76 | + $padBit = ($int >= 0 ? '0' : '1'); |
|
77 | + $padded = str_pad($bin, $length, $padBit, STR_PAD_LEFT); |
|
78 | 78 | |
79 | - $truncated = substr($padded, -$length); |
|
80 | - return self::fromString($truncated); |
|
81 | - } |
|
79 | + $truncated = substr($padded, -$length); |
|
80 | + return self::fromString($truncated); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Returns a non-negative integer encoded by the bits in the bit string. |
|
85 | - * |
|
86 | - * The standard binary encoding is used. The rightmost bit in the string is the least significant in the integer. |
|
87 | - * |
|
88 | - * Only that many rightmost bits are taken which allow the encoded integer be represented correctly by the PHP `int` |
|
89 | - * type. That is 31 bits or 63 bits depending on whether this is a 32-bit or 64-bit compilation of PHP. |
|
90 | - * |
|
91 | - * For getting an arbitrary-length integer instead of the truncated `int`, use {@link toNumber()}. |
|
92 | - * |
|
93 | - * Note that, unlike {@link fromInt()}, this method does NOT work with the two's complement, but rather with the |
|
94 | - * standard binary encoding, and thus never returns any negative number. This is to resemble the PostgreSQL |
|
95 | - * behaviour. |
|
96 | - * |
|
97 | - * @return int |
|
98 | - */ |
|
99 | - public function toInt() |
|
100 | - { |
|
101 | - return bindec(substr($this->bits, -(PHP_INT_SIZE * 8 - 1))); |
|
102 | - } |
|
83 | + /** |
|
84 | + * Returns a non-negative integer encoded by the bits in the bit string. |
|
85 | + * |
|
86 | + * The standard binary encoding is used. The rightmost bit in the string is the least significant in the integer. |
|
87 | + * |
|
88 | + * Only that many rightmost bits are taken which allow the encoded integer be represented correctly by the PHP `int` |
|
89 | + * type. That is 31 bits or 63 bits depending on whether this is a 32-bit or 64-bit compilation of PHP. |
|
90 | + * |
|
91 | + * For getting an arbitrary-length integer instead of the truncated `int`, use {@link toNumber()}. |
|
92 | + * |
|
93 | + * Note that, unlike {@link fromInt()}, this method does NOT work with the two's complement, but rather with the |
|
94 | + * standard binary encoding, and thus never returns any negative number. This is to resemble the PostgreSQL |
|
95 | + * behaviour. |
|
96 | + * |
|
97 | + * @return int |
|
98 | + */ |
|
99 | + public function toInt() |
|
100 | + { |
|
101 | + return bindec(substr($this->bits, -(PHP_INT_SIZE * 8 - 1))); |
|
102 | + } |
|
103 | 103 | |
104 | - public static function fromNumber($number) |
|
105 | - { |
|
106 | - // TODO: make up an object of the class for representing arbitrary-length integers, and fix the interface (method name) and phpdoc |
|
107 | - throw new NotImplementedException(); |
|
108 | - } |
|
104 | + public static function fromNumber($number) |
|
105 | + { |
|
106 | + // TODO: make up an object of the class for representing arbitrary-length integers, and fix the interface (method name) and phpdoc |
|
107 | + throw new NotImplementedException(); |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Returns a non-negative arbitrary-length integer encoded by the bits in the bit string. |
|
112 | - * |
|
113 | - * The standard binary encoding is used. The rightmost bit in the string is the least significant in the integer. |
|
114 | - * |
|
115 | - * Contrary to {@link toInt()}, this method uses all the bits to constitute the result. |
|
116 | - * |
|
117 | - * @return object |
|
118 | - */ |
|
119 | - public function toNumber() |
|
120 | - { |
|
121 | - // TODO: make up an object of the class for representing arbitrary-length integers, and fix the interface (method name) and phpdoc |
|
122 | - throw new NotImplementedException(); |
|
123 | - } |
|
110 | + /** |
|
111 | + * Returns a non-negative arbitrary-length integer encoded by the bits in the bit string. |
|
112 | + * |
|
113 | + * The standard binary encoding is used. The rightmost bit in the string is the least significant in the integer. |
|
114 | + * |
|
115 | + * Contrary to {@link toInt()}, this method uses all the bits to constitute the result. |
|
116 | + * |
|
117 | + * @return object |
|
118 | + */ |
|
119 | + public function toNumber() |
|
120 | + { |
|
121 | + // TODO: make up an object of the class for representing arbitrary-length integers, and fix the interface (method name) and phpdoc |
|
122 | + throw new NotImplementedException(); |
|
123 | + } |
|
124 | 124 | } |
@@ -36,17 +36,13 @@ |
||
36 | 36 | |
37 | 37 | if ($length === null) { |
38 | 38 | return new FixedBitString($bits, $bitsLen); |
39 | - } |
|
40 | - elseif ($length <= 0) { |
|
39 | + } elseif ($length <= 0) { |
|
41 | 40 | throw new \InvalidArgumentException('length is non-positive'); |
42 | - } |
|
43 | - elseif ($length == $bitsLen) { |
|
41 | + } elseif ($length == $bitsLen) { |
|
44 | 42 | return new FixedBitString($bits, $bitsLen); |
45 | - } |
|
46 | - elseif ($length > $bitsLen) { |
|
43 | + } elseif ($length > $bitsLen) { |
|
47 | 44 | return new FixedBitString(str_pad($bits, $length, '0'), $length); |
48 | - } |
|
49 | - else { // $length < $bitsLen |
|
45 | + } else { // $length < $bitsLen |
|
50 | 46 | trigger_error("Bit string truncated to the length of $length", E_USER_WARNING); |
51 | 47 | return new FixedBitString(substr($bits, 0, $length), $length); |
52 | 48 | } |
@@ -55,8 +55,7 @@ |
||
55 | 55 | if (isset($this->offsetMap[$effectiveOffset])) { |
56 | 56 | $sourceOffset = $this->offsetMap[$effectiveOffset]; |
57 | 57 | return parent::tuple($sourceOffset); |
58 | - } |
|
59 | - else { |
|
58 | + } else { |
|
60 | 59 | throw new \OutOfBoundsException("Tuple offset $offset out of the relation bounds"); |
61 | 60 | } |
62 | 61 | } |
@@ -40,8 +40,7 @@ discard block |
||
40 | 40 | if ($escaped) { |
41 | 41 | $lastLiteral .= $c; |
42 | 42 | $escaped = false; |
43 | - } |
|
44 | - else { |
|
43 | + } else { |
|
45 | 44 | switch ($c) { |
46 | 45 | case '\\': |
47 | 46 | $escaped = true; |
@@ -78,8 +77,7 @@ discard block |
||
78 | 77 | } |
79 | 78 | $repl .= $c; |
80 | 79 | $escaped = false; |
81 | - } |
|
82 | - else { |
|
80 | + } else { |
|
83 | 81 | switch ($c) { |
84 | 82 | case '\\': |
85 | 83 | $escaped = true; |
@@ -61,12 +61,12 @@ discard block |
||
61 | 61 | public function uniq($hasher = null, $comparator = null) |
62 | 62 | { |
63 | 63 | if ($hasher === null) { |
64 | - $hasher = new CallbackValueHasher(function ($value) { |
|
64 | + $hasher = new CallbackValueHasher(function($value) { |
|
65 | 65 | return (is_int($value) || is_string($value) ? $value : serialize($value)); |
66 | 66 | }); |
67 | 67 | } |
68 | 68 | elseif ($hasher === 1) { |
69 | - $hasher = new CallbackValueHasher(function ($value) { |
|
69 | + $hasher = new CallbackValueHasher(function($value) { |
|
70 | 70 | return 1; |
71 | 71 | }); |
72 | 72 | } |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | } |
76 | 76 | |
77 | 77 | if ($comparator === null) { |
78 | - $comparator = new CallbackValueComparator(function ($a, $b) { |
|
78 | + $comparator = new CallbackValueComparator(function($a, $b) { |
|
79 | 79 | if (is_object($a) && is_object($b) && $a instanceof IComparable) { |
80 | 80 | return $a->equals($b); |
81 | 81 | } |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | } |
90 | 90 | |
91 | 91 | $hashTable = []; |
92 | - return new FilteredColumn($this, function ($value) use ($hasher, $comparator, &$hashTable) { |
|
92 | + return new FilteredColumn($this, function($value) use ($hasher, $comparator, &$hashTable) { |
|
93 | 93 | $h = $hasher->hash($value); |
94 | 94 | if (!isset($hashTable[$h])) { |
95 | 95 | $hashTable[$h] = [$value]; |
@@ -64,13 +64,11 @@ discard block |
||
64 | 64 | $hasher = new CallbackValueHasher(function ($value) { |
65 | 65 | return (is_int($value) || is_string($value) ? $value : serialize($value)); |
66 | 66 | }); |
67 | - } |
|
68 | - elseif ($hasher === 1) { |
|
67 | + } elseif ($hasher === 1) { |
|
69 | 68 | $hasher = new CallbackValueHasher(function ($value) { |
70 | 69 | return 1; |
71 | 70 | }); |
72 | - } |
|
73 | - elseif (!$hasher instanceof IValueHasher) { |
|
71 | + } elseif (!$hasher instanceof IValueHasher) { |
|
74 | 72 | $hasher = new CallbackValueHasher($hasher); |
75 | 73 | } |
76 | 74 | |
@@ -78,13 +76,11 @@ discard block |
||
78 | 76 | $comparator = new CallbackValueComparator(function ($a, $b) { |
79 | 77 | if (is_object($a) && is_object($b) && $a instanceof IComparable) { |
80 | 78 | return $a->equals($b); |
81 | - } |
|
82 | - else { |
|
79 | + } else { |
|
83 | 80 | return ($a == $b); |
84 | 81 | } |
85 | 82 | }); |
86 | - } |
|
87 | - elseif (!$comparator instanceof IValueComparator) { |
|
83 | + } elseif (!$comparator instanceof IValueComparator) { |
|
88 | 84 | $comparator = new CallbackValueComparator($comparator); |
89 | 85 | } |
90 | 86 | |
@@ -94,8 +90,7 @@ discard block |
||
94 | 90 | if (!isset($hashTable[$h])) { |
95 | 91 | $hashTable[$h] = [$value]; |
96 | 92 | return true; |
97 | - } |
|
98 | - else { |
|
93 | + } else { |
|
99 | 94 | foreach ($hashTable[$h] as $v) { |
100 | 95 | if ($comparator->equal($value, $v)) { |
101 | 96 | return false; |
@@ -82,27 +82,21 @@ discard block |
||
82 | 82 | if (filter_var($colOffsetOrNameOrEvaluator, FILTER_VALIDATE_INT) !== false) { |
83 | 83 | if (isset($this->data[$colOffsetOrNameOrEvaluator])) { |
84 | 84 | return $this->data[$colOffsetOrNameOrEvaluator]; |
85 | - } |
|
86 | - else { |
|
85 | + } else { |
|
87 | 86 | throw new UndefinedColumnException("No column at offset $colOffsetOrNameOrEvaluator"); |
88 | 87 | } |
89 | - } |
|
90 | - else { |
|
88 | + } else { |
|
91 | 89 | if (isset($this->colNameMap[$colOffsetOrNameOrEvaluator])) { |
92 | 90 | return $this->data[$this->colNameMap[$colOffsetOrNameOrEvaluator]]; |
93 | - } |
|
94 | - else { |
|
91 | + } else { |
|
95 | 92 | throw new UndefinedColumnException("No column named $colOffsetOrNameOrEvaluator"); |
96 | 93 | } |
97 | 94 | } |
98 | - } |
|
99 | - elseif ($colOffsetOrNameOrEvaluator instanceof ITupleEvaluator) { |
|
95 | + } elseif ($colOffsetOrNameOrEvaluator instanceof ITupleEvaluator) { |
|
100 | 96 | return $colOffsetOrNameOrEvaluator->evaluate($this); |
101 | - } |
|
102 | - elseif ($colOffsetOrNameOrEvaluator instanceof \Closure) { |
|
97 | + } elseif ($colOffsetOrNameOrEvaluator instanceof \Closure) { |
|
103 | 98 | return call_user_func($colOffsetOrNameOrEvaluator, $this); |
104 | - } |
|
105 | - else { |
|
99 | + } else { |
|
106 | 100 | throw new \InvalidArgumentException('$colOffsetOrNameOrEvaluator'); |
107 | 101 | } |
108 | 102 | } |
@@ -120,8 +114,7 @@ discard block |
||
120 | 114 | { |
121 | 115 | if (isset($this->colNameMap[$name])) { |
122 | 116 | return $this->data[$this->colNameMap[$name]]; |
123 | - } |
|
124 | - else { |
|
117 | + } else { |
|
125 | 118 | return null; |
126 | 119 | } |
127 | 120 | } |
@@ -139,8 +132,7 @@ discard block |
||
139 | 132 | { |
140 | 133 | if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
141 | 134 | return array_key_exists($offset, $this->data); |
142 | - } |
|
143 | - else { |
|
135 | + } else { |
|
144 | 136 | return isset($this->colNameMap[$offset]); |
145 | 137 | } |
146 | 138 | } |
@@ -149,15 +141,13 @@ discard block |
||
149 | 141 | { |
150 | 142 | if (filter_var($offset, FILTER_VALIDATE_INT) !== false) { |
151 | 143 | $key = $offset; |
152 | - } |
|
153 | - else { |
|
144 | + } else { |
|
154 | 145 | $key = $this->colNameMap[$offset]; |
155 | 146 | } |
156 | 147 | |
157 | 148 | if (array_key_exists($key, $this->data)) { |
158 | 149 | return $this->data[$key]; |
159 | - } |
|
160 | - else { |
|
150 | + } else { |
|
161 | 151 | trigger_error("Undefined offset `$offset` for the tuple"); |
162 | 152 | return null; |
163 | 153 | } |
@@ -88,16 +88,13 @@ |
||
88 | 88 | if ($valueOffset >= 0) { |
89 | 89 | if ($valueOffset < $cnt) { |
90 | 90 | return $this->data[$valueOffset]; |
91 | - } |
|
92 | - else { |
|
91 | + } else { |
|
93 | 92 | throw new \OutOfBoundsException("The column does not have offset $valueOffset"); |
94 | 93 | } |
95 | - } |
|
96 | - else { |
|
94 | + } else { |
|
97 | 95 | if (-$valueOffset <= $cnt) { |
98 | 96 | return $this->data[$valueOffset + $cnt]; |
99 | - } |
|
100 | - else { |
|
97 | + } else { |
|
101 | 98 | throw new \OutOfBoundsException("The column does not have offset $valueOffset"); |
102 | 99 | } |
103 | 100 | } |
@@ -20,11 +20,9 @@ discard block |
||
20 | 20 | |
21 | 21 | if ($decider instanceof \Closure) { |
22 | 22 | $this->decider = new CallbackTupleFilter($decider); |
23 | - } |
|
24 | - elseif ($decider instanceof ITupleFilter) { |
|
23 | + } elseif ($decider instanceof ITupleFilter) { |
|
25 | 24 | $this->decider = $decider; |
26 | - } |
|
27 | - else { |
|
25 | + } else { |
|
28 | 26 | throw new \InvalidArgumentException('$decider'); |
29 | 27 | } |
30 | 28 | } |
@@ -60,8 +58,7 @@ discard block |
||
60 | 58 | $effectiveOffset = ($offset >= 0 ? $offset : $this->count() + $offset); |
61 | 59 | if (isset($this->acceptMap[$effectiveOffset])) { |
62 | 60 | return parent::tuple($this->acceptMap[$effectiveOffset]); |
63 | - } |
|
64 | - else { |
|
61 | + } else { |
|
65 | 62 | throw new \OutOfBoundsException("Tuple offset $offset out of the relation bounds"); |
66 | 63 | } |
67 | 64 | } |
@@ -5,11 +5,11 @@ |
||
5 | 5 | |
6 | 6 | interface ITupleEvaluator |
7 | 7 | { |
8 | - /** |
|
9 | - * Computes a value for a given tuple. |
|
10 | - * |
|
11 | - * @param ITuple $tuple |
|
12 | - * @return mixed |
|
13 | - */ |
|
14 | - function evaluate(ITuple $tuple); |
|
8 | + /** |
|
9 | + * Computes a value for a given tuple. |
|
10 | + * |
|
11 | + * @param ITuple $tuple |
|
12 | + * @return mixed |
|
13 | + */ |
|
14 | + function evaluate(ITuple $tuple); |
|
15 | 15 | } |
@@ -20,11 +20,9 @@ discard block |
||
20 | 20 | if ($orig[0] == '/') { |
21 | 21 | $pcres[] = $orig; |
22 | 22 | $repls[] = $new; |
23 | - } |
|
24 | - elseif (is_int($orig) || filter_var((string)$orig, FILTER_VALIDATE_INT)) { |
|
23 | + } elseif (is_int($orig) || filter_var((string)$orig, FILTER_VALIDATE_INT)) { |
|
25 | 24 | $byOffset[$orig] = $new; |
26 | - } |
|
27 | - else { |
|
25 | + } else { |
|
28 | 26 | $pcres[] = self::simpleMacroPatternToPcre($orig); |
29 | 27 | $repls[] = self::simpleMacroReplacementToPcre($new); |
30 | 28 | } |
@@ -35,8 +33,7 @@ discard block |
||
35 | 33 | $origName = $col->getName(); |
36 | 34 | if (isset($byOffset[$colOffset])) { |
37 | 35 | $newName = $byOffset[$colOffset]; |
38 | - } |
|
39 | - else { |
|
36 | + } else { |
|
40 | 37 | $newName = $origName; |
41 | 38 | foreach ($pcres as $i => $pcre) { |
42 | 39 | $newName = preg_replace($pcre, $repls[$i], (string)$origName, -1, $replaced); |