algorithm.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Created by PhpStorm.
  5. * User: stanley-king
  6. * Date: 16/6/24
  7. * Time: 上午1:18
  8. */
  9. function less($x,$y)
  10. {
  11. return $x < $y;
  12. }
  13. class full_set
  14. {
  15. private static $stInstance = null;
  16. public static function instance()
  17. {
  18. if(static::$stInstance == null) {
  19. static::$stInstance = new full_set();
  20. }
  21. }
  22. private function __clone()
  23. {
  24. }
  25. }
  26. class algorithm
  27. {
  28. static function bsearch($x, $list)
  29. {
  30. $left = 0;
  31. $right = count($list)-1;
  32. while ($left <= $right) {
  33. $mid = ($left + $right) >> 1;
  34. if ($list[$mid] == $x) {
  35. return $mid;
  36. } elseif ($list[$mid] > $x) {
  37. $right = $mid - 1;
  38. } elseif ($list[$mid] < $x) {
  39. $left = $mid + 1;
  40. }
  41. }
  42. return -1;
  43. }
  44. static function binary_search($sortarr, $val, $compare = "less")
  45. {
  46. $last = count($sortarr);
  47. $first = self::lower_bonud($sortarr,$val,$compare);
  48. return $first != $last && !$compare($val, $sortarr[$first]);
  49. }
  50. //返回位置标记了一个 >= value 的值。
  51. static function lower_bonud($container, $val,$compare = "less")
  52. {
  53. if(!is_array($container)) {
  54. return -1;
  55. }
  56. $len = count($container);
  57. $first = 0;
  58. while ($len != 0)
  59. {
  60. $l2 = intval($len / 2);
  61. $m = $first + $l2;
  62. if ($compare($container[$m],$val))
  63. {
  64. $first = ++$m;
  65. $len -= $l2 + 1;
  66. }
  67. else {
  68. $len = $l2;
  69. }
  70. }
  71. return $first;
  72. }
  73. //返回位置标记了一个 > value 的值。
  74. static function upper_bound($container, $val,$compare = "less")
  75. {
  76. if (!is_array($container)) {
  77. return -1;
  78. }
  79. $len = count($container);
  80. $first = 0;
  81. while ($len != 0)
  82. {
  83. $l2 = intval($len / 2);
  84. $m = $first + $l2;
  85. if ($compare($val,$container[$m])) {
  86. $len = $l2;
  87. } else {
  88. $first = ++$m;
  89. $len -= $l2 + 1;
  90. }
  91. }
  92. return $first;
  93. }
  94. static function array_insert(&$arr,$pos,$val)
  95. {
  96. if(is_array($val)) {
  97. $left = array_slice($arr, 0, $pos);
  98. $left[] = $val;
  99. $right = array_slice($arr, $pos);
  100. $arr = array_merge($left, $right);
  101. }
  102. else {
  103. array_splice($arr,$pos,0,$val);
  104. }
  105. }
  106. static function array_erase(&$arr,$pos)
  107. {
  108. return array_splice($arr,$pos,1);
  109. }
  110. static function set_intersection($ar1, $ar2)
  111. {
  112. if($ar1 === full_set::instance()) {
  113. return $ar2;
  114. }
  115. if($ar2 === full_set::instance()) {
  116. return $ar1;
  117. }
  118. $count1 = count($ar1);
  119. $count2 = count($ar2);
  120. $pos1 = 0;
  121. $pos2 = 0;
  122. $result = [];
  123. while ($pos1 != $count1 && $pos2 != $count2)
  124. {
  125. $val1 = $ar1[$pos1];
  126. $val2 = $ar2[$pos2];
  127. if ($val1 < $val2) {
  128. ++$pos1;
  129. }
  130. elseif($val1 == $val2) {
  131. $result[] = $val1;
  132. ++$pos1;
  133. ++$pos2;
  134. }
  135. else {
  136. ++$pos2;
  137. }
  138. }
  139. return $result;
  140. }
  141. private static function copy($src,$pos,&$result)
  142. {
  143. $count = count($src);
  144. for (;$pos < $count;++$pos) {
  145. $result[] = $src[$pos];
  146. }
  147. return $result;
  148. }
  149. static function set_union($ar1,$ar2)
  150. {
  151. $count1 = count($ar1);
  152. $count2 = count($ar2);
  153. $pos1 = 0;
  154. $pos2 = 0;
  155. $result = [];
  156. for (; $pos1 != $count1; )
  157. {
  158. if ($pos2 == $count2) {
  159. return self::copy($ar1, $pos1, $result);
  160. }
  161. if ($ar1[$pos1] > $ar2[$pos2])
  162. {
  163. $result[] = $ar2[$pos2];
  164. ++$pos2;
  165. }
  166. elseif($ar1[$pos1] == $ar2[$pos2]) {
  167. $result[] = $ar1[$pos1];
  168. ++$pos1;
  169. ++$pos2;
  170. }
  171. else
  172. {
  173. $result[] = $ar1[$pos1];
  174. ++$pos1;
  175. }
  176. }
  177. return self::copy($ar2,$pos2,$result);
  178. }
  179. public static function not_in($src,$avables)
  180. {
  181. if(empty($src)) return array();
  182. if(empty($avables)) return $src;
  183. $src = array_unique($src);
  184. $avables = array_unique($avables);
  185. sort($src);
  186. sort($avables);
  187. $result = [];
  188. foreach ($src as $val)
  189. {
  190. if(algorithm::binary_search($avables,$val) == false) {
  191. $result[] = $val;
  192. }
  193. }
  194. return $result;
  195. }
  196. }
  197. class uniquer
  198. {
  199. private $mContainer;
  200. private $mCompare;
  201. public function __construct($compare = 'less')
  202. {
  203. $this->mContainer = [];
  204. $this->mCompare = $compare;
  205. }
  206. public function add_value($val)
  207. {
  208. if(algorithm::binary_search($this->mContainer,$val,$this->mCompare) == false) {
  209. $pos = algorithm::lower_bonud($this->mContainer,$val,$this->mCompare);
  210. algorithm::array_insert($this->mContainer,$pos,$val);
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. public function add_values(array $datas)
  217. {
  218. if(!is_array($datas)) return false;
  219. foreach ($datas as $val) {
  220. $this->add_value($val);
  221. }
  222. return true;
  223. }
  224. public function remove_value($val)
  225. {
  226. if(algorithm::binary_search($this->mContainer,$val,$this->mCompare) != false) {
  227. $pos = algorithm::lower_bonud($this->mContainer,$val);
  228. algorithm::array_erase($this->mContainer,$pos);
  229. return true;
  230. } else {
  231. return false;
  232. }
  233. }
  234. public function values() {
  235. return $this->mContainer;
  236. }
  237. public function find($val)
  238. {
  239. return algorithm::binary_search($this->mContainer,$val,$this->mCompare);
  240. }
  241. public function range($lower,$high)
  242. {
  243. if(empty($lower)) {
  244. $pos_low = 0;
  245. }
  246. else {
  247. $pos_low = algorithm::lower_bonud($this->mContainer,$lower,$this->mCompare);
  248. }
  249. if(empty($high)) {
  250. $pos_high = count($this->mContainer);
  251. }
  252. else {
  253. $pos_high = algorithm::lower_bonud($this->mContainer,$high,$this->mCompare);
  254. }
  255. if($pos_low < 0) return [];
  256. return array_slice($this->mContainer, $pos_low, $pos_high - $pos_low);
  257. }
  258. }
  259. class array_helper
  260. {
  261. public static function mapper($items,callable $func)
  262. {
  263. $result = [];
  264. foreach ($items as $item)
  265. {
  266. $key = $func($item);
  267. if($key === false) continue;
  268. $result[$key] = $item;
  269. }
  270. return $result;
  271. }
  272. }