Skip to content

Commit 19670fc

Browse files
committed
Add bit map code
1 parent 2ceb698 commit 19670fc

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/test/java/bit/BitMapTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package bit;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.CoreMatchers.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class BitMapTest {
9+
10+
/*
11+
TASK
12+
O(1)으로 해당 데이터가 존재하는지 판단한다.
13+
*/
14+
15+
@Test
16+
public void test() {
17+
BitMap bitMap = new BitMap();
18+
bitMap.store("1234567");
19+
assertThat(bitMap.exist("1234567"), is(true));
20+
assertThat(bitMap.exist("1234568"), is(false));
21+
}
22+
23+
public class BitMap {
24+
public int[] cache;
25+
26+
public BitMap() {
27+
cache = new int[(10 * 1000 * 1000) / 32];
28+
}
29+
30+
public void store(String data) {
31+
int dataInt = Integer.parseInt(data);
32+
int arrIndex = dataInt / 32;
33+
int byteIndex = dataInt % 32;
34+
cache[arrIndex] = cache[arrIndex] | (1 << byteIndex);
35+
}
36+
37+
public boolean exist(String data) {
38+
int dataInt = Integer.parseInt(data);
39+
int arrIndex = dataInt / 32;
40+
int byteIndex = dataInt % 32;
41+
return (cache[arrIndex] & (1 << byteIndex)) != 0;
42+
}
43+
}
44+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package search;
2+
3+
/**
4+
* Created by Jbee on 2017. 6. 1..
5+
*/
6+
public class BinarySearchTest {
7+
}

0 commit comments

Comments
 (0)