-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBitMap.php
More file actions
154 lines (122 loc) · 2.63 KB
/
BitMap.php
File metadata and controls
154 lines (122 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
class BitMap implements \IteratorAggregate, \ArrayAccess, \Serializable, \Countable {
protected $value = 0;
protected $indexes = array();
function __construct($v = 0) {
$this->value = $v;
}
function __toString() {
return (string)$this->value;
}
public function getValue() {
return $this->value;
}
function offset($key) {
if (is_numeric($key)) {
return pow(2, $key) ;
} elseif (isset($this->indexes[$key])) {
return pow(2, $this->indexes[$key]);
}
return 0;
}
function isTrue($offset) {
return (boolean)($offset === ($this->value & $offset));
}
function toggle($offset) {
$this->value = $this->value ^ $offset;
return $this;
}
function change($offset, $value = true) {
if ($this->isTrue($offset) !== (boolean)$value) {
$this->toggle($offset);
}
return $this;
}
function set($key, $value = true) {
$this->change($this->offset($key), $value);
return $this;
}
function get($key) {
return $this->isTrue($this->offset($key));
}
/**
properties
*/
public function __get($key) {
if (isset($this[$key])) {
return $this[$key];
}
throw new OutOfBoundsException("$key is not a valid bit index");
}
public function __set($key, $value) {
$this[$key] = $value;
}
/**
arrayaccess
*/
public function offsetGet($key){
return $this->isTrue($this->offset($key));
}
public function offsetSet($key, $value){
$this->change($this->offset($key), (boolean)$value);
}
public function offsetExists($key) {
return (is_numeric($key) || isset($this->indexes[$key]));
}
public function offsetUnset($key){
$this->change($this->offset($key), false);
}
/**
Iterator
*/
// protected $iteration_position;
// function rewind() {
// $this->iteration_position = 0;
// }
//
// function current() {
// $indexes = array_values($this->indexes);
// return $this->isTrue($indexes[$this->iteration_position]);
// }
//
// function key() {
// $indexes = array_keys($this->indexes);
// return $indexes[$this->iteration_position];
// }
//
// function next() {
// $this->iteration_position++;
// }
//
// function valid() {
// return $this->iteration_position < count($this->indexes);
// }
public function getIterator() {
$result = array();
foreach ($this->indexes as $key=>$index) {
$result[$key] = $this->isTrue($index);
}
return new ArrayIterator($result);
}
/**
Serializable
*/
public function serialize() {
return $this->value;
}
public function unserialize($data) {
$this->value = (int)$data;
}
/**
Countable
*/
function count() {
return count($this->indexes);
}
}
/*
$o = new BitMap();
$o[0] = true;
$o[4] = true;
echo $o; // 17
*/