Skip to content

Commit

Permalink
Unify & enhance APIs by adding methods to all versions (#5)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Dominik Sandjaja authored Jan 7, 2022
1 parent 1bccbfc commit bb4555b
Show file tree
Hide file tree
Showing 20 changed files with 429 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@ public interface IntFloatMap extends PrimitiveIntKeyMap {

float put(int key, float value);

float getDefaultValue();

float remove(int key);

boolean remove(int key, float value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ public interface IntIntMap extends PrimitiveIntKeyMap {

int put(int key, int value);

int getDefaultValue();

int remove(int key);

boolean remove(int key, int value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@ public interface LongFloatMap extends PrimitiveLongKeyMap {
float getDefaultValue();

float remove(long key);

/**
* Remove this key only if it has the given value.
* @param key
* @param value
* @return
*/
boolean remove(long key, float value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ public interface LongLongMap extends PrimitiveLongKeyMap {

long put(long key, long value);

long getDefaultValue();

long remove(long key);

boolean remove(long key, long value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
public class ConcurrentBusyWaitingIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap {

private final IntFloatMap[] maps;
private final float defauldValue;
private final float defaultValue;

public ConcurrentBusyWaitingIntFloatMap(int numBuckets,
int initialCapacity,
float loadFactor,
float defaultValue) {
super(numBuckets);
this.defauldValue = defaultValue;

this.maps = new IntFloatMap[numBuckets];
this.defaultValue = defaultValue;

for (int i = 0; i < numBuckets; i++) {
maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defaultValue);
}
Expand Down Expand Up @@ -83,6 +85,11 @@ public float put(int key, float value) {
}
}

@Override
public float getDefaultValue() {
return defaultValue;
}

@Override
public float remove(int key) {
int bucket = getBucket(key);
Expand All @@ -100,4 +107,21 @@ public float remove(int key) {
}
}

@Override
public boolean remove(int key, float value) {
int bucket = getBucket(key);

Lock writeLock = locks[bucket].writeLock();

while (true) {
if (writeLock.tryLock()) {
try {
return maps[bucket].remove(key, value);
} finally {
writeLock.unlock();
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
public class ConcurrentBusyWaitingIntIntMap extends PrimitiveConcurrentMap implements IntIntMap {

private final IntIntMap[] maps;
private final int defaultValue;

public ConcurrentBusyWaitingIntIntMap(int numBuckets,
int initialCapacity,
float loadFactor,
int defaultValue) {
super(numBuckets);

this.maps = new IntIntMap[numBuckets];
this.defaultValue = defaultValue;

for (int i = 0; i < numBuckets; i++) {
maps[i] = new PrimitiveFastutilIntIntWrapper(initialCapacity, loadFactor, defaultValue);
}
Expand Down Expand Up @@ -81,6 +85,11 @@ public int put(int key, int value) {
}
}

@Override
public int getDefaultValue() {
return defaultValue;
}

@Override
public int remove(int key) {
int bucket = getBucket(key);
Expand All @@ -97,4 +106,21 @@ public int remove(int key) {
}
}
}

@Override
public boolean remove(int key, int value) {
int bucket = getBucket(key);

Lock writeLock = locks[bucket].writeLock();

while (true) {
if (writeLock.tryLock()) {
try {
return maps[bucket].remove(key, value);
} finally {
writeLock.unlock();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,21 @@ public float remove(long key) {
}
}
}

@Override
public boolean remove(long key, float value) {
int bucket = getBucket(key);

Lock writeLock = locks[bucket].writeLock();

while (true) {
if (writeLock.tryLock()) {
try {
return maps[bucket].remove(key, value);
} finally {
writeLock.unlock();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
public class ConcurrentBusyWaitingLongLongMap extends PrimitiveConcurrentMap implements LongLongMap {

private final LongLongMap[] maps;
private final long defaultValue;

public ConcurrentBusyWaitingLongLongMap(int numBuckets,
int initialCapacity,
float loadFactor,
long defaultValue) {
super(numBuckets);

this.maps = new LongLongMap[numBuckets];
this.defaultValue = defaultValue;

for (int i = 0; i < numBuckets; i++) {
maps[i] = new PrimitiveFastutilLongLongWrapper(initialCapacity, loadFactor, defaultValue);
}
Expand Down Expand Up @@ -81,6 +85,11 @@ public long put(long key, long value) {
}
}

@Override
public long getDefaultValue() {
return defaultValue;
}

@Override
public long remove(long key) {
int bucket = getBucket(key);
Expand All @@ -97,4 +106,21 @@ public long remove(long key) {
}
}
}

@Override
public boolean remove(long key, long value) {
int bucket = getBucket(key);

Lock writeLock = locks[bucket].writeLock();

while (true) {
if (writeLock.tryLock()) {
try {
return maps[bucket].remove(key, value);
} finally {
writeLock.unlock();
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
public class ConcurrentIntFloatMap extends PrimitiveConcurrentMap implements IntFloatMap {

private final IntFloatMap[] maps;
private final float defauldValue;
private final float defaultValue;

public ConcurrentIntFloatMap(int numBuckets,
int initialCapacity,
float loadFactor,
float defaultValue) {
super(numBuckets);
this.defauldValue = defaultValue;

this.maps = new IntFloatMap[numBuckets];
this.defaultValue = defaultValue;

for (int i = 0; i < numBuckets; i++) {
maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defauldValue);
maps[i] = new PrimitiveFastutilIntFloatWrapper(initialCapacity, loadFactor, defaultValue);
}
}

Expand Down Expand Up @@ -79,6 +81,11 @@ public float put(int key, float value) {
return result;
}

@Override
public float getDefaultValue() {
return defaultValue;
}

@Override
public float remove(int key) {
int bucket = getBucket(key);
Expand All @@ -92,4 +99,17 @@ public float remove(int key) {
}
}

@Override
public boolean remove(int key, float value) {
int bucket = getBucket(key);

Lock writeLock = locks[bucket].writeLock();
writeLock.lock();
try {
return maps[bucket].remove(key, value);
} finally {
writeLock.unlock();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.trivago.fastutilconcurrentwrapper.IntIntMap;
import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilIntIntWrapper;

import java.util.concurrent.locks.Lock;

public class ConcurrentIntIntMap extends PrimitiveConcurrentMap implements IntIntMap {

private final IntIntMap[] maps;
private final int defaultValue;

public ConcurrentIntIntMap(
int numBuckets,
Expand All @@ -18,6 +18,7 @@ public ConcurrentIntIntMap(
super(numBuckets);

this.maps = new IntIntMap[numBuckets];
this.defaultValue = defaultValue;

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

@Override
public int getDefaultValue() {
return defaultValue;
}

@Override
public int remove(int key) {
int bucket = getBucket(key);
Expand All @@ -93,4 +99,17 @@ public int remove(int key) {
writeLock.unlock();
}
}

@Override
public boolean remove(int key, int value) {
int bucket = getBucket(key);

Lock writeLock = locks[bucket].writeLock();
writeLock.lock();
try {
return maps[bucket].remove(key, value);
} finally {
writeLock.unlock();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.trivago.fastutilconcurrentwrapper.LongFloatMap;
import com.trivago.fastutilconcurrentwrapper.wrapper.PrimitiveFastutilLongFloatWrapper;

import java.util.concurrent.locks.Lock;

public class ConcurrentLongFloatMap extends PrimitiveConcurrentMap implements LongFloatMap {
Expand Down Expand Up @@ -99,4 +98,17 @@ public float remove(long key) {
}
}

@Override
public boolean remove(long key, float value) {
int bucket = getBucket(key);

Lock writeLock = locks[bucket].writeLock();
writeLock.lock();
try {
return maps[bucket].remove(key, value);
} finally {
writeLock.unlock();
}
}

}
Loading

0 comments on commit bb4555b

Please sign in to comment.