Skip to content

Commit

Permalink
Expose trimToSize in all primitive maps and sets
Browse files Browse the repository at this point in the history
- only trim when necessary
- also deprecate compact()

Signed-off-by: Victor Noël <victor.noel@brennus-analytics.com>
  • Loading branch information
victornoel committed Jan 12, 2023
1 parent c618acd commit d98a770
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1211,9 +1211,26 @@ public class Object<name>HashMap\<K> implements MutableObject<name>Map\<K>, Exte
}
<endif>

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.keys.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @deprecated since 12.0 - Use {@link #trimToSize()} instead
*/
@Deprecated
public void compact()
{
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1238,9 +1238,26 @@ public class Object<name>HashMapWithHashingStrategy\<K> implements MutableObject
}
<endif>

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.keys.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @deprecated since 12.0 - Use {@link #trimToSize()} instead
*/
@Deprecated
public void compact()
{
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,26 @@ public class <name>BooleanHashMap extends AbstractMutableBooleanValuesMap implem
this.values = new BitSet(sizeToAllocate);
}

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.keys.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @deprecated since 12.0 - Use {@link #trimToSize()} instead
*/
@Deprecated
public void compact()
{
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2910,9 +2910,26 @@ public class <name>ObjectHashMap\<V> implements Mutable<name>ObjectMap\<V>, Exte
return new KeySet();
}

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.keys.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @deprecated since 12.0 - Use {@link #trimToSize()} instead
*/
@Deprecated
public void compact()
{
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,26 @@ public class <name1><name2>HashMap extends AbstractMutable<name2>ValuesMap imple
}
}

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.<keyArray>.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @deprecated since 12.0 - Use {@link #trimToSize()} instead
*/
@Deprecated
public void compact()
{
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -942,9 +942,26 @@ public class <name>HashSet extends Abstract<name>Set implements Mutable<name>Set
return new <name>HashSet();
}

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.table.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

/**
* Rehashes every element in the set into a new backing table of the smallest possible size and eliminating removed sentinels.
*
* @deprecated since 12.0 - Use {@link #trimToSize()} instead
*/
@Deprecated
public void compact()
{
this.rehash(this.smallestPowerOfTwoGreaterThan(this.size()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1109,6 +1109,20 @@ private int maxOccupiedWithData()
return Math.min(capacity - 1, capacity / OCCUPIED_DATA_RATIO);
}

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.keys.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

private void rehashAndGrow()
{
this.rehash(this.keys.length << 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,20 @@ private int maxOccupiedWithData()
return Math.min(capacity - 1, capacity / OCCUPIED_DATA_RATIO);
}

/**
* @since 12.0
*/
public boolean trimToSize()
{
int newCapacity = this.smallestPowerOfTwoGreaterThan(this.size());
if (this.keys.length > newCapacity)
{
this.rehash(newCapacity);
return true;
}
return false;
}

private void rehashAndGrow()
{
this.rehash(this.keys.length << 1);
Expand Down

0 comments on commit d98a770

Please sign in to comment.