semi_bitset.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #ifndef SEMI_BITSET_HPP
  2. #define SEMI_BITSET_HPP
  3. #include <cstdint>
  4. #include <vector>
  5. #include <bitset>
  6. #include <limits>
  7. using std::size_t;
  8. static const size_t dynamic = std::numeric_limits<size_t>::max();
  9. static const std::uint64_t lower_half = 0x00000000FFFFFFFF;
  10. template<size_t size_>
  11. struct semi_bitset{
  12. using uint64_t = std::uint64_t;
  13. using uint32_t = std::uint32_t;
  14. size_t size = size_;
  15. inline size_t length(){return size;}
  16. uint64_t data[size_] = {0};
  17. inline semi_bitset(size_t s){}
  18. inline uint64_t chunk(size_t i){
  19. return data[i];
  20. }
  21. };
  22. template<>
  23. struct semi_bitset<dynamic>{
  24. using uint64_t = std::uint64_t;
  25. using uint32_t = std::uint32_t;
  26. size_t size;
  27. std::vector<uint64_t> data;
  28. inline semi_bitset(size_t _size) : size(_size), data(size, 0){}
  29. inline size_t length(){return size;};
  30. inline uint64_t chunk(size_t i){
  31. return data[i];
  32. }
  33. };
  34. /*template<size_t size>
  35. inline semi_bitset<size> operator<<(const semi_bitset<size>& set, int o)const{
  36. if(o < 0){return this->operator>>(-o);}
  37. if(o == 0){return *this;}
  38. semi_bitset<size> ret;
  39. unsigned int jump = o / 32;
  40. unsigned int mod = o % 32;
  41. unsigned int counter = 32 - (o % 32);
  42. for(size_t i = jump;i < size;i++){
  43. if(i < (size - 1))
  44. ret.data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
  45. else
  46. ret.data[i - jump] = ((data[i] << mod) & lower_half);
  47. }
  48. return ret;
  49. }
  50. */
  51. /*template<size_t size, bool containertype>
  52. struct semi_bitset{
  53. using uint64_t = std::uint64_t;
  54. using uint32_t = std::uint32_t;
  55. std::vector<uint64_t> data;
  56. inline semi_bitset() : data(size, 0){}
  57. inline size_t length()const{
  58. return size;
  59. }
  60. inline semi_bitset<size, containertype> operator<<(int o)const{
  61. if(o < 0){return this->operator>>(-o);}
  62. if(o == 0){return *this;}
  63. semi_bitset<size, containertype> ret;
  64. unsigned int jump = o / 32;
  65. unsigned int mod = o % 32;
  66. unsigned int counter = 32 - (o % 32);
  67. for(size_t i = jump;i < size;i++){
  68. if(i < (size - 1))
  69. ret.data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
  70. else
  71. ret.data[i - jump] = ((data[i] << mod) & lower_half);
  72. }
  73. return ret;
  74. }
  75. inline semi_bitset<size, containertype> operator>>(int o)const{
  76. if(o < 0){return this->operator<<(-o);}
  77. if(o == 0){return *this;}
  78. semi_bitset<size, containertype> ret;
  79. unsigned int jump = o / 32;
  80. unsigned int mod = o % 32;
  81. unsigned int counter = 32 - (o % 32);
  82. for(size_t i = jump;i < size;i++){
  83. if(i > jump)
  84. ret.data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
  85. else
  86. ret.data[i] = (data[i - jump] >> mod);
  87. }
  88. return ret;
  89. }
  90. inline semi_bitset<size, containertype>& operator<<=(int o){
  91. if(o < 0){return this->operator>>=(-o);}
  92. if(o == 0){return *this;}
  93. unsigned int jump = o / 32;
  94. unsigned int mod = o % 32;
  95. unsigned int counter = 32 - (o % 32);
  96. for(size_t i = jump;i < size;i++){
  97. if(i < (size - 1))
  98. data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
  99. else
  100. data[i - jump] = ((data[i] << mod) & lower_half);
  101. }
  102. for(size_t i = size - jump;i < size;i++){
  103. data[i] = 0;
  104. }
  105. return *this;
  106. }
  107. inline semi_bitset<size, containertype>& operator>>=(int o){
  108. if(o < 0){return this->operator<<=(-o);}
  109. if(o == 0){return *this;}
  110. unsigned int jump = o / 32;
  111. unsigned int mod = o % 32;
  112. unsigned int counter = 32 - (o % 32);
  113. for(size_t i = size - 1;i >= jump;i--){
  114. if(i > jump)
  115. data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
  116. else
  117. data[i] = (data[i - jump] >> mod);
  118. }
  119. if(jump > 0)
  120. for(size_t i = jump - 1;i >= 0;i--){
  121. data[i] = 0;
  122. if(i == 0)break;
  123. }
  124. return *this;
  125. }
  126. template<size_t osize, bool ocontainertype>
  127. inline semi_bitset<size, containertype> operator&(const semi_bitset<osize, ocontainertype>& o)const{
  128. static_assert(size == osize);
  129. semi_bitset<size, containertype> ret;
  130. for(size_t i = 0;i < size;i++){
  131. ret.data[i] = (data[i] & o.data[i]);
  132. }
  133. return ret;
  134. }
  135. template<size_t osize, bool ocontainertype>
  136. inline semi_bitset<size, containertype> operator^(const semi_bitset<osize, ocontainertype>& o)const{
  137. static_assert(size == osize);
  138. semi_bitset<size, containertype> ret;
  139. for(size_t i = 0;i < size;i++){
  140. ret.data[i] = (data[i] ^ o.data[i]);
  141. }
  142. return ret;
  143. }
  144. template<size_t osize, bool ocontainertype>
  145. inline semi_bitset<size, containertype> operator|(const semi_bitset<osize, ocontainertype>& o)const{
  146. static_assert(size == osize);
  147. semi_bitset<size, containertype> ret;
  148. for(size_t i = 0;i < size;i++){
  149. ret.data[i] = (data[i] | o.data[i]);
  150. }
  151. return ret;
  152. }
  153. template<size_t osize, bool ocontainertype>
  154. inline semi_bitset<size, containertype>& operator&=(const semi_bitset<osize, ocontainertype>& o){
  155. static_assert(size == osize);
  156. for(size_t i = 0;i < size;i++){
  157. data &= o.data[i];
  158. }
  159. return *this;
  160. }
  161. template<size_t osize, bool ocontainertype>
  162. inline semi_bitset<size, containertype>& operator^=(const semi_bitset<osize, ocontainertype>& o){
  163. static_assert(size == osize);
  164. for(size_t i = 0;i < size;i++){
  165. data ^= o.data[i];
  166. }
  167. return *this;
  168. }
  169. template<size_t osize, bool ocontainertype>
  170. inline semi_bitset<size, containertype>& operator|=(const semi_bitset<osize, ocontainertype>& o){
  171. static_assert(size == osize);
  172. for(size_t i = 0;i < size;i++){
  173. data |= o.data[i];
  174. }
  175. return *this;
  176. }
  177. template<typename stream, size_t osize,bool ocontainertype>
  178. inline friend stream& operator<<(stream& s, const semi_bitset<osize, ocontainertype>& o){
  179. for(size_t i = 0;i < o.data.size();i++)
  180. s << std::bitset<32>((uint32_t)(o.data[i] & lower_half));
  181. return s;
  182. }
  183. };
  184. template<std::size_t size>
  185. struct semi_bitset<size, 0>{
  186. using uint64_t = std::uint64_t;
  187. using uint32_t = std::uint32_t;
  188. uint64_t data[size] = {0};
  189. static const bool containertype = 0;
  190. inline semi_bitset<size, containertype> operator<<(int o)const{
  191. if(o < 0){return this->operator>>(-o);}
  192. if(o == 0){return *this;}
  193. semi_bitset<size, containertype> ret;
  194. unsigned int jump = o / 32;
  195. unsigned int mod = o % 32;
  196. unsigned int counter = 32 - (o % 32);
  197. for(size_t i = jump;i < size;i++){
  198. if(i < (size - 1))
  199. ret.data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
  200. else
  201. ret.data[i - jump] = ((data[i] << mod) & lower_half);
  202. }
  203. return ret;
  204. }
  205. inline size_t length()const{
  206. return size;
  207. }
  208. inline semi_bitset<size, containertype> operator>>(int o)const{
  209. if(o < 0){return this->operator<<(-o);}
  210. if(o == 0){return *this;}
  211. semi_bitset<size, containertype> ret;
  212. unsigned int jump = o / 32;
  213. unsigned int mod = o % 32;
  214. unsigned int counter = 32 - (o % 32);
  215. for(size_t i = jump;i < size;i++){
  216. if(i > jump)
  217. ret.data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
  218. else
  219. ret.data[i] = (data[i - jump] >> mod);
  220. }
  221. return ret;
  222. }
  223. inline semi_bitset<size, containertype>& operator<<=(int o){
  224. if(o < 0){return this->operator>>=(-o);}
  225. if(o == 0){return *this;}
  226. unsigned int jump = o / 32;
  227. unsigned int mod = o % 32;
  228. unsigned int counter = 32 - (o % 32);
  229. for(size_t i = jump;i < size;i++){
  230. if(i < (size - 1))
  231. data[i - jump] = ((data[i] << mod) & lower_half) | (data[i + 1] >> counter);
  232. else
  233. data[i - jump] = ((data[i] << mod) & lower_half);
  234. }
  235. for(size_t i = size - jump;i < size;i++){
  236. data[i] = 0;
  237. }
  238. return *this;
  239. }
  240. inline semi_bitset<size, containertype>& operator>>=(int o){
  241. if(o < 0){return this->operator<<=(-o);}
  242. if(o == 0){return *this;}
  243. unsigned int jump = o / 32;
  244. unsigned int mod = o % 32;
  245. unsigned int counter = 32 - (o % 32);
  246. for(size_t i = size - 1;i >= jump;i--){
  247. if(i > jump)
  248. data[i] = (data[i - jump] >> mod) | ((data[i - jump - 1] << counter) & lower_half);
  249. else
  250. data[i] = (data[i - jump] >> mod);
  251. }
  252. if(jump > 0)
  253. for(size_t i = jump - 1;i >= 0;i--){
  254. data[i] = 0;
  255. if(i == 0)break;
  256. }
  257. return *this;
  258. }
  259. template<size_t osize, bool ocontainertype>
  260. inline semi_bitset<size, containertype | ocontainertype> operator&(const semi_bitset<osize, ocontainertype>& o)const{
  261. static_assert(size == osize);
  262. semi_bitset<size, containertype | ocontainertype> ret;
  263. for(size_t i = 0;i < size;i++){
  264. ret.data[i] = (data[i] & o.data[i]);
  265. }
  266. return ret;
  267. }
  268. template<size_t osize, bool ocontainertype>
  269. inline semi_bitset<size, containertype | ocontainertype> operator^(const semi_bitset<osize, ocontainertype>& o)const{
  270. static_assert(size == osize);
  271. semi_bitset<size, containertype | ocontainertype> ret;
  272. for(size_t i = 0;i < size;i++){
  273. ret.data[i] = (data[i] ^ o.data[i]);
  274. }
  275. return ret;
  276. }
  277. template<size_t osize, bool ocontainertype>
  278. inline semi_bitset<size, containertype | ocontainertype> operator|(const semi_bitset<osize, ocontainertype>& o)const{
  279. static_assert(size == osize);
  280. semi_bitset<size, containertype | ocontainertype> ret;
  281. for(size_t i = 0;i < size;i++){
  282. ret.data[i] = (data[i] | o.data[i]);
  283. }
  284. return ret;
  285. }
  286. template<size_t osize, bool ocontainertype>
  287. inline semi_bitset<size, containertype>& operator&=(const semi_bitset<osize, ocontainertype>& o){
  288. static_assert(size == osize);
  289. for(size_t i = 0;i < size;i++){
  290. data &= o.data[i];
  291. }
  292. return *this;
  293. }
  294. template<size_t osize, bool ocontainertype>
  295. inline semi_bitset<size, containertype>& operator^=(const semi_bitset<osize, ocontainertype>& o){
  296. static_assert(size == osize);
  297. for(size_t i = 0;i < size;i++){
  298. data ^= o.data[i];
  299. }
  300. return *this;
  301. }
  302. template<size_t osize, bool ocontainertype>
  303. inline semi_bitset<size, containertype>& operator|=(const semi_bitset<osize, ocontainertype>& o){
  304. static_assert(size == osize);
  305. for(size_t i = 0;i < size;i++){
  306. data |= o.data[i];
  307. }
  308. return *this;
  309. }
  310. };*/
  311. #endif