Skip to content

Commit 857f0e5

Browse files
committed
add src class reverse logic
1 parent 576f107 commit 857f0e5

File tree

1 file changed

+78
-4
lines changed

1 file changed

+78
-4
lines changed

algorithm/Sort/SRC.class.php

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,78 @@ class Sort_SRC implements
66
ArrayAccess,
77
Countable {
88

9+
/**
10+
* 默认元素尺寸
11+
*/
912
const SIZE_ELEMENT = 4;
13+
14+
/**
15+
* 默认值位置
16+
*/
1017
const POS_VALUE = 0;
18+
19+
/**
20+
* 默认值大小
21+
*/
1122
const SIZE_VALUE = 4;
23+
24+
/**
25+
* 默认值类型
26+
*/
1227
const TYPE_VALUE = 'l';
1328

29+
/**
30+
* 源数据
31+
*/
1432
private $_src = '';
1533

34+
/**
35+
* 元素尺寸
36+
*/
1637
private $_sizeElement = self::SIZE_ELEMENT;
1738

39+
/**
40+
* 值位置
41+
*/
1842
private $_posValue = self::POS_VALUE;
1943

44+
/**
45+
* 值尺寸
46+
*/
2047
private $_sizeValue = self::SIZE_VALUE;
2148

49+
/**
50+
* 值类型
51+
*/
2252
private $_typeValue = self::TYPE_VALUE;
2353

54+
/**
55+
* 方向
56+
*/
57+
private $_derict = 1;
58+
59+
/**
60+
* 构造函数
61+
*
62+
* @param string $src 源数据
63+
* @param array $options 配置
64+
*/
2465
public function __construct ($src = '', $options = array()) {
2566

26-
$this->_src = $src;
67+
$this->_src = $src;
2768
$this->_options = $options;
2869
}
2970

71+
public function reverse () {
72+
73+
$this->_derict *= -1;
74+
}
75+
76+
public function clean () {
77+
78+
$this->_src = '';
79+
}
80+
3081
public function offsetExists ($offset) {
3182

3283
return strlen($this->_getElement($offset)) == $this->_sizeElement;
@@ -50,22 +101,45 @@ public function offsetSet ($offset, $value) {
50101

51102
public function offsetUnset ($offset) {
52103

53-
return ;
104+
$this->_src = $this->_derict > 0
105+
? substr_replace($this->_src, '', $offset * $this->_sizeElement * $this->_derict, $this->_sizeElement)
106+
: substr_replace($this->_src, '', ($offset + 1) * $this->_sizeElement * $this->_derict, $this->_sizeElement);
54107
}
55108

56109
public function count () {
57110

58111
return (int) floor(strlen($this->_src) / $this->_sizeElement);
59112
}
60113

114+
public function __toString () {
115+
116+
$str = '[';
117+
118+
for ($offset = 0; $offset < $this->count(); $offset ++) {
119+
120+
$str .= 0 == $offset ? '' : ',';
121+
$str .= $this->offsetGet($offset);
122+
}
123+
124+
return $str . ']';
125+
}
126+
61127
private function _setElement ($offset, $element) {
62128

63-
$this->_replace($offset * $this->_sizeElement, $element);
129+
if ($this->_derict > 0) {
130+
131+
$this->_replace($offset * $this->_sizeElement * $this->_derict, $element);
132+
} else {
133+
134+
$this->_replace(($offset + 1) * $this->_sizeElement * $this->_derict, $element);
135+
}
64136
}
65137

66138
private function _getElement ($offset) {
67139

68-
return substr($this->_src, $offset * $this->_sizeElement, $this->_sizeElement);
140+
return $this->_derict > 0
141+
? substr($this->_src, $offset * $this->_sizeElement * $this->_derict, $this->_sizeElement)
142+
: substr($this->_src, ($offset + 1) * $this->_sizeElement * $this->_derict, $this->_sizeElement);
69143
}
70144

71145
private function _getValue ($element) {

0 commit comments

Comments
 (0)