ArrayList.class.php 5.7 KB

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