public abstract class HashConfig extends Object
Instead of a single load factor, available for configuration in most other hash
collections APIs, HashConfig
allows to configure three loads:
There is an obvious invariant over the loads:
0 <= min load <= target load <= max load <= 1
But the target load shouldn't touch the bounds:
0 < target load < 1
The loads are bounded within the [0, 1]
range,
since all hashes in the library
use open addressing
method of collision resolution.
Also HashConfig
allows to configure the growth factor. When elements
are inserted into the hash container and it grows, when
the hash container load reaches the max load,
hash table's capacity is multiplied by the growth factor, immediately after that the current load
is supposed to be at or a bit higher than the min load. That is why HashConfig
keeps one more invariant:
1.0 < growth factor <= max load / min load
The schema explained above allows much more precise control over memory footprint -- performance tradeoff of hash containers, than a single load factor configuration.
Hash config is immutable, all "setters" return a new independent config object with the corresponding field changed.
Modifier and Type | Method and Description |
---|---|
static HashConfig |
fromLoads(double minLoad,
double targetLoad,
double maxLoad)
Returns a new hash config with the given loads and the growth factor set to
maxLoad / minLoad . |
static HashConfig |
getDefault()
Returns a hash config with 0.(3) min load, 0.5 target load, 0.(6) max load, 2.0 growth factor
and
null shrink condition. |
abstract double |
getGrowthFactor()
Returns the growth factor of this hash config.
|
abstract double |
getMaxLoad()
Returns the max load of this hash config.
|
abstract double |
getMinLoad()
Returns the min load of this hash config.
|
abstract Predicate<HashContainer> |
getShrinkCondition()
Returns the shrink condition of this hash config.
|
abstract double |
getTargetLoad()
Returns the target load of this hash config.
|
HashConfig |
withGrowthFactor(double growthFactor)
Returns a copy of this hash config with the growth factor set to the given value.
|
HashConfig |
withMaxLoad(double maxLoad)
Returns a copy of this hash config with the max load set to the given value.
|
HashConfig |
withMinLoad(double minLoad)
Returns a copy of this hash config with the min load set to the given value.
|
HashConfig |
withShrinkCondition(Predicate<HashContainer> condition)
Returns a copy of this hash config with the shrink condition set to the given predicate.
|
HashConfig |
withTargetLoad(double targetLoad)
Returns a copy of this hash config with the target load set to the given value.
|
@Nonnull public static HashConfig getDefault()
null
shrink condition.@Nonnull public static HashConfig fromLoads(double minLoad, double targetLoad, double maxLoad)
maxLoad / minLoad
.
The shrink condition in the returned hash config is left default, i. e. null
.
minLoad
- the min load, should be in the [0.0, targetLoad]
rangetargetLoad
- the target load, should be in the [minLoad, maxLoad]
rangemaxLoad
- the max load, should be in the [targetLoad, 1.0]
rangemaxLoad / minLoad
IllegalArgumentException
- if the given loads violate
the hash config invariantspublic abstract double getMinLoad()
The default is 0.(3) (one-third).
0.0
, target load
] rangewithMinLoad(double)
public final HashConfig withMinLoad(double minLoad)
Min load allows to limit memory usage of hash containers.
Updatable and mutable linear hash tables can't have min load greater than 0.5.
minLoad
- the new min load, a value
in the [0.0
, target load
] rangeIllegalArgumentException
- if the resulting hash config violates
the invariantsgetMinLoad()
public abstract double getTargetLoad()
HashContainer.shrink()
rehashes a table to the target load.
min load
, max load
] rangewithTargetLoad(double)
public final HashConfig withTargetLoad(double targetLoad)
Target load allows to control basic memory usage -- performance tradeoff for hash tables.
targetLoad
- the new target load, a value in the
[min load
, max load
] rangeIllegalArgumentException
- if the resulting hash config violates
the invariantsgetTargetLoad()
public abstract double getMaxLoad()
target load
, 1.0
] rangewithMaxLoad(double)
public final HashConfig withMaxLoad(double maxLoad)
Max load allows to limit the minimum performance of hash tables, because too dense hash tables operate slowly.
maxLoad
- the new max load, a value
in the [target load
, 1.0
] rangeIllegalArgumentException
- if the resulting hash config violates
the invariantsgetMaxLoad()
public abstract double getGrowthFactor()
1.0
,
max load
/ min load
] rangewithGrowthFactor(double)
public final HashConfig withGrowthFactor(double growthFactor)
Growth factor allows to control memory usage -- performance tradeoff for steadily growing hash tables.
Linear hash tables can't have any growth factor other than 2.0.
growthFactor
- the new growth factor, a value in the [1.0
,
max load
/ min load
] rangeIllegalArgumentException
- if the resulting hash config violates
the invariantsgetGrowthFactor()
@Nullable public abstract Predicate<HashContainer> getShrinkCondition()
Immediately after hash set or map construction from non-distinct sources (e. g. arrays) it's load could be significantly less than the target factor due to expansion. The shrink condition is a predicate which is used to shrink too sparse hash containers automatically.
null
condition is considered as constant false
predicate: never shrink.
It is a default value.
Particularly useful for immutable containers construction, because they couldn't be shrunk manually after being returned from the factory method.
withShrinkCondition(com.koloboke.function.Predicate)
,
HashContainer.shrink()
public final HashConfig withShrinkCondition(@Nullable Predicate<HashContainer> condition)
An example of sensible shrink condition:
conf.withShrinkCondition(h -> h.currentLoad() + 0.1 < h.hashConfig().getTargetLoad());
condition
- the new shrink conditiongetShrinkCondition()