PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/xianpipa/ThinkPHP/Library/Org/Util/ArrayList.class.php

https://gitlab.com/fangfangchen/xianpipa
PHP | 240 lines | 90 code | 26 blank | 124 comment | 5 complexity | bad64d74e62aaded238d640ce64693e7 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Org\Util;
  12. /**
  13. * ArrayList实现类
  14. * @category Think
  15. * @package Think
  16. * @subpackage Util
  17. * @author liu21st <liu21st@gmail.com>
  18. */
  19. class ArrayList implements \IteratorAggregate {
  20. /**
  21. * 集合元素
  22. * @var array
  23. * @access protected
  24. */
  25. protected $_elements = array();
  26. /**
  27. * 架构函数
  28. * @access public
  29. * @param string $elements 初始化数组元素
  30. */
  31. public function __construct($elements = array()) {
  32. if (!empty($elements)) {
  33. $this->_elements = $elements;
  34. }
  35. }
  36. /**
  37. * 若要获得迭代因子,通过getIterator方法实现
  38. * @access public
  39. * @return ArrayObject
  40. */
  41. public function getIterator() {
  42. return new ArrayObject($this->_elements);
  43. }
  44. /**
  45. * 增加元素
  46. * @access public
  47. * @param mixed $element 要添加的元素
  48. * @return boolean
  49. */
  50. public function add($element) {
  51. return (array_push($this->_elements, $element)) ? true : false;
  52. }
  53. //
  54. public function unshift($element) {
  55. return (array_unshift($this->_elements,$element))?true : false;
  56. }
  57. //
  58. public function pop() {
  59. return array_pop($this->_elements);
  60. }
  61. /**
  62. * 增加元素列表
  63. * @access public
  64. * @param ArrayList $list 元素列表
  65. * @return boolean
  66. */
  67. public function addAll($list) {
  68. $before = $this->size();
  69. foreach( $list as $element) {
  70. $this->add($element);
  71. }
  72. $after = $this->size();
  73. return ($before < $after);
  74. }
  75. /**
  76. * 清除所有元素
  77. * @access public
  78. */
  79. public function clear() {
  80. $this->_elements = array();
  81. }
  82. /**
  83. * 是否包含某个元素
  84. * @access public
  85. * @param mixed $element 查找元素
  86. * @return string
  87. */
  88. public function contains($element) {
  89. return (array_search($element, $this->_elements) !== false );
  90. }
  91. /**
  92. * 根据索引取得元素
  93. * @access public
  94. * @param integer $index 索引
  95. * @return mixed
  96. */
  97. public function get($index) {
  98. return $this->_elements[$index];
  99. }
  100. /**
  101. * 查找匹配元素,并返回第一个元素所在位置
  102. * 注意 可能存在0的索引位置 因此要用===False来判断查找失败
  103. * @access public
  104. * @param mixed $element 查找元素
  105. * @return integer
  106. */
  107. public function indexOf($element) {
  108. return array_search($element, $this->_elements);
  109. }
  110. /**
  111. * 判断元素是否为空
  112. * @access public
  113. * @return boolean
  114. */
  115. public function isEmpty() {
  116. return empty($this->_elements);
  117. }
  118. /**
  119. * 最后一个匹配的元素位置
  120. * @access public
  121. * @param mixed $element 查找元素
  122. * @return integer
  123. */
  124. public function lastIndexOf($element) {
  125. for ($i = (count($this->_elements) - 1); $i > 0; $i--) {
  126. if ($element == $this->get($i)) { return $i; }
  127. }
  128. }
  129. public function toJson() {
  130. return json_encode($this->_elements);
  131. }
  132. /**
  133. * 根据索引移除元素
  134. * 返回被移除的元素
  135. * @access public
  136. * @param integer $index 索引
  137. * @return mixed
  138. */
  139. public function remove($index) {
  140. $element = $this->get($index);
  141. if (!is_null($element)) { array_splice($this->_elements, $index, 1); }
  142. return $element;
  143. }
  144. /**
  145. * 移出一定范围的数组列表
  146. * @access public
  147. * @param integer $offset 开始移除位置
  148. * @param integer $length 移除长度
  149. */
  150. public function removeRange($offset , $length) {
  151. array_splice($this->_elements, $offset , $length);
  152. }
  153. /**
  154. * 移出重复的值
  155. * @access public
  156. */
  157. public function unique() {
  158. $this->_elements = array_unique($this->_elements);
  159. }
  160. /**
  161. * 取出一定范围的数组列表
  162. * @access public
  163. * @param integer $offset 开始位置
  164. * @param integer $length 长度
  165. */
  166. public function range($offset,$length=null) {
  167. return array_slice($this->_elements,$offset,$length);
  168. }
  169. /**
  170. * 设置列表元素
  171. * 返回修改之前的值
  172. * @access public
  173. * @param integer $index 索引
  174. * @param mixed $element 元素
  175. * @return mixed
  176. */
  177. public function set($index, $element) {
  178. $previous = $this->get($index);
  179. $this->_elements[$index] = $element;
  180. return $previous;
  181. }
  182. /**
  183. * 获取列表长度
  184. * @access public
  185. * @return integer
  186. */
  187. public function size() {
  188. return count($this->_elements);
  189. }
  190. /**
  191. * 转换成数组
  192. * @access public
  193. * @return array
  194. */
  195. public function toArray() {
  196. return $this->_elements;
  197. }
  198. // 列表排序
  199. public function ksort() {
  200. ksort($this->_elements);
  201. }
  202. // 列表排序
  203. public function asort() {
  204. asort($this->_elements);
  205. }
  206. // 逆向排序
  207. public function rsort() {
  208. rsort($this->_elements);
  209. }
  210. // 自然排序
  211. public function natsort() {
  212. natsort($this->_elements);
  213. }
  214. }