利用php數組實現Bitset位處理模組功能

來源:互聯網
上載者:User

標籤: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位處理模組功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.