Skip to content

Commit bb4555b

Browse files
author
Dominik Sandjaja
authored
Unify & enhance APIs by adding methods to all versions (#5)
* Unify & enhance APIs by adding methods to all versions Adds the getDefaultValue() method to all versions of the maps. In addition, this changeset also adds the remove(key, value) method to all versions. * Add tests for the LongLong variant
1 parent 1bccbfc commit bb4555b

20 files changed

+429
-8
lines changed

src/main/java/com/trivago/fastutilconcurrentwrapper/IntFloatMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,9 @@ public interface IntFloatMap extends PrimitiveIntKeyMap {
88

99
float put(int key, float value);
1010

11+
float getDefaultValue();
12+
1113
float remove(int key);
14+
15+
boolean remove(int key, float value);
1216
}

src/main/java/com/trivago/fastutilconcurrentwrapper/IntIntMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ public interface IntIntMap extends PrimitiveIntKeyMap {
1212

1313
int put(int key, int value);
1414

15+
int getDefaultValue();
16+
1517
int remove(int key);
18+
19+
boolean remove(int key, int value);
1620
}

src/main/java/com/trivago/fastutilconcurrentwrapper/LongFloatMap.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ public interface LongFloatMap extends PrimitiveLongKeyMap {
1515
float getDefaultValue();
1616

1717
float remove(long key);
18+
19+
/**
20+
* Remove this key only if it has the given value.
21+
* @param key
22+
* @param value
23+
* @return
24+
*/
25+
boolean remove(long key, float value);
1826
}

src/main/java/com/trivago/fastutilconcurrentwrapper/LongLongMap.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ public interface LongLongMap extends PrimitiveLongKeyMap {
1212

1313
long put(long key, long value);
1414

15+
long getDefaultValue();
16+
1517
long remove(long key);
18+
19+
boolean remove(long key, long value);
1620
}

src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntFloatMap.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
public class ConcurrentBusyWaitingIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap {
99

1010
private final IntFloatMap[] maps;
11-
private final float defauldValue;
11+
private final float defaultValue;
1212

1313
public ConcurrentBusyWaitingIntFloatMap(int numBuckets,
1414
int initialCapacity,
1515
float loadFactor,
1616
float defaultValue) {
1717
super(numBuckets);
18-
this.defauldValue = defaultValue;
18+
1919
this.maps = new IntFloatMap[numBuckets];
20+
this.defaultValue = defaultValue;
21+
2022
for (int i = 0; i < numBuckets; i++) {
2123
maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defaultValue);
2224
}
@@ -83,6 +85,11 @@ public float put(int key, float value) {
8385
}
8486
}
8587

88+
@Override
89+
public float getDefaultValue() {
90+
return defaultValue;
91+
}
92+
8693
@Override
8794
public float remove(int key) {
8895
int bucket = getBucket(key);
@@ -100,4 +107,21 @@ public float remove(int key) {
100107
}
101108
}
102109

110+
@Override
111+
public boolean remove(int key, float value) {
112+
int bucket = getBucket(key);
113+
114+
Lock writeLock = locks[bucket].writeLock();
115+
116+
while (true) {
117+
if (writeLock.tryLock()) {
118+
try {
119+
return maps[bucket].remove(key, value);
120+
} finally {
121+
writeLock.unlock();
122+
}
123+
}
124+
}
125+
}
126+
103127
}

src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingIntIntMap.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
public class ConcurrentBusyWaitingIntIntMap extends PrimitiveConcurrentMap implements IntIntMap {
99

1010
private final IntIntMap[] maps;
11+
private final int defaultValue;
1112

1213
public ConcurrentBusyWaitingIntIntMap(int numBuckets,
1314
int initialCapacity,
1415
float loadFactor,
1516
int defaultValue) {
1617
super(numBuckets);
18+
1719
this.maps = new IntIntMap[numBuckets];
20+
this.defaultValue = defaultValue;
21+
1822
for (int i = 0; i < numBuckets; i++) {
1923
maps[i] = new PrimitiveFastutilIntIntWrapper(initialCapacity, loadFactor, defaultValue);
2024
}
@@ -81,6 +85,11 @@ public int put(int key, int value) {
8185
}
8286
}
8387

88+
@Override
89+
public int getDefaultValue() {
90+
return defaultValue;
91+
}
92+
8493
@Override
8594
public int remove(int key) {
8695
int bucket = getBucket(key);
@@ -97,4 +106,21 @@ public int remove(int key) {
97106
}
98107
}
99108
}
109+
110+
@Override
111+
public boolean remove(int key, int value) {
112+
int bucket = getBucket(key);
113+
114+
Lock writeLock = locks[bucket].writeLock();
115+
116+
while (true) {
117+
if (writeLock.tryLock()) {
118+
try {
119+
return maps[bucket].remove(key, value);
120+
} finally {
121+
writeLock.unlock();
122+
}
123+
}
124+
}
125+
}
100126
}

