bitset

Best way to store an array of BitSet in database?

送分小仙女□ 提交于 2020-01-14 12:36:16
问题 I got an array of java.util.BitSet that i want to persist in my database, but i don't know whats the best way to do it. To be precise i got x*y true or false value for every entry that i want to store. I tought java.util.BitSet is a good call to try, but i dont really know how could i store it in database. I'm using MySQL database with hibernate(with annotation). This is how i initialize my map: Bitset[] map = new BitSet[x]; for (int i = 0; i < x; i++) { map[i] = new BitSet(y); } Update:

Bitset Reference

做~自己de王妃 提交于 2020-01-13 10:34:08
问题 From http://www.cplusplus.com/reference/stl/bitset/: Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements. How, exactly, does this bit reference work? The only way I could think of would be to use a static array of char s, but then each instance would need to store its index in the array. Since each reference instance would have at least the size of a size_t , that would destroy the

Bitset Reference

匆匆过客 提交于 2020-01-13 10:33:32
问题 From http://www.cplusplus.com/reference/stl/bitset/: Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements. How, exactly, does this bit reference work? The only way I could think of would be to use a static array of char s, but then each instance would need to store its index in the array. Since each reference instance would have at least the size of a size_t , that would destroy the

Why is the std::bitset<8> variable unable to handle 11111111?

我的未来我决定 提交于 2020-01-13 07:52:49
问题 Why is this program showing the following output ? #include <bitset> ... { std::bitset<8> b1(01100100); std::cout<<b1<<std::endl; std::bitset<8> b2(11111111); std::cout<<b2<<std::endl; //see, this variable //has been assigned //the value 11111111 //whereas, during //execution, it takes //the value 11000111 std::cout << "b1 & b2: " << (b1 & b2) << '\n'; std::cout << "b1 | b2: " << (b1 | b2) << '\n'; std::cout << "b1 ^ b2: " << (b1 ^ b2) << '\n'; } This is the OUTPUT: 01000000 11000111 b1 & b2:

让人眼前一亮的算法------布隆过滤器

≯℡__Kan透↙ 提交于 2020-01-11 02:07:58
问题 假设你现在要处理这样一个问题,你有一个网站并且拥有很多访客,每当有用户访问时,你想知道这个ip是不是第一次访问你的网站。这是一个很常见的场景,为了完成这个功能,你很容易就会想到下面这个解决方案: 把访客的ip存进一个hash表中,每当有新的访客到来时,先检查哈希表中是否有改访客的ip,如果有则说明该访客在黑名单中。你还知道,hash表的存取时间复杂度都是O(1),效率很高,因此你对你的方案很是满意。 然后我们假设你的网站已经被1亿个用户访问过,每个ip的长度是15,那么你一共需要15 * 100000000 = 1500000000Bytes = 1.4G,这还没考虑hash冲突的问题(hash表中的槽位越多,越浪费空间,槽位越少,效率越低)。 于是聪明的你稍一思考,又想到可以把ip转换成无符号的int型值来存储,这样一个ip只需要占用4个字节就行了,这时1亿个ip占用的空间是4 * 100000000 = 400000000Bytes = 380M,空间消耗降低了很多。 那还有没有在不影响存取效率的前提下更加节省空间的办法呢? BitSet 32位无符号int型能表示的最大值是4294967295,所有的ip都在这个范围内,我们可以用一个bit位来表示某个ip是否出现过,如果出现过,就把代表该ip的bit位置为1

boost::dynamic_bitset

半腔热情 提交于 2020-01-09 15:45:39
#include <boost/dynamic_bitset.hpp> using namespace boost; int main() { // 构造函数 dynamic_bitset<> db; // 空 dynamic_bitset<> db1(10); // 大小为10 dynamic_bitset<> db2(0x16, BOOST_BINARY(11101));// 大小为22 dynamic_bitset<> db3(std::string("0100")); // 大小为4 dynamic_bitset<> db4(db2); dynamic_bitset<> db5 = db3; cout << db2.to_ulong() << endl;// 29 转换为整数 cout << db3.to_ulong() << endl;// 4 // dynamic_bitset内部由高到底存储二进制位 // 访问 cout << db3[0];// 0 cout << db3[1];// 0 cout << db3[2];// 1 cout << db3[3] << endl;// 0 db3[0] = 1;// 非0就会置为1 cout << db3[0];// 1 cout << db3[1];// 0 cout << db3[2];// 1 cout << db3

BitSet和布隆过滤器(Bloom Filter)

做~自己de王妃 提交于 2020-01-07 17:45:52
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> 布隆过滤器 Bloom Filter 是由Howard Bloom 在 1970 年提出的二进制向量数据结构,它具有很好的空间和时间效率,被用来检测一个元素是不是集合中的一个成员。如果检测结果为是,该元素不一定在集合中;但如果检测结果为否,该元素一定不在集合中。因此Bloom filter具有100%的召回率。这样每个检测请求返回有“在集合内(可能错误)”和“不在集合内(绝对不在集合内)”两种情况,可见 Bloom filter 是牺牲了正确率和时间以节省空间。 当然布隆过滤器也有缺点,主要是误判的问题,随着数据量的增加,误判率也随着增大,解决办法:可以建立一个列表,保存哪些数值是容易被误算的。 Bloom Filter最大的特点是不会存在false negative,即:如果contains()返回false,则该元素一定不在集合中,但会存在一定的true negative,即:如果contains()返回true,则该元素可能在集合中。 Bloom Filter在很多开源框架都有实现,例如: Elasticsearch:org.elasticsearch.common.util.BloomFilter guava:com.google.common.hash.BloomFilter Hadoop:org

find greatest number, x for given y and n such that x ^ y <= n

若如初见. 提交于 2020-01-07 08:35:15
问题 I need to find a greatest number, x for given y and n such that x ^ y <= n Here n can be very large number - 1 <= n <= 10^10 and 1 <= y <= 10^5 for example : for y = 5 and n = 1024 x ^ 5, then x = 4 (4 ^ 5 = 1024) for y = 5 and n = 480 x ^ 5 , then x = 3 (3 ^ 5 = 243, 4 ^ 5 = 1024) - selecting lowest one, so x = 3 i have written a small program, But i want more efficient technique because n and y can be very large. def get(y, n): x = 1 while x ** y <= n: x += 1 return x - 1 回答1: Using a

How to convert a Seq[Byte] into an Array[Boolean] representing each bit in Scala

守給你的承諾、 提交于 2020-01-04 09:27:11
问题 Is there a better way to convert a sequence of Bytes into an Seq[Boolean] where each element represents a bit from the Byte sequence? I'm currently doing this, but byte2Bools seems a little too heavy... object Main extends App { private def byte2Bools(b: Byte) = (0 to 7).foldLeft(ArrayBuffer[Boolean]())((bs, i) => bs += isBitSet(b, i)) private def isBitSet(byte: Byte, bit: Int) = ((byte >> bit) & 1) == 1 val bytes = List[Byte](1, 2, 3) val bools = bytes.flatMap(b => byte2Bools(b)) println

keyPressed event slow on first repeat

冷暖自知 提交于 2020-01-04 04:27:09
问题 Okay so I am sorry for this being a really weird question but it is driving me insane. I handle my WASD movement for my game via: Action ClassWASDKeyPressed = new ClassWASDKeyPressed(); Action ClassWASDKeyReleased = new ClassWASDKeyReleased(); BitKeys movementBitKeys = new BitKeys(); //for WASD movement key pressed/releases //pressed theDesktop.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("W"), "wButtonPress"); theDesktop.getActionMap().put("wButtonPress",