Skip to content

Commit 23aa868

Browse files
authored
New methods for maps (computeIfAbsent, computeIfPresent) (#11)
* Add computeIfAbsent and computeIfPresent Adds computeIfAbsent() and computeIfPresent() methods to all versions of the maps. * Upgrade to Java 11 * More unit tests * Move pom properties to build script * Update fastutil library * release v0.2.0
1 parent af9b4e6 commit 23aa868

19 files changed

+444
-43
lines changed

build.gradle

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,42 @@ allprojects {
1616
}
1717

1818
group 'com.trivago'
19-
version '0.1.1-SNAPSHOT'
19+
version '0.2.0'
2020

21-
sourceCompatibility = 1.8
22-
targetCompatibility = 1.8
21+
mavenPublishing {
22+
pom {
23+
name = "fastutil-concurrent-wrapper"
24+
description = "Set of concurrent wrappers around fastutil primitive maps."
25+
url = "https://github.com/trivago/fastutil-concurrent-wrapper"
26+
licenses {
27+
license {
28+
name = "The Apache Software License, Version 2.0"
29+
url = "https://opensource.org/licenses/Apache-2.0"
30+
distribution = "repo"
31+
}
32+
}
33+
developers {
34+
developer {
35+
id = "mchernyakov"
36+
name = "Mikhail Chernyakov"
37+
url = "https://github.com/mchernyakov"
38+
}
39+
developer {
40+
id = "erdoganf"
41+
name = "Fehim Erdogan"
42+
url = "https://github.com/erdoganf"
43+
}
44+
}
45+
scm {
46+
url = "https://github.com/trivago/fastutil-concurrent-wrapper"
47+
connection = "scm:git:https://github.com/trivago/fastutil-concurrent-wrapper"
48+
developerConnection = "scm:git:https://github.com/trivago/fastutil-concurrent-wrapper"
49+
}
50+
}
51+
}
52+
53+
sourceCompatibility = 11
54+
targetCompatibility = 11
2355

2456
sourceSets {
2557
jmh {
@@ -30,7 +62,7 @@ sourceSets {
3062
}
3163

3264
dependencies {
33-
implementation group: 'it.unimi.dsi', name: 'fastutil', version: '8.5.6'
65+
implementation group: 'it.unimi.dsi', name: 'fastutil', version: '8.5.9'
3466

3567
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
3668
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'

gradle.properties

Lines changed: 0 additions & 20 deletions
This file was deleted.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.trivago.fastutilconcurrentwrapper;
22

3+
import it.unimi.dsi.fastutil.ints.Int2FloatFunction;
4+
5+
import java.util.function.BiFunction;
6+
37
public interface IntFloatMap extends PrimitiveIntKeyMap {
48

59
float DEFAULT_VALUE = 0.0f;
@@ -13,4 +17,8 @@ public interface IntFloatMap extends PrimitiveIntKeyMap {
1317
float remove(int key);
1418

1519
boolean remove(int key, float value);
20+
21+
float computeIfAbsent(int key, Int2FloatFunction mappingFunction);
22+
23+
float computeIfPresent(int key, BiFunction<Integer, Float, Float> mappingFunction);
1624
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.trivago.fastutilconcurrentwrapper;
22

3+
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
4+
5+
import java.util.function.BiFunction;
6+
37
public interface IntIntMap extends PrimitiveIntKeyMap {
48

59
int DEFAULT_VALUE = 0;
@@ -17,4 +21,8 @@ public interface IntIntMap extends PrimitiveIntKeyMap {
1721
int remove(int key);
1822

1923
boolean remove(int key, int value);
24+
25+
int computeIfAbsent(int key, Int2IntFunction mappingFunction);
26+
27+
int computeIfPresent(int key, BiFunction<Integer, Integer, Integer> mappingFunction);
2028
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.trivago.fastutilconcurrentwrapper;
22

3+
import it.unimi.dsi.fastutil.longs.Long2FloatFunction;
4+
5+
import java.util.function.BiFunction;
6+
37
public interface LongFloatMap extends PrimitiveLongKeyMap {
48

59
float DEFAULT_VALUE = 0.0f;
@@ -18,9 +22,14 @@ public interface LongFloatMap extends PrimitiveLongKeyMap {
1822

1923
/**
2024
* Remove this key only if it has the given value.
25+
*
2126
* @param key
2227
* @param value
2328
* @return
2429
*/
2530
boolean remove(long key, float value);
31+
32+
float computeIfAbsent(long key, Long2FloatFunction mappingFunction);
33+
34+
float computeIfPresent(int key, BiFunction<Long, Float, Float> mappingFunction);
2635
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.trivago.fastutilconcurrentwrapper;
22

3+
import it.unimi.dsi.fastutil.longs.Long2LongFunction;
4+
5+
import java.util.function.BiFunction;
6+
37
public interface LongLongMap extends PrimitiveLongKeyMap {
48

59
long DEFAULT_VALUE = 0;
@@ -17,4 +21,8 @@ public interface LongLongMap extends PrimitiveLongKeyMap {
1721
long remove(long key);
1822

1923
boolean remove(long key, long value);
24+
25+
long computeIfAbsent(long key, Long2LongFunction mappingFunction);
26+
27+
long computeIfPresent(long key, BiFunction<Long, Long, Long> mappingFunction);
2028
}

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.trivago.fastutilconcurrentwrapper.IntFloatMap;
44
import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntFloatWrapper;
5+
import it.unimi.dsi.fastutil.ints.Int2FloatFunction;
56

67
import java.util.concurrent.locks.Lock;
8+
import java.util.function.BiFunction;
79

810
public class ConcurrentBusyWaitingIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap {
911

@@ -124,4 +126,37 @@ public boolean remove(int key, float value) {
124126
}
125127
}
126128

129+
@Override
130+
public float computeIfAbsent(int key, Int2FloatFunction mappingFunction) {
131+
int bucket = getBucket(key);
132+
133+
Lock writeLock = locks[bucket].writeLock();
134+
135+
while (true) {
136+
if (writeLock.tryLock()) {
137+
try {
138+
return maps[bucket].computeIfAbsent(key, mappingFunction);
139+
} finally {
140+
writeLock.unlock();
141+
}
142+
}
143+
}
144+
}
145+
146+
@Override
147+
public float computeIfPresent(int key, BiFunction<Integer, Float, Float> mappingFunction) {
148+
int bucket = getBucket(key);
149+
150+
Lock writeLock = locks[bucket].writeLock();
151+
152+
while (true) {
153+
if (writeLock.tryLock()) {
154+
try {
155+
return maps[bucket].computeIfPresent(key, mappingFunction);
156+
} finally {
157+
writeLock.unlock();
158+
}
159+
}
160+
}
161+
}
127162
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.trivago.fastutilconcurrentwrapper.IntIntMap;
44
import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntIntWrapper;
5+
import it.unimi.dsi.fastutil.ints.Int2IntFunction;
56

67
import java.util.concurrent.locks.Lock;
8+
import java.util.function.BiFunction;
79

810
public class ConcurrentBusyWaitingIntIntMap extends PrimitiveConcurrentMap implements IntIntMap {
911

@@ -123,4 +125,38 @@ public boolean remove(int key, int value) {
123125
}
124126
}
125127
}
128+
129+
@Override
130+
public int computeIfAbsent(int key, Int2IntFunction mappingFunction) {
131+
int bucket = getBucket(key);
132+
133+
Lock writeLock = locks[bucket].writeLock();
134+
135+
while (true) {
136+
if (writeLock.tryLock()) {
137+
try {
138+
return maps[bucket].computeIfAbsent(key, mappingFunction);
139+
} finally {
140+
writeLock.unlock();
141+
}
142+
}
143+
}
144+
}
145+
146+
@Override
147+
public int computeIfPresent(int key, BiFunction<Integer, Integer, Integer> mappingFunction) {
148+
int bucket = getBucket(key);
149+
150+
Lock writeLock = locks[bucket].writeLock();
151+
152+
while (true) {
153+
if (writeLock.tryLock()) {
154+
try {
155+
return maps[bucket].computeIfPresent(key, mappingFunction);
156+
} finally {
157+
writeLock.unlock();
158+
}
159+
}
160+
}
161+
}
126162
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.trivago.fastutilconcurrentwrapper.LongFloatMap;
44
import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongFloatWrapper;
5+
import it.unimi.dsi.fastutil.longs.Long2FloatFunction;
56

67
import java.util.concurrent.locks.Lock;
8+
import java.util.function.BiFunction;
79

810
public class ConcurrentBusyWaitingLongFloatMap extends PrimitiveConcurrentMap implements LongFloatMap {
911

@@ -121,4 +123,38 @@ public boolean remove(long key, float value) {
121123
}
122124
}
123125
}
126+
127+
@Override
128+
public float computeIfAbsent(long key, Long2FloatFunction mappingFunction) {
129+
int bucket = getBucket(key);
130+
131+
Lock writeLock = locks[bucket].writeLock();
132+
133+
while (true) {
134+
if (writeLock.tryLock()) {
135+
try {
136+
return maps[bucket].computeIfAbsent(key, mappingFunction);
137+
} finally {
138+
writeLock.unlock();
139+
}
140+
}
141+
}
142+
}
143+
144+
@Override
145+
public float computeIfPresent(int key, BiFunction<Long, Float, Float> mappingFunction) {
146+
int bucket = getBucket(key);
147+
148+
Lock writeLock = locks[bucket].writeLock();
149+
150+
while (true) {
151+
if (writeLock.tryLock()) {
152+
try {
153+
return maps[bucket].computeIfPresent(key, mappingFunction);
154+
} finally {
155+
writeLock.unlock();
156+
}
157+
}
158+
}
159+
}
124160
}

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.trivago.fastutilconcurrentwrapper.LongLongMap;
44
import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongLongWrapper;
5+
import it.unimi.dsi.fastutil.longs.Long2LongFunction;
56

67
import java.util.concurrent.locks.Lock;
8+
import java.util.function.BiFunction;
79

810
public class ConcurrentBusyWaitingLongLongMap extends PrimitiveConcurrentMap implements LongLongMap {
911

@@ -123,4 +125,38 @@ public boolean remove(long key, long value) {
123125
}
124126
}
125127
}
128+
129+
@Override
130+
public long computeIfAbsent(long key, Long2LongFunction mappingFunction) {
131+
int bucket = getBucket(key);
132+
133+
Lock writeLock = locks[bucket].writeLock();
134+
135+
while (true) {
136+
if (writeLock.tryLock()) {
137+
try {
138+
return maps[bucket].computeIfAbsent(key, mappingFunction);
139+
} finally {
140+
writeLock.unlock();
141+
}
142+
}
143+
}
144+
}
145+
146+
@Override
147+
public long computeIfPresent(long key, BiFunction<Long, Long, Long> mappingFunction) {
148+
int bucket = getBucket(key);
149+
150+
Lock writeLock = locks[bucket].writeLock();
151+
152+
while (true) {
153+
if (writeLock.tryLock()) {
154+
try {
155+
return maps[bucket].computeIfPresent(key, mappingFunction);
156+
} finally {
157+
writeLock.unlock();
158+
}
159+
}
160+
}
161+
}
126162
}

0 commit comments

Comments
 (0)