forked from sandersyao/PHP-Data-Structure
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSRC.class.php
More file actions
230 lines (165 loc) · 5.2 KB
/
Copy pathSRC.class.php
File metadata and controls
230 lines (165 loc) · 5.2 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
/**
* 源
*/
class Sort_SRC implements
ArrayAccess,
Countable {
/**
* 默认元素尺寸
*/
const SIZE_ELEMENT = 4;
/**
* 默认值位置
*/
const POS_VALUE = 0;
/**
* 默认值大小
*/
const SIZE_VALUE = 4;
/**
* 默认值类型
*/
const TYPE_VALUE = 'l';
/**
* 源数据
*/
private $_src = '';
/**
* 元素尺寸
*/
private $_sizeElement = self::SIZE_ELEMENT;
/**
* 值位置
*/
private $_posValue = self::POS_VALUE;
/**
* 值尺寸
*/
private $_sizeValue = self::SIZE_VALUE;
/**
* 值类型
*/
private $_typeValue = self::TYPE_VALUE;
/**
* 方向
*/
private $_derict = 1;
/**
* 构造函数
*
* @param string $src 源数据
* @param array $options 配置
*/
public function __construct ($src = '', $options = array()) {
$this->_src = $src;
$this->_options = $options;
}
/**
* 逆序
*/
public function reverse () {
$this->_derict *= -1;
}
/**
* 清空
*/
public function clean () {
$this->_src = '';
}
/**
* 交换
*
* @param int $offsetA 第一个下标
* @param int $offsetB 第二个下标
*/
public function swap ($offsetA, $offsetB) {
if ($offsetA == $offsetB) {
return ;
}
$valueA = $this->offsetGet($offsetA);
$valueB = $this->offsetGet($offsetB);
$valueA ^= $valueB;
$valueB ^= $valueA;
$valueA ^= $valueB;
$this->offsetSet($offsetA, $valueA);
$this->offsetSet($offsetB, $valueB);
}
public function offsetExists ($offset) {
return strlen($this->_getElement($offset)) == $this->_sizeElement;
}
public function offsetGet ($offset) {
$element = $this->_getElement($offset);
return $this->_getValue($element);
}
public function offsetSet ($offset, $value) {
$offset = NULL === $offset ? $this->count() : $offset;
$element = $this->_getElement($offset);
$element = false === $element ? str_repeat(' ', $this->_sizeElement) : $element;
$element = $this->_setValue($element, $value);
$this->_setElement($offset, $element);
}
public function offsetUnset ($offset) {
$this->_src = $this->_derict > 0
? substr_replace($this->_src, '', $offset * $this->_sizeElement * $this->_derict, $this->_sizeElement)
: substr_replace($this->_src, '', ($offset + 1) * $this->_sizeElement * $this->_derict, $this->_sizeElement);
}
public function count () {
return (int) floor(strlen($this->_src) / $this->_sizeElement);
}
public function __toString () {
$str = '[';
for ($offset = 0; $offset < $this->count(); $offset ++) {
$str .= 0 == $offset ? '' : ',';
$str .= $this->offsetGet($offset);
}
return $str . ']';
}
private function _setElement ($offset, $element) {
if ($this->_derict > 0) {
$this->_replace($offset * $this->_sizeElement * $this->_derict, $element);
} else {
$this->_replace(($offset + 1) * $this->_sizeElement * $this->_derict, $element);
}
}
private function _getElement ($offset) {
return $this->_derict > 0
? substr($this->_src, $offset * $this->_sizeElement * $this->_derict, $this->_sizeElement)
: substr($this->_src, ($offset + 1) * $this->_sizeElement * $this->_derict, $this->_sizeElement);
}
private function _getValue ($element) {
@list($null, $value) = unpack($this->_typeValue, substr($element, $this->_posValue, $this->_sizeValue));
return $value;
}
private function _setValue ($element, $value) {
$pack = pack($this->_typeValue, $value);
return substr_replace($element, $pack, $this->_posValue, $this->_sizeValue);
}
private function _setOptions ($options) {
if (isset($options['size_element'])) {
$this->_sizeElement = $options['size_element'];
}
if (isset($options['pos_value'])) {
$this->_posValue = $options['pos_value'];
}
if (isset($options['size_value'])) {
$this->_sizeValue = $options['size_value'];
}
if (isset($options['type_Value'])) {
$this->_typeValue = $options['type_Value'];
}
}
private function _replace ($position, $replacement) {
$posPrev = $position - 1;
while ($posPrev > 0 && false === isset($this->_src[$posPrev])) {
$this->_src[strlen($this->_src)] = ' ';
}
for ($loop = 0, $offset = $position;$loop < strlen($replacement);$loop ++, $offset ++) {
if (isset($this->_src[$offset])) {
$this->_src[$offset] = $replacement[$loop];
} else {
$this->_src .= $replacement[$loop];
}
}
}
}