標籤:style blog http color os io ar 2014 div
接上篇,php的bitset模組介紹和安裝
利用php的bitset模組可以實現c++的bitset相關功能。但是,在生產環境下需要給線上伺服器安裝模組是很危險和麻煩的事,所以需要另闢蹊徑。
我用php的array數組類比了bitset的幾個主要方法的實現過程,其實就是利用Array的key類比記憶體位址,value模地址內容。php的Array還是超級方便的 :)
1 <?php 2 define(‘CHAR_BIT‘, 8); 3 /** 4 * bitset操作php實現 5 * @version:1.0 6 * @author:Kenny{Kenny.F<mailto:[email protected]>} 7 * @since:2014/05/21 8 */ 9 class Bitset {10 11 private $bitset_data = array();12 private $_len = 0;13 14 //分配位元組空間15 function &bitset_empty($bit=0)16 {17 if(!is_numeric($bit) || $bit<0)18 {19 echo "argument must be a positive integer";20 return False;21 }22 $this->_len = $bit;23 return $this->bitset_data;24 }25 26 //位元組位置$bit上的值置為127 public function bitset_incl(&$bitset_data=array(), $bit=0)28 {29 if (!is_numeric($bit) || $bit<0)30 {31 echo "Second argument must be a positive integer";32 return False;33 }34 35 $bitset_temp = isset($bitset_data[intval($bit/CHAR_BIT)]) ? $bitset_data[intval($bit/CHAR_BIT)] : 0;36 $bitset_data[intval($bit/CHAR_BIT)] = $bitset_temp | 1 << ($bit % CHAR_BIT);37 38 unset($bitset_data);39 }40 41 //判斷某個位置bit上的值是否為142 public function bitset_in($bitset_data=array(), $bit=0)43 {44 if (!is_array($bitset_data))45 {46 echo "first argument is not a array";47 return False;48 }49 50 if ($bit < 0)51 {52 return False;53 }54 if ($this->_len == 0)55 {56 return False;57 } elseif($bit >= $this->_len*CHAR_BIT){58 return False;59 } elseif ($bitset_data[intval($bit/CHAR_BIT)] & (1 << ($bit % CHAR_BIT))){60 return True;61 } else{62 return False;63 }64 }65 66 }
你可以查看我的github
利用php數組實現Bitset位處理模組功能