{"action":"created","issue":{"url":"https://api.github.com/repos/ocaml-batteries-team/batteries-included/issues/663","repository_url":"https://api.github.com/repos/ocaml-batteries-team/batteries-included","labels_url":"https://api.github.com/repos/ocaml-batteries-team/batteries-included/issues/663/labels{/name}","comments_url":"https://api.github.com/repos/ocaml-batteries-team/batteries-included/issues/663/comments","events_url":"https://api.github.com/repos/ocaml-batteries-team/batteries-included/issues/663/events","html_url":"https://github.com/ocaml-batteries-team/batteries-included/issues/663","id":133220194,"number":663,"title":"Improve BitSet.count_array","user":{"login":"Chimrod","id":555440,"avatar_url":"https://avatars.githubusercontent.com/u/555440?v=3","gravatar_id":"","url":"https://api.github.com/users/Chimrod","html_url":"https://github.com/Chimrod","followers_url":"https://api.github.com/users/Chimrod/followers","following_url":"https://api.github.com/users/Chimrod/following{/other_user}","gists_url":"https://api.github.com/users/Chimrod/gists{/gist_id}","starred_url":"https://api.github.com/users/Chimrod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Chimrod/subscriptions","organizations_url":"https://api.github.com/users/Chimrod/orgs","repos_url":"https://api.github.com/users/Chimrod/repos","events_url":"https://api.github.com/users/Chimrod/events{/privacy}","received_events_url":"https://api.github.com/users/Chimrod/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":8,"created_at":"2016-02-12T11:43:37Z","updated_at":"2016-02-12T16:00:52Z","closed_at":"2016-02-12T15:02:15Z","body":"The function BitSet.count_array use a recursion to sum each byte in the char. This is as efficient as an iteration over each of them.\r\n\r\nThis code (inspired by H. Warren's Hackers's deligth) only use bit operation, without jump :\r\n\r\n```ocaml\r\nlet count_array =\r\n  let count_bits i =\r\n  let mask1 = 0b01010101\r\n  and mask2 = 0b00110011\r\n  and mask3 = 0b00001111\r\n  in\r\n    let s1 = (i land mask1) + ((i lsr 1) land mask1) in\r\n    let s2 = (s1 land mask2) + ((s1 lsr 2) land mask2) in\r\n    (s2 land mask3) + ((s2 lsr 4) land mask3)\r\n    in\r\n  Array.init 256 count_bits\r\n```\r\n\r\n(It assumes that the integer i is ranged between 0 and 255, according to Char.code result)"},"comment":{"url":"https://api.github.com/repos/ocaml-batteries-team/batteries-included/issues/comments/183386049","html_url":"https://github.com/ocaml-batteries-team/batteries-included/issues/663#issuecomment-183386049","issue_url":"https://api.github.com/repos/ocaml-batteries-team/batteries-included/issues/663","id":183386049,"user":{"login":"gasche","id":426238,"avatar_url":"https://avatars.githubusercontent.com/u/426238?v=3","gravatar_id":"","url":"https://api.github.com/users/gasche","html_url":"https://github.com/gasche","followers_url":"https://api.github.com/users/gasche/followers","following_url":"https://api.github.com/users/gasche/following{/other_user}","gists_url":"https://api.github.com/users/gasche/gists{/gist_id}","starred_url":"https://api.github.com/users/gasche/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gasche/subscriptions","organizations_url":"https://api.github.com/users/gasche/orgs","repos_url":"https://api.github.com/users/gasche/repos","events_url":"https://api.github.com/users/gasche/events{/privacy}","received_events_url":"https://api.github.com/users/gasche/received_events","type":"User","site_admin":false},"created_at":"2016-02-12T16:00:52Z","updated_at":"2016-02-12T16:00:52Z","body":"Indeed; I also think this is good work and would add to the thanks -- I'm always glad when people contribute!"}}