Skip to content

Commit

Permalink
Expose trimToSize() in HashBag implementations
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Noël <victor.noel@brennus-analytics.com>
  • Loading branch information
victornoel committed Jan 12, 2023
1 parent d98a770 commit cee3aad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import org.eclipse.collections.api.bag.Bag;
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.factory.primitive.ObjectIntMaps;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMap;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;

Expand All @@ -38,12 +38,12 @@ public class HashBag<T>

public HashBag()
{
this.items = ObjectIntMaps.mutable.empty();
this.items = new ObjectIntHashMap<>();
}

public HashBag(int size)
{
this.items = ObjectIntMaps.mutable.withInitialCapacity(size);
this.items = new ObjectIntHashMap<>(size);
}

private HashBag(MutableObjectIntMap<T> map)
Expand Down Expand Up @@ -86,6 +86,16 @@ public static <E> HashBag<E> newBagWith(E... elements)
return result;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @since 12.0
*/
public void trimToSize()
{
((ObjectIntHashMap<T>) this.items).trimToSize();
}

@Override
protected int computeHashCode(T item)
{
Expand All @@ -108,7 +118,7 @@ public void writeExternal(ObjectOutput out) throws IOException
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
this.items = ObjectIntMaps.mutable.empty();
this.items = new ObjectIntHashMap<>();
((Externalizable) this.items).readExternal(in);
this.size = (int) this.items.sum();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public final class MultiReaderHashBag<T>
{
private static final long serialVersionUID = 1L;

private MutableBag<T> delegate;
private HashBag<T> delegate;

/**
* @deprecated Empty default constructor used for serialization.
Expand All @@ -90,12 +90,12 @@ public MultiReaderHashBag()
// For Externalizable use only
}

private MultiReaderHashBag(MutableBag<T> newDelegate)
private MultiReaderHashBag(HashBag<T> newDelegate)
{
this(newDelegate, new ReentrantReadWriteLock());
}

private MultiReaderHashBag(MutableBag<T> newDelegate, ReadWriteLock newLock)
private MultiReaderHashBag(HashBag<T> newDelegate, ReadWriteLock newLock)
{
this.lock = newLock;
this.lockWrapper = new ReadWriteLockWrapper(newLock);
Expand All @@ -122,6 +122,16 @@ public static <T> MultiReaderHashBag<T> newBagWith(T... elements)
return new MultiReaderHashBag<>(HashBag.newBagWith(elements));
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @since 12.0
*/
public void trimToSize()
{
this.delegate.trimToSize();
}

@Override
protected MutableBag<T> getDelegate()
{
Expand Down Expand Up @@ -648,7 +658,7 @@ public void writeExternal(ObjectOutput out) throws IOException
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
this.delegate = (MutableBag<T>) in.readObject();
this.delegate = (HashBag<T>) in.readObject();
this.lock = new ReentrantReadWriteLock();
this.lockWrapper = new ReadWriteLockWrapper(this.lock);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
import org.eclipse.collections.api.bag.MutableBag;
import org.eclipse.collections.api.block.HashingStrategy;
import org.eclipse.collections.api.block.predicate.primitive.IntPredicate;
import org.eclipse.collections.api.factory.primitive.ObjectIntHashingStrategyMaps;
import org.eclipse.collections.api.map.primitive.MutableObjectIntMap;
import org.eclipse.collections.impl.bag.mutable.AbstractHashBag;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMap;
import org.eclipse.collections.impl.map.mutable.primitive.ObjectIntHashMapWithHashingStrategy;
import org.eclipse.collections.impl.utility.ArrayIterate;
import org.eclipse.collections.impl.utility.Iterate;

Expand All @@ -37,7 +38,7 @@ public HashBagWithHashingStrategy(HashingStrategy<? super T> hashingStrategy)
throw new IllegalArgumentException("Cannot Instantiate HashBagWithHashingStrategy with null HashingStrategy");
}
this.hashingStrategy = hashingStrategy;
this.items = ObjectIntHashingStrategyMaps.mutable.with(hashingStrategy);
this.items = ObjectIntHashMapWithHashingStrategy.newMap(hashingStrategy);
}

public HashBagWithHashingStrategy(HashingStrategy<? super T> hashingStrategy, int size)
Expand All @@ -47,7 +48,7 @@ public HashBagWithHashingStrategy(HashingStrategy<? super T> hashingStrategy, in
throw new IllegalArgumentException("Cannot Instantiate HashBagWithHashingStrategy with null HashingStrategy");
}
this.hashingStrategy = hashingStrategy;
this.items = ObjectIntHashingStrategyMaps.mutable.withInitialCapacity(hashingStrategy, size);
this.items = ObjectIntHashMapWithHashingStrategy.newMapWithInitialCapacity(hashingStrategy, size);
}

private HashBagWithHashingStrategy(HashingStrategy<? super T> hashingStrategy, MutableObjectIntMap<T> map)
Expand Down Expand Up @@ -90,6 +91,16 @@ public static <E> HashBagWithHashingStrategy<E> newBagWith(HashingStrategy<? sup
return result;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @since 12.0
*/
public void trimToSize()
{
((ObjectIntHashMap<T>) this.items).trimToSize();
}

public HashingStrategy<? super T> hashingStrategy()
{
return this.hashingStrategy;
Expand Down

0 comments on commit cee3aad

Please sign in to comment.