/Backend/ThinkPHP/Lib/ORG/Util/ArrayList.class.php

https://github.com/qhwang0427/backend_php · PHP · 351 lines · 114 code · 27 blank · 210 comment · 5 complexity · 31f535bba66f6588b6f6dbc96ebda3bd 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. // $Id$
  12. /**
  13. +------------------------------------------------------------------------------
  14. * ArrayList实现类
  15. +------------------------------------------------------------------------------
  16. * @category Think
  17. * @package Think
  18. * @subpackage Util
  19. * @author liu21st <liu21st@gmail.com>
  20. * @version $Id$
  21. +------------------------------------------------------------------------------
  22. */
  23. class ArrayList extends Think implements IteratorAggregate
  24. {//类定义开始
  25. /**
  26. +----------------------------------------------------------
  27. * 集合元素
  28. +----------------------------------------------------------
  29. * @var array
  30. * @access protected
  31. +----------------------------------------------------------
  32. */
  33. protected $_elements = array();
  34. /**
  35. +----------------------------------------------------------
  36. * 架构函数
  37. +----------------------------------------------------------
  38. * @access public
  39. +----------------------------------------------------------
  40. * @param string $elements 初始化数组元素
  41. +----------------------------------------------------------
  42. */
  43. public function __construct($elements = array())
  44. {
  45. if (!empty($elements)) {
  46. $this->_elements = $elements;
  47. }
  48. }
  49. /**
  50. +----------------------------------------------------------
  51. * 若要获得迭代因子,通过getIterator方法实现
  52. +----------------------------------------------------------
  53. * @access public
  54. +----------------------------------------------------------
  55. * @return ArrayObject
  56. +----------------------------------------------------------
  57. */
  58. public function getIterator()
  59. {
  60. return new ArrayObject($this->_elements);
  61. }
  62. /**
  63. +----------------------------------------------------------
  64. * 增加元素
  65. +----------------------------------------------------------
  66. * @access public
  67. +----------------------------------------------------------
  68. * @param mixed $element 要添加的元素
  69. +----------------------------------------------------------
  70. * @return boolen
  71. +----------------------------------------------------------
  72. */
  73. public function add($element)
  74. {
  75. return (array_push($this->_elements, $element)) ? true : false;
  76. }
  77. //
  78. public function unshift($element)
  79. {
  80. return (array_unshift($this->_elements,$element))?true : false;
  81. }
  82. //
  83. public function pop()
  84. {
  85. return array_pop($this->_elements);
  86. }
  87. /**
  88. +----------------------------------------------------------
  89. * 增加元素列表
  90. +----------------------------------------------------------
  91. * @access public
  92. +----------------------------------------------------------
  93. * @param ArrayList $list 元素列表
  94. +----------------------------------------------------------
  95. * @return boolen
  96. +----------------------------------------------------------
  97. * @throws ThinkExecption
  98. +----------------------------------------------------------
  99. */
  100. public function addAll($list)
  101. {
  102. $before = $this->size();
  103. foreach( $list as $element) {
  104. $this->add($element);
  105. }
  106. $after = $this->size();
  107. return ($before < $after);
  108. }
  109. /**
  110. +----------------------------------------------------------
  111. * 清除所有元素
  112. +----------------------------------------------------------
  113. * @access public
  114. +----------------------------------------------------------
  115. */
  116. public function clear()
  117. {
  118. $this->_elements = array();
  119. }
  120. /**
  121. +----------------------------------------------------------
  122. * 是否包含某个元素
  123. +----------------------------------------------------------
  124. * @access public
  125. +----------------------------------------------------------
  126. * @param mixed $element 查找元素
  127. +----------------------------------------------------------
  128. * @return string
  129. +----------------------------------------------------------
  130. */
  131. public function contains($element)
  132. {
  133. return (array_search($element, $this->_elements) !== false );
  134. }
  135. /**
  136. +----------------------------------------------------------
  137. * 根据索引取得元素
  138. +----------------------------------------------------------
  139. * @access public
  140. +----------------------------------------------------------
  141. * @param integer $index 索引
  142. +----------------------------------------------------------
  143. * @return mixed
  144. +----------------------------------------------------------
  145. */
  146. public function get($index)
  147. {
  148. return $this->_elements[$index];
  149. }
  150. /**
  151. +----------------------------------------------------------
  152. * 查找匹配元素,并返回第一个元素所在位置
  153. * 注意 可能存在0的索引位置 因此要用===False来判断查找失败
  154. +----------------------------------------------------------
  155. * @access public
  156. +----------------------------------------------------------
  157. * @param mixed $element 查找元素
  158. +----------------------------------------------------------
  159. * @return integer
  160. +----------------------------------------------------------
  161. * @throws ThinkExecption
  162. +----------------------------------------------------------
  163. */
  164. public function indexOf($element)
  165. {
  166. return array_search($element, $this->_elements);
  167. }
  168. /**
  169. +----------------------------------------------------------
  170. * 判断元素是否为空
  171. +----------------------------------------------------------
  172. * @access public
  173. +----------------------------------------------------------
  174. * @return boolen
  175. +----------------------------------------------------------
  176. */
  177. public function isEmpty()
  178. {
  179. return empty($this->_elements);
  180. }
  181. /**
  182. +----------------------------------------------------------
  183. * 最后一个匹配的元素位置
  184. +----------------------------------------------------------
  185. * @access public
  186. +----------------------------------------------------------
  187. * @param mixed $element 查找元素
  188. +----------------------------------------------------------
  189. * @return integer
  190. +----------------------------------------------------------
  191. */
  192. public function lastIndexOf($element)
  193. {
  194. for ($i = (count($this->_elements) - 1); $i > 0; $i--) {
  195. if ($element == $this->get($i)) { return $i; }
  196. }
  197. }
  198. public function toJson()
  199. {
  200. return json_encode($this->_elements);
  201. }
  202. /**
  203. +----------------------------------------------------------
  204. * 根据索引移除元素
  205. * 返回被移除的元素
  206. +----------------------------------------------------------
  207. * @access public
  208. +----------------------------------------------------------
  209. * @param integer $index 索引
  210. +----------------------------------------------------------
  211. * @return mixed
  212. +----------------------------------------------------------
  213. */
  214. public function remove($index)
  215. {
  216. $element = $this->get($index);
  217. if (!is_null($element)) { array_splice($this->_elements, $index, 1); }
  218. return $element;
  219. }
  220. /**
  221. +----------------------------------------------------------
  222. * 移出一定范围的数组列表
  223. +----------------------------------------------------------
  224. * @access public
  225. +----------------------------------------------------------
  226. * @param integer $offset 开始移除位置
  227. * @param integer $length 移除长度
  228. +----------------------------------------------------------
  229. */
  230. public function removeRange($offset , $length)
  231. {
  232. array_splice($this->_elements, $offset , $length);
  233. }
  234. /**
  235. +----------------------------------------------------------
  236. * 移出重复的值
  237. +----------------------------------------------------------
  238. * @access public
  239. +----------------------------------------------------------
  240. */
  241. public function unique() {
  242. $this->_elements = array_unique($this->_elements);
  243. }
  244. /**
  245. +----------------------------------------------------------
  246. * 取出一定范围的数组列表
  247. +----------------------------------------------------------
  248. * @access public
  249. +----------------------------------------------------------
  250. * @param integer $offset 开始位置
  251. * @param integer $length 长度
  252. +----------------------------------------------------------
  253. */
  254. public function range($offset,$length=null)
  255. {
  256. return array_slice($this->_elements,$offset,$length);
  257. }
  258. /**
  259. +----------------------------------------------------------
  260. * 设置列表元素
  261. * 返回修改之前的值
  262. +----------------------------------------------------------
  263. * @access public
  264. +----------------------------------------------------------
  265. * @param integer $index 索引
  266. * @param mixed $element 元素
  267. +----------------------------------------------------------
  268. * @return mixed
  269. +----------------------------------------------------------
  270. */
  271. public function set($index, $element)
  272. {
  273. $previous = $this->get($index);
  274. $this->_elements[$index] = $element;
  275. return $previous;
  276. }
  277. /**
  278. +----------------------------------------------------------
  279. * 获取列表长度
  280. +----------------------------------------------------------
  281. * @access public
  282. +----------------------------------------------------------
  283. * @return integer
  284. +----------------------------------------------------------
  285. */
  286. public function size()
  287. {
  288. return count($this->_elements);
  289. }
  290. /**
  291. +----------------------------------------------------------
  292. * 转换成数组
  293. +----------------------------------------------------------
  294. * @access public
  295. +----------------------------------------------------------
  296. * @return array
  297. +----------------------------------------------------------
  298. */
  299. public function toArray()
  300. {
  301. return $this->_elements;
  302. }
  303. // 列表排序
  304. public function ksort()
  305. {
  306. ksort($this->_elements);
  307. }
  308. // 列表排序
  309. public function asort()
  310. {
  311. asort($this->_elements);
  312. }
  313. // 逆向排序
  314. public function rsort()
  315. {
  316. rsort($this->_elements);
  317. }
  318. // 自然排序
  319. public function natsort()
  320. {
  321. natsort($this->_elements);
  322. }
  323. }//类定义结束
  324. ?>