-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigIntSet.java
More file actions
32 lines (27 loc) · 1.01 KB
/
Copy pathBigIntSet.java
File metadata and controls
32 lines (27 loc) · 1.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.ecwid.dev.ipcounter.intset;
import java.util.concurrent.atomic.AtomicIntegerArray;
import java.util.stream.IntStream;
class BigIntSet implements IntSet {
static final long BIG_SET_EFFICIENT_CAPACITY = 8_000_000;
private static final int FIVE = 5;
private static final int INT_BIT_SIZE = 1 << FIVE;
private static final int BUCKETS_SIZE = 1 << (INT_BIT_SIZE - FIVE);
private static final int BUCKET_MASK = (1 << (FIVE + 1)) - 1;
private final AtomicIntegerArray storage = new AtomicIntegerArray(BUCKETS_SIZE);
BigIntSet() {
}
@Override
public void add(int e) {
int bucketIndex = e >>> FIVE;
int bitIndex = e & BUCKET_MASK;
int bitToSet = 1 << bitIndex;
storage.updateAndGet(bucketIndex, val -> val | bitToSet);
}
@Override
public synchronized long size() {
return IntStream.range(0, storage.length())
.parallel()
.mapToLong(i -> Integer.bitCount(storage.get(i)))
.sum();
}
}