1+ <?php
2+ /**
3+ * MIT License
4+ *
5+ * Copyright (c) 2018 Dogan Ucar
6+ *
7+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8+ * of this software and associated documentation files (the "Software"), to deal
9+ * in the Software without restriction, including without limitation the rights
10+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+ * copies of the Software, and to permit persons to whom the Software is
12+ * furnished to do so, subject to the following conditions:
13+ *
14+ * The above copyright notice and this permission notice shall be included in all
15+ * copies or substantial portions of the Software.
16+ *
17+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+ * SOFTWARE.
24+ */
25+
26+ namespace doganoo \PHPAlgorithms \Datastructure \Vector \BitVector ;
27+
28+
29+ use doganoo \PHPAlgorithms \Common \Exception \InvalidBitLengthException ;
30+ use doganoo \PHPAlgorithms \Common \Interfaces \IVector ;
31+
32+ /**
33+ * Class Vector
34+ *
35+ * @package doganoo\PHPAlgorithms\Datastructure\Maps
36+ */
37+ class IntegerVector implements IVector {
38+ /** @var array|null $array */
39+ private $ array = null ;
40+ /** @var int $bitLength */
41+ private $ bitLength = 32 ;
42+
43+ /**
44+ * Vector constructor.
45+ *
46+ * @param int $bitLength
47+ * @throws InvalidBitLengthException
48+ */
49+ public function __construct (int $ bitLength = 32 ) {
50+ $ this ->array = [];
51+ if (0 === $ bitLength ) throw new InvalidBitLengthException ();
52+ $ this ->bitLength = $ bitLength ;
53+ }
54+
55+
56+ /**
57+ * sets a value in the vector
58+ *
59+ * @param $value
60+ * @return bool
61+ */
62+ public function set ($ value ): bool {
63+ //TODO throw exception instead?
64+ if (\is_int ($ value )) {
65+ $ info = $ this ->getIndexAndMask ($ value );
66+ $ i = $ info ["i " ];
67+ $ k = $ info ["k " ];
68+
69+ $ flag = 1 ;
70+ $ flag = $ flag << $ k ;
71+
72+ if (isset ($ this ->array [$ i ])) {
73+ $ this ->array [$ i ] |= $ flag ;
74+ } else {
75+ $ this ->array [$ i ] = $ flag ;
76+ }
77+ return true ;
78+ }
79+ return false ;
80+ }
81+
82+ private function getIndexAndMask (int $ value ): array {
83+ $ i = $ value / $ this ->bitLength ;
84+ $ k = $ value % $ this ->bitLength ;
85+
86+ $ info = [];
87+ $ info ["i " ] = $ i ;
88+ $ info ["k " ] = $ k ;
89+ return $ info ;
90+ }
91+
92+ /**
93+ * retrieves the value in the vector
94+ *
95+ * @param $value
96+ * @return mixed
97+ */
98+ public function get ($ value ) {
99+ //TODO throw exception instead?!
100+ if (\is_int ($ value )) {
101+ $ info = $ this ->getIndexAndMask ($ value );
102+ $ i = $ info ["i " ];
103+ $ k = $ info ["k " ];
104+
105+ $ flag = 1 ;
106+ $ flag = $ flag << $ k ;
107+
108+ if (isset ($ this ->array [$ i ])) return (($ this ->array [$ i ] & $ flag ) !== 0 );
109+ return false ;
110+ }
111+ return false ;
112+ }
113+
114+ /**
115+ * clears the value in the vector
116+ *
117+ * @param $value
118+ * @return bool
119+ */
120+ public function clear ($ value ): bool {
121+ //TODO throw exception insted?!
122+ if (\is_int ($ value )) {
123+ $ info = $ this ->getIndexAndMask ($ value );
124+ $ i = $ info ["i " ];
125+ $ k = $ info ["k " ];
126+
127+ $ flag = 1 ;
128+ $ flag = $ flag << $ k ;
129+ $ flag = !$ flag ;
130+
131+ if (isset ($ this ->array [$ i ])) {
132+ $ this ->array [$ i ] &= $ flag ;
133+ } else {
134+ $ this ->array [$ i ] = $ this ->array [$ i ] & $ flag ;
135+ }
136+ return true ;
137+ }
138+ return false ;
139+ }
140+
141+ /**
142+ * @param $object
143+ * @return int
144+ */
145+ public function compareTo ($ object ): int {
146+ if ($ object instanceof IntegerVector) {
147+ if (\count (\array_diff ($ this ->array , $ object ->array )) === 0 ) return 0 ;
148+ if (\count ($ this ->array ) < \count ($ object ->array )) return -1 ;
149+ if (\count ($ this ->array ) > \count ($ object ->array )) return 1 ;
150+ }
151+ return -1 ;
152+ }
153+
154+ /**
155+ * Specify data which should be serialized to JSON
156+ *
157+ * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
158+ * @return mixed data which can be serialized by <b>json_encode</b>,
159+ * which is a value of any type other than a resource.
160+ * @since 5.4.0
161+ */
162+ public function jsonSerialize () {
163+ return [
164+ "array " => $ this ->array
165+ , "bit_length " => $ this ->bitLength ,
166+ ];
167+ }
168+ }
0 commit comments