PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/pyhp/List.php

https://github.com/pipifuyj/global
PHP | 93 lines | 93 code | 0 blank | 0 comment | 15 complexity | c1fc6e198c46badbd0f6b261685442a0 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.1
  1. <?php
  2. class PyList{
  3. public $_List;
  4. public $len;
  5. public function PyList(){
  6. $this->_List=func_get_args();
  7. $this->len=func_num_args();
  8. }
  9. public function append(){
  10. $this->extend(func_get_args());
  11. }
  12. public function count($value){
  13. $count=0;
  14. for($i=0;$i<$this->len;$i++)if($this->_List[$i]==$value)$count++;
  15. return $count;
  16. }
  17. public function extend($seq){
  18. array_unshift($seq,$this->len);
  19. call_user_func_array(array($this,"insert"),$seq);
  20. }
  21. public function get($index){
  22. if($this->isIndex($index))return $this->_List[$index];
  23. else throw new Exception("IndexError");
  24. }
  25. public function index($value,$from=0,$to=null){
  26. if($to===null)$to=$this->len;
  27. for($i=$from;$i<$to;$i++)if($this->_List[$i]==$value)return $i;
  28. throw new Exception("ValueError");
  29. }
  30. public function insert(){
  31. $argc=func_num_args();
  32. $argv=func_get_args();
  33. $index=$argv[0];
  34. $len=$argc-1;
  35. for($i=$this->len-1+$len,$ii=$index-1+$len;$i>$ii;$i--)$this->_List[$i]=$this->_List[$i-$len];
  36. for($i=$index,$ii=$index+$len;$i<$ii;$i++)$this->_List[$i]=$argv[$i-$index+1];
  37. $this->len+=$len;
  38. }
  39. public function isIndex($index){
  40. return is_int($index)&&$index>=0&&$index<$this->len;
  41. }
  42. public function pop($index=null){
  43. if($index===null)$index=$this->len;
  44. $value=$this->_List[$index];
  45. for($i=$index;$i<$this->len;$i++)$this->_List[$i]=$this->_List[$i+1];
  46. $this->len--;
  47. return $value;
  48. }
  49. public function remove($value){
  50. $this->pop($this->index($value));
  51. }
  52. public function reverse(){
  53. for($i=0,$ii=$this->len-1;$i<$ii;$i++,$ii--){
  54. $t=$this->_List[$i];
  55. $this->_List[$i]=$this->_List[$ii];
  56. $this->_List[$ii]=$t;
  57. }
  58. }
  59. public function set($index,$value){
  60. if($this->isIndex($index))$this->_List[$index]=$value;
  61. else throw new Exception("IndexError");
  62. }
  63. public function slice($from,$to=null){
  64. if($to===null)$to=$this->len;
  65. $this->len=$to-$from;
  66. for($i=0;$i<$this->len;$i++)$this->_List[$i]=$this->_List[$i+$from];
  67. }
  68. }
  69. if(array_shift(get_included_files())==__file__){
  70. $list=new PyList(1,2,3,"a","b","c");
  71. print_r($list);
  72. $list->append(4,5,6);
  73. print_r($list);
  74. print_r($list->count(1));
  75. $list->extend(array(7,8,9));
  76. print_r($list);
  77. print_r($list->get(2));
  78. print_r($list->index(3));
  79. $list->insert(4,"h","e","l","l","o");
  80. print_r($list);
  81. print_r($list->isIndex(5));
  82. print_r($list->pop(6));
  83. print_r($list);
  84. $list->remove(4);
  85. print_r($list);
  86. $list->reverse();
  87. print_r($list);
  88. $list->set(8,8);
  89. print_r($list);
  90. $list->slice(6,8);
  91. print_r($list);
  92. }
  93. ?>