src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongFloatMap.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,21 @@ public float remove(long key) {
104104
}
105105
}
106106
}
107+
108+
@Override
109+
public boolean remove(long key, float value) {
110+
int bucket = getBucket(key);
111+
112+
Lock writeLock = locks[bucket].writeLock();
113+
114+
while (true) {
115+
if (writeLock.tryLock()) {
116+
try {
117+
return maps[bucket].remove(key, value);
118+
} finally {
119+
writeLock.unlock();
120+
}
121+
}
122+
}
123+
}
107124
}

src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentBusyWaitingLongLongMap.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@
88
public class ConcurrentBusyWaitingLongLongMap extends PrimitiveConcurrentMap implements LongLongMap {
99

1010
private final LongLongMap[] maps;
11+
private final long defaultValue;
1112

1213
public ConcurrentBusyWaitingLongLongMap(int numBuckets,
1314
int initialCapacity,
1415
float loadFactor,
1516
long defaultValue) {
1617
super(numBuckets);
18+
1719
this.maps = new LongLongMap[numBuckets];
20+
this.defaultValue = defaultValue;
21+
1822
for (int i = 0; i < numBuckets; i++) {
1923
maps[i] = new PrimitiveFastutilLongLongWrapper(initialCapacity, loadFactor, defaultValue);
2024
}
@@ -81,6 +85,11 @@ public long put(long key, long value) {
8185
}
8286
}
8387

88+
@Override
89+
public long getDefaultValue() {
90+
return defaultValue;
91+
}
92+
8493
@Override
8594
public long remove(long key) {
8695
int bucket = getBucket(key);
@@ -97,4 +106,21 @@ public long remove(long key) {
97106
}
98107
}
99108
}
109+
110+
@Override
111+
public boolean remove(long key, long value) {
112+
int bucket = getBucket(key);
113+
114+
Lock writeLock = locks[bucket].writeLock();
115+
116+
while (true) {
117+
if (writeLock.tryLock()) {
118+
try {
119+
return maps[bucket].remove(key, value);
120+
} finally {
121+
writeLock.unlock();
122+
}
123+
}
124+
}
125+
}
100126
}

src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntFloatMap.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
public class ConcurrentIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap {
99

1010
private final IntFloatMap[] maps;
11-
private final float defauldValue;
11+
private final float defaultValue;
1212

1313
public ConcurrentIntFloatMap(int numBuckets,
1414
int initialCapacity,
1515
float loadFactor,
1616
float defaultValue) {
1717
super(numBuckets);
18-
this.defauldValue = defaultValue;
18+
1919
this.maps = new IntFloatMap[numBuckets];
20+
this.defaultValue = defaultValue;
21+
2022
for (int i = 0; i < numBuckets; i++) {
21-
maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defauldValue);
23+
maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defaultValue);
2224
}
2325
}
2426

@@ -79,6 +81,11 @@ public float put(int key, float value) {
7981
return result;
8082
}
8183

84+
@Override
85+
public float getDefaultValue() {
86+
return defaultValue;
87+
}
88+
8289
@Override
8390
public float remove(int key) {
8491
int bucket = getBucket(key);
@@ -92,4 +99,17 @@ public float remove(int key) {
9299
}
93100
}
94101

102+
@Override
103+
public boolean remove(int key, float value) {
104+
int bucket = getBucket(key);
105+
106+
Lock writeLock = locks[bucket].writeLock();
107+
writeLock.lock();
108+
try {
109+
return maps[bucket].remove(key, value);
110+
} finally {
111+
writeLock.unlock();
112+
}
113+
}
114+
95115
}

src/main/java/com/trivago/fastutilconcurrentwrapper/map/ConcurrentIntIntMap.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
import com.trivago.fastutilconcurrentwrapper.IntIntMap;
44
import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntIntWrapper;
5-
65
import java.util.concurrent.locks.Lock;
76

87
public class ConcurrentIntIntMap extends PrimitiveConcurrentMap implements IntIntMap {
98

109
private final IntIntMap[] maps;
10+
private final int defaultValue;
1111

1212
public ConcurrentIntIntMap(
1313
int numBuckets,
@@ -18,6 +18,7 @@ public ConcurrentIntIntMap(
1818
super(numBuckets);
1919

2020
this.maps = new IntIntMap[numBuckets];
21+
this.defaultValue = defaultValue;
2122

2223
for (int i = 0; i < numBuckets; i++) {
2324
maps[i] = new PrimitiveFastutilIntIntWrapper(initialCapacity, loadFactor, defaultValue);
@@ -81,6 +82,11 @@ public int put(int key, int value) {
8182
return result;
8283
}
8384

85+
@Override
86+
public int getDefaultValue() {
87+
return defaultValue;
88+
}
89+
8490
@Override
8591
public int remove(int key) {
8692
int bucket = getBucket(key);
@@ -93,4 +99,17 @@ public int remove(int key) {
9399
writeLock.unlock();
94100
}
95101
}
102+
103+
@Override
104+
public boolean remove(int key, int value) {
105+
int bucket = getBucket(key);
106+
107+
Lock writeLock = locks[bucket].writeLock();
108+
writeLock.lock();
109+
try {
110+
return maps[bucket].remove(key, value);
111+
} finally {
112+
writeLock.unlock();
113+
}
114+
}
96115
}

0 commit comments

Comments
 (0)